Chris@1296: # Redmine - project management software
Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1296: #
Chris@1296: # This program is free software; you can redistribute it and/or
Chris@1296: # modify it under the terms of the GNU General Public License
Chris@1296: # as published by the Free Software Foundation; either version 2
Chris@1296: # of the License, or (at your option) any later version.
Chris@1296: #
Chris@1296: # This program is distributed in the hope that it will be useful,
Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1296: # GNU General Public License for more details.
Chris@1296: #
Chris@1296: # You should have received a copy of the GNU General Public License
Chris@1296: # along with this program; if not, write to the Free Software
Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1296:
Chris@1296: require 'redcloth3'
Chris@1296: require 'digest/md5'
Chris@1296:
Chris@1296: module Redmine
Chris@1296: module WikiFormatting
Chris@1296: module Textile
Chris@1296: class Formatter < RedCloth3
Chris@1296: include ActionView::Helpers::TagHelper
Chris@1296: include Redmine::WikiFormatting::LinksHelper
Chris@1296:
Chris@1296: alias :inline_auto_link :auto_link!
Chris@1296: alias :inline_auto_mailto :auto_mailto!
Chris@1296:
Chris@1296: # auto_link rule after textile rules so that it doesn't break !image_url! tags
Chris@1296: RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto]
Chris@1296:
Chris@1296: def initialize(*args)
Chris@1296: super
Chris@1296: self.hard_breaks=true
Chris@1296: self.no_span_caps=true
Chris@1296: self.filter_styles=false
Chris@1296: end
Chris@1296:
Chris@1296: def to_html(*rules)
Chris@1296: @toc = []
Chris@1296: super(*RULES).to_s
Chris@1296: end
Chris@1296:
Chris@1296: def get_section(index)
Chris@1296: section = extract_sections(index)[1]
Chris@1296: hash = Digest::MD5.hexdigest(section)
Chris@1296: return section, hash
Chris@1296: end
Chris@1296:
Chris@1296: def update_section(index, update, hash=nil)
Chris@1296: t = extract_sections(index)
Chris@1296: if hash.present? && hash != Digest::MD5.hexdigest(t[1])
Chris@1296: raise Redmine::WikiFormatting::StaleSectionError
Chris@1296: end
Chris@1296: t[1] = update unless t[1].blank?
Chris@1296: t.reject(&:blank?).join "\n\n"
Chris@1296: end
Chris@1296:
Chris@1296: def extract_sections(index)
Chris@1296: @pre_list = []
Chris@1296: text = self.dup
Chris@1296: rip_offtags text, false, false
Chris@1296: before = ''
Chris@1296: s = ''
Chris@1296: after = ''
Chris@1296: i = 0
Chris@1296: l = 1
Chris@1296: started = false
Chris@1296: ended = false
Chris@1296: text.scan(/(((?:.*?)(\A|\r?\n\s*\r?\n))(h(\d+)(#{A}#{C})\.(?::(\S+))?[ \t](.*?)$)|.*)/m).each do |all, content, lf, heading, level|
Chris@1296: if heading.nil?
Chris@1296: if ended
Chris@1296: after << all
Chris@1296: elsif started
Chris@1296: s << all
Chris@1296: else
Chris@1296: before << all
Chris@1296: end
Chris@1296: break
Chris@1296: end
Chris@1296: i += 1
Chris@1296: if ended
Chris@1296: after << all
Chris@1296: elsif i == index
Chris@1296: l = level.to_i
Chris@1296: before << content
Chris@1296: s << heading
Chris@1296: started = true
Chris@1296: elsif i > index
Chris@1296: s << content
Chris@1296: if level.to_i > l
Chris@1296: s << heading
Chris@1296: else
Chris@1296: after << heading
Chris@1296: ended = true
Chris@1296: end
Chris@1296: else
Chris@1296: before << all
Chris@1296: end
Chris@1296: end
Chris@1296: sections = [before.strip, s.strip, after.strip]
Chris@1296: sections.each {|section| smooth_offtags_without_code_highlighting section}
Chris@1296: sections
Chris@1296: end
Chris@1296:
Chris@1296: private
Chris@1296:
Chris@1296: # Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet.
Chris@1296: # http://code.whytheluckystiff.net/redcloth/changeset/128
Chris@1296: def hard_break( text )
Chris@1296: text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1
" ) if hard_breaks
Chris@1296: end
Chris@1296:
Chris@1296: alias :smooth_offtags_without_code_highlighting :smooth_offtags
Chris@1296: # Patch to add code highlighting support to RedCloth
Chris@1296: def smooth_offtags( text )
Chris@1296: unless @pre_list.empty?
Chris@1296: ## replace
content Chris@1296: text.gsub!(//) do Chris@1296: content = @pre_list[$1.to_i] Chris@1296: if content.match(/ \s?(.+)/m) Chris@1296: content = "
" + Chris@1296: Redmine::SyntaxHighlighting.highlight_by_language($2, $1) Chris@1296: end Chris@1296: content Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end