Class: HtmlMarkup
- Inherits:
-
Wunderbar::BuilderBase
- Object
- Wunderbar::BuilderBase
- HtmlMarkup
- Defined in:
- lib/wunderbar/html-methods.rb
Overview
Wrapper class that understands HTML
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 )
Class Method Summary collapse
- .flatten?(children) ⇒ Boolean
-
.reflow(stream, width) ⇒ Object
reflow long lines.
Instance Method Summary collapse
- #_(children = nil) ⇒ Object
- #_!(text) ⇒ Object
- #_?(text) ⇒ Boolean
- #_coffeescript(text) ⇒ Object
- #_exception(*args) ⇒ Object
- #_head(*args, &block) ⇒ Object
- #_html(*args, &block) ⇒ Object
- #_math(*args, &block) ⇒ Object
- #_svg(*args, &block) ⇒ Object
- #_xhtml(*args, &block) ⇒ Object
- #clear! ⇒ Object
- #html(*args, &block) ⇒ Object
-
#initialize(scope) ⇒ HtmlMarkup
constructor
A new instance of HtmlMarkup.
- #method_missing(name, *args, &block) ⇒ Object
- #xhtml(*args, &block) ⇒ Object
- #xhtml? ⇒ Boolean
Methods inherited from Wunderbar::BuilderBase
#get_binding, #set_variables_from_params
Constructor Details
#initialize(scope) ⇒ HtmlMarkup
Returns a new instance of HtmlMarkup.
15 16 17 18 19 |
# File 'lib/wunderbar/html-methods.rb', line 15 def initialize(scope) @_scope = scope @x = Wunderbar::XmlMarkup.new :scope => scope, :indent => 2, :target => [] @xthml = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
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 147 148 149 150 151 152 |
# File 'lib/wunderbar/html-methods.rb', line 59 def method_missing(name, *args, &block) if name.to_s =~ /^_(\w+)(!|\?|)$/ name, flag = $1, $2 elsif @_scope and @_scope.respond_to? name return @_scope.__send__ name, *args, &block else error = NameError.new "undefined local variable or method `#{name}'", name error.set_backtrace caller raise error end if name.sub!(/_$/,'') @x.margin! return __send__ "_#{name}", *args, &block if respond_to? "_#{name}" end if flag != '!' if %w(script style).include?(name) if String === args.first and not block text = args.shift if !text.include? '&' and !text.include? '<' block = Proc.new do @x.indented_data!(text) end elsif name == 'style' block = Proc.new do @x.indented_data!(text, "/*<![CDATA[*/", "/*]]>*/") end else block = Proc.new do @x.indented_data!(text, "//<![CDATA[", "//]]>") end end end args << {} if args.length == 0 if Hash === args.last args.last[:lang] ||= 'text/javascript' if name == 'script' args.last[:type] ||= 'text/css' if name == 'style' end end # ensure that non-void elements are explicitly closed if not block and not VOID.include?(name) symbol = (args.shift if args.length > 0 and Symbol === args.first) if args.length == 0 or (args.length == 1 and Hash === args.first) args.unshift '' end args.unshift(symbol) if symbol end if String === args.first and args.first.respond_to? :html_safe? if args.first.html_safe? and not block if args.first.include? '>' or args.first.include? '&' markup = args.shift block = Proc.new {_ markup} end end end if Hash === args.last # remove attributes with nil, false values args.last.delete_if {|key, value| !value} # replace boolean 'true' attributes with the name of the attribute args.last.each {|key, value| args.last[key]=key if value == true} end end if flag == '!' @x.disable_indentation! do @x.tag! name, *args, &block end elsif flag == '?' # capture exceptions, produce filtered tracebacks @x.tag!(name, *args) do begin block.call rescue ::Exception => exception = (Hash === args.last)? args.last : {} [:log_level] = 'warn' _exception exception, end end else target = @x.tag! name, *args, &block if block and %w(script style).include?(name) if %w{//]]> /*]]>*/}.include? target[-4] target[-4], target[-3] = target[-3], target[-4] end end target end end |
Class Method Details
.flatten?(children) ⇒ Boolean
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/wunderbar/html-methods.rb', line 296 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 |
.reflow(stream, width) ⇒ Object
reflow long lines
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 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 |
# File 'lib/wunderbar/html-methods.rb', line 315 def self.reflow(stream, width) source = stream.slice!(0..-1) indent = col = 0 breakable = true pre = false while not source.empty? token = source.shift indent = token[/^ */].length if col == 0 if token.start_with? '<' breakable = false pre = true if token == '<pre' end # flow text while token.length + col > width and breakable and not pre break if token[0...-1].include? "\n" split = token.rindex(' ', [width-col,0].max) || token.index(' ') break unless split break if col+split < indent+width/2 stream << token[0...split] << "\n" << (' '*indent) col = indent token = token[split+1..-1] end # break around tags if token.end_with? '>' if col > indent + 4 and stream[-2..-1] == ['<br', '/'] stream << token << "\n" col = 0 token = ' '*indent source[0] = source.first.lstrip unless source.empty? elsif col > width and not pre # break on previous space within text pcol = col stream.reverse_each do |xtoken| break if xtoken.include? "\n" split = xtoken.rindex(' ') breakable = false if xtoken.end_with? '>' if breakable and split col = col - pcol + xtoken.length - split + indent xtoken[split] = "\n#{' '*indent}" break end breakable = true if xtoken.start_with? '<' pcol -= xtoken.length break if pcol < (width + indent)/2 end end breakable = true pre = false if token == '</pre>' end stream << token col += token.length col = 0 if token.end_with? "\n" end end |
Instance Method Details
#_(children = nil) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/wunderbar/html-methods.rb', line 211 def _(children=nil) return @x if children == nil if String === children safe = !children.tainted? safe ||= children.html_safe? if children.respond_to? :html_safe? if safe and (children.include? '<' or children.include? '&') require 'nokogiri' children = Nokogiri::HTML::fragment(children.to_s).children else return @x.indented_text! children end end # remove leading and trailing space children.shift if children.first.text? and children.first.text.strip.empty? if not children.empty? children.pop if children.last.text? and children.last.text.strip.empty? end children.each do |child| if child.text? or child.cdata? text = child.text if text.strip.empty? @x.text! "\n" if text.count("\n")>1 elsif @x.indentation_state!.first == 0 @x.indented_text! text.gsub(/\s+/, ' ') else @x.indented_text! text.strip end elsif child.comment? @x.comment! child.text.sub(/\A /,'').sub(/ \Z/, '') elsif self.class.flatten? child.children block_element = Proc.new do |node| node.element? and HTML5_BLOCK.include?(node.name) end if child.children.any?(&block_element) # indent children, but disable indentation on consecutive # sequences of non-block-elements. Put another way: break # out block elements to a new line. @x.tag!(child.name, child.attributes) do children = child.children.to_a while not children.empty? stop = children.index(&block_element) if stop == 0 _ [children.shift] else @x.disable_indentation! do _ children.shift(stop || children.length) end end end end else # disable indentation on the entire element @x.disable_indentation! do @x.tag!(child.name, child.attributes) {_ child.children} end end elsif child.children.empty? and VOID.include? child.name @x.tag!(child.name, child.attributes) elsif child.children.all? {|gchild| gchild.text?} @x.tag!(child.name, child.text.strip, child.attributes) elsif child.children.any? {|gchild| gchild.cdata?} and (child.text.include? '<' or child.text.include? '&') @x << child else @x.tag!(child.name, child.attributes) {_ child.children} end end end |
#_!(text) ⇒ Object
207 208 209 |
# File 'lib/wunderbar/html-methods.rb', line 207 def _!(text) @x.text! text.to_s end |
#_?(text) ⇒ Boolean
203 204 205 |
# File 'lib/wunderbar/html-methods.rb', line 203 def _?(text) @x.indented_text! text.to_s end |
#_coffeescript(text) ⇒ Object
285 286 287 288 289 290 |
# File 'lib/wunderbar/html-methods.rb', line 285 def _coffeescript(text) require 'coffee-script' _script CoffeeScript.compile(text) rescue LoadError _script text, :lang => 'text/coffeescript' end |
#_exception(*args) ⇒ Object
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 |
# File 'lib/wunderbar/html-methods.rb', line 154 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 Wunderbar.send log_level, text exception.backtrace.each do |frame| next if Wunderbar::CALLERS_TO_IGNORE.any? {|re| frame =~ re} Wunderbar.send log_level, " #{frame}" text += "\n #{frame}" end if traceback_class @x.tag! :pre, text, :class=>traceback_class else @x.tag! :pre, text, :style=>traceback_style end else super end end |
#_head(*args, &block) ⇒ Object
182 183 184 185 186 187 |
# File 'lib/wunderbar/html-methods.rb', line 182 def _head(*args, &block) @x.tag!('head', *args) do @x.tag! :meta, :charset => 'utf-8' block.call if block end end |
#_html(*args, &block) ⇒ Object
46 47 48 |
# File 'lib/wunderbar/html-methods.rb', line 46 def _html(*args, &block) html(*args, &block) end |
#_math(*args, &block) ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/wunderbar/html-methods.rb', line 195 def _math(*args, &block) args << {} if args.empty? if Hash === args.first args.first['xmlns'] = 'http://www.w3.org/1998/Math/MathML' end @x.tag! :math, *args, &block end |
#_svg(*args, &block) ⇒ Object
189 190 191 192 193 |
# File 'lib/wunderbar/html-methods.rb', line 189 def _svg(*args, &block) args << {} if args.empty? args.first['xmlns'] = 'http://www.w3.org/2000/svg' if Hash === args.first @x.tag! :svg, *args, &block end |
#_xhtml(*args, &block) ⇒ Object
50 51 52 53 |
# File 'lib/wunderbar/html-methods.rb', line 50 def _xhtml(*args, &block) @xhtml = true html(*args, &block) end |
#clear! ⇒ Object
292 293 294 |
# File 'lib/wunderbar/html-methods.rb', line 292 def clear! @x.target!.clear end |
#html(*args, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/wunderbar/html-methods.rb', line 26 def html(*args, &block) # default namespace args << {} if args.empty? args.first[:xmlns] ||= 'http://www.w3.org/1999/xhtml' if Hash === args.first @_width = args.first.delete(:_width) if Hash === args.first @x.text! "\xEF\xBB\xBF" @x.declare! :DOCTYPE, :html @x.tag! :html, *args do set_variables_from_params instance_eval(&block) end if @_width self.class.reflow(@x.target!, @_width) end @x.target!.join end |
#xhtml(*args, &block) ⇒ Object
21 22 23 24 |
# File 'lib/wunderbar/html-methods.rb', line 21 def xhtml(*args, &block) @xhtml = true html(*args, &block) end |
#xhtml? ⇒ Boolean
55 56 57 |
# File 'lib/wunderbar/html-methods.rb', line 55 def xhtml? @xhtml end |