2011-12-19 00:19:01 +04:00
|
|
|
#-- encoding: UTF-8
|
2011-12-18 03:50:52 +04:00
|
|
|
#-- copyright
|
|
|
|
# ChiliProject is a project management system.
|
|
|
|
#
|
2012-01-18 22:25:13 +04:00
|
|
|
# Copyright (C) 2010-2012 the ChiliProject Team
|
2011-12-18 03:50:52 +04:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See doc/COPYRIGHT.rdoc for more details.
|
|
|
|
#++
|
|
|
|
|
2011-05-11 21:11:24 +04:00
|
|
|
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?
|
2011-11-26 19:10:24 +04:00
|
|
|
content = content.gsub(macro[:match]) do |match|
|
2011-05-11 21:11:24 +04:00
|
|
|
# Use block form so $1 and $2 are set properly
|
2011-12-18 01:10:53 +04:00
|
|
|
args = " '#{$2}'" if $2
|
|
|
|
"{#{macro[:replace]} #{macro[:new_name]}#{args} #{macro[:replace]}}"
|
2011-05-11 21:11:24 +04:00
|
|
|
end
|
|
|
|
end
|
2011-11-26 19:10:24 +04:00
|
|
|
content
|
2011-05-11 21:11:24 +04:00
|
|
|
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
|
2011-12-18 01:01:20 +04:00
|
|
|
|
|
|
|
# FIXME: remove the deprecated syntax for 4.0, provide a way to migrate
|
|
|
|
# existing pages to the new syntax.
|
|
|
|
#
|
|
|
|
# See ChiliProject::Liquid::Tags for the registration of the tags.
|
|
|
|
Legacy.add('child_pages', :tag)
|
|
|
|
Legacy.add('hello_world', :tag)
|
|
|
|
Legacy.add('include', :tag)
|
2011-12-18 01:07:52 +04:00
|
|
|
|
|
|
|
# Transform the old textile TOC tags to syntax suported by liquid
|
|
|
|
Legacy.add('toc', :tag)
|
|
|
|
Legacy.add('>toc', :tag, "toc_right")
|
|
|
|
Legacy.add('<toc', :tag, "toc_left")
|
|
|
|
|
2011-05-11 21:11:24 +04:00
|
|
|
end
|
|
|
|
end
|