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
This commit is contained in:
Jean-Philippe Lang
2007-11-12 14:36:33 +00:00
parent a8419c1425
commit 8a8f819d27
8 changed files with 154 additions and 15 deletions
+44 -6
View File
@@ -1,13 +1,31 @@
# 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.
require 'redcloth'
require 'coderay'
require 'pp'
module Redmine
module WikiFormatting
private
class TextileFormatter < RedCloth
RULES = [:inline_auto_link, :inline_auto_mailto, :textile, :inline_toc]
class TextileFormatter < RedCloth
RULES = [:inline_auto_link, :inline_auto_mailto, :textile, :inline_toc, :inline_macros]
def initialize(*args)
super
@@ -15,8 +33,9 @@ module Redmine
self.no_span_caps=true
end
def to_html
def to_html(*rules, &block)
@toc = []
@macros_runner = block
super(*RULES).to_s
end
@@ -72,6 +91,25 @@ module Redmine
end
end
MACROS_RE = /
\{\{ # opening tag
([\w]+) # macro name
(\(([^\}]*)\))? # optional arguments
\}\} # closing tag
/x unless const_defined?(:MACROS_RE)
def inline_macros(text)
text.gsub!(MACROS_RE) do
all, macro = $&, $1.downcase
args = ($3 || '').split(',').each(&:strip)
begin
@macros_runner.call(macro, args)
rescue => e
"<div class=\"flash error\">Error executing the <strong>#{macro}</strong> macro (#{e})</div>"
end || all
end
end
AUTO_LINK_RE = %r{
( # leading text
<\w+.*?>| # leading HTML tag, or
@@ -115,8 +153,8 @@ module Redmine
public
def self.to_html(text, options = {})
TextileFormatter.new(text).to_html
def self.to_html(text, options = {}, &block)
TextileFormatter.new(text).to_html(&block)
end
end
end
+81
View File
@@ -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