##// END OF EJS Templates
Fixed: Links to other wiki pages in headings get corrupted (class attributes shown)....
Jean-Philippe Lang -
r758:e1bb4a5baa34
parent child
Show More
@@ -1,122 +1,124
1 1 require 'redcloth'
2 2 require 'coderay'
3 3
4 4 module Redmine
5 5 module WikiFormatting
6 6
7 7 private
8 8
9 9 class TextileFormatter < RedCloth
10 10 RULES = [:inline_auto_link, :inline_auto_mailto, :textile, :inline_toc]
11 11
12 12 def initialize(*args)
13 13 super
14 14 self.hard_breaks=true
15 15 end
16 16
17 17 def to_html
18 18 @toc = []
19 19 super(*RULES).to_s
20 20 end
21 21
22 22 private
23 23
24 24 # Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet.
25 25 # <a href="http://code.whytheluckystiff.net/redcloth/changeset/128">http://code.whytheluckystiff.net/redcloth/changeset/128</a>
26 26 def hard_break( text )
27 27 text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks
28 28 end
29 29
30 30 # Patch to add code highlighting support to RedCloth
31 31 def smooth_offtags( text )
32 32 unless @pre_list.empty?
33 33 ## replace <pre> content
34 34 text.gsub!(/<redpre#(\d+)>/) do
35 35 content = @pre_list[$1.to_i]
36 36 if content.match(/<code\s+class="(\w+)">\s?(.+)/m)
37 37 content = "<code class=\"#{$1} CodeRay\">" +
38 38 CodeRay.scan($2, $1).html(:escape => false, :line_numbers => :inline)
39 39 end
40 40 content
41 41 end
42 42 end
43 43 end
44 44
45 45 # Patch to add 'table of content' support to RedCloth
46 46 def textile_p_withtoc(tag, atts, cite, content)
47 47 if tag =~ /^h(\d)$/
48 48 @toc << [$1.to_i, content]
49 49 end
50 content = "<a name=\"#{@toc.length}-#{content}\" class=\"wiki-page\"></a>" + content
50 content = "<a name=\"#{@toc.length}\" class=\"wiki-page\"></a>" + content
51 51 textile_p(tag, atts, cite, content)
52 52 end
53 53
54 54 alias :textile_h1 :textile_p_withtoc
55 55 alias :textile_h2 :textile_p_withtoc
56 56 alias :textile_h3 :textile_p_withtoc
57 57
58 58 def inline_toc(text)
59 59 text.gsub!(/<p>\{\{([<>]?)toc\}\}<\/p>/i) do
60 60 div_class = 'toc'
61 61 div_class << ' right' if $1 == '>'
62 62 out = "<div class=\"#{div_class}\">"
63 63 @toc.each_with_index do |heading, index|
64 out << "<a href=\"##{index+1}-#{heading.last}\" class=\"heading#{heading.first}\">#{heading.last}</a>"
64 # remove wiki links from the item
65 toc_item = heading.last.gsub(/(\[\[|\]\])/, '')
66 out << "<a href=\"##{index+1}\" class=\"heading#{heading.first}\">#{toc_item}</a>"
65 67 end
66 68 out << '</div>'
67 69 out
68 70 end
69 71 end
70 72
71 73 AUTO_LINK_RE = %r{
72 74 ( # leading text
73 75 <\w+.*?>| # leading HTML tag, or
74 76 [^=<>!:'"/]| # leading punctuation, or
75 77 ^ # beginning of line
76 78 )
77 79 (
78 80 (?:https?://)| # protocol spec, or
79 81 (?:www\.) # www.*
80 82 )
81 83 (
82 84 [-\w]+ # subdomain or domain
83 85 (?:\.[-\w]+)* # remaining subdomains or domain
84 86 (?::\d+)? # port
85 87 (?:/(?:(?:[~\w\+%-]|(?:[,.;:][^\s$]))+)?)* # path
86 88 (?:\?[\w\+%&=.;-]+)? # query string
87 89 (?:\#[\w\-]*)? # trailing anchor
88 90 )
89 91 ([[:punct:]]|\s|<|$) # trailing text
90 92 }x unless const_defined?(:AUTO_LINK_RE)
91 93
92 94 # Turns all urls into clickable links (code from Rails).
93 95 def inline_auto_link(text)
94 96 text.gsub!(AUTO_LINK_RE) do
95 97 all, a, b, c, d = $&, $1, $2, $3, $4
96 98 if a =~ /<a\s/i || a =~ /![<>=]?/
97 99 # don't replace URL's that are already linked
98 100 # and URL's prefixed with ! !> !< != (textile images)
99 101 all
100 102 else
101 103 text = b + c
102 104 %(#{a}<a href="#{b=="www."?"http://www.":b}#{c}">#{text}</a>#{d})
103 105 end
104 106 end
105 107 end
106 108
107 109 # Turns all email addresses into clickable links (code from Rails).
108 110 def inline_auto_mailto(text)
109 111 text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
110 112 text = $1
111 113 %{<a href="mailto:#{$1}" class="email">#{text}</a>}
112 114 end
113 115 end
114 116 end
115 117
116 118 public
117 119
118 120 def self.to_html(text, options = {})
119 121 TextileFormatter.new(text).to_html
120 122 end
121 123 end
122 124 end
General Comments 0
You need to be logged in to leave comments. Login now