lib/stack/template.rb in stack-0.0.3 vs lib/stack/template.rb in stack-0.0.4

- old
+ new

@@ -2,20 +2,30 @@ module Template attr_accessor :generator attr_accessor :basename attr_accessor :path + attr_accessor :original_extension + attr_accessor :extension attr_accessor :raw def initialize(path, generator = nil) self.generator = generator self.path = path + self.original_extension = "html" if File.file?(path) self.basename = File.basename(path) + ext = File.extname(path) + + self.original_extension = ext[1, ext.length] + self.extension = self.original_extension + puts self.original_extension + puts self.extension + read end end def read @@ -24,19 +34,37 @@ f.close end def render(_payload = { }, do_layout = true) _payload = template_payload.deep_merge(_payload) - - content = Liquid::Template.parse(self.raw).render(_payload) + + layout_name = _payload[:layout] + + if (layout_name and do_layout) + puts "has layout! #{layout_name}" + + # get layout + _tpl_payload = self.generator.layouts[layout_name].template_payload + _tpl_payload.delete(:layout) + + puts _tpl_payload.inspect + puts "\n" + + _payload = _payload.merge(_tpl_payload) + end + + puts _payload.inspect + puts "\n\n" + + content = Liquid::Template.parse(self.raw).render(Mash.new(_payload)) content = self.transform(content) - if (layout_name = _payload[:layout]) and (do_layout) + if (layout_name and do_layout) _payload.delete(:layout) begin - _payload = _payload.deep_merge({ "content" => content }) - puts _payload + _payload = _payload.merge({ :content => content }) + content = self.generator.layouts[layout_name].render(_payload) rescue => e STDERR.puts e.to_str end end @@ -44,52 +72,63 @@ return content end def template_payload { + :template => self.to_hash, :generator => self.generator.to_hash }.merge(self.payload) end def payload raise InterfaceNotProvided end + def to_hash + { + + }.merge(self.payload) + end + def transform(content = self.raw) case self.basename when /\.textile/ + self.extension = "html" RedCloth.new(content).to_html - when /\.(mdown|markdown)/ + when /\.(mdown|markdown|mkdn|md)/ + self.extension = "html" Maruku.new(content).to_html + when /\.(less)/ + self.extension = "css" + Less::Engine.new(content).to_css + when /\.(liquid|liq)/ + self.extension = "html" else content end end def write!(content = self.render) - puts write_path - FileUtils.mkdir_p(File.dirname(write_path)) out = File.open(write_path, "w+") out.rewind out.write(content) out.close end def write_path - File.join(Stack::runner.configuration.target, self.write_filename) + File.join(self.generator.target, self.write_filename) end def write_basename - if basename - ext = File.extname(basename) - basename[0..(basename.length - ext.length - 1)] + if self.basename + self.basename.gsub(".#{self.original_extension}", "") else @write_basename ||= Digest::MD5.hexdigest(self.raw) end end def write_filename - "#{write_basename}.html" + "#{write_basename}.#{self.extension}" end end end \ No newline at end of file