diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2010-02-06 10:40:21 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2010-02-06 10:40:21 +0000 |
commit | 2ad8242ae782ea9d87a0d98f8563199da736bdc7 (patch) | |
tree | e7e309e5b1a879d2579d74e95ee7282e9fc6771f /lib/redmine/wiki_formatting.rb | |
parent | 6c8b87fbc8a52b53a0adb53f072508ade7e2dca6 (diff) | |
download | redmine-2ad8242ae782ea9d87a0d98f8563199da736bdc7.tar.gz redmine-2ad8242ae782ea9d87a0d98f8563199da736bdc7.zip |
Adds a setting to cache textile rendering (off by default).
* it uses ActionController cache store which is MemoryStore by default and can be configured with config.action_controller.cache_store
* macro processing was moved out of textile rendering so that it doesn't get cached
* no noticeable improvement is expected for small portions of text, so only texts larger than 2KB are cached
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3372 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib/redmine/wiki_formatting.rb')
-rw-r--r-- | lib/redmine/wiki_formatting.rb | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/lib/redmine/wiki_formatting.rb b/lib/redmine/wiki_formatting.rb index f33ef9e6d..21e8be66b 100644 --- a/lib/redmine/wiki_formatting.rb +++ b/lib/redmine/wiki_formatting.rb @@ -44,7 +44,58 @@ module Redmine end def to_html(format, text, options = {}, &block) - formatter_for(format).new(text).to_html(&block) + text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache && cache_key = cache_key_for(format, options[:object], options[:attribute]) + # Text retrieved from the cache store may be frozen + # We need to dup it so we can do in-place substitutions with gsub! + cache.fetch cache_key do + formatter_for(format).new(text).to_html + end.dup + else + formatter_for(format).new(text).to_html + end + if block_given? + execute_macros(text, block) + end + text + end + + # Returns a cache key for the given text +format+, +object+ and +attribute+ or nil if no caching should be done + def cache_key_for(format, object, attribute) + if object && attribute && !object.new_record? && object.respond_to?(:updated_on) && !format.blank? + "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{object.updated_on.to_s(:number)}" + end + end + + # Returns the cache store used to cache HTML output + def cache + ActionController::Base.cache_store + end + + MACROS_RE = / + (!)? # escaping + ( + \{\{ # opening tag + ([\w]+) # macro name + (\(([^\}]*)\))? # optional arguments + \}\} # closing tag + ) + /x unless const_defined?(:MACROS_RE) + + # Macros substitution + def execute_macros(text, macros_runner) + text.gsub!(MACROS_RE) do + esc, all, macro = $1, $2, $3.downcase + args = ($5 || '').split(',').each(&:strip) + if esc.nil? + begin + macros_runner.call(macro, args) + rescue => e + "<div class=\"flash error\">Error executing the <strong>#{macro}</strong> macro (#{e})</div>" + end || all + else + all + end + end end end |