annotate .svn/pristine/ab/ab2ac870d79ffb5e7c22bd5bd53c01cd412b73f1.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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