[#604] Introduce compatibility layer for third party macros.

This be removed with complete macro removal.
This commit is contained in:
Holger Just 2011-09-01 01:12:57 +02:00
parent 862c9e0fde
commit cc0526cb27
2 changed files with 33 additions and 38 deletions

View File

@ -16,7 +16,6 @@ require 'forwardable'
require 'cgi'
module ApplicationHelper
include Redmine::WikiFormatting::Macros::Definitions
include Redmine::I18n
include GravatarHelper::PublicMethods

View File

@ -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