Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require 'redcloth3' Chris@909: require 'digest/md5' Chris@909: Chris@909: module Redmine Chris@909: module WikiFormatting Chris@909: module Textile Chris@909: class Formatter < RedCloth3 Chris@909: include ActionView::Helpers::TagHelper Chris@909: Chris@909: # auto_link rule after textile rules so that it doesn't break !image_url! tags Chris@909: RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto] Chris@909: Chris@909: def initialize(*args) Chris@909: super Chris@909: self.hard_breaks=true Chris@909: self.no_span_caps=true Chris@909: self.filter_styles=true Chris@909: end Chris@909: Chris@909: def to_html(*rules) Chris@909: @toc = [] Chris@909: super(*RULES).to_s Chris@909: end Chris@909: Chris@909: def get_section(index) Chris@909: section = extract_sections(index)[1] Chris@909: hash = Digest::MD5.hexdigest(section) Chris@909: return section, hash Chris@909: end Chris@909: Chris@909: def update_section(index, update, hash=nil) Chris@909: t = extract_sections(index) Chris@909: if hash.present? && hash != Digest::MD5.hexdigest(t[1]) Chris@909: raise Redmine::WikiFormatting::StaleSectionError Chris@909: end Chris@909: t[1] = update unless t[1].blank? Chris@909: t.reject(&:blank?).join "\n\n" Chris@909: end Chris@909: Chris@909: def extract_sections(index) Chris@909: @pre_list = [] Chris@909: text = self.dup Chris@909: rip_offtags text, false, false Chris@909: before = '' Chris@909: s = '' Chris@909: after = '' Chris@909: i = 0 Chris@909: l = 1 Chris@909: started = false Chris@909: ended = false Chris@909: text.scan(/(((?:.*?)(\A|\r?\n\r?\n))(h(\d+)(#{A}#{C})\.(?::(\S+))? (.*?)$)|.*)/m).each do |all, content, lf, heading, level| Chris@909: if heading.nil? Chris@909: if ended Chris@909: after << all Chris@909: elsif started Chris@909: s << all Chris@909: else Chris@909: before << all Chris@909: end Chris@909: break Chris@909: end Chris@909: i += 1 Chris@909: if ended Chris@909: after << all Chris@909: elsif i == index Chris@909: l = level.to_i Chris@909: before << content Chris@909: s << heading Chris@909: started = true Chris@909: elsif i > index Chris@909: s << content Chris@909: if level.to_i > l Chris@909: s << heading Chris@909: else Chris@909: after << heading Chris@909: ended = true Chris@909: end Chris@909: else Chris@909: before << all Chris@909: end Chris@909: end Chris@909: sections = [before.strip, s.strip, after.strip] Chris@909: sections.each {|section| smooth_offtags_without_code_highlighting section} Chris@909: sections Chris@909: end Chris@909: Chris@909: private Chris@909: Chris@909: # Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet. Chris@909: # http://code.whytheluckystiff.net/redcloth/changeset/128 Chris@909: def hard_break( text ) Chris@909: text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1
" ) if hard_breaks Chris@909: end Chris@909: Chris@909: alias :smooth_offtags_without_code_highlighting :smooth_offtags Chris@909: # Patch to add code highlighting support to RedCloth Chris@909: def smooth_offtags( text ) Chris@909: unless @pre_list.empty? Chris@909: ## replace
 content
Chris@909:             text.gsub!(//) do
Chris@909:               content = @pre_list[$1.to_i]
Chris@909:               if content.match(/\s?(.+)/m)
Chris@909:                 content = "" +
Chris@909:                   Redmine::SyntaxHighlighting.highlight_by_language($2, $1)
Chris@909:               end
Chris@909:               content
Chris@909:             end
Chris@909:           end
Chris@909:         end
Chris@909: 
Chris@909:         AUTO_LINK_RE = %r{
Chris@909:                         (                          # leading text
Chris@909:                           <\w+.*?>|                # leading HTML tag, or
Chris@909:                           [^=<>!:'"/]|             # leading punctuation, or
Chris@909:                           ^                        # beginning of line
Chris@909:                         )
Chris@909:                         (
Chris@909:                           (?:https?://)|           # protocol spec, or
Chris@909:                           (?:s?ftps?://)|
Chris@909:                           (?:www\.)                # www.*
Chris@909:                         )
Chris@909:                         (
Chris@909:                           (\S+?)                   # url
Chris@909:                           (\/)?                    # slash
Chris@909:                         )
Chris@909:                         ((?:>)?|[^\w\=\/;\(\)]*?)               # post
Chris@909:                         (?=<|\s|$)
Chris@909:                        }x unless const_defined?(:AUTO_LINK_RE)
Chris@909: 
Chris@909:         # Turns all urls into clickable links (code from Rails).
Chris@909:         def inline_auto_link(text)
Chris@909:           text.gsub!(AUTO_LINK_RE) do
Chris@909:             all, leading, proto, url, post = $&, $1, $2, $3, $6
Chris@909:             if leading =~ /=]?/
Chris@909:               # don't replace URL's that are already linked
Chris@909:               # and URL's prefixed with ! !> !< != (textile images)
Chris@909:               all
Chris@909:             else
Chris@909:               # Idea below : an URL with unbalanced parethesis and
Chris@909:               # ending by ')' is put into external parenthesis
Chris@909:               if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
Chris@909:                 url=url[0..-2] # discard closing parenth from url
Chris@909:                 post = ")"+post # add closing parenth to post
Chris@909:               end
Chris@909:               tag = content_tag('a', proto + url, :href => "#{proto=="www."?"http://www.":proto}#{url}", :class => 'external')
Chris@909:               %(#{leading}#{tag}#{post})
Chris@909:             end
Chris@909:           end
Chris@909:         end
Chris@909: 
Chris@909:         # Turns all email addresses into clickable links (code from Rails).
Chris@909:         def inline_auto_mailto(text)
Chris@909:           text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
Chris@909:             mail = $1
Chris@909:             if text.match(/]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/)
Chris@909:               mail
Chris@909:             else
Chris@909:               content_tag('a', mail, :href => "mailto:#{mail}", :class => "email")
Chris@909:             end
Chris@909:           end
Chris@909:         end
Chris@909:       end
Chris@909:     end
Chris@909:   end
Chris@909: end