Class: Hlt
- Inherits:
-
Object
- Object
- Hlt
- Defined in:
- lib/hlt.rb
Instance Attribute Summary collapse
-
#to_doc ⇒ Object
readonly
Returns the value of attribute to_doc.
-
#to_html ⇒ Object
readonly
Returns the value of attribute to_html.
Instance Method Summary collapse
-
#initialize(raw_s, pretty: true, declaration: true, style: true, debug: false) ⇒ Hlt
constructor
A new instance of Hlt.
- #render(locals: {}) ⇒ Object
Constructor Details
#initialize(raw_s, pretty: true, declaration: true, style: true, debug: false) ⇒ Hlt
Returns a new instance of Hlt.
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 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 |
# File 'lib/hlt.rb', line 14 def initialize(raw_s, pretty: true, declaration: true, style: true, debug: false) @debug = debug # strip out lines which are blank or only contain a comment #s = raw_s.lines.to_a.reject!{|x| x[/(^\s+\#\s)|(^\s*$)/] } raw_s.strip! s2, martile = fetch_martile raw_s puts 'martile: ' + martile.inspect if @debug 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).*)/] puts 's: ' + s.inspect if @debug a_code = s.scan(/^\[([^\]]+)\]\B/).map(&:first) puts 'a_code: ' + a_code.inspect if @debug s.gsub!(/\n\[[^\]]+\]\B/, " !CODE\n") s2 = s.lines.to_a.map!{|line| hash = "(\s*\{[^\}]+\})?" line.prepend ' ' line.sub!(/^(\s*)\w+: /,'\0' + "\n" + '\1') 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 } s2.unshift "root\n" s3 = s2.join.gsub(/^(\s*)-\s+/,'\1templatecode ').\ gsub(/^(\s*)=\s+/,'\1templateoutput ') puts 's3: ' + s3.inspect if @debug raw_html = LineTree.new(*s3, ignore_non_element: false, debug: debug).to_xml puts 'raw_html: ' + raw_html.inspect if @debug html = raw_html.gsub('!CODE').with_index do |x,i| "\n\n" + a_code[i].lines.map{|x| ' ' * 4 + x}.join + "\n" end martile.each.with_index do |x,i| if @debug then puts 'i: ' + i.inspect puts 'x: ' + x.inspect puts 'html: ' + html.inspect end html.sub!(/<mar(tile|kdown):#{i.to_s}\/>/, RDiscount.new(\ Martile.new(x).to_s).to_html\ .gsub(/<(\w+)>\s*{style:\s*['"]([^'"]+)[^\}]+\}/,\ '<\1 style=\'\2\'>')) end puts 'html_: ' + html.inspect if @debug 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 # remove the style attributes from the document if style == false # if style == false then doc.root.xpath('//.[@style]').each do |e| unless e.attributes[:style][/^clear:/] then e.attributes.delete :style end end end @doc = doc h = {declaration: declaration, pretty: pretty, style: style} html = doc.root.xpath('*'){|x| x.xml(h)}.join("\n") time = Time.now = time.strftime("#{ordinalize(time.day)} %B %Y @ %H:%M") comment = "\n <!-- Generated by Hlt-site_builder on the %s -->\n" % html.sub!(/(?=<\/html>)/, comment) @to_html = html end |
Instance Attribute Details
#to_doc ⇒ Object (readonly)
Returns the value of attribute to_doc.
12 13 14 |
# File 'lib/hlt.rb', line 12 def to_doc @to_doc end |
#to_html ⇒ Object (readonly)
Returns the value of attribute to_html.
12 13 14 |
# File 'lib/hlt.rb', line 12 def to_html @to_html end |
Instance Method Details
#render(locals: {}) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/hlt.rb', line 130 def render(locals: {}) variables = locals.map do |key, value| "#{key} = locals['#{key}']" end s = "xml = RexleBuilder.new\n" s << scanbuild(@doc.to_a) a = eval variables.join("\n") + "\n" + s Rexle.new(a).element('root/.') end |