Mercurial > hg > soundsoftware-site
comparison lib/redmine/wiki_formatting.rb @ 1115:433d4f72a19b redmine-2.2
Update to Redmine SVN revision 11137 on 2.2-stable branch
author | Chris Cannam |
---|---|
date | Mon, 07 Jan 2013 12:01:42 +0000 |
parents | cbb26bc654de |
children | 622f24f53b42 |
comparison
equal
deleted
inserted
replaced
929:5f33065ddc4b | 1115:433d4f72a19b |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2011 Jean-Philippe Lang | 2 # Copyright (C) 2006-2012 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. |
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 | |
18 require 'digest/md5' | |
17 | 19 |
18 module Redmine | 20 module Redmine |
19 module WikiFormatting | 21 module WikiFormatting |
20 class StaleSectionError < Exception; end | 22 class StaleSectionError < Exception; end |
21 | 23 |
48 def format_names | 50 def format_names |
49 @@formatters.keys.map | 51 @@formatters.keys.map |
50 end | 52 end |
51 | 53 |
52 def to_html(format, text, options = {}) | 54 def to_html(format, text, options = {}) |
53 text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, options[:object], options[:attribute]) | 55 text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, text, options[:object], options[:attribute]) |
54 # Text retrieved from the cache store may be frozen | 56 # Text retrieved from the cache store may be frozen |
55 # We need to dup it so we can do in-place substitutions with gsub! | 57 # We need to dup it so we can do in-place substitutions with gsub! |
56 cache_store.fetch cache_key do | 58 cache_store.fetch cache_key do |
57 formatter_for(format).new(text).to_html | 59 formatter_for(format).new(text).to_html |
58 end.dup | 60 end.dup |
65 # Returns true if the text formatter supports single section edit | 67 # Returns true if the text formatter supports single section edit |
66 def supports_section_edit? | 68 def supports_section_edit? |
67 (formatter.instance_methods & ['update_section', :update_section]).any? | 69 (formatter.instance_methods & ['update_section', :update_section]).any? |
68 end | 70 end |
69 | 71 |
70 # Returns a cache key for the given text +format+, +object+ and +attribute+ or nil if no caching should be done | 72 # Returns a cache key for the given text +format+, +text+, +object+ and +attribute+ or nil if no caching should be done |
71 def cache_key_for(format, object, attribute) | 73 def cache_key_for(format, text, object, attribute) |
72 if object && attribute && !object.new_record? && object.respond_to?(:updated_on) && !format.blank? | 74 if object && attribute && !object.new_record? && format.present? |
73 "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{object.updated_on.to_s(:number)}" | 75 "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{Digest::MD5.hexdigest text}" |
74 end | 76 end |
75 end | 77 end |
76 | 78 |
77 # Returns the cache store used to cache HTML output | 79 # Returns the cache store used to cache HTML output |
78 def cache_store | 80 def cache_store |
79 ActionController::Base.cache_store | 81 ActionController::Base.cache_store |
80 end | 82 end |
81 end | 83 end |
82 | 84 |
85 module LinksHelper | |
86 AUTO_LINK_RE = %r{ | |
87 ( # leading text | |
88 <\w+.*?>| # leading HTML tag, or | |
89 [^=<>!:'"/]| # leading punctuation, or | |
90 ^ # beginning of line | |
91 ) | |
92 ( | |
93 (?:https?://)| # protocol spec, or | |
94 (?:s?ftps?://)| | |
95 (?:www\.) # www.* | |
96 ) | |
97 ( | |
98 (\S+?) # url | |
99 (\/)? # slash | |
100 ) | |
101 ((?:>)?|[^[:alnum:]_\=\/;\(\)]*?) # post | |
102 (?=<|\s|$) | |
103 }x unless const_defined?(:AUTO_LINK_RE) | |
104 | |
105 # Destructively remplaces urls into clickable links | |
106 def auto_link!(text) | |
107 text.gsub!(AUTO_LINK_RE) do | |
108 all, leading, proto, url, post = $&, $1, $2, $3, $6 | |
109 if leading =~ /<a\s/i || leading =~ /![<>=]?/ | |
110 # don't replace URL's that are already linked | |
111 # and URL's prefixed with ! !> !< != (textile images) | |
112 all | |
113 else | |
114 # Idea below : an URL with unbalanced parethesis and | |
115 # ending by ')' is put into external parenthesis | |
116 if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) ) | |
117 url=url[0..-2] # discard closing parenth from url | |
118 post = ")"+post # add closing parenth to post | |
119 end | |
120 content = proto + url | |
121 href = "#{proto=="www."?"http://www.":proto}#{url}" | |
122 %(#{leading}<a class="external" href="#{ERB::Util.html_escape href}">#{ERB::Util.html_escape content}</a>#{post}).html_safe | |
123 end | |
124 end | |
125 end | |
126 | |
127 # Destructively remplaces email addresses into clickable links | |
128 def auto_mailto!(text) | |
129 text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do | |
130 mail = $1 | |
131 if text.match(/<a\b[^>]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/) | |
132 mail | |
133 else | |
134 %(<a class="email" href="mailto:#{ERB::Util.html_escape mail}">#{ERB::Util.html_escape mail}</a>).html_safe | |
135 end | |
136 end | |
137 end | |
138 end | |
139 | |
83 # Default formatter module | 140 # Default formatter module |
84 module NullFormatter | 141 module NullFormatter |
85 class Formatter | 142 class Formatter |
86 include ActionView::Helpers::TagHelper | 143 include ActionView::Helpers::TagHelper |
87 include ActionView::Helpers::TextHelper | 144 include ActionView::Helpers::TextHelper |
88 include ActionView::Helpers::UrlHelper | 145 include ActionView::Helpers::UrlHelper |
146 include Redmine::WikiFormatting::LinksHelper | |
89 | 147 |
90 def initialize(text) | 148 def initialize(text) |
91 @text = text | 149 @text = text |
92 end | 150 end |
93 | 151 |
94 def to_html(*args) | 152 def to_html(*args) |
95 simple_format(auto_link(CGI::escapeHTML(@text))) | 153 t = CGI::escapeHTML(@text) |
154 auto_link!(t) | |
155 auto_mailto!(t) | |
156 simple_format(t, {}, :sanitize => false) | |
96 end | 157 end |
97 end | 158 end |
98 | 159 |
99 module Helper | 160 module Helper |
100 def wikitoolbar_for(field_id) | 161 def wikitoolbar_for(field_id) |