# hx/output/liquidtemplate - Liquid templates for Hx # # Copyright (c) 2009-2010 MenTaLguY # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. require 'rubygems' require 'time' require 'liquid' require 'redcloth' require 'cgi' require 'hx' module Hx module Output class LiquidTemplate include Hx::Source module TextFilters def textilize(input) RedCloth.new(input).to_html end def escape_url(input) CGI.escape(input) end def escape_xml(input) CGI.escapeHTML(input) end def path_to_url(input, base_url) "#{base_url}#{input}" end def handleize(input) "id_#{input.to_s.gsub(/[^A-Za-z0-9]/, '_')}" end def xsd_datetime(input) input = Time.parse(input) unless Time === input input.xmlschema end end def initialize(source, options) @source = source @options = {} for key, value in options @options[key.to_s] = value end template_dir = Hx.get_pathname(options, :template_dir) # global, so all LiquidTemplate instances kind of have to agree on the # same template directory for things to work right Liquid::Template.file_system = Liquid::LocalFileSystem.new(template_dir) Liquid::Template.register_filter(TextFilters) template_file = template_dir + options[:template] @template = template_file.open('r') { |s| Liquid::Template.parse(s.read) } @extension = options[:extension] end def each_entry @source.each_entry do |path, entry| unless @extension.nil? output_path = "#{path}.#{@extension}" else output_path = path end output_entry = entry.dup output_entry['content'] = @template.render( 'now' => Time.now, 'options' => @options, 'path' => path, 'entry' => entry ) yield output_path, output_entry end self end end end end