Chris@0: # Redmine - project management software
Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@0: #
Chris@0: # This program is free software; you can redistribute it and/or
Chris@0: # modify it under the terms of the GNU General Public License
Chris@0: # as published by the Free Software Foundation; either version 2
Chris@0: # of the License, or (at your option) any later version.
Chris@909: #
Chris@0: # This program is distributed in the hope that it will be useful,
Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0: # GNU General Public License for more details.
Chris@909: #
Chris@0: # You should have received a copy of the GNU General Public License
Chris@0: # along with this program; if not, write to the Free Software
Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0:
Chris@1115: require 'digest/md5'
Chris@1115:
Chris@0: module Redmine
Chris@0: module WikiFormatting
Chris@909: class StaleSectionError < Exception; end
Chris@909:
Chris@0: @@formatters = {}
Chris@0:
Chris@0: class << self
Chris@0: def map
Chris@0: yield self
Chris@0: end
Chris@909:
Chris@1517: def register(name, formatter, helper, options={})
Chris@1517: name = name.to_s
Chris@1517: raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name]
Chris@1517: @@formatters[name] = {
Chris@1517: :formatter => formatter,
Chris@1517: :helper => helper,
Chris@1517: :label => options[:label] || name.humanize
Chris@1517: }
Chris@0: end
Chris@909:
Chris@909: def formatter
Chris@909: formatter_for(Setting.text_formatting)
Chris@909: end
Chris@909:
Chris@0: def formatter_for(name)
Chris@0: entry = @@formatters[name.to_s]
Chris@0: (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
Chris@0: end
Chris@909:
Chris@0: def helper_for(name)
Chris@0: entry = @@formatters[name.to_s]
Chris@0: (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
Chris@0: end
Chris@909:
Chris@0: def format_names
Chris@0: @@formatters.keys.map
Chris@0: end
Chris@909:
Chris@1517: def formats_for_select
Chris@1517: @@formatters.map {|name, options| [options[:label], name]}
Chris@1517: end
Chris@1517:
Chris@909: def to_html(format, text, options = {})
Chris@1115: text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, text, options[:object], options[:attribute])
Chris@0: # Text retrieved from the cache store may be frozen
Chris@0: # We need to dup it so we can do in-place substitutions with gsub!
Chris@0: cache_store.fetch cache_key do
Chris@0: formatter_for(format).new(text).to_html
Chris@0: end.dup
Chris@0: else
Chris@0: formatter_for(format).new(text).to_html
Chris@0: end
Chris@0: text
Chris@0: end
Chris@0:
Chris@909: # Returns true if the text formatter supports single section edit
Chris@909: def supports_section_edit?
Chris@909: (formatter.instance_methods & ['update_section', :update_section]).any?
Chris@909: end
Chris@909:
Chris@1115: # Returns a cache key for the given text +format+, +text+, +object+ and +attribute+ or nil if no caching should be done
Chris@1115: def cache_key_for(format, text, object, attribute)
Chris@1115: if object && attribute && !object.new_record? && format.present?
Chris@1115: "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{Digest::MD5.hexdigest text}"
Chris@0: end
Chris@0: end
Chris@909:
Chris@0: # Returns the cache store used to cache HTML output
Chris@0: def cache_store
Chris@0: ActionController::Base.cache_store
Chris@0: end
Chris@0: end
Chris@909:
Chris@1115: module LinksHelper
Chris@1115: AUTO_LINK_RE = %r{
Chris@1115: ( # leading text
Chris@1115: <\w+.*?>| # leading HTML tag, or
Chris@1464: [\s\(\[,;]| # leading punctuation, or
Chris@1115: ^ # beginning of line
Chris@1115: )
Chris@1115: (
Chris@1115: (?:https?://)| # protocol spec, or
Chris@1115: (?:s?ftps?://)|
Chris@1115: (?:www\.) # www.*
Chris@1115: )
Chris@1115: (
Chris@1464: ([^<]\S*?) # url
Chris@1115: (\/)? # slash
Chris@1115: )
Chris@1115: ((?:>)?|[^[:alnum:]_\=\/;\(\)]*?) # post
Chris@1115: (?=<|\s|$)
Chris@1115: }x unless const_defined?(:AUTO_LINK_RE)
Chris@1115:
Chris@1115: # Destructively remplaces urls into clickable links
Chris@1115: def auto_link!(text)
Chris@1115: text.gsub!(AUTO_LINK_RE) do
Chris@1115: all, leading, proto, url, post = $&, $1, $2, $3, $6
Chris@1115: if leading =~ /=]?/
Chris@1115: # don't replace URL's that are already linked
Chris@1115: # and URL's prefixed with ! !> !< != (textile images)
Chris@1115: all
Chris@1115: else
Chris@1115: # Idea below : an URL with unbalanced parethesis and
Chris@1115: # ending by ')' is put into external parenthesis
Chris@1115: if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
Chris@1115: url=url[0..-2] # discard closing parenth from url
Chris@1115: post = ")"+post # add closing parenth to post
Chris@1115: end
Chris@1115: content = proto + url
Chris@1115: href = "#{proto=="www."?"http://www.":proto}#{url}"
Chris@1115: %(#{leading}#{ERB::Util.html_escape content}#{post}).html_safe
Chris@1115: end
Chris@1115: end
Chris@1115: end
Chris@1115:
Chris@1115: # Destructively remplaces email addresses into clickable links
Chris@1115: def auto_mailto!(text)
Chris@1115: text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
Chris@1115: mail = $1
Chris@1115: if text.match(/]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/)
Chris@1115: mail
Chris@1115: else
Chris@1115: %(#{ERB::Util.html_escape mail}).html_safe
Chris@1115: end
Chris@1115: end
Chris@1115: end
Chris@1115: end
Chris@1115:
Chris@0: # Default formatter module
Chris@0: module NullFormatter
Chris@0: class Formatter
Chris@0: include ActionView::Helpers::TagHelper
Chris@0: include ActionView::Helpers::TextHelper
Chris@0: include ActionView::Helpers::UrlHelper
Chris@1115: include Redmine::WikiFormatting::LinksHelper
Chris@909:
Chris@0: def initialize(text)
Chris@0: @text = text
Chris@0: end
Chris@909:
Chris@0: def to_html(*args)
Chris@1115: t = CGI::escapeHTML(@text)
Chris@1115: auto_link!(t)
Chris@1115: auto_mailto!(t)
Chris@1115: simple_format(t, {}, :sanitize => false)
Chris@0: end
Chris@0: end
Chris@909:
Chris@0: module Helper
Chris@0: def wikitoolbar_for(field_id)
Chris@0: end
Chris@909:
Chris@0: def heads_for_wiki_formatter
Chris@0: end
Chris@909:
Chris@0: def initial_page_content(page)
Chris@0: page.pretty_title.to_s
Chris@0: end
Chris@0: end
Chris@0: end
Chris@0: end
Chris@0: end