Module: ChiliProject::Liquid::Legacy

Defined in:
lib/chili_project/liquid/legacy.rb

Overview

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

Class Method Summary collapse

Class Method Details

.add(name, liquid_type, new_name = nil) ⇒ Object

Add support for a legacy macro syntax that was converted to liquid


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chili_project/liquid/legacy.rb', line 54

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

.macrosObject

Holds the list of legacy macros


29
30
31
# File 'lib/chili_project/liquid/legacy.rb', line 29

def self.macros
  @macros ||= {}
end

.run_macros(content) ⇒ Object

“Runs” legacy macros by doing a gsub of their values to the new Liquid ones


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chili_project/liquid/legacy.rb', line 36

def self.run_macros(content)
  macros.each do |macro_name, macro|
    next unless macro[:match].present? && macro[:replace].present?
    content = content.gsub(macro[:match]) do |match|
      # Use block form so $1 and $2 are set properly
      args = " '#{$2}'" if $2
      "{#{macro[:replace]} #{macro[:new_name]}#{args} #{macro[:replace]}}"
    end
  end
  content
end