Revision 912:5e80956cc792 lib/redmine/wiki_formatting

View differences:

lib/redmine/wiki_formatting/macros.rb
1
# redMine - project management software
2
# Copyright (C) 2006-2007  Jean-Philippe Lang
1
# Redmine - project management software
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.
......
23 23
          method_name = "macro_#{name}"
24 24
          send(method_name, obj, args) if respond_to?(method_name)
25 25
        end
26
        
26

  
27 27
        def extract_macro_options(args, *keys)
28 28
          options = {}
29 29
          while args.last.to_s.strip =~ %r{^(.+)\=(.+)$} && keys.include?($1.downcase.to_sym)
......
33 33
          return [args, options]
34 34
        end
35 35
      end
36
      
36

  
37 37
      @@available_macros = {}
38
      
38

  
39 39
      class << self
40 40
        # Called with a block to define additional macros.
41 41
        # Macro blocks accept 2 arguments:
42 42
        # * obj: the object that is rendered
43 43
        # * args: macro arguments
44
        # 
44
        #
45 45
        # Plugins can use this method to define new macros:
46
        # 
46
        #
47 47
        #   Redmine::WikiFormatting::Macros.register do
48 48
        #     desc "This is my macro"
49 49
        #     macro :my_macro do |obj, args|
......
53 53
        def register(&block)
54 54
          class_eval(&block) if block_given?
55 55
        end
56
              
56

  
57 57
      private
58 58
        # Defines a new macro with the given name and block.
59 59
        def macro(name, &block)
......
63 63
          raise "Can not create a macro without a block!" unless block_given?
64 64
          Definitions.send :define_method, "macro_#{name}".downcase, &block
65 65
        end
66
    
66

  
67 67
        # Sets description for the next macro to be defined
68 68
        def desc(txt)
69 69
          @@desc = txt
70 70
        end
71 71
      end
72
          
72

  
73 73
      # Builtin macros
74 74
      desc "Sample macro."
75 75
      macro :hello_world do |obj, args|
76 76
        "Hello world! Object: #{obj.class.name}, " + (args.empty? ? "Called with no argument." : "Arguments: #{args.join(', ')}")
77 77
      end
78
    
78

  
79 79
      desc "Displays a list of all available macros, including description if available."
80
      macro :macro_list do
80
      macro :macro_list do |obj, args|
81 81
        out = ''
82 82
        @@available_macros.keys.collect(&:to_s).sort.each do |macro|
83 83
          out << content_tag('dt', content_tag('code', macro))
......
85 85
        end
86 86
        content_tag('dl', out)
87 87
      end
88
      
88

  
89 89
      desc "Displays a list of child pages. With no argument, it displays the child pages of the current wiki page. Examples:\n\n" +
90 90
             "  !{{child_pages}} -- can be used from a wiki page only\n" +
91 91
             "  !{{child_pages(Foo)}} -- lists all children of page Foo\n" +
......
104 104
        pages = ([page] + page.descendants).group_by(&:parent_id)
105 105
        render_page_hierarchy(pages, options[:parent] ? page.parent_id : page.id)
106 106
      end
107
      
107

  
108 108
      desc "Include a wiki page. Example:\n\n  !{{include(Foo)}}\n\nor to include a page of a specific project wiki:\n\n  !{{include(projectname:Foo)}}"
109 109
      macro :include do |obj, args|
110 110
        page = Wiki.find_page(args.first.to_s, :project => @project)
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