Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: module Redmine Chris@0: module WikiFormatting Chris@0: module Macros Chris@0: module Definitions Chris@0: def exec_macro(name, obj, args) Chris@0: method_name = "macro_#{name}" Chris@0: send(method_name, obj, args) if respond_to?(method_name) Chris@0: end Chris@909: Chris@0: def extract_macro_options(args, *keys) Chris@0: options = {} Chris@0: while args.last.to_s.strip =~ %r{^(.+)\=(.+)$} && keys.include?($1.downcase.to_sym) Chris@0: options[$1.downcase.to_sym] = $2 Chris@0: args.pop Chris@0: end Chris@0: return [args, options] Chris@0: end Chris@0: end Chris@909: Chris@0: @@available_macros = {} Chris@909: Chris@0: class << self Chris@0: # Called with a block to define additional macros. Chris@0: # Macro blocks accept 2 arguments: Chris@0: # * obj: the object that is rendered Chris@0: # * args: macro arguments Chris@909: # Chris@0: # Plugins can use this method to define new macros: Chris@909: # Chris@0: # Redmine::WikiFormatting::Macros.register do Chris@0: # desc "This is my macro" Chris@0: # macro :my_macro do |obj, args| Chris@0: # "My macro output" Chris@0: # end Chris@0: # end Chris@0: def register(&block) Chris@0: class_eval(&block) if block_given? Chris@0: end Chris@909: Chris@0: private Chris@0: # Defines a new macro with the given name and block. Chris@0: def macro(name, &block) Chris@0: name = name.to_sym if name.is_a?(String) Chris@0: @@available_macros[name] = @@desc || '' Chris@0: @@desc = nil Chris@0: raise "Can not create a macro without a block!" unless block_given? Chris@0: Definitions.send :define_method, "macro_#{name}".downcase, &block Chris@0: end Chris@909: Chris@0: # Sets description for the next macro to be defined Chris@0: def desc(txt) Chris@0: @@desc = txt Chris@0: end Chris@0: end Chris@909: Chris@0: # Builtin macros Chris@0: desc "Sample macro." Chris@0: macro :hello_world do |obj, args| Chris@0: "Hello world! Object: #{obj.class.name}, " + (args.empty? ? "Called with no argument." : "Arguments: #{args.join(', ')}") Chris@0: end Chris@909: Chris@0: desc "Displays a list of all available macros, including description if available." Chris@909: macro :macro_list do |obj, args| Chris@0: out = '' Chris@0: @@available_macros.keys.collect(&:to_s).sort.each do |macro| Chris@0: out << content_tag('dt', content_tag('code', macro)) Chris@0: out << content_tag('dd', textilizable(@@available_macros[macro.to_sym])) Chris@0: end Chris@0: content_tag('dl', out) Chris@0: end Chris@909: Chris@0: desc "Displays a list of child pages. With no argument, it displays the child pages of the current wiki page. Examples:\n\n" + Chris@0: " !{{child_pages}} -- can be used from a wiki page only\n" + Chris@0: " !{{child_pages(Foo)}} -- lists all children of page Foo\n" + Chris@0: " !{{child_pages(Foo, parent=1)}} -- same as above with a link to page Foo" Chris@0: macro :child_pages do |obj, args| Chris@0: args, options = extract_macro_options(args, :parent) Chris@0: page = nil Chris@0: if args.size > 0 Chris@0: page = Wiki.find_page(args.first.to_s, :project => @project) Chris@0: elsif obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version) Chris@0: page = obj.page Chris@0: else Chris@0: raise 'With no argument, this macro can be called from wiki pages only.' Chris@0: end Chris@0: raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project) Chris@0: pages = ([page] + page.descendants).group_by(&:parent_id) Chris@0: render_page_hierarchy(pages, options[:parent] ? page.parent_id : page.id) Chris@0: end Chris@909: Chris@0: 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@0: macro :include do |obj, args| Chris@0: page = Wiki.find_page(args.first.to_s, :project => @project) Chris@0: raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project) Chris@0: @included_wiki_pages ||= [] Chris@0: raise 'Circular inclusion detected' if @included_wiki_pages.include?(page.title) Chris@0: @included_wiki_pages << page.title chris@37: out = textilizable(page.content, :text, :attachments => page.attachments, :headings => false) Chris@0: @included_wiki_pages.pop Chris@0: out Chris@0: end Chris@0: end Chris@0: end Chris@0: end