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