class HltSiteBuilder
Public Class Methods
new(file, options={})
click to toggle source
# File lib/hlt-site_builder.rb, line 29 def initialize(file, options={}) @opt = {style: true}.merge options @dynarex = Dynarex.new @dynarex.parse File.read file if @dynarex.summary['container_id'] then @opt.merge!(container_id: @dynarex.summary['container_id']) end keys = @dynarex.records.keys @template = keys.shift @pages = keys.inject({}) do |r, x| label, val = x.split(/\n/,2) r.merge({label.strip.to_sym => val}) end s = @pages.keys.map do |name| "def #{name.to_s.downcase.gsub(/\s+/,'_').gsub(/\W+/,'')}() @pages[:'#{name}']; end" end.join("\n") self.instance_eval s end
Public Instance Methods
docs_each() { |filename, rexle| ... }
click to toggle source
# File lib/hlt-site_builder.rb, line 55 def docs_each() self.pages_each do |filename, filecontents| yield(filename, Rexle.new(filecontents)) end end
generate(options={}, &blk)
click to toggle source
# File lib/hlt-site_builder.rb, line 63 def generate(options={}, &blk) opt = {css: false}.merge options style = @opt[:style] if opt[:css] == true then @opt[:style] = true generate_pages @pages.keys, &blk @htc = HtmlToCss.new path = 'css' FileUtils.mkdir_p path unless File.exists? path File.write 'css/layout.css', @htc.to_layout File.write 'css/style.css', @htc.to_style @opt[:style] = style end generate_pages @pages.keys, &blk end
pages_each() { |filename, content| ... }
click to toggle source
# File lib/hlt-site_builder.rb, line 95 def pages_each() pages = @pages.to_a pages[0][0] = 'index' pages.each do |page| pg_name = page[0] filename = format_filename(pg_name) content = File.read filename yield(filename, content) end end
to_layout()
click to toggle source
# File lib/hlt-site_builder.rb, line 92 def to_layout() @htc.to_layout end
to_style()
click to toggle source
# File lib/hlt-site_builder.rb, line 93 def to_style() @htc.to_style end
Private Instance Methods
format_filename(name)
click to toggle source
# File lib/hlt-site_builder.rb, line 111 def format_filename(name) "%s.html" % name.to_s.downcase.gsub(' ','-') end
Also aliased as: f
generate_pages(a, &blk)
click to toggle source
generate the html for the 1st item.
# File lib/hlt-site_builder.rb, line 119 def generate_pages(a, &blk) container_selector = @opt[:container_selector] a.each do |name| doc = @template.to_doc doc2 = ("main\n" + @pages[name]).to_doc e = doc.css(container_selector).first e2 = e.element('./.') # if there is child elements within the target element then we add # the new nodes before it. if e2 then doc2.root.elements.each do |node| e2.insert_before node end else doc2.root.elements.each do |node| e.add node end end title = doc.root.element('head/title') title.text = name.to_s + ' | ' + title.text.unescape if block_given? then blk.call(name.to_s.downcase.gsub(/\s+/,'_').gsub(/\W+/,''), doc) end filename = format_filename(name) 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 #puts 'saving ' + filename File.write filename, doc.content(pretty: true) end FileUtils.mv f(a[0]), 'index.html' end