Class: Hpricot::Elem

Inherits:
Object
  • Object
show all
Defined in:
lib/html2slim/hpricot_monkeypatches.rb

Instance Method Summary collapse

Instance Method Details

#slim(lvl = 0) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/html2slim/hpricot_monkeypatches.rb', line 37

def slim(lvl=0)
  r = ('  ' * lvl)
  if self.name == 'div' and (self.has_attribute?('id') || self.has_attribute?('class'))
    r += ''
  else
    r += self.name
  end
  if(self.has_attribute?('id'))
    r += "##{self['id']}"
    self.remove_attribute('id')
  end
  if(self.has_attribute?('class'))
    r += ".#{self['class'].split(/\s+/).join('.')}"
    self.remove_attribute('class')
  end
  unless attributes_as_html.to_s.strip.empty?
    r += "[#{attributes_as_html.to_s.strip}]"
  end
  r
end

#to_slim(lvl = 0) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/html2slim/hpricot_monkeypatches.rb', line 57

def to_slim(lvl=0)
  if respond_to?(:children) and children
    return %(#{self.slim(lvl)}\n#{children.map { |x| x.to_slim(lvl+1) }.select{|e| !e.nil? }.join("\n")})
  else
    self.slim(lvl)
  end
end