Class: Hlt

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_s, options = {}) ⇒ Hlt

Returns a new instance of Hlt.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/hlt.rb', line 12

def initialize(raw_s, options={})
  
  opt = {pretty: true, declaration: true, style: true}.merge(options)

  # strip out lines which are blank or only contain a comment
  #s = raw_s.lines.to_a.reject!{|x| x[/(^\s+\#\s)|(^\s*$)/] }
      
  s2, markdown = fetch_markdown raw_s
  s, xml_list = filter_xml(s2)

  #s = raw_s
  # strip out the text from the line containing a comment
  s.gsub!(/((^#\s|\s#\s).*)/,'').strip if s[/((^#\s|\s#\s).*)/]
  a_code = s.scan(/^\[([^\]]+)\]\n/).map(&:first)
  s.gsub!(/\n\[[^\]]+\]\n/, " !CODE\n")

  s2 = s.lines.to_a.map!{|line| 

    hash = "(\s*\{[^\}]+\})?"

    r = line.sub(/^\s*(\w+)?(?:[\.#]\w+){1,}#{hash}/) do |x| 

      raw_attrs = x.slice!(/\{.*\}/)
      attrs = raw_attrs[1..-2] if raw_attrs

      a2 = []
      tag = x[/(^\s*\w*)[#\.]/,1] || 'div'
      tag += 'div' if tag.strip.empty?

      x.sub(/(?:\.\w+){1,}/) do |x2|
        a = x2[/(?:\.\w+){1,}/].split('.')
        a.shift
        a2 << "class: '%s'" % a.join(' ')
      end

      x.sub(/#\w+/) {|x2| a2 << "id: '%s'" % x2[1..-1] }

      a2 << attrs if attrs
      "%s {%s}" % [tag, a2.join(', ')]

    end

    r
  }.join


  raw_html = LineTree.new(s2).to_xml

  html = raw_html.gsub('!CODE').with_index do |x,i| 
    "\n\n" + a_code[i].lines.map{|x| ' ' * 4 + x}.join + "\n"
  end

  markdown.each.with_index do |x,i|
    html.sub!(/<markdown:#{i}\/>/, RDiscount.new(x).to_html
      .gsub(/<(\w+)>{style:\s*['"]([^'"]+)[^\}]+\}/,'<\1 style=\'\2\'>'))
  end
  
  doc = Rexle.new(html)
  
  xml_list.each.with_index do |xml,i|
    e = doc.root.element('//xml:' + i.to_s)
    e.insert_before Rexle.new(xml).root
    e.delete
  end
  
  if opt[:style] == false then
    doc.root.xpath('//.[@style]').each do |e| 
      unless e.attributes[:style][/^clear:/] then
        e.attributes.delete :style 
      end
    end
  end    
  
  
  @to_html = doc.xml opt

end

Instance Attribute Details

#to_htmlObject (readonly)

Returns the value of attribute to_html.



10
11
12
# File 'lib/hlt.rb', line 10

def to_html
  @to_html
end