Skip to content

Commit

Permalink
Add include_by_table macro
Browse files Browse the repository at this point in the history
  • Loading branch information
taikii committed Sep 15, 2022
1 parent 55582b9 commit a4bb657
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,43 @@ This plugin makes possible include wiki section.
Caution!) This plugin will overwrite the default include macro.

## Examples

```
{{include(Foo)}}
{{include(Foo, Bar)}} -- to include Bar section of Foo page
```

### Example: Include a section.

```
> {{include(Child, Sec2)}}
```
![Example2](doc/images/example_1.png)

### Example: Include a section with options.

```
> {{include(Child, Sec2, noheading, nosubsection)}}
```
![Example2](doc/images/example_2.png)

### Example: Include a section with options in table.
### Example: Include pages in table.

Both of the below codes give the same result.

```
{{include_by_table(Sec1, Sec2)
Child1
Child2
Child3
}}
```

```
> |_. Title |
> | {{include(Child, Sec2, noheading, nosubsection)}} |
|_. Wiki page |_. Sec1 |_. Sec2 |
| [[Child1]] | {{include(Child1, Sec1, noheading, nosubsection, noraise)}} | {{include(Child1, Sec2, noheading, nosubsection, noraise)}} |
| [[Child2]] | {{include(Child2, Sec1, noheading, nosubsection, noraise)}} | {{include(Child2, Sec2, noheading, nosubsection, noraise)}} |
| [[Child3]] | {{include(Child3, Sec1, noheading, nosubsection, noraise)}} | {{include(Child3, Sec2, noheading, nosubsection, noraise)}} |
```
![Example2](doc/images/example_3.png)

Expand Down
Binary file modified doc/images/example_3.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 52 additions & 2 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
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)
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
Expand Down Expand Up @@ -48,7 +54,14 @@
sectext, hash = Redmine::WikiFormatting.formatter.new(page.content.text).get_section(index)
end

raise 'Section not found' if sectext.nil?
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
Expand All @@ -66,10 +79,47 @@
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(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) do
concat l("label_wiki_page")
end)
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
end
end

0 comments on commit a4bb657

Please sign in to comment.