From cc0526cb27d6b6a17834a6eb55d6789cd29454d4 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Thu, 1 Sep 2011 01:12:57 +0200 Subject: [PATCH] [#604] Introduce compatibility layer for third party macros. This be removed with complete macro removal. --- app/helpers/application_helper.rb | 1 - lib/redmine/wiki_formatting/macros.rb | 70 +++++++++++++-------------- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index a4eabcb4..f621138c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -16,7 +16,6 @@ require 'forwardable' require 'cgi' module ApplicationHelper - include Redmine::WikiFormatting::Macros::Definitions include Redmine::I18n include GravatarHelper::PublicMethods diff --git a/lib/redmine/wiki_formatting/macros.rb b/lib/redmine/wiki_formatting/macros.rb index faef7a8b..28ea57d7 100644 --- a/lib/redmine/wiki_formatting/macros.rb +++ b/lib/redmine/wiki_formatting/macros.rb @@ -12,58 +12,54 @@ # See doc/COPYRIGHT.rdoc for more details. #++ +# DECREACATED SINCE 3.0 - TO BE REMOVED IN 4.0 +# The whole macro concept is deprecated. It is to be completely replaced by +# Liquid tags and variables. + +require 'dispatcher' + module Redmine module WikiFormatting module Macros - module Definitions - def exec_macro(name, obj, args) - method_name = "macro_#{name}" - send(method_name, obj, args) if respond_to?(method_name) - end - - def extract_macro_options(args, *keys) - options = {} - while args.last.to_s.strip =~ %r{^(.+)\=(.+)$} && keys.include?($1.downcase.to_sym) - options[$1.downcase.to_sym] = $2 - args.pop - end - return [args, options] - end - end - - @@available_macros = {} + @available_macros = {} class << self - # Called with a block to define additional macros. - # Macro blocks accept 2 arguments: - # * obj: the object that is rendered - # * args: macro arguments - # - # Plugins can use this method to define new macros: - # - # Redmine::WikiFormatting::Macros.register do - # desc "This is my macro" - # macro :my_macro do |obj, args| - # "My macro output" - # end - # end def register(&block) + ActiveSupport::Deprecation.warn("Macros are deprecated. Use Liquid filters and tags instead", caller.drop(3)) class_eval(&block) if block_given? end private + # Sets description for the next macro to be defined + def desc(txt) + @desc = txt + end + # Defines a new macro with the given name and block. def macro(name, &block) name = name.to_sym if name.is_a?(String) - @@available_macros[name] = @@desc || '' - @@desc = nil + @available_macros[name] = @desc || '' + @desc = nil raise "Can not create a macro without a block!" unless block_given? - Definitions.send :define_method, "macro_#{name}".downcase, &block - end - # Sets description for the next macro to be defined - def desc(txt) - @@desc = txt + tag = Class.new(::Liquid::Tag) do + def initialize(tag_name, markup, tokens) + if markup =~ self.class::Syntax + @args = $1[1..-2].split(',').collect(&:strip) + else + raise ::Liquid::SyntaxError.new("Syntax error in tag '#{name}'") + end + end + end + tag.send :define_method, :render do |context| + context.registers[:view].instance_exec context.registers[:object], @args, &block + end + tag.const_set 'Syntax', /(#{::Liquid::QuotedFragment})/ + + Dispatcher.to_prepare do + ChiliProject::Liquid::Tags.register_tag(name, tag, :html => true) + ChiliProject::Liquid::Legacy.add(name, :tag) + end end end end