annotate .svn/pristine/c6/c6ff1374312284d6c1b8200e5a3e6f1fba439441.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 cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 module Redmine
Chris@909 19 module WikiFormatting
Chris@909 20 class StaleSectionError < Exception; end
Chris@909 21
Chris@909 22 @@formatters = {}
Chris@909 23
Chris@909 24 class << self
Chris@909 25 def map
Chris@909 26 yield self
Chris@909 27 end
Chris@909 28
Chris@909 29 def register(name, formatter, helper)
Chris@909 30 raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name.to_s]
Chris@909 31 @@formatters[name.to_s] = {:formatter => formatter, :helper => helper}
Chris@909 32 end
Chris@909 33
Chris@909 34 def formatter
Chris@909 35 formatter_for(Setting.text_formatting)
Chris@909 36 end
Chris@909 37
Chris@909 38 def formatter_for(name)
Chris@909 39 entry = @@formatters[name.to_s]
Chris@909 40 (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
Chris@909 41 end
Chris@909 42
Chris@909 43 def helper_for(name)
Chris@909 44 entry = @@formatters[name.to_s]
Chris@909 45 (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
Chris@909 46 end
Chris@909 47
Chris@909 48 def format_names
Chris@909 49 @@formatters.keys.map
Chris@909 50 end
Chris@909 51
Chris@909 52 def to_html(format, text, options = {})
Chris@909 53 text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, options[:object], options[:attribute])
Chris@909 54 # Text retrieved from the cache store may be frozen
Chris@909 55 # We need to dup it so we can do in-place substitutions with gsub!
Chris@909 56 cache_store.fetch cache_key do
Chris@909 57 formatter_for(format).new(text).to_html
Chris@909 58 end.dup
Chris@909 59 else
Chris@909 60 formatter_for(format).new(text).to_html
Chris@909 61 end
Chris@909 62 text
Chris@909 63 end
Chris@909 64
Chris@909 65 # Returns true if the text formatter supports single section edit
Chris@909 66 def supports_section_edit?
Chris@909 67 (formatter.instance_methods & ['update_section', :update_section]).any?
Chris@909 68 end
Chris@909 69
Chris@909 70 # Returns a cache key for the given text +format+, +object+ and +attribute+ or nil if no caching should be done
Chris@909 71 def cache_key_for(format, object, attribute)
Chris@909 72 if object && attribute && !object.new_record? && object.respond_to?(:updated_on) && !format.blank?
Chris@909 73 "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{object.updated_on.to_s(:number)}"
Chris@909 74 end
Chris@909 75 end
Chris@909 76
Chris@909 77 # Returns the cache store used to cache HTML output
Chris@909 78 def cache_store
Chris@909 79 ActionController::Base.cache_store
Chris@909 80 end
Chris@909 81 end
Chris@909 82
Chris@909 83 # Default formatter module
Chris@909 84 module NullFormatter
Chris@909 85 class Formatter
Chris@909 86 include ActionView::Helpers::TagHelper
Chris@909 87 include ActionView::Helpers::TextHelper
Chris@909 88 include ActionView::Helpers::UrlHelper
Chris@909 89
Chris@909 90 def initialize(text)
Chris@909 91 @text = text
Chris@909 92 end
Chris@909 93
Chris@909 94 def to_html(*args)
Chris@909 95 simple_format(auto_link(CGI::escapeHTML(@text)))
Chris@909 96 end
Chris@909 97 end
Chris@909 98
Chris@909 99 module Helper
Chris@909 100 def wikitoolbar_for(field_id)
Chris@909 101 end
Chris@909 102
Chris@909 103 def heads_for_wiki_formatter
Chris@909 104 end
Chris@909 105
Chris@909 106 def initial_page_content(page)
Chris@909 107 page.pretty_title.to_s
Chris@909 108 end
Chris@909 109 end
Chris@909 110 end
Chris@909 111 end
Chris@909 112 end