##// END OF EJS Templates
Removed debug messages (#18349)....
Jean-Philippe Lang -
r13212:48205aa7d982
parent child
Show More
@@ -1,186 +1,183
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 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 'digest/md5'
19 19
20 20 module Redmine
21 21 module WikiFormatting
22 22 class StaleSectionError < Exception; end
23 23
24 24 @@formatters = {}
25 25
26 26 class << self
27 27 def map
28 28 yield self
29 29 end
30 30
31 31 def register(name, formatter, helper, options={})
32 32 name = name.to_s
33 33 raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name]
34 34 @@formatters[name] = {
35 35 :formatter => formatter,
36 36 :helper => helper,
37 37 :label => options[:label] || name.humanize
38 38 }
39 39 end
40 40
41 41 def formatter
42 42 formatter_for(Setting.text_formatting)
43 43 end
44 44
45 45 def formatter_for(name)
46 46 entry = @@formatters[name.to_s]
47 47 (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
48 48 end
49 49
50 50 def helper_for(name)
51 51 entry = @@formatters[name.to_s]
52 52 (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
53 53 end
54 54
55 55 def format_names
56 56 @@formatters.keys.map
57 57 end
58 58
59 59 def formats_for_select
60 60 @@formatters.map {|name, options| [options[:label], name]}
61 61 end
62 62
63 63 def to_html(format, text, options = {})
64 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 65 # Text retrieved from the cache store may be frozen
66 66 # We need to dup it so we can do in-place substitutions with gsub!
67 67 cache_store.fetch cache_key do
68 68 formatter_for(format).new(text).to_html
69 69 end.dup
70 70 else
71 71 formatter_for(format).new(text).to_html
72 72 end
73 73 text
74 74 end
75 75
76 76 # Returns true if the text formatter supports single section edit
77 77 def supports_section_edit?
78 78 (formatter.instance_methods & ['update_section', :update_section]).any?
79 79 end
80 80
81 81 # Returns a cache key for the given text +format+, +text+, +object+ and +attribute+ or nil if no caching should be done
82 82 def cache_key_for(format, text, object, attribute)
83 83 if object && attribute && !object.new_record? && format.present?
84 84 "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{Digest::MD5.hexdigest text}"
85 85 end
86 86 end
87 87
88 88 # Returns the cache store used to cache HTML output
89 89 def cache_store
90 90 ActionController::Base.cache_store
91 91 end
92 92 end
93 93
94 94 module LinksHelper
95 95 AUTO_LINK_RE = %r{
96 96 ( # leading text
97 97 <\w+[^>]*?>| # leading HTML tag, or
98 98 [\s\(\[,;]| # leading punctuation, or
99 99 ^ # beginning of line
100 100 )
101 101 (
102 102 (?:https?://)| # protocol spec, or
103 103 (?:s?ftps?://)|
104 104 (?:www\.) # www.*
105 105 )
106 106 (
107 107 ([^<]\S*?) # url
108 108 (\/)? # slash
109 109 )
110 110 ((?:&gt;)?|[^[:alnum:]_\=\/;\(\)]*?) # post
111 111 (?=<|\s|$)
112 112 }x unless const_defined?(:AUTO_LINK_RE)
113 113
114 114 # Destructively replaces urls into clickable links
115 115 def auto_link!(text)
116 Rails.logger.debug "====================="
117 116 Rails.logger.debug text
118 Rails.logger.debug "====================="
119 117 text.gsub!(AUTO_LINK_RE) do
120 118 all, leading, proto, url, post = $&, $1, $2, $3, $6
121 Rails.logger.debug all
122 119 if leading =~ /<a\s/i || leading =~ /![<>=]?/
123 120 # don't replace URLs that are already linked
124 121 # and URLs prefixed with ! !> !< != (textile images)
125 122 all
126 123 else
127 124 # Idea below : an URL with unbalanced parenthesis and
128 125 # ending by ')' is put into external parenthesis
129 126 if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
130 127 url=url[0..-2] # discard closing parenthesis from url
131 128 post = ")"+post # add closing parenthesis to post
132 129 end
133 130 content = proto + url
134 131 href = "#{proto=="www."?"http://www.":proto}#{url}"
135 132 %(#{leading}<a class="external" href="#{ERB::Util.html_escape href}">#{ERB::Util.html_escape content}</a>#{post}).html_safe
136 133 end
137 134 end
138 135 end
139 136
140 137 # Destructively replaces email addresses into clickable links
141 138 def auto_mailto!(text)
142 139 text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
143 140 mail = $1
144 141 if text.match(/<a\b[^>]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/)
145 142 mail
146 143 else
147 144 %(<a class="email" href="mailto:#{ERB::Util.html_escape mail}">#{ERB::Util.html_escape mail}</a>).html_safe
148 145 end
149 146 end
150 147 end
151 148 end
152 149
153 150 # Default formatter module
154 151 module NullFormatter
155 152 class Formatter
156 153 include ActionView::Helpers::TagHelper
157 154 include ActionView::Helpers::TextHelper
158 155 include ActionView::Helpers::UrlHelper
159 156 include Redmine::WikiFormatting::LinksHelper
160 157
161 158 def initialize(text)
162 159 @text = text
163 160 end
164 161
165 162 def to_html(*args)
166 163 t = CGI::escapeHTML(@text)
167 164 auto_link!(t)
168 165 auto_mailto!(t)
169 166 simple_format(t, {}, :sanitize => false)
170 167 end
171 168 end
172 169
173 170 module Helper
174 171 def wikitoolbar_for(field_id)
175 172 end
176 173
177 174 def heads_for_wiki_formatter
178 175 end
179 176
180 177 def initial_page_content(page)
181 178 page.pretty_title.to_s
182 179 end
183 180 end
184 181 end
185 182 end
186 183 end
General Comments 0
You need to be logged in to leave comments. Login now