lib/wunderbar/node.rb in wunderbar-0.18.3 vs lib/wunderbar/node.rb in wunderbar-0.19.0
- old
+ new
@@ -18,13 +18,14 @@
def initialize(name, *args)
@name = name
@text = nil
@attrs = {}
@children = []
- @name += args.shift.inspect if Symbol === args.first
+ args -= symbols = args.find_all {|arg| Symbol === arg}
@attrs = args.pop.to_hash if args.last.respond_to? :to_hash
@text = args.shift.to_s unless args.empty?
+ symbols.each {|sym| @attrs[sym] = true}
end
def method_missing(*args)
if args.length == 0
attrs[:class] = (attrs[:class].to_s.split(' ') + [name]).join(' ')
@@ -71,26 +72,39 @@
if children.empty?
if text
if options[:pre]
line += ">#{options[:pre]}#{text}#{options[:post]}</#{name}>"
else
+ width = options[:width] if name != :pre
line += ">#{text.to_s.gsub(/[&<>]/,ESCAPE)}</#{name}>"
+ if width and line.length > width
+ reflowed = IndentedTextNode.reflow(indent, line, width)
+ line = reflowed.pop
+ result.push *reflowed
+ end
end
elsif VOID.include? name.to_s
line += "/>"
else
line += "></#{name}>"
end
elsif CompactNode === self
work = []
walk(work, nil, options)
- if @width
+ width = options[:width]
+ if width
line += ">"
(work+["</#{name}>"]).each do |node|
- if line.length + node.length > @width
+ if line.length + node.length > width
result << line.rstrip
line = indent.to_s
+ if line.length + node.length > width and !node.include? '<'
+ reflowed = IndentedTextNode.reflow(indent.to_s,
+ "#{indent}#{node}", width)
+ node = reflowed.pop.sub(/^#{indent}/, '')
+ result.push *reflowed
+ end
end
line += node
end
else
line += ">#{work.join}</#{name}>"
@@ -173,18 +187,33 @@
result << @text.to_s.gsub(/[&<>]/,ESCAPE)
end
end
class IndentedTextNode < TextNode
+ def self.reflow(indent, line, width)
+ return [line] if line.include? "\n" or not width
+
+ result = []
+ while line.length > width
+ split = line.rindex(' ', width)
+ break if not split or split <= indent.to_s.length
+ result << line[0...split]
+ line = "#{indent}#{line[split+1..-1]}"
+ end
+
+ result << line
+ end
+
def serialize(options, result, indent)
if indent
text = CDATANode.normalize(@text, indent)
else
text = @text
end
- result << text.to_s.gsub(/[&<>]/,ESCAPE)
+ result.push *IndentedTextNode.reflow(indent,
+ text.to_s.gsub(/[&<>]/,ESCAPE), options[:width])
end
end
class ScriptNode < CDATANode
def pre; "//<![CDATA["; end
@@ -194,17 +223,10 @@
class StyleNode < CDATANode
def pre; "/*<![CDATA[*/"; end
def post; "/*]]>*/"; end
end
- module CompactNode
- def width=(value)
- @width = value
- end
- def width
- @width
- end
- end
+ module CompactNode; end
module SpacedNode; end
end