OSDN Git Service

Adds a setting to cache textile rendering (off by default).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 6 Feb 2010 10:40:21 +0000 (10:40 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 6 Feb 2010 10:40:21 +0000 (10:40 +0000)
* 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

44 files changed:
app/helpers/application_helper.rb
app/views/settings/_general.rhtml
config/environment.rb
config/locales/bg.yml
config/locales/bs.yml
config/locales/ca.yml
config/locales/cs.yml
config/locales/da.yml
config/locales/de.yml
config/locales/el.yml
config/locales/en.yml
config/locales/es.yml
config/locales/eu.yml
config/locales/fi.yml
config/locales/fr.yml
config/locales/gl.yml
config/locales/he.yml
config/locales/hr.yml
config/locales/hu.yml
config/locales/id.yml
config/locales/it.yml
config/locales/ja.yml
config/locales/ko.yml
config/locales/lt.yml
config/locales/nl.yml
config/locales/no.yml
config/locales/pl.yml
config/locales/pt-BR.yml
config/locales/pt.yml
config/locales/ro.yml
config/locales/ru.yml
config/locales/sk.yml
config/locales/sl.yml
config/locales/sr.yml
config/locales/sv.yml
config/locales/th.yml
config/locales/tr.yml
config/locales/uk.yml
config/locales/vi.yml
config/locales/zh-TW.yml
config/locales/zh.yml
config/settings.yml
lib/redmine/wiki_formatting.rb
lib/redmine/wiki_formatting/textile/formatter.rb

index c6f743d..02db2ca 100644 (file)
@@ -396,13 +396,14 @@ module ApplicationHelper
       text = args.shift
     when 2
       obj = args.shift
-      text = obj.send(args.shift).to_s
+      attr = args.shift
+      text = obj.send(attr).to_s
     else
       raise ArgumentError, 'invalid arguments to textilizable'
     end
     return '' if text.blank?
 
-    text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text) { |macro, args| exec_macro(macro, obj, args) }
+    text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) }
     
     only_path = options.delete(:only_path) == false ? false : true
 
index ff0dde3..a2f0145 100644 (file)
@@ -20,6 +20,8 @@
 
 <p><%= setting_select :text_formatting, Redmine::WikiFormatting.format_names.collect{|name| [name, name.to_s]}, :blank => :label_none %></p>
 
+<p><%= setting_check_box :cache_formatted_text %></p>
+
 <p><%= setting_select :wiki_compression, [['Gzip', 'gzip']], :blank => :label_none %></p>
 
 <p><%= setting_text_field :feeds_limit, :size => 6 %></p>
index 97fc54a..f89695f 100644 (file)
@@ -32,7 +32,7 @@ Rails::Initializer.run do |config|
 
   # Enable page/fragment caching by setting a file-based store
   # (remember to create the caching directory and make it readable to the application)
-  # config.action_controller.fragment_cache_store = :file_store, "#{RAILS_ROOT}/cache"
+  # config.action_controller.cache_store = :file_store, "#{RAILS_ROOT}/tmp/cache"
   
   # Activate observers that should always be running
   # config.active_record.observers = :cacher, :garbage_collector
index d48c093..6e87d60 100644 (file)
@@ -879,3 +879,4 @@ bg:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 6d297fe..54d6589 100644 (file)
@@ -903,3 +903,4 @@ bs:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index dc033a2..878cb1e 100644 (file)
@@ -882,3 +882,4 @@ ca:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index dd4cea3..5ca186b 100644 (file)
@@ -885,3 +885,4 @@ cs:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 1da8ac2..cdee281 100644 (file)
@@ -905,3 +905,4 @@ da:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index ddb292c..f8a7c9f 100644 (file)
@@ -905,3 +905,4 @@ de:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 3943cb5..c5d4f15 100644 (file)
@@ -885,3 +885,4 @@ el:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index c8b0ddb..8109e57 100644 (file)
@@ -329,6 +329,7 @@ en:
   setting_issue_done_ratio_issue_status: Use the issue status
   setting_start_of_week: Start calendars on
   setting_rest_api_enabled: Enable REST web service
+  setting_cache_formatted_text: Cache formatted text
   
   permission_add_project: Create project
   permission_add_subprojects: Create subprojects
index 8bc7c8c..b34bfdb 100644 (file)
@@ -929,3 +929,4 @@ es:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 04bb5c5..3540806 100644 (file)
@@ -889,3 +889,4 @@ eu:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages\r
+  setting_cache_formatted_text: Cache formatted text\r
index 9ed1115..3a7cbea 100644 (file)
@@ -915,3 +915,4 @@ fi:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 99c4361..663b6f7 100644 (file)
@@ -352,6 +352,7 @@ fr:
   setting_rest_api_enabled: Activer l'API REST
   setting_gravatar_default: Image Gravatar par défaut
   setting_start_of_week: Jour de début des calendriers
+  setting_cache_formatted_text: Mettre en cache le texte formaté
   
   permission_add_project: Créer un projet
   permission_add_subprojects: Créer des sous-projets
index b606ad4..deb8563 100644 (file)
@@ -905,3 +905,4 @@ gl:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index c2604ee..1e2ecc6 100644 (file)
@@ -889,3 +889,4 @@ he:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 4244e5b..7ee3def 100644 (file)
@@ -892,3 +892,4 @@ hr:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index f419aa3..7fac563 100644 (file)
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index db92948..cf63d73 100644 (file)
@@ -897,3 +897,4 @@ id:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index a3c12ad..334e885 100644 (file)
@@ -892,3 +892,4 @@ it:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 4a0fb89..69a8c09 100644 (file)
@@ -914,3 +914,4 @@ ja:
   enumeration_activities: 作業分類 (時間トラッキング)
   enumeration_system_activity: システム作業分類
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 99fc180..0edb2b5 100644 (file)
@@ -945,3 +945,4 @@ ko:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 43158bc..e49c09f 100644 (file)
@@ -953,3 +953,4 @@ lt:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 024db32..a5f69b3 100644 (file)
@@ -867,3 +867,4 @@ nl:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 726dadb..39c54a9 100644 (file)
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 5d4e62e..40605bb 100644 (file)
@@ -910,3 +910,4 @@ pl:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 53f968a..17647e4 100644 (file)
@@ -913,3 +913,4 @@ pt-BR:
   label_board_locked: Travado
   label_change_log: Registro de alterações
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 5d9d8ce..6a25981 100644 (file)
@@ -897,3 +897,4 @@ pt:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 257cf8e..bfefe51 100644 (file)
@@ -882,3 +882,4 @@ ro:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 2dd4952..5821f0b 100644 (file)
@@ -993,3 +993,4 @@ ru:
   label_board_sticky: Прикреплена
   label_board_locked: Заблокирована
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index c1070ab..ce86fc3 100644 (file)
@@ -884,3 +884,4 @@ sk:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index b12c77d..7967dee 100644 (file)
@@ -881,3 +881,4 @@ sl:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 28eaaaa..6b8c7ed 100644 (file)
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index d0488ad..cca36ec 100644 (file)
@@ -934,3 +934,4 @@ sv:
   enumeration_activities: Aktiviteter (tidsuppföljning)
   enumeration_system_activity: Systemaktivitet
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index c598285..35461ab 100644 (file)
@@ -882,3 +882,4 @@ th:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 86b365d..482dcfa 100644 (file)
@@ -912,3 +912,4 @@ tr:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 4ae064d..f813559 100644 (file)
@@ -881,3 +881,4 @@ uk:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index c66cdd5..a359ed2 100644 (file)
@@ -944,3 +944,4 @@ vi:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index db5bfa2..28aedbc 100644 (file)
   enumeration_activities: 活動 (時間追蹤)
   enumeration_system_activity: 系統活動
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index 8e7dc4d..50f4867 100644 (file)
@@ -907,3 +907,4 @@ zh:
   label_board_sticky: Sticky
   label_board_locked: Locked
   permission_export_wiki_pages: Export wiki pages
+  setting_cache_formatted_text: Cache formatted text
index cebfbb5..1e905ed 100644 (file)
@@ -53,6 +53,8 @@ plain_text_mail:
   default: 0
 text_formatting:
   default: textile
+cache_formatted_text:
+  default: 0
 wiki_compression:
   default: ""
 default_language:
index f33ef9e..21e8be6 100644 (file)
@@ -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
     
index 7e20c7b..ab66ba5 100644 (file)
@@ -24,7 +24,7 @@ module Redmine
       class Formatter < RedCloth3
         
         # auto_link rule after textile rules so that it doesn't break !image_url! tags
-        RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto, :inline_toc, :inline_macros]
+        RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto, :inline_toc]
         
         def initialize(*args)
           super
@@ -33,9 +33,8 @@ module Redmine
           self.filter_styles=true
         end
         
-        def to_html(*rules, &block)
+        def to_html(*rules)
           @toc = []
-          @macros_runner = block
           super(*RULES).to_s
         end
   
@@ -102,32 +101,6 @@ module Redmine
           end
         end
         
-        MACROS_RE = /
-                      (!)?                        # escaping
-                      (
-                      \{\{                        # opening tag
-                      ([\w]+)                     # macro name
-                      (\(([^\}]*)\))?             # optional arguments
-                      \}\}                        # closing tag
-                      )
-                    /x unless const_defined?(:MACROS_RE)
-        
-        def inline_macros(text)
-          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
-        
         AUTO_LINK_RE = %r{
                         (                          # leading text
                           <\w+.*?>|                # leading HTML tag, or