annotate .svn/pristine/31/31f8831dd1cf0c2ef63a3b5e3c999a2d0f32707b.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 require 'redcloth3'
Chris@1494 19 require 'digest/md5'
Chris@1494 20
Chris@1494 21 module Redmine
Chris@1494 22 module WikiFormatting
Chris@1494 23 module Textile
Chris@1494 24 class Formatter < RedCloth3
Chris@1494 25 include ActionView::Helpers::TagHelper
Chris@1494 26 include Redmine::WikiFormatting::LinksHelper
Chris@1494 27
Chris@1494 28 alias :inline_auto_link :auto_link!
Chris@1494 29 alias :inline_auto_mailto :auto_mailto!
Chris@1494 30
Chris@1494 31 # auto_link rule after textile rules so that it doesn't break !image_url! tags
Chris@1494 32 RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto]
Chris@1494 33
Chris@1494 34 def initialize(*args)
Chris@1494 35 super
Chris@1494 36 self.hard_breaks=true
Chris@1494 37 self.no_span_caps=true
Chris@1494 38 self.filter_styles=false
Chris@1494 39 end
Chris@1494 40
Chris@1494 41 def to_html(*rules)
Chris@1494 42 @toc = []
Chris@1494 43 super(*RULES).to_s
Chris@1494 44 end
Chris@1494 45
Chris@1494 46 def get_section(index)
Chris@1494 47 section = extract_sections(index)[1]
Chris@1494 48 hash = Digest::MD5.hexdigest(section)
Chris@1494 49 return section, hash
Chris@1494 50 end
Chris@1494 51
Chris@1494 52 def update_section(index, update, hash=nil)
Chris@1494 53 t = extract_sections(index)
Chris@1494 54 if hash.present? && hash != Digest::MD5.hexdigest(t[1])
Chris@1494 55 raise Redmine::WikiFormatting::StaleSectionError
Chris@1494 56 end
Chris@1494 57 t[1] = update unless t[1].blank?
Chris@1494 58 t.reject(&:blank?).join "\n\n"
Chris@1494 59 end
Chris@1494 60
Chris@1494 61 def extract_sections(index)
Chris@1494 62 @pre_list = []
Chris@1494 63 text = self.dup
Chris@1494 64 rip_offtags text, false, false
Chris@1494 65 before = ''
Chris@1494 66 s = ''
Chris@1494 67 after = ''
Chris@1494 68 i = 0
Chris@1494 69 l = 1
Chris@1494 70 started = false
Chris@1494 71 ended = false
Chris@1494 72 text.scan(/(((?:.*?)(\A|\r?\n\s*\r?\n))(h(\d+)(#{A}#{C})\.(?::(\S+))?[ \t](.*?)$)|.*)/m).each do |all, content, lf, heading, level|
Chris@1494 73 if heading.nil?
Chris@1494 74 if ended
Chris@1494 75 after << all
Chris@1494 76 elsif started
Chris@1494 77 s << all
Chris@1494 78 else
Chris@1494 79 before << all
Chris@1494 80 end
Chris@1494 81 break
Chris@1494 82 end
Chris@1494 83 i += 1
Chris@1494 84 if ended
Chris@1494 85 after << all
Chris@1494 86 elsif i == index
Chris@1494 87 l = level.to_i
Chris@1494 88 before << content
Chris@1494 89 s << heading
Chris@1494 90 started = true
Chris@1494 91 elsif i > index
Chris@1494 92 s << content
Chris@1494 93 if level.to_i > l
Chris@1494 94 s << heading
Chris@1494 95 else
Chris@1494 96 after << heading
Chris@1494 97 ended = true
Chris@1494 98 end
Chris@1494 99 else
Chris@1494 100 before << all
Chris@1494 101 end
Chris@1494 102 end
Chris@1494 103 sections = [before.strip, s.strip, after.strip]
Chris@1494 104 sections.each {|section| smooth_offtags_without_code_highlighting section}
Chris@1494 105 sections
Chris@1494 106 end
Chris@1494 107
Chris@1494 108 private
Chris@1494 109
Chris@1494 110 # Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet.
Chris@1494 111 # <a href="http://code.whytheluckystiff.net/redcloth/changeset/128">http://code.whytheluckystiff.net/redcloth/changeset/128</a>
Chris@1494 112 def hard_break( text )
Chris@1494 113 text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks
Chris@1494 114 end
Chris@1494 115
Chris@1494 116 alias :smooth_offtags_without_code_highlighting :smooth_offtags
Chris@1494 117 # Patch to add code highlighting support to RedCloth
Chris@1494 118 def smooth_offtags( text )
Chris@1494 119 unless @pre_list.empty?
Chris@1494 120 ## replace <pre> content
Chris@1494 121 text.gsub!(/<redpre#(\d+)>/) do
Chris@1494 122 content = @pre_list[$1.to_i]
Chris@1494 123 if content.match(/<code\s+class="(\w+)">\s?(.+)/m)
Chris@1494 124 content = "<code class=\"#{$1} syntaxhl\">" +
Chris@1494 125 Redmine::SyntaxHighlighting.highlight_by_language($2, $1)
Chris@1494 126 end
Chris@1494 127 content
Chris@1494 128 end
Chris@1494 129 end
Chris@1494 130 end
Chris@1494 131 end
Chris@1494 132 end
Chris@1494 133 end
Chris@1494 134 end