Class: Wunderbar::Node
- Inherits:
-
Object
- Object
- Wunderbar::Node
- Defined in:
- lib/wunderbar/node.rb
Constant Summary collapse
- VOID =
%w( area base br col command embed hr img input keygen link meta param source track wbr )
- ESCAPE =
{ "'" => ''', '&' => '&', '"' => '"', '<' => '<', '>' => '>', }
Instance Attribute Summary collapse
-
#attrs ⇒ Object
Returns the value of attribute attrs.
-
#children ⇒ Object
Returns the value of attribute children.
-
#name ⇒ Object
Returns the value of attribute name.
-
#node ⇒ Object
Returns the value of attribute node.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
- #add_child(child) ⇒ Object
-
#initialize(name, *args) ⇒ Node
constructor
A new instance of Node.
- #method_missing(*args) ⇒ Object
- #serialize(options = {}, result = [], indent = '') ⇒ Object
- #walk(result, indent, options) ⇒ Object
Constructor Details
#initialize(name, *args) ⇒ Node
Returns a new instance of Node.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/wunderbar/node.rb', line 18 def initialize(name, *args) @name = name @text = nil @attrs = {} @children = [] 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/wunderbar/node.rb', line 29 def method_missing(*args) if args.length == 0 attrs[:class] = (attrs[:class].to_s.split(' ') + [name]).join(' ') else name = args.first.to_s err = NameError.new "undefined local variable or method `#{name}'", name err.set_backtrace caller raise err end end |
Instance Attribute Details
#attrs ⇒ Object
Returns the value of attribute attrs.
3 4 5 |
# File 'lib/wunderbar/node.rb', line 3 def attrs @attrs end |
#children ⇒ Object
Returns the value of attribute children.
3 4 5 |
# File 'lib/wunderbar/node.rb', line 3 def children @children end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/wunderbar/node.rb', line 3 def name @name end |
#node ⇒ Object
Returns the value of attribute node.
3 4 5 |
# File 'lib/wunderbar/node.rb', line 3 def node @node end |
#parent ⇒ Object
Returns the value of attribute parent.
3 4 5 |
# File 'lib/wunderbar/node.rb', line 3 def parent @parent end |
#text ⇒ Object
Returns the value of attribute text.
3 4 5 |
# File 'lib/wunderbar/node.rb', line 3 def text @text end |
Instance Method Details
#add_child(child) ⇒ Object
40 41 42 43 |
# File 'lib/wunderbar/node.rb', line 40 def add_child(child) @children << child child.parent = self end |
#serialize(options = {}, result = [], indent = '') ⇒ Object
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 |
# File 'lib/wunderbar/node.rb', line 62 def serialize( = {}, result = [], indent = '') line = "#{indent}<#{name}" attrs.each do |name, value| next unless value name = name.to_s.gsub('_','-') if Symbol === name value=name if value==true line += " #{name}=\"#{value.to_s.gsub(/[&\"<>]/,ESCAPE)}\"" end if children.empty? if text if [:pre] line += ">#{[:pre]}#{text}#{[:post]}</#{name}>" else width = [:width] if name != :pre line += ">#{text.to_s.gsub(/[&<>]/,ESCAPE)}</#{name}>" if indent and 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 and not CompactNode === parent work = [] walk(work, nil, ) width = [:width] if width line += ">" work = work.join + "</#{name}>" if line.length + work.length <= width line += work else # split work into tokens with balanced <> tokens = work.split(' ') (tokens.length-1).downto(1) do |i| if tokens[i].count('<') != tokens[i].count('>') tokens[i-1,2] = tokens[i-1] + ' ' + tokens[i] end end line += tokens.shift # add tokens to line, breaking when line length would exceed width tokens.each do |token| if line.length + token.length < width line += ' ' + token else result << line line = indent.to_s + token end end end else line += ">#{work.join}</#{name}>" end else result << line+">#{[:pre]}" if parent walk(result, indent, ) unless children.empty? line = "#{indent}#{[:post]}</#{name}>" end result << line if parent result end |
#walk(result, indent, options) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/wunderbar/node.rb', line 45 def walk(result, indent, ) indent += [:indent] if indent and parent first = true spaced = false children.each do |child| next unless child result << '' if (spaced or SpacedNode === child) and not first if String === child result << child else child.serialize(, result, indent) end first = false spaced = (SpacedNode === child) end end |