annotate .svn/pristine/70/707bbdb0f8c20e3b11fe0c8814dfe7f46c9c6c3a.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 module Redmine
Chris@1295 19 module WikiFormatting
Chris@1295 20 module Macros
Chris@1295 21 module Definitions
Chris@1295 22 # Returns true if +name+ is the name of an existing macro
Chris@1295 23 def macro_exists?(name)
Chris@1295 24 Redmine::WikiFormatting::Macros.available_macros.key?(name.to_sym)
Chris@1295 25 end
Chris@1295 26
Chris@1295 27 def exec_macro(name, obj, args, text)
Chris@1295 28 macro_options = Redmine::WikiFormatting::Macros.available_macros[name.to_sym]
Chris@1295 29 return unless macro_options
Chris@1295 30
Chris@1295 31 method_name = "macro_#{name}"
Chris@1295 32 unless macro_options[:parse_args] == false
Chris@1295 33 args = args.split(',').map(&:strip)
Chris@1295 34 end
Chris@1295 35
Chris@1295 36 begin
Chris@1295 37 if self.class.instance_method(method_name).arity == 3
Chris@1295 38 send(method_name, obj, args, text)
Chris@1295 39 elsif text
Chris@1295 40 raise "This macro does not accept a block of text"
Chris@1295 41 else
Chris@1295 42 send(method_name, obj, args)
Chris@1295 43 end
Chris@1295 44 rescue => e
Chris@1295 45 "<div class=\"flash error\">Error executing the <strong>#{h name}</strong> macro (#{h e.to_s})</div>".html_safe
Chris@1295 46 end
Chris@1295 47 end
Chris@1295 48
Chris@1295 49 def extract_macro_options(args, *keys)
Chris@1295 50 options = {}
Chris@1295 51 while args.last.to_s.strip =~ %r{^(.+?)\=(.+)$} && keys.include?($1.downcase.to_sym)
Chris@1295 52 options[$1.downcase.to_sym] = $2
Chris@1295 53 args.pop
Chris@1295 54 end
Chris@1295 55 return [args, options]
Chris@1295 56 end
Chris@1295 57 end
Chris@1295 58
Chris@1295 59 @@available_macros = {}
Chris@1295 60 mattr_accessor :available_macros
Chris@1295 61
Chris@1295 62 class << self
Chris@1295 63 # Plugins can use this method to define new macros:
Chris@1295 64 #
Chris@1295 65 # Redmine::WikiFormatting::Macros.register do
Chris@1295 66 # desc "This is my macro"
Chris@1295 67 # macro :my_macro do |obj, args|
Chris@1295 68 # "My macro output"
Chris@1295 69 # end
Chris@1295 70 #
Chris@1295 71 # desc "This is my macro that accepts a block of text"
Chris@1295 72 # macro :my_macro do |obj, args, text|
Chris@1295 73 # "My macro output"
Chris@1295 74 # end
Chris@1295 75 # end
Chris@1295 76 def register(&block)
Chris@1295 77 class_eval(&block) if block_given?
Chris@1295 78 end
Chris@1295 79
Chris@1295 80 # Defines a new macro with the given name, options and block.
Chris@1295 81 #
Chris@1295 82 # Options:
Chris@1295 83 # * :desc - A description of the macro
Chris@1295 84 # * :parse_args => false - Disables arguments parsing (the whole arguments
Chris@1295 85 # string is passed to the macro)
Chris@1295 86 #
Chris@1295 87 # Macro blocks accept 2 or 3 arguments:
Chris@1295 88 # * obj: the object that is rendered (eg. an Issue, a WikiContent...)
Chris@1295 89 # * args: macro arguments
Chris@1295 90 # * text: the block of text given to the macro (should be present only if the
Chris@1295 91 # macro accepts a block of text). text is a String or nil if the macro is
Chris@1295 92 # invoked without a block of text.
Chris@1295 93 #
Chris@1295 94 # Examples:
Chris@1295 95 # By default, when the macro is invoked, the coma separated list of arguments
Chris@1295 96 # is split and passed to the macro block as an array. If no argument is given
Chris@1295 97 # the macro will be invoked with an empty array:
Chris@1295 98 #
Chris@1295 99 # macro :my_macro do |obj, args|
Chris@1295 100 # # args is an array
Chris@1295 101 # # and this macro do not accept a block of text
Chris@1295 102 # end
Chris@1295 103 #
Chris@1295 104 # You can disable arguments spliting with the :parse_args => false option. In
Chris@1295 105 # this case, the full string of arguments is passed to the macro:
Chris@1295 106 #
Chris@1295 107 # macro :my_macro, :parse_args => false do |obj, args|
Chris@1295 108 # # args is a string
Chris@1295 109 # end
Chris@1295 110 #
Chris@1295 111 # Macro can optionally accept a block of text:
Chris@1295 112 #
Chris@1295 113 # macro :my_macro do |obj, args, text|
Chris@1295 114 # # this macro accepts a block of text
Chris@1295 115 # end
Chris@1295 116 #
Chris@1295 117 # Macros are invoked in formatted text using double curly brackets. Arguments
Chris@1295 118 # must be enclosed in parenthesis if any. A new line after the macro name or the
Chris@1295 119 # arguments starts the block of text that will be passe to the macro (invoking
Chris@1295 120 # a macro that do not accept a block of text with some text will fail).
Chris@1295 121 # Examples:
Chris@1295 122 #
Chris@1295 123 # No arguments:
Chris@1295 124 # {{my_macro}}
Chris@1295 125 #
Chris@1295 126 # With arguments:
Chris@1295 127 # {{my_macro(arg1, arg2)}}
Chris@1295 128 #
Chris@1295 129 # With a block of text:
Chris@1295 130 # {{my_macro
Chris@1295 131 # multiple lines
Chris@1295 132 # of text
Chris@1295 133 # }}
Chris@1295 134 #
Chris@1295 135 # With arguments and a block of text
Chris@1295 136 # {{my_macro(arg1, arg2)
Chris@1295 137 # multiple lines
Chris@1295 138 # of text
Chris@1295 139 # }}
Chris@1295 140 #
Chris@1295 141 # If a block of text is given, the closing tag }} must be at the start of a new line.
Chris@1295 142 def macro(name, options={}, &block)
Chris@1295 143 options.assert_valid_keys(:desc, :parse_args)
Chris@1295 144 unless name.to_s.match(/\A\w+\z/)
Chris@1295 145 raise "Invalid macro name: #{name} (only 0-9, A-Z, a-z and _ characters are accepted)"
Chris@1295 146 end
Chris@1295 147 unless block_given?
Chris@1295 148 raise "Can not create a macro without a block!"
Chris@1295 149 end
Chris@1295 150 name = name.to_sym if name.is_a?(String)
Chris@1295 151 available_macros[name] = {:desc => @@desc || ''}.merge(options)
Chris@1295 152 @@desc = nil
Chris@1295 153 Definitions.send :define_method, "macro_#{name}".downcase, &block
Chris@1295 154 end
Chris@1295 155
Chris@1295 156 # Sets description for the next macro to be defined
Chris@1295 157 def desc(txt)
Chris@1295 158 @@desc = txt
Chris@1295 159 end
Chris@1295 160 end
Chris@1295 161
Chris@1295 162 # Builtin macros
Chris@1295 163 desc "Sample macro."
Chris@1295 164 macro :hello_world do |obj, args, text|
Chris@1295 165 h("Hello world! Object: #{obj.class.name}, " +
Chris@1295 166 (args.empty? ? "Called with no argument" : "Arguments: #{args.join(', ')}") +
Chris@1295 167 " and " + (text.present? ? "a #{text.size} bytes long block of text." : "no block of text.")
Chris@1295 168 )
Chris@1295 169 end
Chris@1295 170
Chris@1295 171 desc "Displays a list of all available macros, including description if available."
Chris@1295 172 macro :macro_list do |obj, args|
Chris@1295 173 out = ''.html_safe
Chris@1295 174 @@available_macros.each do |macro, options|
Chris@1295 175 out << content_tag('dt', content_tag('code', macro.to_s))
Chris@1295 176 out << content_tag('dd', textilizable(options[:desc]))
Chris@1295 177 end
Chris@1295 178 content_tag('dl', out)
Chris@1295 179 end
Chris@1295 180
Chris@1295 181 desc "Displays a list of child pages. With no argument, it displays the child pages of the current wiki page. Examples:\n\n" +
Chris@1295 182 " !{{child_pages}} -- can be used from a wiki page only\n" +
Chris@1295 183 " !{{child_pages(depth=2)}} -- display 2 levels nesting only\n"
Chris@1295 184 " !{{child_pages(Foo)}} -- lists all children of page Foo\n" +
Chris@1295 185 " !{{child_pages(Foo, parent=1)}} -- same as above with a link to page Foo"
Chris@1295 186 macro :child_pages do |obj, args|
Chris@1295 187 args, options = extract_macro_options(args, :parent, :depth)
Chris@1295 188 options[:depth] = options[:depth].to_i if options[:depth].present?
Chris@1295 189
Chris@1295 190 page = nil
Chris@1295 191 if args.size > 0
Chris@1295 192 page = Wiki.find_page(args.first.to_s, :project => @project)
Chris@1295 193 elsif obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)
Chris@1295 194 page = obj.page
Chris@1295 195 else
Chris@1295 196 raise 'With no argument, this macro can be called from wiki pages only.'
Chris@1295 197 end
Chris@1295 198 raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project)
Chris@1295 199 pages = page.self_and_descendants(options[:depth]).group_by(&:parent_id)
Chris@1295 200 render_page_hierarchy(pages, options[:parent] ? page.parent_id : page.id)
Chris@1295 201 end
Chris@1295 202
Chris@1295 203 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)}}"
Chris@1295 204 macro :include do |obj, args|
Chris@1295 205 page = Wiki.find_page(args.first.to_s, :project => @project)
Chris@1295 206 raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project)
Chris@1295 207 @included_wiki_pages ||= []
Chris@1295 208 raise 'Circular inclusion detected' if @included_wiki_pages.include?(page.title)
Chris@1295 209 @included_wiki_pages << page.title
Chris@1295 210 out = textilizable(page.content, :text, :attachments => page.attachments, :headings => false)
Chris@1295 211 @included_wiki_pages.pop
Chris@1295 212 out
Chris@1295 213 end
Chris@1295 214
Chris@1295 215 desc "Inserts of collapsed block of text. Example:\n\n {{collapse(View details...)\nThis is a block of text that is collapsed by default.\nIt can be expanded by clicking a link.\n}}"
Chris@1295 216 macro :collapse do |obj, args, text|
Chris@1295 217 html_id = "collapse-#{Redmine::Utils.random_hex(4)}"
Chris@1295 218 show_label = args[0] || l(:button_show)
Chris@1295 219 hide_label = args[1] || args[0] || l(:button_hide)
Chris@1295 220 js = "$('##{html_id}-show, ##{html_id}-hide').toggle(); $('##{html_id}').fadeToggle(150);"
Chris@1295 221 out = ''.html_safe
Chris@1295 222 out << link_to_function(show_label, js, :id => "#{html_id}-show", :class => 'collapsible collapsed')
Chris@1295 223 out << link_to_function(hide_label, js, :id => "#{html_id}-hide", :class => 'collapsible', :style => 'display:none;')
Chris@1295 224 out << content_tag('div', textilizable(text, :object => obj), :id => html_id, :class => 'collapsed-text', :style => 'display:none;')
Chris@1295 225 out
Chris@1295 226 end
Chris@1295 227
Chris@1295 228 desc "Displays a clickable thumbnail of an attached image. Examples:\n\n<pre>{{thumbnail(image.png)}}\n{{thumbnail(image.png, size=300, title=Thumbnail)}}</pre>"
Chris@1295 229 macro :thumbnail do |obj, args|
Chris@1295 230 args, options = extract_macro_options(args, :size, :title)
Chris@1295 231 filename = args.first
Chris@1295 232 raise 'Filename required' unless filename.present?
Chris@1295 233 size = options[:size]
Chris@1295 234 raise 'Invalid size parameter' unless size.nil? || size.match(/^\d+$/)
Chris@1295 235 size = size.to_i
Chris@1295 236 size = nil unless size > 0
Chris@1295 237 if obj && obj.respond_to?(:attachments) && attachment = Attachment.latest_attach(obj.attachments, filename)
Chris@1295 238 title = options[:title] || attachment.title
Chris@1295 239 img = image_tag(url_for(:controller => 'attachments', :action => 'thumbnail', :id => attachment, :size => size), :alt => attachment.filename)
Chris@1295 240 link_to(img, url_for(:controller => 'attachments', :action => 'show', :id => attachment), :class => 'thumbnail', :title => title)
Chris@1295 241 else
Chris@1295 242 raise "Attachment #{filename} not found"
Chris@1295 243 end
Chris@1295 244 end
Chris@1295 245 end
Chris@1295 246 end
Chris@1295 247 end