annotate .svn/pristine/ea/ea0bc227b1530590ba52e2e3b4994bd583bd172a.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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