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