# 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 '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 class TemplateSource include Hx::Filter def initialize(options) # global, so all LiquidTemplate instances have to agree on the # same template directory for things to work right @template_dir = Hx.get_pathname(options, :template_dir) Liquid::Template.file_system = Liquid::LocalFileSystem.new(@template_dir) Liquid::Template.register_filter(TextFilters) end def each_entry_path(selector) # enumeration currently unneeded self end def get_entry(path) template_file = @template_dir + "#{path}.liquid" {"template" => Liquid::Template.parse(template_file.read)} end end def initialize(input, options) @input = input @options = {} for key, value in options @options[key.to_s] = value end begin LiquidTemplate.const_get(:TEMPLATE_SOURCE) rescue NameError # use filter cache to avoid template re-parse # (doesn't help with partials yet) template_source = Cache.new(TemplateSource.new(options)) LiquidTemplate.const_set(:TEMPLATE_SOURCE, template_source) end @template_name = options[:template] # force early failure in case of a missing template TEMPLATE_SOURCE.get_entry(@template_name) @extension = options[:extension] @content_type = options[:content_type] @strip_extension_re = nil @strip_extension_re = /\.#{Regexp.quote(@extension)}$/ if @extension end def each_entry_path(selector) @input.each_entry_path(Path::ALL) do |path| path = "#{path}.#{@extension}" unless @extension.nil? yield path if selector.accept_path? path end self end def get_entry(path) path = path.sub(@strip_extension_re, '') if @strip_extension_re entry = @input.get_entry(path) output_entry = {} template = TEMPLATE_SOURCE.get_entry(@template_name)["template"] output_entry['content'] = Hx::LazyContent.new do template.render( 'now' => Time.now, 'options' => @options, 'path' => path, 'entry' => entry ) end output_entry['content_type'] = @content_type if @content_type if entry.has_key? 'updated' output_entry['created'] = output_entry['updated'] = entry['updated'] end output_entry end end end end