Revision 912:5e80956cc792 lib/redmine/wiki_formatting/textile

View differences:

lib/redmine/wiki_formatting/textile/formatter.rb
1 1
# Redmine - project management software
2
# Copyright (C) 2006-2010  Jean-Philippe Lang
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3 3
#
4 4
# This program is free software; you can redistribute it and/or
5 5
# modify it under the terms of the GNU General Public License
6 6
# as published by the Free Software Foundation; either version 2
7 7
# of the License, or (at your option) any later version.
8
# 
8
#
9 9
# This program is distributed in the hope that it will be useful,
10 10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 12
# GNU General Public License for more details.
13
# 
13
#
14 14
# You should have received a copy of the GNU General Public License
15 15
# along with this program; if not, write to the Free Software
16 16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 17

  
18 18
require 'redcloth3'
19
require 'digest/md5'
19 20

  
20 21
module Redmine
21 22
  module WikiFormatting
22 23
    module Textile
23 24
      class Formatter < RedCloth3
24 25
        include ActionView::Helpers::TagHelper
25
        
26

  
26 27
        # auto_link rule after textile rules so that it doesn't break !image_url! tags
27 28
        RULES = [:textile, :block_markdown_rule, :inline_auto_link, :inline_auto_mailto]
28
        
29

  
29 30
        def initialize(*args)
30 31
          super
31 32
          self.hard_breaks=true
32 33
          self.no_span_caps=true
33 34
          self.filter_styles=true
34 35
        end
35
        
36

  
36 37
        def to_html(*rules)
37 38
          @toc = []
38 39
          super(*RULES).to_s
39 40
        end
40
  
41

  
42
        def get_section(index)
43
          section = extract_sections(index)[1]
44
          hash = Digest::MD5.hexdigest(section)
45
          return section, hash
46
        end
47

  
48
        def update_section(index, update, hash=nil)
49
          t = extract_sections(index)
50
          if hash.present? && hash != Digest::MD5.hexdigest(t[1])
51
            raise Redmine::WikiFormatting::StaleSectionError
52
          end
53
          t[1] = update unless t[1].blank?
54
          t.reject(&:blank?).join "\n\n"
55
        end
56

  
57
        def extract_sections(index)
58
          @pre_list = []
59
          text = self.dup
60
          rip_offtags text, false, false
61
          before = ''
62
          s = ''
63
          after = ''
64
          i = 0
65
          l = 1
66
          started = false
67
          ended = false
68
          text.scan(/(((?:.*?)(\A|\r?\n\r?\n))(h(\d+)(#{A}#{C})\.(?::(\S+))? (.*?)$)|.*)/m).each do |all, content, lf, heading, level|
69
            if heading.nil?
70
              if ended
71
                after << all
72
              elsif started
73
                s << all
74
              else
75
                before << all
76
              end
77
              break
78
            end
79
            i += 1
80
            if ended
81
              after << all
82
            elsif i == index
83
              l = level.to_i
84
              before << content
85
              s << heading
86
              started = true
87
            elsif i > index
88
              s << content
89
              if level.to_i > l
90
                s << heading
91
              else
92
                after << heading
93
                ended = true
94
              end
95
            else
96
              before << all
97
            end
98
          end
99
          sections = [before.strip, s.strip, after.strip]
100
          sections.each {|section| smooth_offtags_without_code_highlighting section}
101
          sections
102
        end
103

  
41 104
      private
42
  
105

  
43 106
        # Patch for RedCloth.  Fixed in RedCloth r128 but _why hasn't released it yet.
44 107
        # <a href="http://code.whytheluckystiff.net/redcloth/changeset/128">http://code.whytheluckystiff.net/redcloth/changeset/128</a>
45
        def hard_break( text ) 
108
        def hard_break( text )
46 109
          text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks
47 110
        end
48
        
111

  
112
        alias :smooth_offtags_without_code_highlighting :smooth_offtags
49 113
        # Patch to add code highlighting support to RedCloth
50 114
        def smooth_offtags( text )
51 115
          unless @pre_list.empty?
......
53 117
            text.gsub!(/<redpre#(\d+)>/) do
54 118
              content = @pre_list[$1.to_i]
55 119
              if content.match(/<code\s+class="(\w+)">\s?(.+)/m)
56
                content = "<code class=\"#{$1} syntaxhl\">" + 
120
                content = "<code class=\"#{$1} syntaxhl\">" +
57 121
                  Redmine::SyntaxHighlighting.highlight_by_language($2, $1)
58 122
              end
59 123
              content
60 124
            end
61 125
          end
62 126
        end
63
        
127

  
64 128
        AUTO_LINK_RE = %r{
65 129
                        (                          # leading text
66 130
                          <\w+.*?>|                # leading HTML tag, or
67
                          [^=<>!:'"/]|             # leading punctuation, or 
131
                          [^=<>!:'"/]|             # leading punctuation, or
68 132
                          ^                        # beginning of line
69 133
                        )
70 134
                        (
......
79 143
                        ((?:&gt;)?|[^\w\=\/;\(\)]*?)               # post
80 144
                        (?=<|\s|$)
81 145
                       }x unless const_defined?(:AUTO_LINK_RE)
82
  
146

  
83 147
        # Turns all urls into clickable links (code from Rails).
84 148
        def inline_auto_link(text)
85 149
          text.gsub!(AUTO_LINK_RE) do
......
100 164
            end
101 165
          end
102 166
        end
103
  
167

  
104 168
        # Turns all email addresses into clickable links (code from Rails).
105 169
        def inline_auto_mailto(text)
106 170
          text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
lib/redmine/wiki_formatting/textile/helper.rb
5 5
# modify it under the terms of the GNU General Public License
6 6
# as published by the Free Software Foundation; either version 2
7 7
# of the License, or (at your option) any later version.
8
# 
8
#
9 9
# This program is distributed in the hope that it will be useful,
10 10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 12
# GNU General Public License for more details.
13
# 
13
#
14 14
# You should have received a copy of the GNU General Public License
15 15
# along with this program; if not, write to the Free Software
16 16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
......
25 25
          url = "#{Redmine::Utils.relative_url_root}/help/wiki_syntax.html"
26 26
          help_link = link_to(l(:setting_text_formatting), url,
27 27
            :onclick => "window.open(\"#{ url }\", \"\", \"resizable=yes, location=no, width=300, height=640, menubar=no, status=no, scrollbars=yes\"); return false;")
28
      
28

  
29 29
          javascript_tag("var wikiToolbar = new jsToolBar($('#{field_id}')); wikiToolbar.setHelpLink('#{escape_javascript help_link}'); wikiToolbar.draw();")
30 30
        end
31
      
31

  
32 32
        def initial_page_content(page)
33 33
          "h1. #{@page.pretty_title}"
34 34
        end
35
      
35

  
36 36
        def heads_for_wiki_formatter
37 37
          unless @heads_for_wiki_formatter_included
38 38
            content_for :header_tags do

Also available in: Unified diff