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 @ 1298:4f746d8966dd

History | View | Annotate | Download (6 KB)

1 0:513646585e45 Chris
# Redmine - project management software
2 1295:622f24f53b42 Chris
# Copyright (C) 2006-2013  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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 909:cbb26bc654de Chris
#
9 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
#
14 0:513646585e45 Chris
# 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 1115:433d4f72a19b Chris
require 'digest/md5'
19
20 0:513646585e45 Chris
module Redmine
21
  module WikiFormatting
22 909:cbb26bc654de Chris
    class StaleSectionError < Exception; end
23
24 0:513646585e45 Chris
    @@formatters = {}
25
26
    class << self
27
      def map
28
        yield self
29
      end
30 909:cbb26bc654de Chris
31 0:513646585e45 Chris
      def register(name, formatter, helper)
32
        raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name.to_s]
33
        @@formatters[name.to_s] = {:formatter => formatter, :helper => helper}
34
      end
35 909:cbb26bc654de Chris
36
      def formatter
37
        formatter_for(Setting.text_formatting)
38
      end
39
40 0:513646585e45 Chris
      def formatter_for(name)
41
        entry = @@formatters[name.to_s]
42
        (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
43
      end
44 909:cbb26bc654de Chris
45 0:513646585e45 Chris
      def helper_for(name)
46
        entry = @@formatters[name.to_s]
47
        (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
48
      end
49 909:cbb26bc654de Chris
50 0:513646585e45 Chris
      def format_names
51
        @@formatters.keys.map
52
      end
53 909:cbb26bc654de Chris
54
      def to_html(format, text, options = {})
55 1115:433d4f72a19b Chris
        text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, text, options[:object], options[:attribute])
56 0:513646585e45 Chris
          # Text retrieved from the cache store may be frozen
57
          # We need to dup it so we can do in-place substitutions with gsub!
58
          cache_store.fetch cache_key do
59
            formatter_for(format).new(text).to_html
60
          end.dup
61
        else
62
          formatter_for(format).new(text).to_html
63
        end
64
        text
65
      end
66
67 909:cbb26bc654de Chris
      # Returns true if the text formatter supports single section edit
68
      def supports_section_edit?
69
        (formatter.instance_methods & ['update_section', :update_section]).any?
70
      end
71
72 1115:433d4f72a19b Chris
      # Returns a cache key for the given text +format+, +text+, +object+ and +attribute+ or nil if no caching should be done
73
      def cache_key_for(format, text, object, attribute)
74
        if object && attribute && !object.new_record? && format.present?
75
          "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{Digest::MD5.hexdigest text}"
76 0:513646585e45 Chris
        end
77
      end
78 909:cbb26bc654de Chris
79 0:513646585e45 Chris
      # Returns the cache store used to cache HTML output
80
      def cache_store
81
        ActionController::Base.cache_store
82
      end
83
    end
84 909:cbb26bc654de Chris
85 1115:433d4f72a19b Chris
    module LinksHelper
86
      AUTO_LINK_RE = %r{
87
                      (                          # leading text
88
                        <\w+.*?>|                # leading HTML tag, or
89 1295:622f24f53b42 Chris
                        [\s\(\[,;]|              # leading punctuation, or
90 1115:433d4f72a19b Chris
                        ^                        # beginning of line
91
                      )
92
                      (
93
                        (?:https?://)|           # protocol spec, or
94
                        (?:s?ftps?://)|
95
                        (?:www\.)                # www.*
96
                      )
97
                      (
98 1295:622f24f53b42 Chris
                        ([^<]\S*?)               # url
99 1115:433d4f72a19b Chris
                        (\/)?                    # slash
100
                      )
101
                      ((?:&gt;)?|[^[:alnum:]_\=\/;\(\)]*?)               # post
102
                      (?=<|\s|$)
103
                     }x unless const_defined?(:AUTO_LINK_RE)
104
105
      # Destructively remplaces urls into clickable links
106
      def auto_link!(text)
107
        text.gsub!(AUTO_LINK_RE) do
108
          all, leading, proto, url, post = $&, $1, $2, $3, $6
109
          if leading =~ /<a\s/i || leading =~ /![<>=]?/
110
            # don't replace URL's that are already linked
111
            # and URL's prefixed with ! !> !< != (textile images)
112
            all
113
          else
114
            # Idea below : an URL with unbalanced parethesis and
115
            # ending by ')' is put into external parenthesis
116
            if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
117
              url=url[0..-2] # discard closing parenth from url
118
              post = ")"+post # add closing parenth to post
119
            end
120
            content = proto + url
121
            href = "#{proto=="www."?"http://www.":proto}#{url}"
122
            %(#{leading}<a class="external" href="#{ERB::Util.html_escape href}">#{ERB::Util.html_escape content}</a>#{post}).html_safe
123
          end
124
        end
125
      end
126
127
      # Destructively remplaces email addresses into clickable links
128
      def auto_mailto!(text)
129
        text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
130
          mail = $1
131
          if text.match(/<a\b[^>]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/)
132
            mail
133
          else
134
            %(<a class="email" href="mailto:#{ERB::Util.html_escape mail}">#{ERB::Util.html_escape mail}</a>).html_safe
135
          end
136
        end
137
      end
138
    end
139
140 0:513646585e45 Chris
    # Default formatter module
141
    module NullFormatter
142
      class Formatter
143
        include ActionView::Helpers::TagHelper
144
        include ActionView::Helpers::TextHelper
145
        include ActionView::Helpers::UrlHelper
146 1115:433d4f72a19b Chris
        include Redmine::WikiFormatting::LinksHelper
147 909:cbb26bc654de Chris
148 0:513646585e45 Chris
        def initialize(text)
149
          @text = text
150
        end
151 909:cbb26bc654de Chris
152 0:513646585e45 Chris
        def to_html(*args)
153 1115:433d4f72a19b Chris
          t = CGI::escapeHTML(@text)
154
          auto_link!(t)
155
          auto_mailto!(t)
156
          simple_format(t, {}, :sanitize => false)
157 0:513646585e45 Chris
        end
158
      end
159 909:cbb26bc654de Chris
160 0:513646585e45 Chris
      module Helper
161
        def wikitoolbar_for(field_id)
162
        end
163 909:cbb26bc654de Chris
164 0:513646585e45 Chris
        def heads_for_wiki_formatter
165
        end
166 909:cbb26bc654de Chris
167 0:513646585e45 Chris
        def initial_page_content(page)
168
          page.pretty_title.to_s
169
        end
170
      end
171
    end
172
  end
173
end