class DomRender

Attributes

to_a[R]

Public Class Methods

new(x, debug: false) click to toggle source
# 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

Public Instance Methods

render(x) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/dom_render.rb, line 114
def script(e, attributes)
  []
end
style(e, attributes) click to toggle source
# File lib/dom_render.rb, line 118
def style(e, attributes)
  []
end

Private Instance Methods

expand_shorthand(s) click to toggle source

expands a CSS shorthand for a margin or padding property e.g. “1em 1.5em” #=> ['1em','1.5em','1em','1.5em']

# File lib/dom_render.rb, line 148
def expand_shorthand(s)
  puts 'inside expand'
  a = s.scan(/\d+(?:\.\d+)?\s*(?:em|px)?/)

  case a.length
  when 1 then [a[0]] * 4
  when 2 then [a[0],a[-1],a[0],a[-1]]
  when 4 then a
  else
    (a + [0,0,0,0]).take 4
  end
  
end
fetch_style(attributes={}) click to toggle source
# File lib/dom_render.rb, line 135
def fetch_style(attributes={})

  attributes[:style].split(';').inject({}) do |r, x|

    k, v = x.split(':',2)
    r.merge(k.to_sym => v)
  end

end