annotate lib/redmine/wiki_formatting/textile/formatter.rb @ 976:0befb332f41a get_statistics

get stats up to current date
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Thu, 25 Oct 2012 13:50:45 +0100
parents cbb26bc654de
children 433d4f72a19b
rev   line source
Chris@0 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 require 'redcloth3'
Chris@909 19 require 'digest/md5'
Chris@0 20
Chris@0 21 module Redmine
Chris@0 22 module WikiFormatting
Chris@0 23 module Textile
Chris@0 24 class Formatter < RedCloth3
Chris@0 25 include ActionView::Helpers::TagHelper
Chris@909 26
Chris@0 27 # auto_link rule after textile rules so that it doesn't break !image_url! tags
chris@37 28 RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto]
Chris@909 29
Chris@0 30 def initialize(*args)
Chris@0 31 super
Chris@0 32 self.hard_breaks=true
Chris@0 33 self.no_span_caps=true
Chris@0 34 self.filter_styles=true
Chris@0 35 end
Chris@909 36
Chris@0 37 def to_html(*rules)
Chris@0 38 @toc = []
Chris@0 39 super(*RULES).to_s
Chris@0 40 end
Chris@909 41
Chris@909 42 def get_section(index)
Chris@909 43 section = extract_sections(index)[1]
Chris@909 44 hash = Digest::MD5.hexdigest(section)
Chris@909 45 return section, hash
Chris@909 46 end
Chris@909 47
Chris@909 48 def update_section(index, update, hash=nil)
Chris@909 49 t = extract_sections(index)
Chris@909 50 if hash.present? && hash != Digest::MD5.hexdigest(t[1])
Chris@909 51 raise Redmine::WikiFormatting::StaleSectionError
Chris@909 52 end
Chris@909 53 t[1] = update unless t[1].blank?
Chris@909 54 t.reject(&:blank?).join "\n\n"
Chris@909 55 end
Chris@909 56
Chris@909 57 def extract_sections(index)
Chris@909 58 @pre_list = []
Chris@909 59 text = self.dup
Chris@909 60 rip_offtags text, false, false
Chris@909 61 before = ''
Chris@909 62 s = ''
Chris@909 63 after = ''
Chris@909 64 i = 0
Chris@909 65 l = 1
Chris@909 66 started = false
Chris@909 67 ended = false
Chris@909 68 text.scan(/(((?:.*?)(\A|\r?\n\r?\n))(h(\d+)(#{A}#{C})\.(?::(\S+))? (.*?)$)|.*)/m).each do |all, content, lf, heading, level|
Chris@909 69 if heading.nil?
Chris@909 70 if ended
Chris@909 71 after << all
Chris@909 72 elsif started
Chris@909 73 s << all
Chris@909 74 else
Chris@909 75 before << all
Chris@909 76 end
Chris@909 77 break
Chris@909 78 end
Chris@909 79 i += 1
Chris@909 80 if ended
Chris@909 81 after << all
Chris@909 82 elsif i == index
Chris@909 83 l = level.to_i
Chris@909 84 before << content
Chris@909 85 s << heading
Chris@909 86 started = true
Chris@909 87 elsif i > index
Chris@909 88 s << content
Chris@909 89 if level.to_i > l
Chris@909 90 s << heading
Chris@909 91 else
Chris@909 92 after << heading
Chris@909 93 ended = true
Chris@909 94 end
Chris@909 95 else
Chris@909 96 before << all
Chris@909 97 end
Chris@909 98 end
Chris@909 99 sections = [before.strip, s.strip, after.strip]
Chris@909 100 sections.each {|section| smooth_offtags_without_code_highlighting section}
Chris@909 101 sections
Chris@909 102 end
Chris@909 103
Chris@0 104 private
Chris@909 105
Chris@0 106 # Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet.
Chris@0 107 # <a href="http://code.whytheluckystiff.net/redcloth/changeset/128">http://code.whytheluckystiff.net/redcloth/changeset/128</a>
Chris@909 108 def hard_break( text )
Chris@441 109 text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks
Chris@0 110 end
Chris@909 111
Chris@909 112 alias :smooth_offtags_without_code_highlighting :smooth_offtags
Chris@0 113 # Patch to add code highlighting support to RedCloth
Chris@0 114 def smooth_offtags( text )
Chris@0 115 unless @pre_list.empty?
Chris@0 116 ## replace <pre> content
Chris@0 117 text.gsub!(/<redpre#(\d+)>/) do
Chris@0 118 content = @pre_list[$1.to_i]
Chris@0 119 if content.match(/<code\s+class="(\w+)">\s?(.+)/m)
Chris@909 120 content = "<code class=\"#{$1} syntaxhl\">" +
Chris@0 121 Redmine::SyntaxHighlighting.highlight_by_language($2, $1)
Chris@0 122 end
Chris@0 123 content
Chris@0 124 end
Chris@0 125 end
Chris@0 126 end
Chris@909 127
Chris@0 128 AUTO_LINK_RE = %r{
Chris@0 129 ( # leading text
Chris@0 130 <\w+.*?>| # leading HTML tag, or
Chris@909 131 [^=<>!:'"/]| # leading punctuation, or
Chris@0 132 ^ # beginning of line
Chris@0 133 )
Chris@0 134 (
Chris@0 135 (?:https?://)| # protocol spec, or
Chris@0 136 (?:s?ftps?://)|
Chris@0 137 (?:www\.) # www.*
Chris@0 138 )
Chris@0 139 (
Chris@0 140 (\S+?) # url
Chris@0 141 (\/)? # slash
Chris@0 142 )
chris@37 143 ((?:&gt;)?|[^\w\=\/;\(\)]*?) # post
Chris@0 144 (?=<|\s|$)
Chris@0 145 }x unless const_defined?(:AUTO_LINK_RE)
Chris@909 146
Chris@0 147 # Turns all urls into clickable links (code from Rails).
Chris@0 148 def inline_auto_link(text)
Chris@0 149 text.gsub!(AUTO_LINK_RE) do
Chris@0 150 all, leading, proto, url, post = $&, $1, $2, $3, $6
Chris@0 151 if leading =~ /<a\s/i || leading =~ /![<>=]?/
Chris@0 152 # don't replace URL's that are already linked
Chris@0 153 # and URL's prefixed with ! !> !< != (textile images)
Chris@0 154 all
Chris@0 155 else
Chris@0 156 # Idea below : an URL with unbalanced parethesis and
Chris@0 157 # ending by ')' is put into external parenthesis
Chris@0 158 if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
Chris@0 159 url=url[0..-2] # discard closing parenth from url
Chris@0 160 post = ")"+post # add closing parenth to post
Chris@0 161 end
Chris@0 162 tag = content_tag('a', proto + url, :href => "#{proto=="www."?"http://www.":proto}#{url}", :class => 'external')
Chris@0 163 %(#{leading}#{tag}#{post})
Chris@0 164 end
Chris@0 165 end
Chris@0 166 end
Chris@909 167
Chris@0 168 # Turns all email addresses into clickable links (code from Rails).
Chris@0 169 def inline_auto_mailto(text)
Chris@0 170 text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
Chris@0 171 mail = $1
Chris@0 172 if text.match(/<a\b[^>]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/)
Chris@0 173 mail
Chris@0 174 else
Chris@0 175 content_tag('a', mail, :href => "mailto:#{mail}", :class => "email")
Chris@0 176 end
Chris@0 177 end
Chris@0 178 end
Chris@0 179 end
Chris@0 180 end
Chris@0 181 end
Chris@0 182 end