module Stack module Template attr_accessor :generator attr_accessor :basename attr_accessor :path attr_accessor :raw def initialize(path, generator = nil) self.generator = generator self.path = path if File.file?(path) self.basename = File.basename(path) read end end def read f = File.open(self.path) self.raw = f.read f.close end def render(_payload = { }, do_layout = true) _payload = template_payload.deep_merge(_payload) content = Liquid::Template.parse(self.raw).render(_payload) content = self.transform(content) if (layout_name = _payload[:layout]) and (do_layout) _payload.delete(:layout) begin _payload = _payload.deep_merge({ "content" => content }) puts _payload content = self.generator.layouts[layout_name].render(_payload) rescue => e STDERR.puts e.to_str end end return content end def template_payload { :generator => self.generator.to_hash }.merge(self.payload) end def payload raise InterfaceNotProvided end def transform(content = self.raw) case self.basename when /\.textile/ RedCloth.new(content).to_html when /\.(mdown|markdown)/ Maruku.new(content).to_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) end def write_basename if basename ext = File.extname(basename) basename[0..(basename.length - ext.length - 1)] else @write_basename ||= Digest::MD5.hexdigest(self.raw) end end def write_filename "#{write_basename}.html" end end end