Chris@0: # Redmine - project management software
Chris@0: # Copyright (C) 2006-2008 Jean-Philippe Lang
Chris@0: #
Chris@0: # This program is free software; you can redistribute it and/or
Chris@0: # modify it under the terms of the GNU General Public License
Chris@0: # as published by the Free Software Foundation; either version 2
Chris@0: # of the License, or (at your option) any later version.
Chris@0: #
Chris@0: # This program is distributed in the hope that it will be useful,
Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0: # GNU General Public License for more details.
Chris@0: #
Chris@0: # You should have received a copy of the GNU General Public License
Chris@0: # along with this program; if not, write to the Free Software
Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0:
Chris@0: require 'redcloth3'
Chris@0:
Chris@0: module Redmine
Chris@0: module WikiFormatting
Chris@0: module Textile
Chris@0: class Formatter < RedCloth3
Chris@0: include ActionView::Helpers::TagHelper
Chris@0:
Chris@0: # auto_link rule after textile rules so that it doesn't break !image_url! tags
Chris@0: RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto, :inline_toc]
Chris@0:
Chris@0: def initialize(*args)
Chris@0: super
Chris@0: self.hard_breaks=true
Chris@0: self.no_span_caps=true
Chris@0: self.filter_styles=true
Chris@0: end
Chris@0:
Chris@0: def to_html(*rules)
Chris@0: @toc = []
Chris@0: super(*RULES).to_s
Chris@0: end
Chris@0:
Chris@0: private
Chris@0:
Chris@0: # Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet.
Chris@0: # http://code.whytheluckystiff.net/redcloth/changeset/128
Chris@0: def hard_break( text )
Chris@0: text.gsub!( /(.)\n(?!\n|\Z|>| *([#*=]+(\s|$)|[{|]))/, "\\1
" ) if hard_breaks
Chris@0: end
Chris@0:
Chris@0: # Patch to add code highlighting support to RedCloth
Chris@0: def smooth_offtags( text )
Chris@0: unless @pre_list.empty?
Chris@0: ## replace
content Chris@0: text.gsub!(//) do Chris@0: content = @pre_list[$1.to_i] Chris@0: if content.match(/ \s?(.+)/m) Chris@0: content = "
" + Chris@0: Redmine::SyntaxHighlighting.highlight_by_language($2, $1) Chris@0: end Chris@0: content Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: # Patch to add 'table of content' support to RedCloth Chris@0: def textile_p_withtoc(tag, atts, cite, content) Chris@0: # removes wiki links from the item Chris@0: toc_item = content.gsub(/(\[\[([^\]\|]*)(\|([^\]]*))?\]\])/) { $4 || $2 } Chris@0: # sanitizes titles from links Chris@0: # see redcloth3.rb, same as "#{pre}#{text}#{post}" Chris@0: toc_item.gsub!(LINK_RE) { [$2, $4, $9].join } Chris@0: # sanitizes image links from titles Chris@0: toc_item.gsub!(IMAGE_RE) { [$5].join } Chris@0: # removes styles Chris@0: # eg. %{color:red}Triggers% => Triggers Chris@0: toc_item.gsub! %r[%\{[^\}]*\}([^%]+)%], '\\1' Chris@0: Chris@0: # replaces non word caracters by dashes Chris@0: anchor = toc_item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-') Chris@0: Chris@0: unless anchor.blank? Chris@0: if tag =~ /^h(\d)$/ Chris@0: @toc << [$1.to_i, anchor, toc_item] Chris@0: end Chris@0: atts << " id=\"#{anchor}\"" Chris@0: content = content + "¶" Chris@0: end Chris@0: textile_p(tag, atts, cite, content) Chris@0: end Chris@0: Chris@0: alias :textile_h1 :textile_p_withtoc Chris@0: alias :textile_h2 :textile_p_withtoc Chris@0: alias :textile_h3 :textile_p_withtoc Chris@0: Chris@0: def inline_toc(text) Chris@0: text.gsub!(/
\{\{([<>]?)toc\}\}<\/p>/i) do Chris@0: div_class = 'toc' Chris@0: div_class << ' right' if $1 == '>' Chris@0: div_class << ' left' if $1 == '<' Chris@0: out = "
" Chris@0: @toc.each do |heading| Chris@0: level, anchor, toc_item = heading Chris@0: out << "
' Chris@0: out Chris@0: end Chris@0: end Chris@0: Chris@0: AUTO_LINK_RE = %r{ Chris@0: ( # leading text Chris@0: <\w+.*?>| # leading HTML tag, or Chris@0: [^=<>!:'"/]| # leading punctuation, or Chris@0: ^ # beginning of line Chris@0: ) Chris@0: ( Chris@0: (?:https?://)| # protocol spec, or Chris@0: (?:s?ftps?://)| Chris@0: (?:www\.) # www.* Chris@0: ) Chris@0: ( Chris@0: (\S+?) # url Chris@0: (\/)? # slash Chris@0: ) Chris@0: ([^\w\=\/;\(\)]*?) # post Chris@0: (?=<|\s|$) Chris@0: }x unless const_defined?(:AUTO_LINK_RE) Chris@0: Chris@0: # Turns all urls into clickable links (code from Rails). Chris@0: def inline_auto_link(text) Chris@0: text.gsub!(AUTO_LINK_RE) do Chris@0: all, leading, proto, url, post = $&, $1, $2, $3, $6 Chris@0: if leading =~ /=]?/ Chris@0: # don't replace URL's that are already linked Chris@0: # and URL's prefixed with ! !> !< != (textile images) Chris@0: all Chris@0: else Chris@0: # Idea below : an URL with unbalanced parethesis and Chris@0: # ending by ')' is put into external parenthesis Chris@0: if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) ) Chris@0: url=url[0..-2] # discard closing parenth from url Chris@0: post = ")"+post # add closing parenth to post Chris@0: end Chris@0: tag = content_tag('a', proto + url, :href => "#{proto=="www."?"http://www.":proto}#{url}", :class => 'external') Chris@0: %(#{leading}#{tag}#{post}) Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: # Turns all email addresses into clickable links (code from Rails). Chris@0: def inline_auto_mailto(text) Chris@0: text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do Chris@0: mail = $1 Chris@0: if text.match(/]*>(.*)(#{Regexp.escape(mail)})(.*)<\/a>/) Chris@0: mail Chris@0: else Chris@0: content_tag('a', mail, :href => "mailto:#{mail}", :class => "email") Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end- #{toc_item}
\n" Chris@0: end Chris@0: out << '