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 / textile / .svn / text-base / formatter.rb.svn-base @ 441:cbce1fd3b1b7

History | View | Annotate | Download (4.61 KB)

1 0:513646585e45 Chris
# Redmine - project management software
2 37:94944d00e43c chris
# Copyright (C) 2006-2010  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
#
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
require 'redcloth3'
19
20
module Redmine
21
  module WikiFormatting
22
    module Textile
23
      class Formatter < RedCloth3
24
        include ActionView::Helpers::TagHelper
25
26
        # auto_link rule after textile rules so that it doesn't break !image_url! tags
27 37:94944d00e43c chris
        RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto]
28 0:513646585e45 Chris
29
        def initialize(*args)
30
          super
31
          self.hard_breaks=true
32
          self.no_span_caps=true
33
          self.filter_styles=true
34
        end
35
36
        def to_html(*rules)
37
          @toc = []
38
          super(*RULES).to_s
39
        end
40
41
      private
42
43
        # Patch for RedCloth.  Fixed in RedCloth r128 but _why hasn't released it yet.
44
        # <a href="http://code.whytheluckystiff.net/redcloth/changeset/128">http://code.whytheluckystiff.net/redcloth/changeset/128</a>
45
        def hard_break( text )
46 441:cbce1fd3b1b7 Chris
          text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks
47 0:513646585e45 Chris
        end
48
49
        # Patch to add code highlighting support to RedCloth
50
        def smooth_offtags( text )
51
          unless @pre_list.empty?
52
            ## replace <pre> content
53
            text.gsub!(/<redpre#(\d+)>/) do
54
              content = @pre_list[$1.to_i]
55
              if content.match(/<code\s+class="(\w+)">\s?(.+)/m)
56
                content = "<code class=\"#{$1} syntaxhl\">" +
57
                  Redmine::SyntaxHighlighting.highlight_by_language($2, $1)
58
              end
59
              content
60
            end
61
          end
62
        end
63
64
        AUTO_LINK_RE = %r{
65
                        (                          # leading text
66
                          <\w+.*?>|                # leading HTML tag, or
67
                          [^=<>!:'"/]|             # leading punctuation, or
68
                          ^                        # beginning of line
69
                        )
70
                        (
71
                          (?:https?://)|           # protocol spec, or
72
                          (?:s?ftps?://)|
73
                          (?:www\.)                # www.*
74
                        )
75
                        (
76
                          (\S+?)                   # url
77
                          (\/)?                    # slash
78
                        )
79 37:94944d00e43c chris
                        ((?:&gt;)?|[^\w\=\/;\(\)]*?)               # post
80 0:513646585e45 Chris
                        (?=<|\s|$)
81
                       }x unless const_defined?(:AUTO_LINK_RE)
82
83
        # Turns all urls into clickable links (code from Rails).
84
        def inline_auto_link(text)
85
          text.gsub!(AUTO_LINK_RE) do
86
            all, leading, proto, url, post = $&, $1, $2, $3, $6
87
            if leading =~ /<a\s/i || leading =~ /![<>=]?/
88
              # don't replace URL's that are already linked
89
              # and URL's prefixed with ! !> !< != (textile images)
90
              all
91
            else
92
              # Idea below : an URL with unbalanced parethesis and
93
              # ending by ')' is put into external parenthesis
94
              if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
95
                url=url[0..-2] # discard closing parenth from url
96
                post = ")"+post # add closing parenth to post
97
              end
98
              tag = content_tag('a', proto + url, :href => "#{proto=="www."?"http://www.":proto}#{url}", :class => 'external')
99
              %(#{leading}#{tag}#{post})
100
            end
101
          end
102
        end
103
104
        # Turns all email addresses into clickable links (code from Rails).
105
        def inline_auto_mailto(text)
106
          text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
107
            mail = $1
108
            if text.match(/<a\b[^>]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/)
109
              mail
110
            else
111
              content_tag('a', mail, :href => "mailto:#{mail}", :class => "email")
112
            end
113
          end
114
        end
115
      end
116
    end
117
  end
118
end