@@ -0,0 +1,81 | |||||
|
1 | # redMine - project management software | |||
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |||
|
3 | # | |||
|
4 | # This program is free software; you can redistribute it and/or | |||
|
5 | # modify it under the terms of the GNU General Public License | |||
|
6 | # as published by the Free Software Foundation; either version 2 | |||
|
7 | # of the License, or (at your option) any later version. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
14 | # You should have received a copy of the GNU General Public License | |||
|
15 | # along with this program; if not, write to the Free Software | |||
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
|
17 | ||||
|
18 | module Redmine | |||
|
19 | module WikiFormatting | |||
|
20 | module Macros | |||
|
21 | module Definitions | |||
|
22 | def exec_macro(name, obj, args) | |||
|
23 | method_name = "macro_#{name}" | |||
|
24 | send(method_name, obj, args) if respond_to?(method_name) | |||
|
25 | end | |||
|
26 | end | |||
|
27 | ||||
|
28 | @@available_macros = {} | |||
|
29 | ||||
|
30 | class << self | |||
|
31 | # Called with a block to define additional macros. | |||
|
32 | # Macro blocks accept 2 arguments: | |||
|
33 | # * obj: the object that is rendered | |||
|
34 | # * args: macro arguments | |||
|
35 | # | |||
|
36 | # Plugins can use this method to define new macros: | |||
|
37 | # | |||
|
38 | # Redmine::WikiFormatting::Macros.register do | |||
|
39 | # desc "This is my macro" | |||
|
40 | # macro :my_macro do |obj, args| | |||
|
41 | # "My macro output" | |||
|
42 | # end | |||
|
43 | # end | |||
|
44 | def register(&block) | |||
|
45 | class_eval(&block) if block_given? | |||
|
46 | end | |||
|
47 | ||||
|
48 | private | |||
|
49 | # Defines a new macro with the given name and block. | |||
|
50 | def macro(name, &block) | |||
|
51 | name = name.to_sym if name.is_a?(String) | |||
|
52 | @@available_macros[name] = @@desc || '' | |||
|
53 | @@desc = nil | |||
|
54 | raise "Can not create a macro without a block!" unless block_given? | |||
|
55 | Definitions.send :define_method, "macro_#{name}".downcase, &block | |||
|
56 | end | |||
|
57 | ||||
|
58 | # Sets description for the next macro to be defined | |||
|
59 | def desc(txt) | |||
|
60 | @@desc = txt | |||
|
61 | end | |||
|
62 | end | |||
|
63 | ||||
|
64 | # Builtin macros | |||
|
65 | desc "Example macro." | |||
|
66 | macro :hello_world do |obj, args| | |||
|
67 | "Hello world! Object: #{obj.class.name}, " + (args.empty? ? "Called with no argument." : "Arguments: #{args.join(', ')}") | |||
|
68 | end | |||
|
69 | ||||
|
70 | desc "Displays a list of all available macros, including description if available." | |||
|
71 | macro :macro_list do | |||
|
72 | out = '' | |||
|
73 | @@available_macros.keys.collect(&:to_s).sort.each do |macro| | |||
|
74 | out << content_tag('dt', content_tag('code', macro)) | |||
|
75 | out << content_tag('dd', simple_format(@@available_macros[macro.to_sym])) | |||
|
76 | end | |||
|
77 | content_tag('dl', out) | |||
|
78 | end | |||
|
79 | end | |||
|
80 | end | |||
|
81 | end |
@@ -16,6 +16,7 | |||||
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | module ApplicationHelper |
|
18 | module ApplicationHelper | |
|
19 | include Redmine::WikiFormatting::Macros::Definitions | |||
19 |
|
20 | |||
20 | def current_role |
|
21 | def current_role | |
21 | @current_role ||= User.current.role_for_project(@project) |
|
22 | @current_role ||= User.current.role_for_project(@project) | |
@@ -130,15 +131,28 module ApplicationHelper | |||||
130 | :preview => 'r', |
|
131 | :preview => 'r', | |
131 | :quick_search => 'f', |
|
132 | :quick_search => 'f', | |
132 | :search => '4', |
|
133 | :search => '4', | |
133 | }.freeze |
|
134 | }.freeze unless const_defined?(:ACCESSKEYS) | |
134 |
|
135 | |||
135 | def accesskey(s) |
|
136 | def accesskey(s) | |
136 | ACCESSKEYS[s] |
|
137 | ACCESSKEYS[s] | |
137 | end |
|
138 | end | |
138 |
|
139 | |||
139 |
# |
|
140 | # Formats text according to system settings. | |
140 | def textilizable(text, options = {}) |
|
141 | # 2 ways to call this method: | |
141 | return "" if text.blank? |
|
142 | # * with a String: textilizable(text, options) | |
|
143 | # * with an object and one of its attribute: textilizable(issue, :description, options) | |||
|
144 | def textilizable(*args) | |||
|
145 | options = args.last.is_a?(Hash) ? args.pop : {} | |||
|
146 | case args.size | |||
|
147 | when 1 | |||
|
148 | obj = nil | |||
|
149 | text = args.shift || '' | |||
|
150 | when 2 | |||
|
151 | obj = args.shift | |||
|
152 | text = obj.send(args.shift) | |||
|
153 | else | |||
|
154 | raise ArgumentError, 'invalid arguments to textilizable' | |||
|
155 | end | |||
142 |
|
156 | |||
143 | # when using an image link, try to use an attachment, if possible |
|
157 | # when using an image link, try to use an attachment, if possible | |
144 | attachments = options[:attachments] |
|
158 | attachments = options[:attachments] | |
@@ -158,7 +172,8 module ApplicationHelper | |||||
158 | end |
|
172 | end | |
159 |
|
173 | |||
160 | text = (Setting.text_formatting == 'textile') ? |
|
174 | text = (Setting.text_formatting == 'textile') ? | |
161 | Redmine::WikiFormatting.to_html(text) : simple_format(auto_link(h(text))) |
|
175 | Redmine::WikiFormatting.to_html(text) { |macro, args| exec_macro(macro, obj, args) } : | |
|
176 | simple_format(auto_link(h(text))) | |||
162 |
|
177 | |||
163 | # different methods for formatting wiki links |
|
178 | # different methods for formatting wiki links | |
164 | case options[:wiki_links] |
|
179 | case options[:wiki_links] |
@@ -64,7 +64,7 end %> | |||||
64 | <% end %> |
|
64 | <% end %> | |
65 |
|
65 | |||
66 | <p><strong><%=l(:field_description)%></strong></p> |
|
66 | <p><strong><%=l(:field_description)%></strong></p> | |
67 |
<%= textilizable @issue |
|
67 | <%= textilizable @issue, :description, :attachments => @issue.attachments %> | |
68 |
|
68 | |||
69 | <% if @issue.attachments.any? %> |
|
69 | <% if @issue.attachments.any? %> | |
70 | <%= link_to_attachments @issue.attachments, :delete_url => (authorize_for('issues', 'destroy_attachment') ? {:controller => 'issues', :action => 'destroy_attachment', :id => @issue} : nil) %> |
|
70 | <%= link_to_attachments @issue.attachments, :delete_url => (authorize_for('issues', 'destroy_attachment') ? {:controller => 'issues', :action => 'destroy_attachment', :id => @issue} : nil) %> |
@@ -1,3 +1,3 | |||||
1 | <div class="wiki"> |
|
1 | <div class="wiki"> | |
2 |
<%= textilizable content |
|
2 | <%= textilizable content, :text, :attachments => content.page.attachments %> | |
3 | </div> |
|
3 | </div> |
@@ -9,6 +9,6 h1, h2, h3, h4 { font-family: Trebuchet MS,Georgia,"Times New Roman",serif; } | |||||
9 | </style> |
|
9 | </style> | |
10 | </head> |
|
10 | </head> | |
11 | <body> |
|
11 | <body> | |
12 |
<%= textilizable @content |
|
12 | <%= textilizable @content, :text, :wiki_links => :local %> | |
13 | </body> |
|
13 | </body> | |
14 | </html> |
|
14 | </html> |
@@ -20,7 +20,7 h1, h2, h3, h4 { font-family: Trebuchet MS,Georgia,"Times New Roman",serif; } | |||||
20 | <% @pages.each do |page| %> |
|
20 | <% @pages.each do |page| %> | |
21 | <hr /> |
|
21 | <hr /> | |
22 | <a name="<%= page.title %>" /> |
|
22 | <a name="<%= page.title %>" /> | |
23 |
<%= textilizable page.content |
|
23 | <%= textilizable page.content ,:text, :wiki_links => :anchor %> | |
24 | <% end %> |
|
24 | <% end %> | |
25 |
|
25 | |||
26 | </body> |
|
26 | </body> |
@@ -1,13 +1,31 | |||||
|
1 | # redMine - project management software | |||
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |||
|
3 | # | |||
|
4 | # This program is free software; you can redistribute it and/or | |||
|
5 | # modify it under the terms of the GNU General Public License | |||
|
6 | # as published by the Free Software Foundation; either version 2 | |||
|
7 | # of the License, or (at your option) any later version. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
14 | # You should have received a copy of the GNU General Public License | |||
|
15 | # along with this program; if not, write to the Free Software | |||
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
|
17 | ||||
1 | require 'redcloth' |
|
18 | require 'redcloth' | |
2 | require 'coderay' |
|
19 | require 'coderay' | |
3 | require 'pp' |
|
20 | ||
4 | module Redmine |
|
21 | module Redmine | |
5 | module WikiFormatting |
|
22 | module WikiFormatting | |
6 |
|
23 | |||
7 | private |
|
24 | private | |
8 |
|
25 | |||
9 |
class TextileFormatter < RedCloth |
|
26 | class TextileFormatter < RedCloth | |
10 | RULES = [:inline_auto_link, :inline_auto_mailto, :textile, :inline_toc] |
|
27 | ||
|
28 | RULES = [:inline_auto_link, :inline_auto_mailto, :textile, :inline_toc, :inline_macros] | |||
11 |
|
29 | |||
12 | def initialize(*args) |
|
30 | def initialize(*args) | |
13 | super |
|
31 | super | |
@@ -15,8 +33,9 module Redmine | |||||
15 | self.no_span_caps=true |
|
33 | self.no_span_caps=true | |
16 | end |
|
34 | end | |
17 |
|
35 | |||
18 | def to_html |
|
36 | def to_html(*rules, &block) | |
19 | @toc = [] |
|
37 | @toc = [] | |
|
38 | @macros_runner = block | |||
20 | super(*RULES).to_s |
|
39 | super(*RULES).to_s | |
21 | end |
|
40 | end | |
22 |
|
41 | |||
@@ -72,6 +91,25 module Redmine | |||||
72 | end |
|
91 | end | |
73 | end |
|
92 | end | |
74 |
|
93 | |||
|
94 | MACROS_RE = / | |||
|
95 | \{\{ # opening tag | |||
|
96 | ([\w]+) # macro name | |||
|
97 | (\(([^\}]*)\))? # optional arguments | |||
|
98 | \}\} # closing tag | |||
|
99 | /x unless const_defined?(:MACROS_RE) | |||
|
100 | ||||
|
101 | def inline_macros(text) | |||
|
102 | text.gsub!(MACROS_RE) do | |||
|
103 | all, macro = $&, $1.downcase | |||
|
104 | args = ($3 || '').split(',').each(&:strip) | |||
|
105 | begin | |||
|
106 | @macros_runner.call(macro, args) | |||
|
107 | rescue => e | |||
|
108 | "<div class=\"flash error\">Error executing the <strong>#{macro}</strong> macro (#{e})</div>" | |||
|
109 | end || all | |||
|
110 | end | |||
|
111 | end | |||
|
112 | ||||
75 | AUTO_LINK_RE = %r{ |
|
113 | AUTO_LINK_RE = %r{ | |
76 | ( # leading text |
|
114 | ( # leading text | |
77 | <\w+.*?>| # leading HTML tag, or |
|
115 | <\w+.*?>| # leading HTML tag, or | |
@@ -115,8 +153,8 module Redmine | |||||
115 |
|
153 | |||
116 | public |
|
154 | public | |
117 |
|
155 | |||
118 | def self.to_html(text, options = {}) |
|
156 | def self.to_html(text, options = {}, &block) | |
119 |
TextileFormatter.new(text).to_html |
|
157 | TextileFormatter.new(text).to_html(&block) | |
120 | end |
|
158 | end | |
121 | end |
|
159 | end | |
122 | end |
|
160 | end |
@@ -70,4 +70,9 class ApplicationHelperTest < HelperTestCase | |||||
70 | @project = Project.find(1) |
|
70 | @project = Project.find(1) | |
71 | to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) } |
|
71 | to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) } | |
72 | end |
|
72 | end | |
|
73 | ||||
|
74 | def test_macro_hello_world | |||
|
75 | text = "{{hello_world}}" | |||
|
76 | assert textilizable(text).match(/Hello world!/) | |||
|
77 | end | |||
73 | end |
|
78 | end |
General Comments 0
You need to be logged in to leave comments.
Login now