Chris@0
|
1 # redMine - project management software
|
Chris@0
|
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 module Redmine
|
Chris@0
|
19 module WikiFormatting
|
Chris@0
|
20 module Macros
|
Chris@0
|
21 module Definitions
|
Chris@0
|
22 def exec_macro(name, obj, args)
|
Chris@0
|
23 method_name = "macro_#{name}"
|
Chris@0
|
24 send(method_name, obj, args) if respond_to?(method_name)
|
Chris@0
|
25 end
|
Chris@0
|
26
|
Chris@0
|
27 def extract_macro_options(args, *keys)
|
Chris@0
|
28 options = {}
|
Chris@0
|
29 while args.last.to_s.strip =~ %r{^(.+)\=(.+)$} && keys.include?($1.downcase.to_sym)
|
Chris@0
|
30 options[$1.downcase.to_sym] = $2
|
Chris@0
|
31 args.pop
|
Chris@0
|
32 end
|
Chris@0
|
33 return [args, options]
|
Chris@0
|
34 end
|
Chris@0
|
35 end
|
Chris@0
|
36
|
Chris@0
|
37 @@available_macros = {}
|
Chris@0
|
38
|
Chris@0
|
39 class << self
|
Chris@0
|
40 # Called with a block to define additional macros.
|
Chris@0
|
41 # Macro blocks accept 2 arguments:
|
Chris@0
|
42 # * obj: the object that is rendered
|
Chris@0
|
43 # * args: macro arguments
|
Chris@0
|
44 #
|
Chris@0
|
45 # Plugins can use this method to define new macros:
|
Chris@0
|
46 #
|
Chris@0
|
47 # Redmine::WikiFormatting::Macros.register do
|
Chris@0
|
48 # desc "This is my macro"
|
Chris@0
|
49 # macro :my_macro do |obj, args|
|
Chris@0
|
50 # "My macro output"
|
Chris@0
|
51 # end
|
Chris@0
|
52 # end
|
Chris@0
|
53 def register(&block)
|
Chris@0
|
54 class_eval(&block) if block_given?
|
Chris@0
|
55 end
|
Chris@0
|
56
|
Chris@0
|
57 private
|
Chris@0
|
58 # Defines a new macro with the given name and block.
|
Chris@0
|
59 def macro(name, &block)
|
Chris@0
|
60 name = name.to_sym if name.is_a?(String)
|
Chris@0
|
61 @@available_macros[name] = @@desc || ''
|
Chris@0
|
62 @@desc = nil
|
Chris@0
|
63 raise "Can not create a macro without a block!" unless block_given?
|
Chris@0
|
64 Definitions.send :define_method, "macro_#{name}".downcase, &block
|
Chris@0
|
65 end
|
Chris@0
|
66
|
Chris@0
|
67 # Sets description for the next macro to be defined
|
Chris@0
|
68 def desc(txt)
|
Chris@0
|
69 @@desc = txt
|
Chris@0
|
70 end
|
Chris@0
|
71 end
|
Chris@0
|
72
|
Chris@0
|
73 # Builtin macros
|
Chris@0
|
74 desc "Sample macro."
|
Chris@0
|
75 macro :hello_world do |obj, args|
|
Chris@0
|
76 "Hello world! Object: #{obj.class.name}, " + (args.empty? ? "Called with no argument." : "Arguments: #{args.join(', ')}")
|
Chris@0
|
77 end
|
Chris@0
|
78
|
Chris@0
|
79 desc "Displays a list of all available macros, including description if available."
|
Chris@0
|
80 macro :macro_list do
|
Chris@0
|
81 out = ''
|
Chris@0
|
82 @@available_macros.keys.collect(&:to_s).sort.each do |macro|
|
Chris@0
|
83 out << content_tag('dt', content_tag('code', macro))
|
Chris@0
|
84 out << content_tag('dd', textilizable(@@available_macros[macro.to_sym]))
|
Chris@0
|
85 end
|
Chris@0
|
86 content_tag('dl', out)
|
Chris@0
|
87 end
|
Chris@0
|
88
|
Chris@0
|
89 desc "Displays a list of child pages. With no argument, it displays the child pages of the current wiki page. Examples:\n\n" +
|
Chris@0
|
90 " !{{child_pages}} -- can be used from a wiki page only\n" +
|
Chris@0
|
91 " !{{child_pages(Foo)}} -- lists all children of page Foo\n" +
|
Chris@0
|
92 " !{{child_pages(Foo, parent=1)}} -- same as above with a link to page Foo"
|
Chris@0
|
93 macro :child_pages do |obj, args|
|
Chris@0
|
94 args, options = extract_macro_options(args, :parent)
|
Chris@0
|
95 page = nil
|
Chris@0
|
96 if args.size > 0
|
Chris@0
|
97 page = Wiki.find_page(args.first.to_s, :project => @project)
|
Chris@0
|
98 elsif obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)
|
Chris@0
|
99 page = obj.page
|
Chris@0
|
100 else
|
Chris@0
|
101 raise 'With no argument, this macro can be called from wiki pages only.'
|
Chris@0
|
102 end
|
Chris@0
|
103 raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project)
|
Chris@0
|
104 pages = ([page] + page.descendants).group_by(&:parent_id)
|
Chris@0
|
105 render_page_hierarchy(pages, options[:parent] ? page.parent_id : page.id)
|
Chris@0
|
106 end
|
Chris@0
|
107
|
Chris@0
|
108 desc "Include a wiki page. Example:\n\n !{{include(Foo)}}\n\nor to include a page of a specific project wiki:\n\n !{{include(projectname:Foo)}}"
|
Chris@0
|
109 macro :include do |obj, args|
|
Chris@0
|
110 page = Wiki.find_page(args.first.to_s, :project => @project)
|
Chris@0
|
111 raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project)
|
Chris@0
|
112 @included_wiki_pages ||= []
|
Chris@0
|
113 raise 'Circular inclusion detected' if @included_wiki_pages.include?(page.title)
|
Chris@0
|
114 @included_wiki_pages << page.title
|
chris@37
|
115 out = textilizable(page.content, :text, :attachments => page.attachments, :headings => false)
|
Chris@0
|
116 @included_wiki_pages.pop
|
Chris@0
|
117 out
|
Chris@0
|
118 end
|
Chris@0
|
119 end
|
Chris@0
|
120 end
|
Chris@0
|
121 end
|