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, nocomment: true, debug: false) ⇒ Hlt
constructor
A new instance of Hlt.
- #render(locals: {}) ⇒ Object
Constructor Details
#initialize(raw_s, pretty: true, declaration: true, style: true, nocomment: true, debug: false) ⇒ Hlt
Returns a new instance of Hlt.
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 129 130 131 132 |
# File 'lib/hlt.rb', line 15 def initialize(raw_s, pretty: true, declaration: true, style: true, nocomment: 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).debug if @debug a_code = s.scan(/^\[([^\]]+)\]\B/).map(&:first) puts ('a_code: ' + a_code.inspect).debug 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).debug if @debug raw_html = LineTree.new(*s3, ignore_non_element: false, debug: debug).to_xml puts ('raw_html: ' + raw_html.inspect).debug 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).debug puts ('x: ' + x.inspect).debug puts ('html: ' + html.inspect).debug 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).debug 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") unless nocomment comment = "\n <!-- Generated by Hlt-site_builder on the %s -->\n" % html.sub!(/(?=<\/html>)/, comment) end @to_html = html end |
Instance Attribute Details
#to_doc ⇒ Object (readonly)
Returns the value of attribute to_doc.
13 14 15 |
# File 'lib/hlt.rb', line 13 def to_doc @to_doc end |
#to_html ⇒ Object (readonly)
Returns the value of attribute to_html.
13 14 15 |
# File 'lib/hlt.rb', line 13 def to_html @to_html end |
Instance Method Details
#render(locals: {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/hlt.rb', line 134 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 |