Chris@0: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 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@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@0: def register(name, formatter, helper) Chris@0: raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name.to_s] Chris@0: @@formatters[name.to_s] = {:formatter => formatter, :helper => helper} 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@909: def to_html(format, text, options = {}) Chris@0: text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, 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@0: # Returns a cache key for the given text +format+, +object+ and +attribute+ or nil if no caching should be done Chris@0: def cache_key_for(format, object, attribute) Chris@0: if object && attribute && !object.new_record? && object.respond_to?(:updated_on) && !format.blank? Chris@0: "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{object.updated_on.to_s(:number)}" 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@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@909: Chris@0: def initialize(text) Chris@0: @text = text Chris@0: end Chris@909: Chris@0: def to_html(*args) Chris@0: simple_format(auto_link(CGI::escapeHTML(@text))) 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