[#604] Run the output of the WikiFormatted text into Liquid

The current view's instance variables are sent to liquid dynamically
This commit is contained in:
Eric Davis 2011-04-26 12:44:02 -07:00 committed by Holger Just
parent 981143f78f
commit 72fa3ff920
1 changed files with 42 additions and 1 deletions

View File

@ -461,7 +461,29 @@ module ApplicationHelper
project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
only_path = options.delete(:only_path) == false ? false : true
text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) }
begin
text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) }
liquid_template = Liquid::Template.parse(text)
liquid_variables = get_view_instance_variables_for_liquid
liquid_variables.merge!({'current_user' => User.current})
liquid_variables.merge!({'toc' => '{{toc}}'}) # Pass toc through to replace later
liquid_variables.merge!(ChiliProject::Liquid::Variables.macro_backwards_compatibility)
# Pass :view in a register so this view (with helpers) can be used inside of a tag
text = liquid_template.render(liquid_variables, :registers => {:view => self})
# Add Liquid errors to the log
if Rails.logger && Rails.logger.debug?
msg = ""
liquid_template.errors.each do |exception|
msg << "[Liquid Error] #{exception.message}\n:\n#{exception.backtrace.join("\n")}"
msg << "\n\n"
end
Rails.logger.debug msg
end
text
rescue Liquid::SyntaxError
# Skip Liquid if there is a syntax error
end
@parsed_headings = []
text = parse_non_pre_blocks(text) do |text|
@ -1006,4 +1028,23 @@ module ApplicationHelper
def link_to_content_update(text, url_params = {}, html_options = {})
link_to(text, url_params, html_options)
end
def get_view_instance_variables_for_liquid
self.instance_variables.reject do |ivar|
ivar.match(/@_/) || # Rails "internal" variables: @_foo
ivar.match(/@template/) ||
ivar == '@output_buffer' ||
ivar == '@cookies' ||
ivar == '@helpers' ||
ivar == '@real_format' ||
ivar == '@assigns_added' ||
ivar == '@assigns' ||
ivar == '@view_paths' ||
ivar == '@controller'
end.inject({}) do |acc,ivar|
acc[ivar.sub('@','')] = instance_variable_get(ivar)
acc
end
end
end