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