Chris@1517: # Redmine - project management software Chris@1517: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1517: # Chris@1517: # This program is free software; you can redistribute it and/or Chris@1517: # modify it under the terms of the GNU General Public License Chris@1517: # as published by the Free Software Foundation; either version 2 Chris@1517: # of the License, or (at your option) any later version. Chris@1517: # Chris@1517: # This program is distributed in the hope that it will be useful, Chris@1517: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1517: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1517: # GNU General Public License for more details. Chris@1517: # Chris@1517: # You should have received a copy of the GNU General Public License Chris@1517: # along with this program; if not, write to the Free Software Chris@1517: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1517: Chris@1517: module Redmine Chris@1517: module WikiFormatting Chris@1517: module Macros Chris@1517: module Definitions Chris@1517: # Returns true if +name+ is the name of an existing macro Chris@1517: def macro_exists?(name) Chris@1517: Redmine::WikiFormatting::Macros.available_macros.key?(name.to_sym) Chris@1517: end Chris@1517: Chris@1517: def exec_macro(name, obj, args, text) Chris@1517: macro_options = Redmine::WikiFormatting::Macros.available_macros[name.to_sym] Chris@1517: return unless macro_options Chris@1517: Chris@1517: method_name = "macro_#{name}" Chris@1517: unless macro_options[:parse_args] == false Chris@1517: args = args.split(',').map(&:strip) Chris@1517: end Chris@1517: Chris@1517: begin Chris@1517: if self.class.instance_method(method_name).arity == 3 Chris@1517: send(method_name, obj, args, text) Chris@1517: elsif text Chris@1517: raise "This macro does not accept a block of text" Chris@1517: else Chris@1517: send(method_name, obj, args) Chris@1517: end Chris@1517: rescue => e Chris@1517: "
Error executing the #{h name} macro (#{h e.to_s})
".html_safe Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def extract_macro_options(args, *keys) Chris@1517: options = {} Chris@1517: while args.last.to_s.strip =~ %r{^(.+?)\=(.+)$} && keys.include?($1.downcase.to_sym) Chris@1517: options[$1.downcase.to_sym] = $2 Chris@1517: args.pop Chris@1517: end Chris@1517: return [args, options] Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: @@available_macros = {} Chris@1517: mattr_accessor :available_macros Chris@1517: Chris@1517: class << self Chris@1517: # Plugins can use this method to define new macros: Chris@1517: # Chris@1517: # Redmine::WikiFormatting::Macros.register do Chris@1517: # desc "This is my macro" Chris@1517: # macro :my_macro do |obj, args| Chris@1517: # "My macro output" Chris@1517: # end Chris@1517: # Chris@1517: # desc "This is my macro that accepts a block of text" Chris@1517: # macro :my_macro do |obj, args, text| Chris@1517: # "My macro output" Chris@1517: # end Chris@1517: # end Chris@1517: def register(&block) Chris@1517: class_eval(&block) if block_given? Chris@1517: end Chris@1517: Chris@1517: # Defines a new macro with the given name, options and block. Chris@1517: # Chris@1517: # Options: Chris@1517: # * :desc - A description of the macro Chris@1517: # * :parse_args => false - Disables arguments parsing (the whole arguments Chris@1517: # string is passed to the macro) Chris@1517: # Chris@1517: # Macro blocks accept 2 or 3 arguments: Chris@1517: # * obj: the object that is rendered (eg. an Issue, a WikiContent...) Chris@1517: # * args: macro arguments Chris@1517: # * text: the block of text given to the macro (should be present only if the Chris@1517: # macro accepts a block of text). text is a String or nil if the macro is Chris@1517: # invoked without a block of text. Chris@1517: # Chris@1517: # Examples: Chris@1517: # By default, when the macro is invoked, the coma separated list of arguments Chris@1517: # is split and passed to the macro block as an array. If no argument is given Chris@1517: # the macro will be invoked with an empty array: Chris@1517: # Chris@1517: # macro :my_macro do |obj, args| Chris@1517: # # args is an array Chris@1517: # # and this macro do not accept a block of text Chris@1517: # end Chris@1517: # Chris@1517: # You can disable arguments spliting with the :parse_args => false option. In Chris@1517: # this case, the full string of arguments is passed to the macro: Chris@1517: # Chris@1517: # macro :my_macro, :parse_args => false do |obj, args| Chris@1517: # # args is a string Chris@1517: # end Chris@1517: # Chris@1517: # Macro can optionally accept a block of text: Chris@1517: # Chris@1517: # macro :my_macro do |obj, args, text| Chris@1517: # # this macro accepts a block of text Chris@1517: # end Chris@1517: # Chris@1517: # Macros are invoked in formatted text using double curly brackets. Arguments Chris@1517: # must be enclosed in parenthesis if any. A new line after the macro name or the Chris@1517: # arguments starts the block of text that will be passe to the macro (invoking Chris@1517: # a macro that do not accept a block of text with some text will fail). Chris@1517: # Examples: Chris@1517: # Chris@1517: # No arguments: Chris@1517: # {{my_macro}} Chris@1517: # Chris@1517: # With arguments: Chris@1517: # {{my_macro(arg1, arg2)}} Chris@1517: # Chris@1517: # With a block of text: Chris@1517: # {{my_macro Chris@1517: # multiple lines Chris@1517: # of text Chris@1517: # }} Chris@1517: # Chris@1517: # With arguments and a block of text Chris@1517: # {{my_macro(arg1, arg2) Chris@1517: # multiple lines Chris@1517: # of text Chris@1517: # }} Chris@1517: # Chris@1517: # If a block of text is given, the closing tag }} must be at the start of a new line. Chris@1517: def macro(name, options={}, &block) Chris@1517: options.assert_valid_keys(:desc, :parse_args) Chris@1517: unless name.to_s.match(/\A\w+\z/) Chris@1517: raise "Invalid macro name: #{name} (only 0-9, A-Z, a-z and _ characters are accepted)" Chris@1517: end Chris@1517: unless block_given? Chris@1517: raise "Can not create a macro without a block!" Chris@1517: end Chris@1517: name = name.to_s.downcase.to_sym Chris@1517: available_macros[name] = {:desc => @@desc || ''}.merge(options) Chris@1517: @@desc = nil Chris@1517: Definitions.send :define_method, "macro_#{name}", &block Chris@1517: end Chris@1517: Chris@1517: # Sets description for the next macro to be defined Chris@1517: def desc(txt) Chris@1517: @@desc = txt Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Builtin macros Chris@1517: desc "Sample macro." Chris@1517: macro :hello_world do |obj, args, text| Chris@1517: h("Hello world! Object: #{obj.class.name}, " + Chris@1517: (args.empty? ? "Called with no argument" : "Arguments: #{args.join(', ')}") + Chris@1517: " and " + (text.present? ? "a #{text.size} bytes long block of text." : "no block of text.") Chris@1517: ) Chris@1517: end Chris@1517: Chris@1517: desc "Displays a list of all available macros, including description if available." Chris@1517: macro :macro_list do |obj, args| Chris@1517: out = ''.html_safe Chris@1517: @@available_macros.each do |macro, options| Chris@1517: out << content_tag('dt', content_tag('code', macro.to_s)) Chris@1517: out << content_tag('dd', textilizable(options[:desc])) Chris@1517: end Chris@1517: content_tag('dl', out) Chris@1517: end Chris@1517: Chris@1517: desc "Displays a list of child pages. With no argument, it displays the child pages of the current wiki page. Examples:\n\n" + Chris@1517: " !{{child_pages}} -- can be used from a wiki page only\n" + Chris@1517: " !{{child_pages(depth=2)}} -- display 2 levels nesting only\n" Chris@1517: " !{{child_pages(Foo)}} -- lists all children of page Foo\n" + Chris@1517: " !{{child_pages(Foo, parent=1)}} -- same as above with a link to page Foo" Chris@1517: macro :child_pages do |obj, args| Chris@1517: args, options = extract_macro_options(args, :parent, :depth) Chris@1517: options[:depth] = options[:depth].to_i if options[:depth].present? Chris@1517: Chris@1517: page = nil Chris@1517: if args.size > 0 Chris@1517: page = Wiki.find_page(args.first.to_s, :project => @project) Chris@1517: elsif obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version) Chris@1517: page = obj.page Chris@1517: else Chris@1517: raise 'With no argument, this macro can be called from wiki pages only.' Chris@1517: end Chris@1517: raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project) Chris@1517: pages = page.self_and_descendants(options[:depth]).group_by(&:parent_id) Chris@1517: render_page_hierarchy(pages, options[:parent] ? page.parent_id : page.id) Chris@1517: end Chris@1517: Chris@1517: 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@1517: macro :include do |obj, args| Chris@1517: page = Wiki.find_page(args.first.to_s, :project => @project) Chris@1517: raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project) Chris@1517: @included_wiki_pages ||= [] Chris@1517: raise 'Circular inclusion detected' if @included_wiki_pages.include?(page.title) Chris@1517: @included_wiki_pages << page.title Chris@1517: out = textilizable(page.content, :text, :attachments => page.attachments, :headings => false) Chris@1517: @included_wiki_pages.pop Chris@1517: out Chris@1517: end Chris@1517: Chris@1517: 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@1517: macro :collapse do |obj, args, text| Chris@1517: html_id = "collapse-#{Redmine::Utils.random_hex(4)}" Chris@1517: show_label = args[0] || l(:button_show) Chris@1517: hide_label = args[1] || args[0] || l(:button_hide) Chris@1517: js = "$('##{html_id}-show, ##{html_id}-hide').toggle(); $('##{html_id}').fadeToggle(150);" Chris@1517: out = ''.html_safe Chris@1517: out << link_to_function(show_label, js, :id => "#{html_id}-show", :class => 'collapsible collapsed') Chris@1517: out << link_to_function(hide_label, js, :id => "#{html_id}-hide", :class => 'collapsible', :style => 'display:none;') Chris@1517: out << content_tag('div', textilizable(text, :object => obj, :headings => false), :id => html_id, :class => 'collapsed-text', :style => 'display:none;') Chris@1517: out Chris@1517: end Chris@1517: Chris@1517: desc "Displays a clickable thumbnail of an attached image. Examples:\n\n
{{thumbnail(image.png)}}\n{{thumbnail(image.png, size=300, title=Thumbnail)}}
" Chris@1517: macro :thumbnail do |obj, args| Chris@1517: args, options = extract_macro_options(args, :size, :title) Chris@1517: filename = args.first Chris@1517: raise 'Filename required' unless filename.present? Chris@1517: size = options[:size] Chris@1517: raise 'Invalid size parameter' unless size.nil? || size.match(/^\d+$/) Chris@1517: size = size.to_i Chris@1517: size = nil unless size > 0 Chris@1517: if obj && obj.respond_to?(:attachments) && attachment = Attachment.latest_attach(obj.attachments, filename) Chris@1517: title = options[:title] || attachment.title Chris@1517: thumbnail_url = url_for(:controller => 'attachments', :action => 'thumbnail', :id => attachment, :size => size, :only_path => false) Chris@1517: image_url = url_for(:controller => 'attachments', :action => 'show', :id => attachment, :only_path => false) Chris@1517: Chris@1517: img = image_tag(thumbnail_url, :alt => attachment.filename) Chris@1517: link_to(img, image_url, :class => 'thumbnail', :title => title) Chris@1517: else Chris@1517: raise "Attachment #{filename} not found" Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: end