marcros.rb.diff

Anh Kỳ Huỳnh, 2008-11-15 17:37

Download (3.41 KB)

View differences:

lib/redmine/wiki_formatting/macros.rb (working copy)
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.
......
24 24
          send(method_name, obj, args) if respond_to?(method_name)
25 25
        end
26 26
      end
27
      
27

  
28 28
      @@available_macros = {}
29
      
29

  
30 30
      class << self
31 31
        # Called with a block to define additional macros.
32 32
        # Macro blocks accept 2 arguments:
33 33
        # * obj: the object that is rendered
34 34
        # * args: macro arguments
35
        # 
35
        #
36 36
        # Plugins can use this method to define new macros:
37
        # 
37
        #
38 38
        #   Redmine::WikiFormatting::Macros.register do
39 39
        #     desc "This is my macro"
40 40
        #     macro :my_macro do |obj, args|
......
44 44
        def register(&block)
45 45
          class_eval(&block) if block_given?
46 46
        end
47
              
47

  
48 48
      private
49 49
        # Defines a new macro with the given name and block.
50 50
        def macro(name, &block)
......
54 54
          raise "Can not create a macro without a block!" unless block_given?
55 55
          Definitions.send :define_method, "macro_#{name}".downcase, &block
56 56
        end
57
    
57

  
58 58
        # Sets description for the next macro to be defined
59 59
        def desc(txt)
60 60
          @@desc = txt
61 61
        end
62 62
      end
63
          
63

  
64 64
      # Builtin macros
65 65
      desc "Sample macro."
66 66
      macro :hello_world do |obj, args|
67 67
        "Hello world! Object: #{obj.class.name}, " + (args.empty? ? "Called with no argument." : "Arguments: #{args.join(', ')}")
68 68
      end
69
    
69

  
70 70
      desc "Displays a list of all available macros, including description if available."
71 71
      macro :macro_list do
72 72
        out = ''
......
76 76
        end
77 77
        content_tag('dl', out)
78 78
      end
79
      
79

  
80 80
      desc "Displays a list of child pages."
81 81
      macro :child_pages do |obj, args|
82 82
        raise 'This macro applies to wiki pages only.' unless obj.is_a?(WikiContent)
83 83
        render_page_hierarchy(obj.page.descendants.group_by(&:parent_id), obj.page.id)
84 84
      end
85
      
85

  
86 86
      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)}}"
87 87
      macro :include do |obj, args|
88 88
        project = @project
......
102 102
        @included_wiki_pages.pop
103 103
        out
104 104
      end
105

  
106
      desc "Attachment links."
107
      macro :file do |obj, args|
108
        out = ''
109
        id = args.first.to_s
110
        title = id
111
        if title =~ %r{^([^\:]+)\:(.*)$}
112
          id, title = $1, $2
113
        end
114
        out << "<a href=\"/attachments/download/#{id}\">#{title}</a>"
115
        out
116
      end
117

  
105 118
    end
106 119
  end
107 120
end