@@ -1,256 +1,256 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | module Redmine |
|
19 | 19 | module WikiFormatting |
|
20 | 20 | module Macros |
|
21 | 21 | module Definitions |
|
22 | 22 | # Returns true if +name+ is the name of an existing macro |
|
23 | 23 | def macro_exists?(name) |
|
24 | 24 | Redmine::WikiFormatting::Macros.available_macros.key?(name.to_sym) |
|
25 | 25 | end |
|
26 | 26 | |
|
27 | 27 | def exec_macro(name, obj, args, text) |
|
28 | 28 | macro_options = Redmine::WikiFormatting::Macros.available_macros[name.to_sym] |
|
29 | 29 | return unless macro_options |
|
30 | 30 | |
|
31 | 31 | method_name = "macro_#{name}" |
|
32 | 32 | unless macro_options[:parse_args] == false |
|
33 | 33 | args = args.split(',').map(&:strip) |
|
34 | 34 | end |
|
35 | 35 | |
|
36 | 36 | begin |
|
37 | 37 | if self.class.instance_method(method_name).arity == 3 |
|
38 | 38 | send(method_name, obj, args, text) |
|
39 | 39 | elsif text |
|
40 | 40 | raise "This macro does not accept a block of text" |
|
41 | 41 | else |
|
42 | 42 | send(method_name, obj, args) |
|
43 | 43 | end |
|
44 | 44 | rescue => e |
|
45 | 45 | "<div class=\"flash error\">Error executing the <strong>#{h name}</strong> macro (#{h e.to_s})</div>".html_safe |
|
46 | 46 | end |
|
47 | 47 | end |
|
48 | 48 | |
|
49 | 49 | def extract_macro_options(args, *keys) |
|
50 | 50 | options = {} |
|
51 | 51 | while args.last.to_s.strip =~ %r{^(.+?)\=(.+)$} && keys.include?($1.downcase.to_sym) |
|
52 | 52 | options[$1.downcase.to_sym] = $2 |
|
53 | 53 | args.pop |
|
54 | 54 | end |
|
55 | 55 | return [args, options] |
|
56 | 56 | end |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | @@available_macros = {} |
|
60 | 60 | mattr_accessor :available_macros |
|
61 | 61 | |
|
62 | 62 | class << self |
|
63 | 63 | # Plugins can use this method to define new macros: |
|
64 | 64 | # |
|
65 | 65 | # Redmine::WikiFormatting::Macros.register do |
|
66 | 66 | # desc "This is my macro" |
|
67 | 67 | # macro :my_macro do |obj, args| |
|
68 | 68 | # "My macro output" |
|
69 | 69 | # end |
|
70 | 70 | # |
|
71 | 71 | # desc "This is my macro that accepts a block of text" |
|
72 | 72 | # macro :my_macro do |obj, args, text| |
|
73 | 73 | # "My macro output" |
|
74 | 74 | # end |
|
75 | 75 | # end |
|
76 | 76 | def register(&block) |
|
77 | 77 | class_eval(&block) if block_given? |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | # Defines a new macro with the given name, options and block. |
|
81 | 81 | # |
|
82 | 82 | # Options: |
|
83 | 83 | # * :desc - A description of the macro |
|
84 | 84 | # * :parse_args => false - Disables arguments parsing (the whole arguments |
|
85 | 85 | # string is passed to the macro) |
|
86 | 86 | # |
|
87 | 87 | # Macro blocks accept 2 or 3 arguments: |
|
88 | 88 | # * obj: the object that is rendered (eg. an Issue, a WikiContent...) |
|
89 | 89 | # * args: macro arguments |
|
90 | 90 | # * text: the block of text given to the macro (should be present only if the |
|
91 | 91 | # macro accepts a block of text). text is a String or nil if the macro is |
|
92 | 92 | # invoked without a block of text. |
|
93 | 93 | # |
|
94 | 94 | # Examples: |
|
95 | 95 | # By default, when the macro is invoked, the comma separated list of arguments |
|
96 | 96 | # is split and passed to the macro block as an array. If no argument is given |
|
97 | 97 | # the macro will be invoked with an empty array: |
|
98 | 98 | # |
|
99 | 99 | # macro :my_macro do |obj, args| |
|
100 | 100 | # # args is an array |
|
101 | 101 | # # and this macro do not accept a block of text |
|
102 | 102 | # end |
|
103 | 103 | # |
|
104 | 104 | # You can disable arguments spliting with the :parse_args => false option. In |
|
105 | 105 | # this case, the full string of arguments is passed to the macro: |
|
106 | 106 | # |
|
107 | 107 | # macro :my_macro, :parse_args => false do |obj, args| |
|
108 | 108 | # # args is a string |
|
109 | 109 | # end |
|
110 | 110 | # |
|
111 | 111 | # Macro can optionally accept a block of text: |
|
112 | 112 | # |
|
113 | 113 | # macro :my_macro do |obj, args, text| |
|
114 | 114 | # # this macro accepts a block of text |
|
115 | 115 | # end |
|
116 | 116 | # |
|
117 | 117 | # Macros are invoked in formatted text using double curly brackets. Arguments |
|
118 | 118 | # must be enclosed in parenthesis if any. A new line after the macro name or the |
|
119 | 119 | # arguments starts the block of text that will be passe to the macro (invoking |
|
120 | 120 | # a macro that do not accept a block of text with some text will fail). |
|
121 | 121 | # Examples: |
|
122 | 122 | # |
|
123 | 123 | # No arguments: |
|
124 | 124 | # {{my_macro}} |
|
125 | 125 | # |
|
126 | 126 | # With arguments: |
|
127 | 127 | # {{my_macro(arg1, arg2)}} |
|
128 | 128 | # |
|
129 | 129 | # With a block of text: |
|
130 | 130 | # {{my_macro |
|
131 | 131 | # multiple lines |
|
132 | 132 | # of text |
|
133 | 133 | # }} |
|
134 | 134 | # |
|
135 | 135 | # With arguments and a block of text |
|
136 | 136 | # {{my_macro(arg1, arg2) |
|
137 | 137 | # multiple lines |
|
138 | 138 | # of text |
|
139 | 139 | # }} |
|
140 | 140 | # |
|
141 | 141 | # If a block of text is given, the closing tag }} must be at the start of a new line. |
|
142 | 142 | def macro(name, options={}, &block) |
|
143 | 143 | options.assert_valid_keys(:desc, :parse_args) |
|
144 | 144 | unless name.to_s.match(/\A\w+\z/) |
|
145 | 145 | raise "Invalid macro name: #{name} (only 0-9, A-Z, a-z and _ characters are accepted)" |
|
146 | 146 | end |
|
147 | 147 | unless block_given? |
|
148 | 148 | raise "Can not create a macro without a block!" |
|
149 | 149 | end |
|
150 | 150 | name = name.to_s.downcase.to_sym |
|
151 | 151 | available_macros[name] = {:desc => @@desc || ''}.merge(options) |
|
152 | 152 | @@desc = nil |
|
153 | 153 | Definitions.send :define_method, "macro_#{name}", &block |
|
154 | 154 | end |
|
155 | 155 | |
|
156 | 156 | # Sets description for the next macro to be defined |
|
157 | 157 | def desc(txt) |
|
158 | 158 | @@desc = txt |
|
159 | 159 | end |
|
160 | 160 | end |
|
161 | 161 | |
|
162 | 162 | # Builtin macros |
|
163 | 163 | desc "Sample macro." |
|
164 | 164 | macro :hello_world do |obj, args, text| |
|
165 | 165 | h("Hello world! Object: #{obj.class.name}, " + |
|
166 | 166 | (args.empty? ? "Called with no argument" : "Arguments: #{args.join(', ')}") + |
|
167 | 167 | " and " + (text.present? ? "a #{text.size} bytes long block of text." : "no block of text.") |
|
168 | 168 | ) |
|
169 | 169 | end |
|
170 | 170 | |
|
171 | 171 | desc "Displays a list of all available macros, including description if available." |
|
172 | 172 | macro :macro_list do |obj, args| |
|
173 | 173 | out = ''.html_safe |
|
174 | 174 | @@available_macros.each do |macro, options| |
|
175 | 175 | out << content_tag('dt', content_tag('code', macro.to_s)) |
|
176 | 176 | out << content_tag('dd', content_tag('pre', options[:desc])) |
|
177 | 177 | end |
|
178 | 178 | content_tag('dl', out) |
|
179 | 179 | end |
|
180 | 180 | |
|
181 | 181 | desc "Displays a list of child pages. With no argument, it displays the child pages of the current wiki page. Examples:\n\n" + |
|
182 | 182 | "{{child_pages}} -- can be used from a wiki page only\n" + |
|
183 | 183 | "{{child_pages(depth=2)}} -- display 2 levels nesting only\n" + |
|
184 | 184 | "{{child_pages(Foo)}} -- lists all children of page Foo\n" + |
|
185 | 185 | "{{child_pages(Foo, parent=1)}} -- same as above with a link to page Foo" |
|
186 | 186 | macro :child_pages do |obj, args| |
|
187 | 187 | args, options = extract_macro_options(args, :parent, :depth) |
|
188 | 188 | options[:depth] = options[:depth].to_i if options[:depth].present? |
|
189 | 189 | |
|
190 | 190 | page = nil |
|
191 | 191 | if args.size > 0 |
|
192 | 192 | page = Wiki.find_page(args.first.to_s, :project => @project) |
|
193 | 193 | elsif obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version) |
|
194 | 194 | page = obj.page |
|
195 | 195 | else |
|
196 | 196 | raise 'With no argument, this macro can be called from wiki pages only.' |
|
197 | 197 | end |
|
198 | 198 | raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project) |
|
199 | 199 | pages = page.self_and_descendants(options[:depth]).group_by(&:parent_id) |
|
200 | 200 | render_page_hierarchy(pages, options[:parent] ? page.parent_id : page.id) |
|
201 | 201 | end |
|
202 | 202 | |
|
203 | 203 | desc "Includes a wiki page. Examples:\n\n" + |
|
204 | 204 | "{{include(Foo)}}\n" + |
|
205 | 205 | "{{include(projectname:Foo)}} -- to include a page of a specific project wiki" |
|
206 | 206 | macro :include do |obj, args| |
|
207 | 207 | page = Wiki.find_page(args.first.to_s, :project => @project) |
|
208 | 208 | raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project) |
|
209 | 209 | @included_wiki_pages ||= [] |
|
210 |
raise 'Circular inclusion detected' if @included_wiki_pages.include?(page. |
|
|
211 |
@included_wiki_pages << page. |
|
|
210 | raise 'Circular inclusion detected' if @included_wiki_pages.include?(page.id) | |
|
211 | @included_wiki_pages << page.id | |
|
212 | 212 | out = textilizable(page.content, :text, :attachments => page.attachments, :headings => false) |
|
213 | 213 | @included_wiki_pages.pop |
|
214 | 214 | out |
|
215 | 215 | end |
|
216 | 216 | |
|
217 | 217 | desc "Inserts of collapsed block of text. Examples:\n\n" + |
|
218 | 218 | "{{collapse\nThis is a block of text that is collapsed by default.\nIt can be expanded by clicking a link.\n}}\n\n" + |
|
219 | 219 | "{{collapse(View details...)\nWith custom link text.\n}}" |
|
220 | 220 | macro :collapse do |obj, args, text| |
|
221 | 221 | html_id = "collapse-#{Redmine::Utils.random_hex(4)}" |
|
222 | 222 | show_label = args[0] || l(:button_show) |
|
223 | 223 | hide_label = args[1] || args[0] || l(:button_hide) |
|
224 | 224 | js = "$('##{html_id}-show, ##{html_id}-hide').toggle(); $('##{html_id}').fadeToggle(150);" |
|
225 | 225 | out = ''.html_safe |
|
226 | 226 | out << link_to_function(show_label, js, :id => "#{html_id}-show", :class => 'collapsible collapsed') |
|
227 | 227 | out << link_to_function(hide_label, js, :id => "#{html_id}-hide", :class => 'collapsible', :style => 'display:none;') |
|
228 | 228 | out << content_tag('div', textilizable(text, :object => obj, :headings => false), :id => html_id, :class => 'collapsed-text', :style => 'display:none;') |
|
229 | 229 | out |
|
230 | 230 | end |
|
231 | 231 | |
|
232 | 232 | desc "Displays a clickable thumbnail of an attached image. Examples:\n\n" + |
|
233 | 233 | "{{thumbnail(image.png)}}\n" + |
|
234 | 234 | "{{thumbnail(image.png, size=300, title=Thumbnail)}} -- with custom title and size" |
|
235 | 235 | macro :thumbnail do |obj, args| |
|
236 | 236 | args, options = extract_macro_options(args, :size, :title) |
|
237 | 237 | filename = args.first |
|
238 | 238 | raise 'Filename required' unless filename.present? |
|
239 | 239 | size = options[:size] |
|
240 | 240 | raise 'Invalid size parameter' unless size.nil? || size.match(/^\d+$/) |
|
241 | 241 | size = size.to_i |
|
242 | 242 | size = nil unless size > 0 |
|
243 | 243 | if obj && obj.respond_to?(:attachments) && attachment = Attachment.latest_attach(obj.attachments, filename) |
|
244 | 244 | title = options[:title] || attachment.title |
|
245 | 245 | thumbnail_url = url_for(:controller => 'attachments', :action => 'thumbnail', :id => attachment, :size => size, :only_path => @only_path) |
|
246 | 246 | image_url = url_for(:controller => 'attachments', :action => 'show', :id => attachment, :only_path => @only_path) |
|
247 | 247 | |
|
248 | 248 | img = image_tag(thumbnail_url, :alt => attachment.filename) |
|
249 | 249 | link_to(img, image_url, :class => 'thumbnail', :title => title) |
|
250 | 250 | else |
|
251 | 251 | raise "Attachment #{filename} not found" |
|
252 | 252 | end |
|
253 | 253 | end |
|
254 | 254 | end |
|
255 | 255 | end |
|
256 | 256 | end |
General Comments 0
You need to be logged in to leave comments.
Login now