comparison lib/redmine/wiki_formatting/textile/.svn/text-base/formatter.rb.svn-base @ 37:94944d00e43c

* Update to SVN trunk rev 4411
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Fri, 19 Nov 2010 13:24:41 +0000
parents 513646585e45
children cbce1fd3b1b7
comparison
equal deleted inserted replaced
22:40f7cfd4df19 37:94944d00e43c
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang 2 # Copyright (C) 2006-2010 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
22 module Textile 22 module Textile
23 class Formatter < RedCloth3 23 class Formatter < RedCloth3
24 include ActionView::Helpers::TagHelper 24 include ActionView::Helpers::TagHelper
25 25
26 # auto_link rule after textile rules so that it doesn't break !image_url! tags 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] 27 RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto]
28 28
29 def initialize(*args) 29 def initialize(*args)
30 super 30 super
31 self.hard_breaks=true 31 self.hard_breaks=true
32 self.no_span_caps=true 32 self.no_span_caps=true
59 content 59 content
60 end 60 end
61 end 61 end
62 end 62 end
63 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{ 64 AUTO_LINK_RE = %r{
110 ( # leading text 65 ( # leading text
111 <\w+.*?>| # leading HTML tag, or 66 <\w+.*?>| # leading HTML tag, or
112 [^=<>!:'"/]| # leading punctuation, or 67 [^=<>!:'"/]| # leading punctuation, or
113 ^ # beginning of line 68 ^ # beginning of line
119 ) 74 )
120 ( 75 (
121 (\S+?) # url 76 (\S+?) # url
122 (\/)? # slash 77 (\/)? # slash
123 ) 78 )
124 ([^\w\=\/;\(\)]*?) # post 79 ((?:&gt;)?|[^\w\=\/;\(\)]*?) # post
125 (?=<|\s|$) 80 (?=<|\s|$)
126 }x unless const_defined?(:AUTO_LINK_RE) 81 }x unless const_defined?(:AUTO_LINK_RE)
127 82
128 # Turns all urls into clickable links (code from Rails). 83 # Turns all urls into clickable links (code from Rails).
129 def inline_auto_link(text) 84 def inline_auto_link(text)