##// END OF EJS Templates
Include macro can include a page of another project wiki using !{{include(projectname:Foo)}} (#1052)....
Jean-Philippe Lang -
r1335:0329094f015b
parent child
Show More
@@ -1,98 +1,101
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 def exec_macro(name, obj, args)
23 23 method_name = "macro_#{name}"
24 24 send(method_name, obj, args) if respond_to?(method_name)
25 25 end
26 26 end
27 27
28 28 @@available_macros = {}
29 29
30 30 class << self
31 31 # Called with a block to define additional macros.
32 32 # Macro blocks accept 2 arguments:
33 33 # * obj: the object that is rendered
34 34 # * args: macro arguments
35 35 #
36 36 # Plugins can use this method to define new macros:
37 37 #
38 38 # Redmine::WikiFormatting::Macros.register do
39 39 # desc "This is my macro"
40 40 # macro :my_macro do |obj, args|
41 41 # "My macro output"
42 42 # end
43 43 # end
44 44 def register(&block)
45 45 class_eval(&block) if block_given?
46 46 end
47 47
48 48 private
49 49 # Defines a new macro with the given name and block.
50 50 def macro(name, &block)
51 51 name = name.to_sym if name.is_a?(String)
52 52 @@available_macros[name] = @@desc || ''
53 53 @@desc = nil
54 54 raise "Can not create a macro without a block!" unless block_given?
55 55 Definitions.send :define_method, "macro_#{name}".downcase, &block
56 56 end
57 57
58 58 # Sets description for the next macro to be defined
59 59 def desc(txt)
60 60 @@desc = txt
61 61 end
62 62 end
63 63
64 64 # Builtin macros
65 65 desc "Sample macro."
66 66 macro :hello_world do |obj, args|
67 67 "Hello world! Object: #{obj.class.name}, " + (args.empty? ? "Called with no argument." : "Arguments: #{args.join(', ')}")
68 68 end
69 69
70 70 desc "Displays a list of all available macros, including description if available."
71 71 macro :macro_list do
72 72 out = ''
73 73 @@available_macros.keys.collect(&:to_s).sort.each do |macro|
74 74 out << content_tag('dt', content_tag('code', macro))
75 75 out << content_tag('dd', textilizable(@@available_macros[macro.to_sym]))
76 76 end
77 77 content_tag('dl', out)
78 78 end
79 79
80 desc "Include a wiki page. Example:\n\n !{{include(Foo)}}"
80 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)}}"
81 81 macro :include do |obj, args|
82 if @project && !@project.wiki.nil?
83 page = @project.wiki.find_page(args.first)
84 if page && page.content
82 project = @project
83 title = args.first.to_s
84 if title =~ %r{^([^\:]+)\:(.*)$}
85 project_identifier, title = $1, $2
86 project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier)
87 end
88 raise 'Unknow project' unless project && User.current.allowed_to?(:view_wiki_pages, project)
89 raise 'No wiki for this project' unless !project.wiki.nil?
90 page = project.wiki.find_page(title)
91 raise "Page #{args.first} doesn't exist" unless page && page.content
85 92 @included_wiki_pages ||= []
86 93 raise 'Circular inclusion detected' if @included_wiki_pages.include?(page.title)
87 94 @included_wiki_pages << page.title
88 95 out = textilizable(page.content, :text, :attachments => page.attachments)
89 96 @included_wiki_pages.pop
90 97 out
91 else
92 raise "Page #{args.first} doesn't exist"
93 end
94 end
95 98 end
96 99 end
97 100 end
98 101 end
@@ -1,198 +1,216
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 require File.dirname(__FILE__) + '/../../test_helper'
19 19
20 20 class ApplicationHelperTest < HelperTestCase
21 21 include ApplicationHelper
22 22 include ActionView::Helpers::TextHelper
23 23 fixtures :projects, :repositories, :changesets, :trackers, :issue_statuses, :issues, :documents, :versions, :wikis, :wiki_pages, :wiki_contents
24 24
25 25 def setup
26 26 super
27 27 end
28 28
29 29 def test_auto_links
30 30 to_test = {
31 31 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
32 32 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
33 33 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
34 34 'http://foo.bar/foo.bar#foo.bar.' => '<a class="external" href="http://foo.bar/foo.bar#foo.bar">http://foo.bar/foo.bar#foo.bar</a>.',
35 35 'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
36 36 'http://foo.bar/page?p=1&t=z&s=' => '<a class="external" href="http://foo.bar/page?p=1&#38;t=z&#38;s=">http://foo.bar/page?p=1&#38;t=z&#38;s=</a>',
37 37 'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>'
38 38 }
39 39 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
40 40 end
41 41
42 42 def test_auto_mailto
43 43 assert_equal '<p><a href="mailto:test@foo.bar" class="email">test@foo.bar</a></p>',
44 44 textilizable('test@foo.bar')
45 45 end
46 46
47 47 def test_inline_images
48 48 to_test = {
49 49 '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
50 50 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
51 51 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
52 52 'with style !{width:100px;height100px}http://foo.bar/image.jpg!' => 'with style <img src="http://foo.bar/image.jpg" style="width:100px;height100px;" alt="" />',
53 53 }
54 54 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
55 55 end
56 56
57 57 def test_textile_external_links
58 58 to_test = {
59 59 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
60 60 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
61 61 '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>'
62 62 }
63 63 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
64 64 end
65 65
66 66 def test_redmine_links
67 67 issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
68 68 :class => 'issue', :title => 'Error 281 when updating a recipe (New)')
69 69
70 70 changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
71 71 :class => 'changeset', :title => 'My very first commit')
72 72
73 73 document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
74 74 :class => 'document')
75 75
76 76 version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
77 77 :class => 'version')
78 78
79 79 source_url = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => 'some/file'}
80 80
81 81 to_test = {
82 82 # tickets
83 83 '#3, #3 and #3.' => "#{issue_link}, #{issue_link} and #{issue_link}.",
84 84 # changesets
85 85 'r1' => changeset_link,
86 86 # documents
87 87 'document#1' => document_link,
88 88 'document:"Test document"' => document_link,
89 89 # versions
90 90 'version#2' => version_link,
91 91 'version:1.0' => version_link,
92 92 'version:"1.0"' => version_link,
93 93 # source
94 94 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'),
95 95 'source:/some/file@52' => link_to('source:/some/file@52', source_url.merge(:rev => 52), :class => 'source'),
96 96 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url.merge(:anchor => 'L110'), :class => 'source'),
97 97 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url.merge(:rev => 52, :anchor => 'L110'), :class => 'source'),
98 98 'export:/some/file' => link_to('export:/some/file', source_url.merge(:format => 'raw'), :class => 'source download'),
99 99 # escaping
100 100 '!#3.' => '#3.',
101 101 '!r1' => 'r1',
102 102 '!document#1' => 'document#1',
103 103 '!document:"Test document"' => 'document:"Test document"',
104 104 '!version#2' => 'version#2',
105 105 '!version:1.0' => 'version:1.0',
106 106 '!version:"1.0"' => 'version:"1.0"',
107 107 '!source:/some/file' => 'source:/some/file',
108 108 # invalid expressions
109 109 'source:' => 'source:'
110 110 }
111 111 @project = Project.find(1)
112 112 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
113 113 end
114 114
115 115 def test_wiki_links
116 116 to_test = {
117 117 '[[CookBook documentation]]' => '<a href="/wiki/ecookbook/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
118 118 '[[Another page|Page]]' => '<a href="/wiki/ecookbook/Another_page" class="wiki-page">Page</a>',
119 119 # page that doesn't exist
120 120 '[[Unknown page]]' => '<a href="/wiki/ecookbook/Unknown_page" class="wiki-page new">Unknown page</a>',
121 121 '[[Unknown page|404]]' => '<a href="/wiki/ecookbook/Unknown_page" class="wiki-page new">404</a>',
122 122 # link to another project wiki
123 123 '[[onlinestore:]]' => '<a href="/wiki/onlinestore/" class="wiki-page">onlinestore</a>',
124 124 '[[onlinestore:|Wiki]]' => '<a href="/wiki/onlinestore/" class="wiki-page">Wiki</a>',
125 125 '[[onlinestore:Start page]]' => '<a href="/wiki/onlinestore/Start_page" class="wiki-page">Start page</a>',
126 126 '[[onlinestore:Start page|Text]]' => '<a href="/wiki/onlinestore/Start_page" class="wiki-page">Text</a>',
127 127 '[[onlinestore:Unknown page]]' => '<a href="/wiki/onlinestore/Unknown_page" class="wiki-page new">Unknown page</a>',
128 128 # escaping
129 129 '![[Another page|Page]]' => '[[Another page|Page]]',
130 130 }
131 131 @project = Project.find(1)
132 132 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
133 133 end
134 134
135 135 def test_html_tags
136 136 to_test = {
137 137 "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
138 138 "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
139 139 "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
140 140 # do not escape pre/code tags
141 141 "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
142 142 "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
143 143 "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
144 144 }
145 145 to_test.each { |text, result| assert_equal result, textilizable(text) }
146 146 end
147 147
148 148 def test_wiki_links_in_tables
149 149 to_test = {"|Cell 11|Cell 12|Cell 13|\n|Cell 21|Cell 22||\n|Cell 31||Cell 33|" =>
150 150 '<tr><td>Cell 11</td><td>Cell 12</td><td>Cell 13</td></tr>' +
151 151 '<tr><td>Cell 21</td><td>Cell 22</td></tr>' +
152 152 '<tr><td>Cell 31</td><td>Cell 33</td></tr>',
153 153
154 154 "|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|" =>
155 155 '<tr><td><a href="/wiki/ecookbook/Page" class="wiki-page new">Link title</a></td>' +
156 156 '<td><a href="/wiki/ecookbook/Other_Page" class="wiki-page new">Other title</a></td>' +
157 157 '</tr><tr><td>Cell 21</td><td><a href="/wiki/ecookbook/Last_page" class="wiki-page new">Last page</a></td></tr>'
158 158 }
159 159 @project = Project.find(1)
160 160 to_test.each { |text, result| assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '') }
161 161 end
162 162
163 163 def test_macro_hello_world
164 164 text = "{{hello_world}}"
165 165 assert textilizable(text).match(/Hello world!/)
166 166 # escaping
167 167 text = "!{{hello_world}}"
168 168 assert_equal '<p>{{hello_world}}</p>', textilizable(text)
169 169 end
170 170
171 def test_macro_include
172 @project = Project.find(1)
173 # include a page of the current project wiki
174 text = "{{include(Another page)}}"
175 assert textilizable(text).match(/This is a link to a ticket/)
176
177 @project = nil
178 # include a page of a specific project wiki
179 text = "{{include(ecookbook:Another page)}}"
180 assert textilizable(text).match(/This is a link to a ticket/)
181
182 text = "{{include(ecookbook:)}}"
183 assert textilizable(text).match(/CookBook documentation/)
184
185 text = "{{include(unknowidentifier:somepage)}}"
186 assert textilizable(text).match(/Unknow project/)
187 end
188
171 189 def test_date_format_default
172 190 today = Date.today
173 191 Setting.date_format = ''
174 192 assert_equal l_date(today), format_date(today)
175 193 end
176 194
177 195 def test_date_format
178 196 today = Date.today
179 197 Setting.date_format = '%d %m %Y'
180 198 assert_equal today.strftime('%d %m %Y'), format_date(today)
181 199 end
182 200
183 201 def test_time_format_default
184 202 now = Time.now
185 203 Setting.date_format = ''
186 204 Setting.time_format = ''
187 205 assert_equal l_datetime(now), format_time(now)
188 206 assert_equal l_time(now), format_time(now, false)
189 207 end
190 208
191 209 def test_time_format
192 210 now = Time.now
193 211 Setting.date_format = '%d %m %Y'
194 212 Setting.time_format = '%H %M'
195 213 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now)
196 214 assert_equal now.strftime('%H %M'), format_time(now, false)
197 215 end
198 216 end
General Comments 0
You need to be logged in to leave comments. Login now