;]
[askyou.git] / lib / redmine / wiki_formatting.rb
blobdcce0fc44f3cf5037bbc04a1bbe0df0caafddd75
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.
8
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.
13
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.
18 module Redmine
19   module WikiFormatting
20     @@formatters = {}
22     class << self
23       def map
24         yield self
25       end
26       
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}
30       end
31       
32       def formatter_for(name)
33         entry = @@formatters[name.to_s]
34         (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
35       end
36       
37       def helper_for(name)
38         entry = @@formatters[name.to_s]
39         (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
40       end
41       
42       def format_names
43         @@formatters.keys.map
44       end
45       
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
52           end.dup
53         else
54           formatter_for(format).new(text).to_html
55         end
56         if block_given?
57           execute_macros(text, block)
58         end
59         text
60       end
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)}"
66         end
67       end
68       
69       # Returns the cache store used to cache HTML output
70       def cache_store
71         ActionController::Base.cache_store
72       end
73       
74       MACROS_RE = /
75                     (!)?                        # escaping
76                     (
77                     \{\{                        # opening tag
78                     ([\w]+)                     # macro name
79                     (\(([^\}]*)\))?             # optional arguments
80                     \}\}                        # closing tag
81                     )
82                   /x unless const_defined?(:MACROS_RE)
83       
84       # Macros substitution
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)
89           if esc.nil?
90             begin
91               macros_runner.call(macro, args)
92             rescue => e
93               "<div class=\"flash error\">Error executing the <strong>#{macro}</strong> macro (#{e})</div>"
94             end || all
95           else
96             all
97           end
98         end
99       end
100     end
101     
102     # Default formatter module
103     module NullFormatter
104       class Formatter
105         include ActionView::Helpers::TagHelper
106         include ActionView::Helpers::TextHelper
107         include ActionView::Helpers::UrlHelper
108         
109         def initialize(text)
110           @text = text
111         end
112         
113         def to_html(*args)
114           simple_format(auto_link(CGI::escapeHTML(@text)))
115         end
116       end
117       
118       module Helper
119         def wikitoolbar_for(field_id)
120         end
121       
122         def heads_for_wiki_formatter
123         end
124       
125         def initial_page_content(page)
126           page.pretty_title.to_s
127         end
128       end
129     end
130   end