To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / 4e / 4e64ceb850aaa04a7acc9aad3a69b5f58d199fa5.svn-base @ 1297:0a574315af3e
History | View | Annotate | Download (6.42 KB)
| 1 |
# Redmine - project management software |
|---|---|
| 2 |
# Copyright (C) 2006-2011 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 |
require 'digest/md5' |
| 20 |
|
| 21 |
module Redmine |
| 22 |
module WikiFormatting |
| 23 |
module Textile |
| 24 |
class Formatter < RedCloth3 |
| 25 |
include ActionView::Helpers::TagHelper |
| 26 |
|
| 27 |
# auto_link rule after textile rules so that it doesn't break !image_url! tags |
| 28 |
RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto] |
| 29 |
|
| 30 |
def initialize(*args) |
| 31 |
super |
| 32 |
self.hard_breaks=true |
| 33 |
self.no_span_caps=true |
| 34 |
self.filter_styles=true |
| 35 |
end |
| 36 |
|
| 37 |
def to_html(*rules) |
| 38 |
@toc = [] |
| 39 |
super(*RULES).to_s |
| 40 |
end |
| 41 |
|
| 42 |
def get_section(index) |
| 43 |
section = extract_sections(index)[1] |
| 44 |
hash = Digest::MD5.hexdigest(section) |
| 45 |
return section, hash |
| 46 |
end |
| 47 |
|
| 48 |
def update_section(index, update, hash=nil) |
| 49 |
t = extract_sections(index) |
| 50 |
if hash.present? && hash != Digest::MD5.hexdigest(t[1]) |
| 51 |
raise Redmine::WikiFormatting::StaleSectionError |
| 52 |
end |
| 53 |
t[1] = update unless t[1].blank? |
| 54 |
t.reject(&:blank?).join "\n\n" |
| 55 |
end |
| 56 |
|
| 57 |
def extract_sections(index) |
| 58 |
@pre_list = [] |
| 59 |
text = self.dup |
| 60 |
rip_offtags text, false, false |
| 61 |
before = '' |
| 62 |
s = '' |
| 63 |
after = '' |
| 64 |
i = 0 |
| 65 |
l = 1 |
| 66 |
started = false |
| 67 |
ended = false |
| 68 |
text.scan(/(((?:.*?)(\A|\r?\n\r?\n))(h(\d+)(#{A}#{C})\.(?::(\S+))? (.*?)$)|.*)/m).each do |all, content, lf, heading, level|
|
| 69 |
if heading.nil? |
| 70 |
if ended |
| 71 |
after << all |
| 72 |
elsif started |
| 73 |
s << all |
| 74 |
else |
| 75 |
before << all |
| 76 |
end |
| 77 |
break |
| 78 |
end |
| 79 |
i += 1 |
| 80 |
if ended |
| 81 |
after << all |
| 82 |
elsif i == index |
| 83 |
l = level.to_i |
| 84 |
before << content |
| 85 |
s << heading |
| 86 |
started = true |
| 87 |
elsif i > index |
| 88 |
s << content |
| 89 |
if level.to_i > l |
| 90 |
s << heading |
| 91 |
else |
| 92 |
after << heading |
| 93 |
ended = true |
| 94 |
end |
| 95 |
else |
| 96 |
before << all |
| 97 |
end |
| 98 |
end |
| 99 |
sections = [before.strip, s.strip, after.strip] |
| 100 |
sections.each {|section| smooth_offtags_without_code_highlighting section}
|
| 101 |
sections |
| 102 |
end |
| 103 |
|
| 104 |
private |
| 105 |
|
| 106 |
# Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet. |
| 107 |
# <a href="http://code.whytheluckystiff.net/redcloth/changeset/128">http://code.whytheluckystiff.net/redcloth/changeset/128</a> |
| 108 |
def hard_break( text ) |
| 109 |
text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks
|
| 110 |
end |
| 111 |
|
| 112 |
alias :smooth_offtags_without_code_highlighting :smooth_offtags |
| 113 |
# Patch to add code highlighting support to RedCloth |
| 114 |
def smooth_offtags( text ) |
| 115 |
unless @pre_list.empty? |
| 116 |
## replace <pre> content |
| 117 |
text.gsub!(/<redpre#(\d+)>/) do |
| 118 |
content = @pre_list[$1.to_i] |
| 119 |
if content.match(/<code\s+class="(\w+)">\s?(.+)/m) |
| 120 |
content = "<code class=\"#{$1} syntaxhl\">" +
|
| 121 |
Redmine::SyntaxHighlighting.highlight_by_language($2, $1) |
| 122 |
end |
| 123 |
content |
| 124 |
end |
| 125 |
end |
| 126 |
end |
| 127 |
|
| 128 |
AUTO_LINK_RE = %r{
|
| 129 |
( # leading text |
| 130 |
<\w+.*?>| # leading HTML tag, or |
| 131 |
[^=<>!:'"/]| # leading punctuation, or |
| 132 |
^ # beginning of line |
| 133 |
) |
| 134 |
( |
| 135 |
(?:https?://)| # protocol spec, or |
| 136 |
(?:s?ftps?://)| |
| 137 |
(?:www\.) # www.* |
| 138 |
) |
| 139 |
( |
| 140 |
(\S+?) # url |
| 141 |
(\/)? # slash |
| 142 |
) |
| 143 |
((?:>)?|[^\w\=\/;\(\)]*?) # post |
| 144 |
(?=<|\s|$) |
| 145 |
}x unless const_defined?(:AUTO_LINK_RE) |
| 146 |
|
| 147 |
# Turns all urls into clickable links (code from Rails). |
| 148 |
def inline_auto_link(text) |
| 149 |
text.gsub!(AUTO_LINK_RE) do |
| 150 |
all, leading, proto, url, post = $&, $1, $2, $3, $6 |
| 151 |
if leading =~ /<a\s/i || leading =~ /![<>=]?/ |
| 152 |
# don't replace URL's that are already linked |
| 153 |
# and URL's prefixed with ! !> !< != (textile images) |
| 154 |
all |
| 155 |
else |
| 156 |
# Idea below : an URL with unbalanced parethesis and |
| 157 |
# ending by ')' is put into external parenthesis |
| 158 |
if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
|
| 159 |
url=url[0..-2] # discard closing parenth from url |
| 160 |
post = ")"+post # add closing parenth to post |
| 161 |
end |
| 162 |
tag = content_tag('a', proto + url, :href => "#{proto=="www."?"http://www.":proto}#{url}", :class => 'external')
|
| 163 |
%(#{leading}#{tag}#{post})
|
| 164 |
end |
| 165 |
end |
| 166 |
end |
| 167 |
|
| 168 |
# Turns all email addresses into clickable links (code from Rails). |
| 169 |
def inline_auto_mailto(text) |
| 170 |
text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do |
| 171 |
mail = $1 |
| 172 |
if text.match(/<a\b[^>]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/)
|
| 173 |
|
| 174 |
else |
| 175 |
content_tag('a', mail, :href => "mailto:#{mail}", :class => "email")
|
| 176 |
end |
| 177 |
end |
| 178 |
end |
| 179 |
end |
| 180 |
end |
| 181 |
end |
| 182 |
end |