This changes how the liquid integration works. It now integrates the Textile conversion step. This was necessary because if you first convert the snippets inside of loops and conditionals from Textile to HTML, you loose some important context information which is required to e.g. build proper lists in textile. We expect the standard case that Liquid tags return Textile markup instead of HTML. Thus, we can convert the final textile markup to HTML as a very last step. To allow existing and new macros (or tags) to return HTML for advanced usage, we save their respective output into the context and put a placeholder string into the generated markup. After the transformation to HTML, we insert the previously generated HTML into the string using search+replace in lib/chili_project/liquid/template.rb. Tags have to be registered using :html => true for this special treatment.
97 lines
3.2 KiB
Ruby
97 lines
3.2 KiB
Ruby
module ChiliProject
|
|
module Liquid
|
|
class Template < ::Liquid::Template
|
|
# creates a new <tt>Template</tt> object from liquid source code
|
|
def self.parse(source)
|
|
template = self.new
|
|
template.parse(source)
|
|
template
|
|
end
|
|
|
|
|
|
def context_from_render_options(*args)
|
|
# This method is pulled out straight from the original
|
|
# Liquid::Template#render
|
|
context = case args.first
|
|
when ::Liquid::Context
|
|
args.shift
|
|
when Hash
|
|
::Liquid::Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors)
|
|
when nil
|
|
::Liquid::Context.new(assigns, instance_assigns, registers, @rethrow_errors)
|
|
else
|
|
raise ArgumentError, "Expect Hash or Liquid::Context as parameter"
|
|
end
|
|
|
|
case args.last
|
|
when Hash
|
|
options = args.pop
|
|
|
|
if options[:registers].is_a?(Hash)
|
|
self.registers.merge!(options[:registers])
|
|
end
|
|
|
|
if options[:filters]
|
|
context.add_filters(options[:filters])
|
|
end
|
|
|
|
when Module
|
|
context.add_filters(args.pop)
|
|
when Array
|
|
context.add_filters(args.pop)
|
|
end
|
|
context
|
|
end
|
|
|
|
# Render takes a hash with local variables.
|
|
#
|
|
# if you use the same filters over and over again consider registering them globally
|
|
# with <tt>Template.register_filter</tt>
|
|
#
|
|
# Following options can be passed:
|
|
#
|
|
# * <tt>filters</tt> : array with local filters
|
|
# * <tt>registers</tt> : hash with register variables. Those can be accessed from
|
|
# filters and tags and might be useful to integrate liquid more with its host application
|
|
#
|
|
def render(*args)
|
|
return '' if @root.nil?
|
|
|
|
context = context_from_render_options(*args)
|
|
context.registers[:html_results] ||= {}
|
|
|
|
# ENTER THE RENDERING STAGE
|
|
|
|
# 1. Render the input as Liquid
|
|
# Some tags might register final HTML output in this stage.
|
|
begin
|
|
# for performance reasons we get a array back here. join will make a string out of it
|
|
result = @root.render(context)
|
|
result.respond_to?(:join) ? result.join : result
|
|
ensure
|
|
@errors = context.errors
|
|
end
|
|
|
|
# 2. Perform the Wiki markup transformation (e.g. Textile)
|
|
obj = context.registers[:object]
|
|
attr = context.registers[:attribute]
|
|
result = Redmine::WikiFormatting.to_html(Setting.text_formatting, result, :object => obj, :attribute => attr)
|
|
|
|
# 3. Now finally, replace the captured raw HTML bits in the final content
|
|
length = nil
|
|
# replace HTML results until we can find no additional variables
|
|
while length != context.registers[:html_results].length do
|
|
length = context.registers[:html_results].length
|
|
context.registers[:html_results].delete_if do |key, value|
|
|
# We use the block variant to avoid the evaluation of escaped
|
|
# characters in +value+ during substitution.
|
|
result.sub!(key) { |match| value }
|
|
end
|
|
end
|
|
|
|
result
|
|
end
|
|
end
|
|
end
|
|
end
|