annotate lib/redmine/wiki_formatting/textile/formatter.rb @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents 3e4c3460b6ca
children e248c7af89ec
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 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@1115 26 include Redmine::WikiFormatting::LinksHelper
Chris@1115 27
Chris@1115 28 alias :inline_auto_link :auto_link!
Chris@1115 29 alias :inline_auto_mailto :auto_mailto!
Chris@909 30
Chris@0 31 # auto_link rule after textile rules so that it doesn't break !image_url! tags
chris@37 32 RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto]
Chris@909 33
Chris@0 34 def initialize(*args)
Chris@0 35 super
Chris@0 36 self.hard_breaks=true
Chris@0 37 self.no_span_caps=true
Chris@1115 38 self.filter_styles=false
Chris@0 39 end
Chris@909 40
Chris@0 41 def to_html(*rules)
Chris@0 42 @toc = []
Chris@0 43 super(*RULES).to_s
Chris@0 44 end
Chris@909 45
Chris@909 46 def get_section(index)
Chris@909 47 section = extract_sections(index)[1]
Chris@909 48 hash = Digest::MD5.hexdigest(section)
Chris@909 49 return section, hash
Chris@909 50 end
Chris@909 51
Chris@909 52 def update_section(index, update, hash=nil)
Chris@909 53 t = extract_sections(index)
Chris@909 54 if hash.present? && hash != Digest::MD5.hexdigest(t[1])
Chris@909 55 raise Redmine::WikiFormatting::StaleSectionError
Chris@909 56 end
Chris@909 57 t[1] = update unless t[1].blank?
Chris@909 58 t.reject(&:blank?).join "\n\n"
Chris@909 59 end
Chris@909 60
Chris@909 61 def extract_sections(index)
Chris@909 62 @pre_list = []
Chris@909 63 text = self.dup
Chris@909 64 rip_offtags text, false, false
Chris@909 65 before = ''
Chris@909 66 s = ''
Chris@909 67 after = ''
Chris@909 68 i = 0
Chris@909 69 l = 1
Chris@909 70 started = false
Chris@909 71 ended = false
Chris@1294 72 text.scan(/(((?:.*?)(\A|\r?\n\s*\r?\n))(h(\d+)(#{A}#{C})\.(?::(\S+))?[ \t](.*?)$)|.*)/m).each do |all, content, lf, heading, level|
Chris@909 73 if heading.nil?
Chris@909 74 if ended
Chris@909 75 after << all
Chris@909 76 elsif started
Chris@909 77 s << all
Chris@909 78 else
Chris@909 79 before << all
Chris@909 80 end
Chris@909 81 break
Chris@909 82 end
Chris@909 83 i += 1
Chris@909 84 if ended
Chris@909 85 after << all
Chris@909 86 elsif i == index
Chris@909 87 l = level.to_i
Chris@909 88 before << content
Chris@909 89 s << heading
Chris@909 90 started = true
Chris@909 91 elsif i > index
Chris@909 92 s << content
Chris@909 93 if level.to_i > l
Chris@909 94 s << heading
Chris@909 95 else
Chris@909 96 after << heading
Chris@909 97 ended = true
Chris@909 98 end
Chris@909 99 else
Chris@909 100 before << all
Chris@909 101 end
Chris@909 102 end
Chris@909 103 sections = [before.strip, s.strip, after.strip]
Chris@909 104 sections.each {|section| smooth_offtags_without_code_highlighting section}
Chris@909 105 sections
Chris@909 106 end
Chris@909 107
Chris@0 108 private
Chris@909 109
Chris@0 110 # Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet.
Chris@0 111 # <a href="http://code.whytheluckystiff.net/redcloth/changeset/128">http://code.whytheluckystiff.net/redcloth/changeset/128</a>
Chris@909 112 def hard_break( text )
Chris@441 113 text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks
Chris@0 114 end
Chris@909 115
Chris@909 116 alias :smooth_offtags_without_code_highlighting :smooth_offtags
Chris@0 117 # Patch to add code highlighting support to RedCloth
Chris@0 118 def smooth_offtags( text )
Chris@0 119 unless @pre_list.empty?
Chris@0 120 ## replace <pre> content
Chris@0 121 text.gsub!(/<redpre#(\d+)>/) do
Chris@0 122 content = @pre_list[$1.to_i]
Chris@0 123 if content.match(/<code\s+class="(\w+)">\s?(.+)/m)
Chris@909 124 content = "<code class=\"#{$1} syntaxhl\">" +
Chris@0 125 Redmine::SyntaxHighlighting.highlight_by_language($2, $1)
Chris@0 126 end
Chris@0 127 content
Chris@0 128 end
Chris@0 129 end
Chris@0 130 end
Chris@0 131 end
Chris@0 132 end
Chris@0 133 end
Chris@0 134 end