1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 def register(name, formatter, helper)
28 raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name.to_s]
29 @@formatters[name.to_s] = {:formatter => formatter, :helper => helper}
32 def formatter_for(name)
33 entry = @@formatters[name.to_s]
34 (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
38 entry = @@formatters[name.to_s]
39 (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
46 def to_html(format, text, options = {}, &block)
47 text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, options[:object], options[:attribute])
48 # Text retrieved from the cache store may be frozen
49 # We need to dup it so we can do in-place substitutions with gsub!
50 cache_store.fetch cache_key do
51 formatter_for(format).new(text).to_html
54 formatter_for(format).new(text).to_html
57 execute_macros(text, block)
62 # Returns a cache key for the given text +format+, +object+ and +attribute+ or nil if no caching should be done
63 def cache_key_for(format, object, attribute)
64 if object && attribute && !object.new_record? && object.respond_to?(:updated_on) && !format.blank?
65 "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{object.updated_on.to_s(:number)}"
69 # Returns the cache store used to cache HTML output
71 ActionController::Base.cache_store
79 (\(([^\}]*)\))? # optional arguments
82 /x unless const_defined?(:MACROS_RE)
85 def execute_macros(text, macros_runner)
86 text.gsub!(MACROS_RE) do
87 esc, all, macro = $1, $2, $3.downcase
88 args = ($5 || '').split(',').each(&:strip)
91 macros_runner.call(macro, args)
93 "<div class=\"flash error\">Error executing the <strong>#{macro}</strong> macro (#{e})</div>"
102 # Default formatter module
105 include ActionView::Helpers::TagHelper
106 include ActionView::Helpers::TextHelper
107 include ActionView::Helpers::UrlHelper
114 simple_format(auto_link(CGI::escapeHTML(@text)))
119 def wikitoolbar_for(field_id)
122 def heads_for_wiki_formatter
125 def initial_page_content(page)
126 page.pretty_title.to_s