To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / lib / redmine / wiki_formatting.rb @ 924:18beae6cb226

History | View | Annotate | Download (3.55 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3
#
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.
17

    
18
module Redmine
19
  module WikiFormatting
20
    class StaleSectionError < Exception; end
21

    
22
    @@formatters = {}
23

    
24
    class << self
25
      def map
26
        yield self
27
      end
28

    
29
      def register(name, formatter, helper)
30
        raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name.to_s]
31
        @@formatters[name.to_s] = {:formatter => formatter, :helper => helper}
32
      end
33

    
34
      def formatter
35
        formatter_for(Setting.text_formatting)
36
      end
37

    
38
      def formatter_for(name)
39
        entry = @@formatters[name.to_s]
40
        (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
41
      end
42

    
43
      def helper_for(name)
44
        entry = @@formatters[name.to_s]
45
        (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
46
      end
47

    
48
      def format_names
49
        @@formatters.keys.map
50
      end
51

    
52
      def to_html(format, text, options = {})
53
        text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, options[:object], options[:attribute])
54
          # Text retrieved from the cache store may be frozen
55
          # We need to dup it so we can do in-place substitutions with gsub!
56
          cache_store.fetch cache_key do
57
            formatter_for(format).new(text).to_html
58
          end.dup
59
        else
60
          formatter_for(format).new(text).to_html
61
        end
62
        text
63
      end
64

    
65
      # Returns true if the text formatter supports single section edit
66
      def supports_section_edit?
67
        (formatter.instance_methods & ['update_section', :update_section]).any?
68
      end
69

    
70
      # Returns a cache key for the given text +format+, +object+ and +attribute+ or nil if no caching should be done
71
      def cache_key_for(format, object, attribute)
72
        if object && attribute && !object.new_record? && object.respond_to?(:updated_on) && !format.blank?
73
          "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{object.updated_on.to_s(:number)}"
74
        end
75
      end
76

    
77
      # Returns the cache store used to cache HTML output
78
      def cache_store
79
        ActionController::Base.cache_store
80
      end
81
    end
82

    
83
    # Default formatter module
84
    module NullFormatter
85
      class Formatter
86
        include ActionView::Helpers::TagHelper
87
        include ActionView::Helpers::TextHelper
88
        include ActionView::Helpers::UrlHelper
89

    
90
        def initialize(text)
91
          @text = text
92
        end
93

    
94
        def to_html(*args)
95
          simple_format(auto_link(CGI::escapeHTML(@text)))
96
        end
97
      end
98

    
99
      module Helper
100
        def wikitoolbar_for(field_id)
101
        end
102

    
103
        def heads_for_wiki_formatter
104
        end
105

    
106
        def initial_page_content(page)
107
          page.pretty_title.to_s
108
        end
109
      end
110
    end
111
  end
112
end