Class: DomRender

Inherits:
Object
  • Object
show all
Includes:
InspectArray
Defined in:
lib/dom_render.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InspectArray

#scan

Constructor Details

#initialize(x, debug: false) ⇒ DomRender

Returns a new instance of DomRender.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dom_render.rb', line 50

def initialize(x, debug: false)
                         
  @debug = debug
  
  raise "DomRender#initialize: supplied parameter cannot be nil" unless x
  
  doc = if x.kind_of? Rexle then
    x
  else
    Rexle.new x.gsub(/\n/,'')
  end
  
  @a = render doc.root
end

Instance Attribute Details

#to_a(inspect: false, verbose: false) ⇒ Object (readonly)

Returns the value of attribute to_a.



48
49
50
# File 'lib/dom_render.rb', line 48

def to_a
  @to_a
end

Instance Method Details

#render(x) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dom_render.rb', line 65

def render(x)
  
  puts 'inside render'.info if @debug
  
  style = x.attributes.has_key?(:style) ? fetch_style(x.attributes) : {}
  puts 'style: ' + style.inspect if @debug
  puts 'x.name: ' + x.name.to_sym.inspect if @debug
  r = method(x.name.to_sym).call(x, x.attributes.merge(style))
  puts 'r: ' + r.inspect if @debug
  
  return [] unless r and r.length > 0

  # jr051216 the following statement was commented out because it caused a 
  #      bug when reading style attributes by exploding the coordinates array
  #if r.last.nil? or r.last.empty? then
  #  r[0..-2].flatten(1)
  #else
    r
  #end
end

#render_all(x) ⇒ Object



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
# File 'lib/dom_render.rb', line 86

def render_all(x)

  puts 'x.children: ' + x.children.inspect if @debug
  len = x.children.length - 1

  r = x.children.map.with_index do |obj,i|

    if obj.is_a? String then
      if obj.strip.length > 0 then
        i == 0 ? obj.lstrip.sub(/\s+$/,' ') : obj.sub(/^\s+/,' ')          
      else
        ''
      end
    elsif obj.is_a? Rexle::Element
      
      puts 'obj: ' + obj.inspect
      a = render obj
      puts ('_a: ' + a.inspect).debug if @debug
      
      a.length > 1 ? a : nil
    end

  end

  r.compact

end

#script(e, attributes) ⇒ Object



114
115
116
# File 'lib/dom_render.rb', line 114

def script(e, attributes)
  []
end

#style(e, attributes) ⇒ Object



118
119
120
# File 'lib/dom_render.rb', line 118

def style(e, attributes)
  []
end