annotate .svn/pristine/30/3023c43eca89098e7ad3d81f2db133b0643baf81.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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