# 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::Filter 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(input, options) @input = input @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 = Liquid::Template.parse(template_file.read) @extension = options[:extension] @mime_type = options[:mime_type] @strip_extension_re = nil @strip_extension_re = /\.#{Regexp.quote(@extension)}$/ if @extension end def each_entry_path @input.each_entry_path do |path| unless @extension.nil? yield "#{path}.#{@extension}" else yield path end end self end def get_entry(path) path = path.sub(@strip_extension_re, '') if @strip_extension_re entry = @input.get_entry(path) output_entry = {} output_entry['content'] = Hx::LazyContent.new do @template.render( 'now' => Time.now, 'options' => @options, 'path' => path, 'entry' => entry ) end output_entry['mime_type'] = @mime_type if @mime_type if entry.has_key? 'updated' output_entry['created'] = output_entry['updated'] = entry['updated'] end output_entry end end end end