forked from taikii/redmine_include_macro_extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.rb
86 lines (75 loc) · 3.25 KB
/
init.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Redmine::Plugin.register :redmine_include_macro_extension do
name 'Include macro extension plugin'
author 'Taiki IKEGAME'
description 'This plugin makes possible include wiki section.'
version '0.0.4'
url 'https://github.com/sbriand/redmine_include_macro_extension'
author_url 'https://github.com/taikii'
Redmine::WikiFormatting::Macros.register do
Redmine::WikiFormatting::Macros::Definitions.send :alias_method, "macro_include_original", "macro_include"
desc "Includes a wiki page. Examples:\n\n" +
"{{include(Foo)}}\n" +
"{{include(Foo, Bar)}} -- to include Bar section of Foo page\n" +
"{{include(projectname:Foo)}} -- to include a page of a specific project wiki"
macro :include do |obj, args|
if args.size == 1
args.push("DUMMY")
send("macro_include_original", obj, args)
else
page = Wiki.find_page(args.first.to_s, :project => @project)
raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project)
@included_wiki_pages ||= []
secname = args[1].to_s
regex = nil
case Setting.text_formatting
when "textile"
regex = '(?:\A|\r?\n\s*\r?\n)h\d+\.[ \t]+(.*?)(?=\r?\n\s*\r?\n|\z)'
when "markdown"
regex = '(?:\A|\r?\n)#+ +(.*?)(?=\r?\n|\z)'
end
index = 0
if regex
page.content.text.scan(/#{regex}/m).each.with_index(1) do |matched, i|
if matched.first.gsub(/[\r\n]/, '') == secname
index = i
break
end
end
end
sectext = nil
if index > 0 && Redmine::WikiFormatting.supports_section_edit?
sectext, hash = Redmine::WikiFormatting.formatter.new(page.content.text).get_section(index)
end
raise 'Section not found' if sectext.nil?
raise 'Circular inclusion detected' if @included_wiki_pages.include?(page.title) || @included_wiki_pages.include?(page.title + ':' + secname)
if args.size > 2
options = args[2..-1]
if options.include?("nosubsection")
subsecidx = sectext.index(/#{regex}/m, sectext.index(/#{regex}/m) + 1)
if subsecidx
sectext = sectext[0 .. (subsecidx - 1)]
end
end
if options.include?("noheading")
sectext.sub!(/#{regex}/m, '')
end
end
@included_wiki_pages << page.title + ':' + secname
out = textilizable(sectext, :attachments => page.attachments, :headings => false)
@included_wiki_pages.pop
content_tag('div',
link_to(l(:label_button_view), project_wiki_page_path(page.project, page.title),
:class => 'icon-only icon-magnifier'),
:class => "contextual",
:title => l(:label_button_view),
:id => "view-#{page.title}") +
content_tag('div',
link_to(l(:label_button_edit), project_wiki_page_path(page.project, page.title) + '/edit',
:class => 'icon-only icon-edit'),
:class => "contextual",
:title => l(:label_button_edit),
:id => "edit-#{page.title}") + out
end
end
end
end