##// END OF EJS Templates
Merged r14237 (#19735)....
Jean-Philippe Lang -
r13866:b8811f47ccf5
parent child
Show More
@@ -1,182 +1,182
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'digest/md5'
18 require 'digest/md5'
19
19
20 module Redmine
20 module Redmine
21 module WikiFormatting
21 module WikiFormatting
22 class StaleSectionError < Exception; end
22 class StaleSectionError < Exception; end
23
23
24 @@formatters = {}
24 @@formatters = {}
25
25
26 class << self
26 class << self
27 def map
27 def map
28 yield self
28 yield self
29 end
29 end
30
30
31 def register(name, formatter, helper, options={})
31 def register(name, formatter, helper, options={})
32 name = name.to_s
32 name = name.to_s
33 raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name]
33 raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name]
34 @@formatters[name] = {
34 @@formatters[name] = {
35 :formatter => formatter,
35 :formatter => formatter,
36 :helper => helper,
36 :helper => helper,
37 :label => options[:label] || name.humanize
37 :label => options[:label] || name.humanize
38 }
38 }
39 end
39 end
40
40
41 def formatter
41 def formatter
42 formatter_for(Setting.text_formatting)
42 formatter_for(Setting.text_formatting)
43 end
43 end
44
44
45 def formatter_for(name)
45 def formatter_for(name)
46 entry = @@formatters[name.to_s]
46 entry = @@formatters[name.to_s]
47 (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
47 (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
48 end
48 end
49
49
50 def helper_for(name)
50 def helper_for(name)
51 entry = @@formatters[name.to_s]
51 entry = @@formatters[name.to_s]
52 (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
52 (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
53 end
53 end
54
54
55 def format_names
55 def format_names
56 @@formatters.keys.map
56 @@formatters.keys.map
57 end
57 end
58
58
59 def formats_for_select
59 def formats_for_select
60 @@formatters.map {|name, options| [options[:label], name]}
60 @@formatters.map {|name, options| [options[:label], name]}
61 end
61 end
62
62
63 def to_html(format, text, options = {})
63 def to_html(format, text, options = {})
64 text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, text, options[:object], options[:attribute])
64 text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, text, options[:object], options[:attribute])
65 # Text retrieved from the cache store may be frozen
65 # Text retrieved from the cache store may be frozen
66 # We need to dup it so we can do in-place substitutions with gsub!
66 # We need to dup it so we can do in-place substitutions with gsub!
67 cache_store.fetch cache_key do
67 cache_store.fetch cache_key do
68 formatter_for(format).new(text).to_html
68 formatter_for(format).new(text).to_html
69 end.dup
69 end.dup
70 else
70 else
71 formatter_for(format).new(text).to_html
71 formatter_for(format).new(text).to_html
72 end
72 end
73 text
73 text
74 end
74 end
75
75
76 # Returns true if the text formatter supports single section edit
76 # Returns true if the text formatter supports single section edit
77 def supports_section_edit?
77 def supports_section_edit?
78 (formatter.instance_methods & ['update_section', :update_section]).any?
78 (formatter.instance_methods & ['update_section', :update_section]).any?
79 end
79 end
80
80
81 # Returns a cache key for the given text +format+, +text+, +object+ and +attribute+ or nil if no caching should be done
81 # Returns a cache key for the given text +format+, +text+, +object+ and +attribute+ or nil if no caching should be done
82 def cache_key_for(format, text, object, attribute)
82 def cache_key_for(format, text, object, attribute)
83 if object && attribute && !object.new_record? && format.present?
83 if object && attribute && !object.new_record? && format.present?
84 "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{Digest::MD5.hexdigest text}"
84 "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{Digest::MD5.hexdigest text}"
85 end
85 end
86 end
86 end
87
87
88 # Returns the cache store used to cache HTML output
88 # Returns the cache store used to cache HTML output
89 def cache_store
89 def cache_store
90 ActionController::Base.cache_store
90 ActionController::Base.cache_store
91 end
91 end
92 end
92 end
93
93
94 module LinksHelper
94 module LinksHelper
95 AUTO_LINK_RE = %r{
95 AUTO_LINK_RE = %r{
96 ( # leading text
96 ( # leading text
97 <\w+[^>]*?>| # leading HTML tag, or
97 <\w+[^>]*?>| # leading HTML tag, or
98 [\s\(\[,;]| # leading punctuation, or
98 [\s\(\[,;]| # leading punctuation, or
99 ^ # beginning of line
99 ^ # beginning of line
100 )
100 )
101 (
101 (
102 (?:https?://)| # protocol spec, or
102 (?:https?://)| # protocol spec, or
103 (?:s?ftps?://)|
103 (?:s?ftps?://)|
104 (?:www\.) # www.*
104 (?:www\.) # www.*
105 )
105 )
106 (
106 (
107 ([^<]\S*?) # url
107 ([^<]\S*?) # url
108 (\/)? # slash
108 (\/)? # slash
109 )
109 )
110 ((?:&gt;)?|[^[:alnum:]_\=\/;\(\)]*?) # post
110 ((?:&gt;)?|[^[:alnum:]_\=\/;\(\)]*?) # post
111 (?=<|\s|$)
111 (?=<|\s|$)
112 }x unless const_defined?(:AUTO_LINK_RE)
112 }x unless const_defined?(:AUTO_LINK_RE)
113
113
114 # Destructively replaces urls into clickable links
114 # Destructively replaces urls into clickable links
115 def auto_link!(text)
115 def auto_link!(text)
116 text.gsub!(AUTO_LINK_RE) do
116 text.gsub!(AUTO_LINK_RE) do
117 all, leading, proto, url, post = $&, $1, $2, $3, $6
117 all, leading, proto, url, post = $&, $1, $2, $3, $6
118 if leading =~ /<a\s/i || leading =~ /![<>=]?/
118 if leading =~ /<a\s/i || leading =~ /![<>=]?/
119 # don't replace URLs that are already linked
119 # don't replace URLs that are already linked
120 # and URLs prefixed with ! !> !< != (textile images)
120 # and URLs prefixed with ! !> !< != (textile images)
121 all
121 all
122 else
122 else
123 # Idea below : an URL with unbalanced parenthesis and
123 # Idea below : an URL with unbalanced parenthesis and
124 # ending by ')' is put into external parenthesis
124 # ending by ')' is put into external parenthesis
125 if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
125 if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
126 url=url[0..-2] # discard closing parenthesis from url
126 url=url[0..-2] # discard closing parenthesis from url
127 post = ")"+post # add closing parenthesis to post
127 post = ")"+post # add closing parenthesis to post
128 end
128 end
129 content = proto + url
129 content = proto + url
130 href = "#{proto=="www."?"http://www.":proto}#{url}"
130 href = "#{proto=="www."?"http://www.":proto}#{url}"
131 %(#{leading}<a class="external" href="#{ERB::Util.html_escape href}">#{ERB::Util.html_escape content}</a>#{post}).html_safe
131 %(#{leading}<a class="external" href="#{ERB::Util.html_escape href}">#{ERB::Util.html_escape content}</a>#{post}).html_safe
132 end
132 end
133 end
133 end
134 end
134 end
135
135
136 # Destructively replaces email addresses into clickable links
136 # Destructively replaces email addresses into clickable links
137 def auto_mailto!(text)
137 def auto_mailto!(text)
138 text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
138 text.gsub!(/([\w\.!#\$%\-+.\/]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
139 mail = $1
139 mail = $1
140 if text.match(/<a\b[^>]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/)
140 if text.match(/<a\b[^>]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/)
141 mail
141 mail
142 else
142 else
143 %(<a class="email" href="mailto:#{ERB::Util.html_escape mail}">#{ERB::Util.html_escape mail}</a>).html_safe
143 %(<a class="email" href="mailto:#{ERB::Util.html_escape mail}">#{ERB::Util.html_escape mail}</a>).html_safe
144 end
144 end
145 end
145 end
146 end
146 end
147 end
147 end
148
148
149 # Default formatter module
149 # Default formatter module
150 module NullFormatter
150 module NullFormatter
151 class Formatter
151 class Formatter
152 include ActionView::Helpers::TagHelper
152 include ActionView::Helpers::TagHelper
153 include ActionView::Helpers::TextHelper
153 include ActionView::Helpers::TextHelper
154 include ActionView::Helpers::UrlHelper
154 include ActionView::Helpers::UrlHelper
155 include Redmine::WikiFormatting::LinksHelper
155 include Redmine::WikiFormatting::LinksHelper
156
156
157 def initialize(text)
157 def initialize(text)
158 @text = text
158 @text = text
159 end
159 end
160
160
161 def to_html(*args)
161 def to_html(*args)
162 t = CGI::escapeHTML(@text)
162 t = CGI::escapeHTML(@text)
163 auto_link!(t)
163 auto_link!(t)
164 auto_mailto!(t)
164 auto_mailto!(t)
165 simple_format(t, {}, :sanitize => false)
165 simple_format(t, {}, :sanitize => false)
166 end
166 end
167 end
167 end
168
168
169 module Helper
169 module Helper
170 def wikitoolbar_for(field_id)
170 def wikitoolbar_for(field_id)
171 end
171 end
172
172
173 def heads_for_wiki_formatter
173 def heads_for_wiki_formatter
174 end
174 end
175
175
176 def initial_page_content(page)
176 def initial_page_content(page)
177 page.pretty_title.to_s
177 page.pretty_title.to_s
178 end
178 end
179 end
179 end
180 end
180 end
181 end
181 end
182 end
182 end
@@ -1,77 +1,83
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../../../test_helper', __FILE__)
18 require File.expand_path('../../../../test_helper', __FILE__)
19
19
20 class Redmine::WikiFormattingTest < ActiveSupport::TestCase
20 class Redmine::WikiFormattingTest < ActiveSupport::TestCase
21 fixtures :issues
21 fixtures :issues
22
22
23 def test_textile_formatter
23 def test_textile_formatter
24 assert_equal Redmine::WikiFormatting::Textile::Formatter, Redmine::WikiFormatting.formatter_for('textile')
24 assert_equal Redmine::WikiFormatting::Textile::Formatter, Redmine::WikiFormatting.formatter_for('textile')
25 assert_equal Redmine::WikiFormatting::Textile::Helper, Redmine::WikiFormatting.helper_for('textile')
25 assert_equal Redmine::WikiFormatting::Textile::Helper, Redmine::WikiFormatting.helper_for('textile')
26 end
26 end
27
27
28 def test_null_formatter
28 def test_null_formatter
29 assert_equal Redmine::WikiFormatting::NullFormatter::Formatter, Redmine::WikiFormatting.formatter_for('')
29 assert_equal Redmine::WikiFormatting::NullFormatter::Formatter, Redmine::WikiFormatting.formatter_for('')
30 assert_equal Redmine::WikiFormatting::NullFormatter::Helper, Redmine::WikiFormatting.helper_for('')
30 assert_equal Redmine::WikiFormatting::NullFormatter::Helper, Redmine::WikiFormatting.helper_for('')
31 end
31 end
32
32
33 def test_formats_for_select
33 def test_formats_for_select
34 assert_include ['Textile', 'textile'], Redmine::WikiFormatting.formats_for_select
34 assert_include ['Textile', 'textile'], Redmine::WikiFormatting.formats_for_select
35 end
35 end
36
36
37 def test_should_link_urls_and_email_addresses
37 def test_should_link_urls_and_email_addresses
38 raw = <<-DIFF
38 raw = <<-DIFF
39 This is a sample *text* with a link: http://www.redmine.org
39 This is a sample *text* with a link: http://www.redmine.org
40 and an email address foo@example.net
40 and an email address foo@example.net
41 DIFF
41 DIFF
42
42
43 expected = <<-EXPECTED
43 expected = <<-EXPECTED
44 <p>This is a sample *text* with a link: <a class="external" href="http://www.redmine.org">http://www.redmine.org</a><br />
44 <p>This is a sample *text* with a link: <a class="external" href="http://www.redmine.org">http://www.redmine.org</a><br />
45 and an email address <a class="email" href="mailto:foo@example.net">foo@example.net</a></p>
45 and an email address <a class="email" href="mailto:foo@example.net">foo@example.net</a></p>
46 EXPECTED
46 EXPECTED
47
47
48 assert_equal expected.gsub(%r{[\r\n\t]}, ''), Redmine::WikiFormatting::NullFormatter::Formatter.new(raw).to_html.gsub(%r{[\r\n\t]}, '')
48 assert_equal expected.gsub(%r{[\r\n\t]}, ''), Redmine::WikiFormatting::NullFormatter::Formatter.new(raw).to_html.gsub(%r{[\r\n\t]}, '')
49 end
49 end
50
50
51 def test_should_link_email_with_slashes
52 raw = 'foo/bar@example.net'
53 expected = '<p><a class="email" href="mailto:foo/bar@example.net">foo/bar@example.net</a></p>'
54 assert_equal expected.gsub(%r{[\r\n\t]}, ''), Redmine::WikiFormatting::NullFormatter::Formatter.new(raw).to_html.gsub(%r{[\r\n\t]}, '')
55 end
56
51 def test_links_separated_with_line_break_should_link
57 def test_links_separated_with_line_break_should_link
52 raw = <<-DIFF
58 raw = <<-DIFF
53 link: https://www.redmine.org
59 link: https://www.redmine.org
54 http://www.redmine.org
60 http://www.redmine.org
55 DIFF
61 DIFF
56
62
57 expected = <<-EXPECTED
63 expected = <<-EXPECTED
58 <p>link: <a class="external" href="https://www.redmine.org">https://www.redmine.org</a><br />
64 <p>link: <a class="external" href="https://www.redmine.org">https://www.redmine.org</a><br />
59 <a class="external" href="http://www.redmine.org">http://www.redmine.org</a></p>
65 <a class="external" href="http://www.redmine.org">http://www.redmine.org</a></p>
60 EXPECTED
66 EXPECTED
61
67
62 end
68 end
63
69
64 def test_supports_section_edit
70 def test_supports_section_edit
65 with_settings :text_formatting => 'textile' do
71 with_settings :text_formatting => 'textile' do
66 assert_equal true, Redmine::WikiFormatting.supports_section_edit?
72 assert_equal true, Redmine::WikiFormatting.supports_section_edit?
67 end
73 end
68
74
69 with_settings :text_formatting => '' do
75 with_settings :text_formatting => '' do
70 assert_equal false, Redmine::WikiFormatting.supports_section_edit?
76 assert_equal false, Redmine::WikiFormatting.supports_section_edit?
71 end
77 end
72 end
78 end
73
79
74 def test_cache_key_for_saved_object_should_no_be_nil
80 def test_cache_key_for_saved_object_should_no_be_nil
75 assert_not_nil Redmine::WikiFormatting.cache_key_for('textile', 'Text', Issue.find(1), :description)
81 assert_not_nil Redmine::WikiFormatting.cache_key_for('textile', 'Text', Issue.find(1), :description)
76 end
82 end
77 end
83 end
General Comments 0
You need to be logged in to leave comments. Login now