Class: Wunderbar::HtmlMarkup
- Inherits:
-
BuilderBase
- Object
- BuilderBase
- Wunderbar::HtmlMarkup
- Defined in:
- lib/wunderbar/html-methods.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 )
Class Method Summary collapse
- .flatten?(children) ⇒ Boolean
-
.reflow(stream, width) ⇒ Object
reflow long lines.
Instance Method Summary collapse
- #_(text = nil, &block) ⇒ Object
- #_!(text) ⇒ Object
- #_coffeescript(text) ⇒ Object
- #_exception(*args) ⇒ Object
- #_head(*args, &block) ⇒ Object
- #_html(*args, &block) ⇒ Object
- #_math(*args, &block) ⇒ Object
- #_p(*args, &block) ⇒ Object
- #_pre(*args, &block) ⇒ Object
- #_svg(*args, &block) ⇒ Object
- #clear! ⇒ Object
- #html(*args, &block) ⇒ Object
-
#initialize(scope) ⇒ HtmlMarkup
constructor
A new instance of HtmlMarkup.
- #method_missing(name, *args, &block) ⇒ Object
Methods inherited from BuilderBase
#get_binding, #set_variables_from_params
Constructor Details
#initialize(scope) ⇒ HtmlMarkup
Returns a new instance of HtmlMarkup.
16 17 18 19 |
# File 'lib/wunderbar/html-methods.rb', line 16 def initialize(scope) @_scope = scope @x = XmlMarkup.new :scope => scope, :indent => 2, :target => [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *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 |
# File 'lib/wunderbar/html-methods.rb', line 47 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 err = NameError.new "undefined local variable or method `#{name}'", name err.set_backtrace caller raise err 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 and args.first =~ /[>&]/ markup = args.shift block = Proc.new {_ {markup}} 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
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/wunderbar/html-methods.rb', line 247 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
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/wunderbar/html-methods.rb', line 266 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
#_(text = nil, &block) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/wunderbar/html-methods.rb', line 208 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? 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 @x[*children] end |
#_!(text) ⇒ Object
204 205 206 |
# File 'lib/wunderbar/html-methods.rb', line 204 def _!(text) @x.text! text.to_s end |
#_coffeescript(text) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/wunderbar/html-methods.rb', line 236 def _coffeescript(text) require 'coffee-script' _script CoffeeScript.compile(text) rescue LoadError _script text, :lang => 'text/coffeescript' end |
#_exception(*args) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/wunderbar/html-methods.rb', line 140 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 @x.tag! :pre, text, :class=>traceback_class else @x.tag! :pre, text, :style=>traceback_style end else super end end |
#_head(*args, &block) ⇒ Object
168 169 170 171 172 173 |
# File 'lib/wunderbar/html-methods.rb', line 168 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
43 44 45 |
# File 'lib/wunderbar/html-methods.rb', line 43 def _html(*args, &block) html(*args, &block) end |
#_math(*args, &block) ⇒ Object
192 193 194 195 196 197 198 |
# File 'lib/wunderbar/html-methods.rb', line 192 def _math(*args, &block) args << {} if args.empty? if Hash === args.first args.first['xmlns'] = 'http://www.w3.org/1998/Math/MathML' end @x.proxiable_tag! :math, *args, &block end |
#_p(*args, &block) ⇒ Object
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/wunderbar/html-methods.rb', line 175 def _p(*args, &block) if args.length >= 1 and String === args.first and args.first.include? "\n" text = args.shift @x.tag! :p, *args do @x.indented_text! text end else super end end |
#_pre(*args, &block) ⇒ Object
200 201 202 |
# File 'lib/wunderbar/html-methods.rb', line 200 def _pre(*args, &block) @x.disable_indentation! { @x.tag! :pre, *args, &block } end |
#_svg(*args, &block) ⇒ Object
186 187 188 189 190 |
# File 'lib/wunderbar/html-methods.rb', line 186 def _svg(*args, &block) args << {} if args.empty? args.first['xmlns'] = 'http://www.w3.org/2000/svg' if Hash === args.first @x.proxiable_tag! :svg, *args, &block end |
#clear! ⇒ Object
243 244 245 |
# File 'lib/wunderbar/html-methods.rb', line 243 def clear! @x.target!.clear end |
#html(*args, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/wunderbar/html-methods.rb', line 21 def html(*args, &block) # default namespace args << {} if args.empty? if Hash === args.first args.first[:xmlns] ||= 'http://www.w3.org/1999/xhtml' end @_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 |