comparison lib/redmine/wiki_formatting/textile/.svn/text-base/formatter.rb.svn-base @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children 94944d00e43c
comparison
equal deleted inserted replaced
-1:000000000000 0:513646585e45
1 # Redmine - project management software
2 # Copyright (C) 2006-2008 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 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 RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto, :inline_toc]
28
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 text.gsub!( /(.)\n(?!\n|\Z|>| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks
47 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 # Patch to add 'table of content' support to RedCloth
65 def textile_p_withtoc(tag, atts, cite, content)
66 # removes wiki links from the item
67 toc_item = content.gsub(/(\[\[([^\]\|]*)(\|([^\]]*))?\]\])/) { $4 || $2 }
68 # sanitizes titles from links
69 # see redcloth3.rb, same as "#{pre}#{text}#{post}"
70 toc_item.gsub!(LINK_RE) { [$2, $4, $9].join }
71 # sanitizes image links from titles
72 toc_item.gsub!(IMAGE_RE) { [$5].join }
73 # removes styles
74 # eg. %{color:red}Triggers% => Triggers
75 toc_item.gsub! %r[%\{[^\}]*\}([^%]+)%], '\\1'
76
77 # replaces non word caracters by dashes
78 anchor = toc_item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
79
80 unless anchor.blank?
81 if tag =~ /^h(\d)$/
82 @toc << [$1.to_i, anchor, toc_item]
83 end
84 atts << " id=\"#{anchor}\""
85 content = content + "<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a>"
86 end
87 textile_p(tag, atts, cite, content)
88 end
89
90 alias :textile_h1 :textile_p_withtoc
91 alias :textile_h2 :textile_p_withtoc
92 alias :textile_h3 :textile_p_withtoc
93
94 def inline_toc(text)
95 text.gsub!(/<p>\{\{([<>]?)toc\}\}<\/p>/i) do
96 div_class = 'toc'
97 div_class << ' right' if $1 == '>'
98 div_class << ' left' if $1 == '<'
99 out = "<ul class=\"#{div_class}\">"
100 @toc.each do |heading|
101 level, anchor, toc_item = heading
102 out << "<li class=\"heading#{level}\"><a href=\"##{anchor}\">#{toc_item}</a></li>\n"
103 end
104 out << '</ul>'
105 out
106 end
107 end
108
109 AUTO_LINK_RE = %r{
110 ( # leading text
111 <\w+.*?>| # leading HTML tag, or
112 [^=<>!:'"/]| # leading punctuation, or
113 ^ # beginning of line
114 )
115 (
116 (?:https?://)| # protocol spec, or
117 (?:s?ftps?://)|
118 (?:www\.) # www.*
119 )
120 (
121 (\S+?) # url
122 (\/)? # slash
123 )
124 ([^\w\=\/;\(\)]*?) # post
125 (?=<|\s|$)
126 }x unless const_defined?(:AUTO_LINK_RE)
127
128 # Turns all urls into clickable links (code from Rails).
129 def inline_auto_link(text)
130 text.gsub!(AUTO_LINK_RE) do
131 all, leading, proto, url, post = $&, $1, $2, $3, $6
132 if leading =~ /<a\s/i || leading =~ /![<>=]?/
133 # don't replace URL's that are already linked
134 # and URL's prefixed with ! !> !< != (textile images)
135 all
136 else
137 # Idea below : an URL with unbalanced parethesis and
138 # ending by ')' is put into external parenthesis
139 if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
140 url=url[0..-2] # discard closing parenth from url
141 post = ")"+post # add closing parenth to post
142 end
143 tag = content_tag('a', proto + url, :href => "#{proto=="www."?"http://www.":proto}#{url}", :class => 'external')
144 %(#{leading}#{tag}#{post})
145 end
146 end
147 end
148
149 # Turns all email addresses into clickable links (code from Rails).
150 def inline_auto_mailto(text)
151 text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
152 mail = $1
153 if text.match(/<a\b[^>]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/)
154 mail
155 else
156 content_tag('a', mail, :href => "mailto:#{mail}", :class => "email")
157 end
158 end
159 end
160 end
161 end
162 end
163 end