Class: Wunderbar::HtmlMarkup
- Inherits:
-
Overridable
- Object
- BuilderBase
- Overridable
- Wunderbar::HtmlMarkup
- Defined in:
- lib/wunderbar/opal.rb,
lib/wunderbar/script.rb,
lib/wunderbar/markdown.rb,
lib/wunderbar/angularjs.rb,
lib/wunderbar/coffeescript.rb,
lib/wunderbar/html-methods.rb,
lib/wunderbar/coderay.rb
Constant Summary collapse
- VOID =
%w( area base br col command embed hr img input keygen link meta param source track wbr )- HTML5_BLOCK =
%w( # https://developer.mozilla.org/en/HTML/Block-level_elements address article aside audio blockquote br canvas dd div dl fieldset figcaption figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr noscript ol output p pre section table tfoot ul video )- HEAD =
%w(title base link style meta)
Class Method Summary collapse
Instance Method Summary collapse
- #_(text = nil, &block) ⇒ Object
- #_!(text) ⇒ Object
- #__(text = nil) ⇒ Object
- #_coderay(*args) ⇒ Object
- #_coffeescript(text) ⇒ Object
- #_exception(*args) ⇒ Object
- #_head(*args, &block) ⇒ Object
- #_html(*args, &block) ⇒ Object
- #_markdown(string) ⇒ Object
- #_math(*args, &block) ⇒ Object
- #_ng_template(attrs = {}, &block) ⇒ Object
- #_ol(*args, &block) ⇒ Object
- #_p(*args, &block) ⇒ Object
- #_pre(*args, &block) ⇒ Object
- #_script(*args, &block) ⇒ Object
- #_svg(*args, &block) ⇒ Object
- #_tr(*args, &block) ⇒ Object
- #_ul(*args, &block) ⇒ Object
- #clear! ⇒ Object
- #find_name(name) ⇒ Object
- #html(*args, &block) ⇒ Object
-
#initialize(scope) ⇒ HtmlMarkup
constructor
A new instance of HtmlMarkup.
- #method_missing(name, *args, &block) ⇒ Object
- #proxiable_tag!(name, *args, &block) ⇒ Object
- #tag!(name, *args, &block) ⇒ Object
Methods inherited from Overridable
Methods inherited from BuilderBase
#get_binding, #set_variables_from_params
Constructor Details
#initialize(scope) ⇒ HtmlMarkup
Returns a new instance of HtmlMarkup.
42 43 44 45 |
# File 'lib/wunderbar/html-methods.rb', line 42 def initialize(scope) @_scope = scope @_x = XmlMarkup.new :scope => scope, :indent => 2 end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/wunderbar/html-methods.rb', line 152 def method_missing(name, *args, &block) if name =~ /^_(\w+)(!|\?|)$/ name, flag = $1, $2 elsif @_scope and @_scope.respond_to? name return @_scope.__send__ name, *args, &block else err = NameError.new "undefined local variable or method `#{name}'", name err.set_backtrace caller raise err end if name.sub!(/_$/,'') @_x.spaced! if flag != '!' and respond_to? "_#{name}" return __send__ "_#{name}#{flag}", *args, &block end end name = name.to_s.gsub('_', '-') if flag != '!' if String === args.first and args.first.respond_to? :html_safe? if args.first.html_safe? and not block and args.first =~ /[>&]/ markup = args.shift block = Proc.new {_ {markup}} end end end if flag == '!' @_x.compact! { tag! name, *args, &block } elsif flag == '?' # capture exceptions, produce filtered tracebacks tag!(name, *args) do begin block.call rescue ::Exception => exception = (Hash === args.last)? args.last : {} [:log_level] = 'warn' _exception exception, end end elsif .templates.include? name x = self.class.new({}) instance_variables.each do |ivar| x.instance_variable_set ivar, instance_variable_get(ivar) end if Hash === args.last args.last.each do |name, value| x.instance_variable_set "@#{name}", value end end save_yield = .templates['yield'] begin .templates['yield'] = block if block x.instance_eval &.templates[name] ensure .templates['yield'] = save_yield .templates.delete 'yield' unless save_yield end else tag! name, *args, &block end end |
Class Method Details
.flatten?(children) ⇒ Boolean
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/wunderbar/html-methods.rb', line 400 def self.flatten?(children) # do any of the text nodes need special processing to preserve spacing? flatten = false space = true if children.any? {|child| child.text? and !child.text.strip.empty?} children.each do |child| if child.text? or child.element? unless child.text == '' flatten = true if not space and not child.text =~ /\A\s/ space = (child.text =~ /\s\Z/) end space = true if child.element? and HTML5_BLOCK.include? child.name end end end flatten end |
Instance Method Details
#_(text = nil, &block) ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/wunderbar/html-methods.rb', line 334 def _(text=nil, &block) unless block if text if text.respond_to? :html_safe? and text.html_safe? _ {text} else @_x.indented_text! text.to_s end end return @_x end children = block.call if String === children safe = !children.tainted? safe ||= children.html_safe? if children.respond_to? :html_safe? safe &&= defined? Nokogiri ok = safe || defined? Sanitize safe = true if ok and (children.include? '<' or children.include? '&') doc = Nokogiri::HTML::fragment(children.to_s) Sanitize.new.clean_node! doc.dup.untaint if not safe children = doc.children.to_a # ignore leading whitespace while not children.empty? and children.first.text? break unless children.first.text.strip.empty? children.shift end # gather up candidate head elements pending_head = [] while not children.empty? and children.first.element? break unless (HEAD+['script']).include? children.first.name pending_head << children.shift end # rebuild head element if any candidates were found unless pending_head.empty? head = Nokogiri::XML::Node.new('head', pending_head.first.document) pending_head.each {|child| head << child} children.unshift head end else return @_x.indented_text! children end end @_x[*children] end |
#_!(text) ⇒ Object
330 331 332 |
# File 'lib/wunderbar/html-methods.rb', line 330 def _!(text) @_x.text! text.to_s.chomp end |
#__(text = nil) ⇒ Object
387 388 389 390 391 392 393 394 |
# File 'lib/wunderbar/html-methods.rb', line 387 def __(text=nil) if text @_x.spaced! @_x.indented_text! text else @_x.text! "" end end |
#_coderay(*args) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/wunderbar/coderay.rb', line 16 def _coderay(*args) # allow arguments in any order, disambiguate based on type lang, string, attrs = :ruby, '', {} args.each do |arg| case arg when Symbol; lang = arg when String; string = arg when Hash; attrs = arg end end base = _{ CodeRay.scan(CDATANode.normalize(string), lang).div } # remove wrapping divs while base.length == 1 and base.first.name == 'div' div = base.first.parent.children.pop div.children.each {|child| child.parent = base.first.parent} base.first.parent.children += base.first.children base = div.children end # add attrs provided to pre element if base.length == 1 and base.first.name == 'pre' base.first.attrs.merge! attrs end end |
#_coffeescript(text) ⇒ Object
6 7 8 |
# File 'lib/wunderbar/coffeescript.rb', line 6 def _coffeescript(text) _script CoffeeScript.compile(text) end |
#_exception(*args) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/wunderbar/html-methods.rb', line 235 def _exception(*args) exception = args.first if exception.respond_to? :backtrace = (Hash === args.last)? args.last : {} traceback_class = .delete(:traceback_class) traceback_style = .delete(:traceback_style) traceback_style ||= 'background-color:#ff0; margin: 1em 0; ' + 'padding: 1em; border: 4px solid red; border-radius: 1em' text = exception.inspect log_level = .delete(:log_level) || :error .send log_level, text exception.backtrace.each do |frame| next if ::CALLERS_TO_IGNORE.any? {|re| frame =~ re} .send log_level, " #{frame}" text += "\n #{frame}" end if traceback_class tag! :pre, text, :class=>traceback_class else tag! :pre, text, :style=>traceback_style end else super end end |
#_head(*args, &block) ⇒ Object
263 264 265 266 267 268 |
# File 'lib/wunderbar/html-methods.rb', line 263 def _head(*args, &block) tag!('head', *args) do tag! :meta, :charset => 'utf-8' block.call if block end end |
#_html(*args, &block) ⇒ Object
148 149 150 |
# File 'lib/wunderbar/html-methods.rb', line 148 def _html(*args, &block) html(*args, &block) end |
#_markdown(string) ⇒ Object
8 9 10 |
# File 'lib/wunderbar/markdown.rb', line 8 def _markdown(string) _{ Kramdown::Document.new(CDATANode.normalize(string)).to_html } end |
#_math(*args, &block) ⇒ Object
287 288 289 290 291 292 293 |
# File 'lib/wunderbar/html-methods.rb', line 287 def _math(*args, &block) args << {} if args.empty? if Hash === args.first args.first['xmlns'] = 'http://www.w3.org/1998/Math/MathML' end proxiable_tag! :math, *args, &block end |
#_ng_template(attrs = {}, &block) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/wunderbar/angularjs.rb', line 10 def _ng_template(attrs={}, &block) if attrs.empty? and not block proxiable_tag! :ng_template else attrs[:type] ||= 'text/ng-template' tag! :script, attrs, &block end end |
#_ol(*args, &block) ⇒ Object
310 311 312 313 314 315 316 317 318 |
# File 'lib/wunderbar/html-methods.rb', line 310 def _ol(*args, &block) iterable = args.first.respond_to? :each if iterable and (args.length > 1 or not args.first.respond_to? :to_hash) list = args.shift tag!(:ol, *args) {list.each {|arg| _li arg }} else super end end |
#_p(*args, &block) ⇒ Object
270 271 272 273 274 275 276 277 278 279 |
# File 'lib/wunderbar/html-methods.rb', line 270 def _p(*args, &block) if args.length >= 1 and String === args.first and args.first.include? "\n" text = args.shift tag! :p, *args do @_x.indented_text! text end else super end end |
#_pre(*args, &block) ⇒ Object
295 296 297 298 |
# File 'lib/wunderbar/html-methods.rb', line 295 def _pre(*args, &block) args.first.chomp! if String === args.first and args.first.end_with? "\n" @_x.compact! { tag! :pre, *args, &block } end |
#_script(*args, &block) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/wunderbar/opal.rb', line 10 def _script(*args, &block) if block args.unshift Opal.parse(block.to_source(:strip_enclosure => true)) super *args, &nil else super end end |
#_svg(*args, &block) ⇒ Object
281 282 283 284 285 |
# File 'lib/wunderbar/html-methods.rb', line 281 def _svg(*args, &block) args << {} if args.empty? args.first['xmlns'] = 'http://www.w3.org/2000/svg' if Hash === args.first proxiable_tag! :svg, *args, &block end |
#_tr(*args, &block) ⇒ Object
320 321 322 323 324 325 326 327 328 |
# File 'lib/wunderbar/html-methods.rb', line 320 def _tr(*args, &block) iterable = args.first.respond_to? :each if iterable and (args.length > 1 or not args.first.respond_to? :to_hash) list = args.shift tag!(:tr, *args) {list.each {|arg| _td arg }} else super end end |
#_ul(*args, &block) ⇒ Object
300 301 302 303 304 305 306 307 308 |
# File 'lib/wunderbar/html-methods.rb', line 300 def _ul(*args, &block) iterable = args.first.respond_to? :each if iterable and (args.length > 1 or not args.first.respond_to? :to_hash) list = args.shift.dup tag!(:ul, *args) {list.each {|arg| _li arg }} else super end end |
#clear! ⇒ Object
396 397 398 |
# File 'lib/wunderbar/html-methods.rb', line 396 def clear! @_x.clear! end |
#find_name(name) ⇒ Object
110 111 112 |
# File 'lib/wunderbar/html-methods.rb', line 110 def find_name(name) proc {|child| child.respond_to? :name and child.name == name} end |
#html(*args, &block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/wunderbar/html-methods.rb', line 47 def html(*args, &block) # default namespace args << {} if args.empty? if Hash === args.first args.first[:xmlns] ||= 'http://www.w3.org/1999/xhtml' @_x.width = args.first.delete(:_width).to_i if args.first[:_width] end if ''.respond_to? :encoding bom = "\ufeff" else bom = "\xEF\xBB\xBF" end title = args.shift if String === args.first @_x.declare! :DOCTYPE, :html html = tag! :html, *args do set_variables_from_params _title title if title instance_eval(&block) end pending_head = [] pending_body = [] head = nil body = nil html.children.each do |child| next unless child if String === child pending_body << child elsif child.name == 'head' head = child elsif child.name == 'body' body = child elsif HEAD.include? child.name pending_head << child elsif child.name == 'script' if pending_body.empty? pending_head << child else pending_body << child end else pending_body << child end end @_x.instance_eval {@node = html} head = _head_ if not head body = _body nil if not body html.children.unshift(head.parent.children.delete(head)) html.children.push(body.parent.children.delete(body)) head.parent = body.parent = html head.children.compact! body.children.compact! [ [pending_head, head], [pending_body, body] ].each do |list, node| list.each do |child| html.children.delete(child) node.add_child child end end def find_name(name) proc {|child| child.respond_to? :name and child.name == name} end if not head.children.any? &find_name('title') h1 = body.children.find &find_name('h1') head.add_child Node.new('title', h1.text) if h1 and h1.text end base = head.children.index &find_name('base') if base head.children.insert 1, head.children.delete_at(base) if base > 1 if @_scope.env.respond_to? :[] and @_scope.env['DOCUMENT_ROOT'] # compute relative path from base to the current working directory require 'pathname' base = @_scope.env['DOCUMENT_ROOT'] + head.children[1].attrs[:href] base += 'index.html' if base.end_with? '/' base = Pathname.new(base).parent prefix = Pathname.new(Dir.pwd).relative_path_from(base).to_s + '/' prefix = nil if prefix == './' else prefix = nil end head.children.insert 2, *Asset.declarations(head, prefix) else head.children.insert 1, *Asset.declarations(head, nil) end title = head.children.index {|child| child.name == 'title'} if title and title > 1 head.children.insert 1, head.children.delete_at(title) end bom + @_x.target! end |
#proxiable_tag!(name, *args, &block) ⇒ Object
226 227 228 229 230 231 232 233 |
# File 'lib/wunderbar/html-methods.rb', line 226 def proxiable_tag!(name, *args, &block) node = @_x.tag! name, *args, &block if !block CssProxy.new(self, node) else node end end |
#tag!(name, *args, &block) ⇒ Object
217 218 219 220 221 222 223 224 |
# File 'lib/wunderbar/html-methods.rb', line 217 def tag!(name, *args, &block) node = @_x.tag! name, *args, &block if !block and args.empty? CssProxy.new(self, node) else node end end |