-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.rb
157 lines (143 loc) · 4.97 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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 '1.0.7'
url 'https://github.com/taikii/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)
if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project)
if args.include?("noraise")
return ''
else
raise 'Page not found'
end
end
@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","common_mark"
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
if sectext.nil?
if args.include?("noraise")
return ''
else
raise 'Section not found'
end
end
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 = ''.html_safe
out = textilizable(sectext, :attachments => page.attachments, :headings => false)
@included_wiki_pages.pop
out
end
end
desc "Includes by table wiki pages. Examples:\n\n" +
"{{include_by_table(Section1, Section2)\n" +
"Page1\n" +
"Page2\n" +
"}}"
macro :include_by_table do |obj, args, text|
out = ''.html_safe
out << content_tag(:table) do
concat(content_tag(:thead) do
concat(content_tag(:tr) do
concat (content_tag(:th))
args.each {|col|
concat (content_tag(:th) do
concat col
end)
}
end)
end)
text.lines.map(&:chomp).each {|line|
next if line.blank?
concat (content_tag(:tr) do
concat (content_tag(:td) do
concat textilizable("[[" + line + "]]")
end)
args.each {|col|
concat (content_tag(:td) do
concat send("macro_include", obj, [line, col, "noheading", "nosubsection", "noraise"]).gsub(/[\r\n]/, '').html_safe
end)
}
end)
}
end
end
desc "Includes by table wiki pages. Examples:\n\n" +
"{{include_by_table_transpose(Page1, Page2)\n" +
"Section1\n" +
"Section2\n" +
"}}"
macro :include_by_table_transpose do |obj, args, text|
out = ''.html_safe
out << content_tag(:table) do
concat(content_tag(:thead) do
concat(content_tag(:tr) do
concat (content_tag(:th))
args.each {|col|
concat (content_tag(:th) do
concat textilizable("[[" + col + "]]")
end)
}
end)
end)
text.lines.map(&:chomp).each {|line|
next if line.blank?
concat (content_tag(:tr) do
concat (content_tag(:td) do
concat line
end)
args.each {|col|
concat (content_tag(:td) do
concat send("macro_include", obj, [col, line, "noheading", "nosubsection", "noraise"]).gsub(/[\r\n]/, '').html_safe
end)
}
end)
}
end
end
end
end