Class: Wunderbar::XmlMarkup
- Inherits:
-
BuilderClass
- Object
- BuilderBase
- BuilderClass
- Wunderbar::XmlMarkup
- Defined in:
- lib/wunderbar/builder.rb
Instance Attribute Summary collapse
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
-
#<<(data) ⇒ Object
insert verbatim.
- #[](*children) ⇒ Object
- #clear! ⇒ Object
- #comment!(text) ⇒ Object
- #compact!(&block) ⇒ Object
- #declare!(*args) ⇒ Object
- #indented_text!(text) ⇒ Object
-
#initialize(args) ⇒ XmlMarkup
constructor
A new instance of XmlMarkup.
-
#method_missing(method, *args, &block) ⇒ Object
forward to Wunderbar or @_scope.
- #methods ⇒ Object
- #pdf=(value) ⇒ Object
- #pdf? ⇒ Boolean
- #respond_to?(method) ⇒ Boolean
- #spaced! ⇒ Object
-
#system(command, opts = {}) ⇒ Object
execute a system command, echoing stdin, stdout, and stderr.
-
#tag!(sym, *args, &block) ⇒ Object
avoid method_missing overhead for the most common case.
- #target! ⇒ Object
- #text!(text) ⇒ Object
Methods inherited from BuilderClass
Methods inherited from BuilderBase
#get_binding, #set_variables_from_params
Constructor Details
#initialize(args) ⇒ XmlMarkup
Returns a new instance of XmlMarkup.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/wunderbar/builder.rb', line 33 def initialize(args) @_scope = args.delete(:scope) @_indent = args.delete(:indent) or 2 @_pdf = false @doc = Node.new(nil) @node = @doc @indentation_enabled = true @width = nil @spaced = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
forward to Wunderbar or @_scope
47 48 49 50 51 52 53 54 55 |
# File 'lib/wunderbar/builder.rb', line 47 def method_missing(method, *args, &block) if Wunderbar.respond_to? method Wunderbar.send method, *args, &block elsif @_scope and @_scope.respond_to? method @_scope.send method, *args, &block else super end end |
Instance Attribute Details
#width ⇒ Object
Returns the value of attribute width.
44 45 46 |
# File 'lib/wunderbar/builder.rb', line 44 def width @width end |
Instance Method Details
#<<(data) ⇒ Object
insert verbatim
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 |
# File 'lib/wunderbar/builder.rb', line 245 def <<(data) if defined? Nokogiri if not String === data or data.include? '<' or data.include? '&' data = Nokogiri::HTML::fragment(data.to_s).to_xml # fix CDATA in most cases (notably scripts) data.gsub!(/<!\[CDATA\[(.*?)\]\]>/m) do if $1.include? '<' or $1.include? '&' "//<![CDATA[\n#{$1}\n//]]>" else $1 end end # fix CDATA for style elements data.gsub!(/<style([^>])*>\/\/<!\[CDATA\[\n(.*?)\s+\/\/\]\]>/m) do if $2.include? '<' or $2.include? '&' "<style#{$1}>/*<![CDATA[*/\n#{$2.gsub("\n\Z",'')}\n/*]]>*/" else $1 end end end end if String === data @node.children << data else @node.add_child data end end |
#[](*children) ⇒ Object
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 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 |
# File 'lib/wunderbar/builder.rb', line 277 def [](*children) if children.length == 1 and children.first.respond_to? :root children = [children.first.root] end # remove leading and trailing space if children.first.text? and children.first.text.strip.empty? children.shift end if not children.empty? children.pop if children.last.text? and children.last.text.strip.empty? end children.map do |child| if child.text? or child.cdata? text = child.text if not @indentation_enabled text! text elsif text.strip.empty? text! "" if text.count("\n")>1 else indented_text! text end elsif child.comment? comment! child.text.sub(/\A /,'').sub(/ \Z/, '') elsif HtmlMarkup.flatten? child.children block_element = Proc.new do |node| node.element? and HtmlMarkup::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. tag!(child) do children = child.children.to_a while not children.empty? stop = children.index(&block_element) if stop == 0 self[children.shift] else compact!(nil) do self[*children.shift(stop || children.length)] end end end end else # disable indentation on the entire element compact! do tag!(child) {self[*child.children]} end end elsif child.children.empty? and HtmlMarkup::VOID.include? child.name tag!(child) elsif child.children.all?(&:text?) tag!(child, child.text.strip) elsif child.children.any?(&:cdata?) and child.text =~ /[<&]/ self << child elsif child.name == 'pre' compact! { tag!(child) {self[*child.children]} } elsif child.name == 'head' head = tag!(child) {self[*child.children]} html = @doc.children.last if html.name == :html head.parent.children.pop html.children.unshift head head.parent = html end head else tag!(child) {self[*child.children]} end end end |
#clear! ⇒ Object
93 94 95 96 |
# File 'lib/wunderbar/builder.rb', line 93 def clear! @doc.children.clear @node = @doc end |
#comment!(text) ⇒ Object
77 78 79 |
# File 'lib/wunderbar/builder.rb', line 77 def comment! text @node.children << CommentNode.new(text) end |
#compact!(&block) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/wunderbar/builder.rb', line 98 def compact!(&block) begin @indentation_enabled = false block.call ensure @indentation_enabled = true end end |
#declare!(*args) ⇒ Object
73 74 75 |
# File 'lib/wunderbar/builder.rb', line 73 def declare! *args @node.children << DocTypeNode.new(*args) end |
#indented_text!(text) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/wunderbar/builder.rb', line 81 def indented_text!(text) return if text.length == 0 and not @spaced text = IndentedTextNode.new(text) text.extend SpacedNode if @spaced @node.children << text @spaced = false end |
#methods ⇒ Object
57 58 59 60 61 |
# File 'lib/wunderbar/builder.rb', line 57 def methods result = super + Wunderbar.methods result += @_scope.methods if @_scope result.uniq end |
#pdf=(value) ⇒ Object
160 161 162 |
# File 'lib/wunderbar/builder.rb', line 160 def pdf=(value) @_pdf = value end |
#pdf? ⇒ Boolean
164 165 166 |
# File 'lib/wunderbar/builder.rb', line 164 def pdf? @_pdf end |
#respond_to?(method) ⇒ Boolean
63 64 65 66 67 |
# File 'lib/wunderbar/builder.rb', line 63 def respond_to?(method) respond true if Wunderbar.respond_to? method respond true if @_scope and @_scope.respond_to? method super end |
#spaced! ⇒ Object
107 108 109 |
# File 'lib/wunderbar/builder.rb', line 107 def spaced! @spaced = true end |
#system(command, opts = {}) ⇒ Object
execute a system command, echoing stdin, stdout, and stderr
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 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 |
# File 'lib/wunderbar/builder.rb', line 169 def system(command, opts={}) if command.respond_to? :flatten flat = command.flatten secret = command - flat begin # if available, use escape as it does prettier quoting require 'escape' echo = Escape.shell_command(command.compact - secret) rescue LoadError # std-lib function that gets the job done require 'shellwords' echo = Shellwords.join(command.compact - secret) end command = flat.compact.map(&:dup).map(&:untaint) else echo = command command = [command] end patterns = opts[:hilite] || [] patterns=[patterns] if String === patterns or Regexp === patterns patterns.map! do |pattern| String === pattern ? Regexp.new(Regexp.escape(pattern)) : pattern end require 'open3' tag = opts[:tag] || 'pre' output_class = opts[:class] || {} stdin = output_class[:stdin] || '_stdin' stdout = output_class[:stdout] || '_stdout' stderr = output_class[:stderr] || '_stderr' hilite = output_class[:hilite] || '_stdout _hilite' tag! tag, echo, :class=>stdin unless opts[:echo] == false require 'thread' semaphore = Mutex.new Open3.popen3(*command) do |pin, pout, perr, wait| [ Thread.new do until pout.eof? out_line = pout.readline.chomp semaphore.synchronize do if patterns.any? {|pattern| out_line =~ pattern} tag! tag, out_line, :class=>hilite else tag! tag, out_line, :class=>stdout end end end end, Thread.new do until perr.eof? err_line = perr.readline.chomp semaphore.synchronize do tag! tag, err_line, :class=>stderr end end end, Thread.new do if opts[:stdin].respond_to? :read require 'fileutils' FileUtils.copy_stream opts[:stdin], pin elsif opts[:stdin] pin.write opts[:stdin].to_s end pin.close end ].each {|thread| thread.join} wait and wait.value.exitstatus end end |
#tag!(sym, *args, &block) ⇒ Object
avoid method_missing overhead for the most common case
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 153 154 155 156 157 158 |
# File 'lib/wunderbar/builder.rb', line 112 def tag!(sym, *args, &block) current_node = @node if sym.respond_to? :children node = sym attributes = node.attributes if node.attribute_nodes.any?(&:namespace) attributes = Hash[node.attribute_nodes.map { |attr| name = attr.name name = "#{attr.namespace.prefix}:#{name}" if attr.namespace [name, attr.value] }] end attributes.merge!(node.namespaces) if node.namespaces args.push attributes if node.namespace and node.namespace.prefix sym = "#{node.namespace.prefix}:#{node.name}" else sym = node.name end end if Class === args.first and args.first < Node node = args.shift.new sym, *args else node = Node.new sym, *args end node.extend CompactNode unless @indentation_enabled if @spaced node.extend SpacedNode @spaced = false end node.text = args.first if String === args.first @node.add_child node @node = node if block block.call(self) @node.children << nil if @node.children.empty? end node ensure @node = current_node end |
#target! ⇒ Object
89 90 91 |
# File 'lib/wunderbar/builder.rb', line 89 def target! "#{@doc.serialize(indent: ' ' * @_indent, width: @width).join("\n")}\n" end |