diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2007-11-12 14:36:33 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2007-11-12 14:36:33 +0000 |
commit | 8a8f819d273e25fa28fb492da92125fb06d1ab01 (patch) | |
tree | 110c62a735f1bddcd3996b25a4f0cab56ddd5553 /lib/redmine/wiki_formatting | |
parent | a8419c1425b55b91edf24db044725d099b6065a8 (diff) | |
download | redmine-8a8f819d273e25fa28fb492da92125fb06d1ab01.tar.gz redmine-8a8f819d273e25fa28fb492da92125fb06d1ab01.zip |
Added wiki macros support. 2 builtin macros are defined: hello_world (sample macro that displays the arguments) and macro_list (display the list of installed macros).
git-svn-id: http://redmine.rubyforge.org/svn/trunk@897 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib/redmine/wiki_formatting')
-rw-r--r-- | lib/redmine/wiki_formatting/macros.rb | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/redmine/wiki_formatting/macros.rb b/lib/redmine/wiki_formatting/macros.rb new file mode 100644 index 000000000..f9920afdb --- /dev/null +++ b/lib/redmine/wiki_formatting/macros.rb @@ -0,0 +1,81 @@ +# redMine - project management software +# Copyright (C) 2006-2007 Jean-Philippe Lang +# +# 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. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +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 + end + + @@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) + class_eval(&block) if block_given? + end + + private + # 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 + 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 + end + end + + # Builtin macros + desc "Example macro." + macro :hello_world do |obj, args| + "Hello world! Object: #{obj.class.name}, " + (args.empty? ? "Called with no argument." : "Arguments: #{args.join(', ')}") + end + + desc "Displays a list of all available macros, including description if available." + macro :macro_list do + out = '' + @@available_macros.keys.collect(&:to_s).sort.each do |macro| + out << content_tag('dt', content_tag('code', macro)) + out << content_tag('dd', simple_format(@@available_macros[macro.to_sym])) + end + content_tag('dl', out) + end + end + end +end |