diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7b2d50e4..a4eabcb4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -462,6 +462,7 @@ module ApplicationHelper only_path = options.delete(:only_path) == false ? false : true begin + ChiliProject::Liquid::Legacy.run_macros(text) liquid_template = ChiliProject::Liquid::Template.parse(text) liquid_variables = get_view_instance_variables_for_liquid liquid_variables.merge!({'current_user' => User.current}) diff --git a/lib/chili_project/liquid.rb b/lib/chili_project/liquid.rb index e9aeffd3..9dae8d24 100644 --- a/lib/chili_project/liquid.rb +++ b/lib/chili_project/liquid.rb @@ -4,4 +4,4 @@ require 'chili_project/liquid/tags' module ChiliProject module Liquid end -end +end \ No newline at end of file diff --git a/lib/chili_project/liquid/legacy.rb b/lib/chili_project/liquid/legacy.rb new file mode 100644 index 00000000..c2a23768 --- /dev/null +++ b/lib/chili_project/liquid/legacy.rb @@ -0,0 +1,57 @@ +module ChiliProject + module Liquid + # Legacy is used to support older Redmine style macros by converting + # them to Liquid objects (tags, filters) on the fly by doing basic + # string substitution. This is done before the Liquid processing + # so the converted macros work like normal + # + module Legacy + # Holds the list of legacy macros + # + # @param [Regexp] :match The regex to match on the legacy macro + # @param [String] :replace The string to replace with. E.g. "%" converts + # "{{ }}" to "{% %}" + # @param [String] :new_name The new name of the Liquid object + def self.macros + @macros ||= {} + end + + # "Runs" legacy macros by doing a gsub of their values to the new Liquid ones + # + # @param [String] content The pre-Liquid content + def self.run_macros(content) + macros.each do |macro_name, macro| + next unless macro[:match].present? && macro[:replace].present? + content.gsub!(macro[:match]) do |match| + # Use block form so $1 and $2 are set properly + "{#{macro[:replace]} #{macro[:new_name]} '#{$2}' #{macro[:replace]}}" + end + end + end + + # Add support for a legacy macro syntax that was converted to liquid + # + # @param [String] name The legacy macro name + # @param [Symbol] liquid_type The type of Liquid object to use. Supported: :tag + # @param [optional, String] new_name The new name of the liquid object, used + # to rename a macro + def self.add(name, liquid_type, new_name=nil) + new_name = name unless new_name.present? + case liquid_type + when :tag + + macros[name.to_s] = { + # Example values the regex matches + # {{name}} + # {{ name }} + # {{ name 'arg' }} + # {{ name('arg') }} + :match => Regexp.new(/\{\{(#{name})(?:\(([^\}]*)\))?\}\}/), + :replace => "%", + :new_name => new_name + } + end + end + end + end +end diff --git a/lib/chili_project/liquid/variables.rb b/lib/chili_project/liquid/variables.rb new file mode 100644 index 00000000..ad23da38 --- /dev/null +++ b/lib/chili_project/liquid/variables.rb @@ -0,0 +1,14 @@ +module ChiliProject + module Liquid + module Variables + # Liquid "variables" that are used for backwards compatability with macros + # + # Variables are used in liquid like {{var}} + def self.macro_backwards_compatibility + { + 'macro_list' => "Use the '{% variable_list %}' tag to see all Liquid variables and '{% tag_list %}' to see all of the Liquid tags." + } + end + end + end +end