Project

General

Profile

RedmineMacros » History » Version 3

Jean-Philippe Lang, 2012-10-17 22:35

1 1 Jean-Philippe Lang
h1. Macros
2
3
{{>toc}}
4
5 2 Jean-Philippe Lang
Redmine includes a macros system that lets you add custom functions to insert dynamic content in formatted text.
6
7 1 Jean-Philippe Lang
h2. Invoking macros in formatted text
8
9
Macros are invoked using double curly brackets. Arguments must be enclosed in parenthesis if any. A new line after the macro name or the arguments starts the block of text that will be passed to the macro (invoking a macro that do not accept a block of text with some text will fail).
10
11
Examples:
12
13
<pre>
14
No arguments:
15
{{my_macro}}
16
17
With arguments:
18
{{my_macro(arg1, arg2)}}
19
20
With a block of text:
21
{{my_macro
22
multiple lines
23
of text
24
}}
25
26
With arguments and a block of text
27
{{my_macro(arg1, arg2)
28
multiple lines
29
of text
30
}}
31
</pre>
32
33
If a block of text is given, the closing tag @}}@ must be at the start of a new line.
34
35
h2. Adding custom macros
36
37
Custom macros can be added from a Redmine plugin using the following syntax:
38
39
<pre><code class="ruby">
40
Redmine::WikiFormatting::Macros.macro(name, options={}, &block)
41
</code></pre>
42
43
Options:
44
45
* @:desc@ - A description of the macro
46
* @:parse_args => false@ - Disables arguments parsing (the whole arguments string is passed to the macro)
47
48
Macro blocks accept 2 or 3 arguments:
49
* @obj@ - the object that is rendered (eg. an Issue, a WikiContent...)
50
* @args@ - the macro arguments
51
* @text@ - the block of text given to the macro (should be present only if the macro accepts a block of text). text is a String or nil if the macro is invoked without a block of text.  
52
53 2 Jean-Philippe Lang
The block is called each time the macro is invoked in formatted text and its return value is inserted in the final output. This return value must be a html escaped string.
54
55 1 Jean-Philippe Lang
Examples:
56
57
By default, when the macro is invoked, the coma separated list of arguments is split and passed to the macro block as an array. If no argument is given the macro will be invoked with an empty array:
58
59
<pre><code class="ruby">
60
macro :my_macro, :desc => 'This is a custom macro' do |obj, args|
61
  # args is an array
62
  # and this macro does not accept a block of text
63
end
64
</code></pre>
65
66 2 Jean-Philippe Lang
You can disable arguments spliting with the @:parse_args => false@ option. In this case, the full string of arguments is passed to the macro:
67 1 Jean-Philippe Lang
68
<pre><code class="ruby">
69
macro :my_macro, :parse_args => false do |obj, args|
70
  # args is a string
71
end
72
</code></pre>
73
74 2 Jean-Philippe Lang
Macro can optionally accept a block of text as a third argument:
75 1 Jean-Philippe Lang
76
<pre><code class="ruby">
77
macro :my_macro do |obj, args, text|
78
  # this macro accepts a block of text
79 3 Jean-Philippe Lang
  # text is a String that represent the whole text passed to the macro
80 1 Jean-Philippe Lang
end
81
</code></pre>
82 2 Jean-Philippe Lang
83
As a starting point for writing your own macros, you can have a look at the builtin macros definitions: source:/tags/2.1.0/lib/redmine/wiki_formatting/macros.rb#L162