Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
bc43ad71
Commit
bc43ad71
authored
Feb 23, 2016
by
Robert Speicher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replace Gollum `[[_TOC_]]` tag with result of TableOfContentsFilter
Closes #2494
parent
9f80118e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
8 deletions
+85
-8
lib/banzai/filter/gollum_tags_filter.rb
lib/banzai/filter/gollum_tags_filter.rb
+28
-7
lib/banzai/pipeline/wiki_pipeline.rb
lib/banzai/pipeline/wiki_pipeline.rb
+5
-1
spec/lib/banzai/filter/gollum_tags_filter_spec.rb
spec/lib/banzai/filter/gollum_tags_filter_spec.rb
+52
-0
No files found.
lib/banzai/filter/gollum_tags_filter.rb
View file @
bc43ad71
...
@@ -50,21 +50,30 @@ module Banzai
...
@@ -50,21 +50,30 @@ module Banzai
# See https://github.com/gollum/gollum/wiki
# See https://github.com/gollum/gollum/wiki
#
#
# Rubular: http://rubular.com/r/7dQnE5CUCH
# Rubular: http://rubular.com/r/7dQnE5CUCH
TAGS_PATTERN
=
%r{
\[\[
(.+?)
\]\]
}
TAGS_PATTERN
=
%r{
\[\[
(.+?)
\]\]
}
.
freeze
# Pattern to match allowed image extensions
# Pattern to match allowed image extensions
ALLOWED_IMAGE_EXTENSIONS
=
%r{.+(jpg|png|gif|svg|bmp)
\z
}i
ALLOWED_IMAGE_EXTENSIONS
=
%r{.+(jpg|png|gif|svg|bmp)
\z
}i
.
freeze
def
call
def
call
search_text_nodes
(
doc
).
each
do
|
node
|
search_text_nodes
(
doc
).
each
do
|
node
|
content
=
node
.
content
# A Gollum ToC tag is `[[_TOC_]]`, but due to MarkdownFilter running
# before this one, it will be converted into `[[<em>TOC</em>]]`, so it
# needs special-case handling
if
toc_tag?
(
node
)
next
unless
result
[
:toc
].
present?
next
unless
content
.
match
(
TAGS_PATTERN
)
process_toc_tag
(
node
)
else
content
=
node
.
content
html
=
process_tag
(
$1
)
next
unless
content
=~
TAGS_PATTERN
if
html
&&
html
!=
node
.
content
html
=
process_tag
(
$1
)
node
.
replace
(
html
)
if
html
&&
html
!=
node
.
content
node
.
replace
(
html
)
end
end
end
end
end
...
@@ -73,6 +82,12 @@ module Banzai
...
@@ -73,6 +82,12 @@ module Banzai
private
private
# Replace an entire `[[<em>TOC</em>]]` node with the result generated by
# TableOfContentsFilter
def
process_toc_tag
(
node
)
node
.
parent
.
parent
.
replace
(
result
[
:toc
])
end
# Process a single tag into its final HTML form.
# Process a single tag into its final HTML form.
#
#
# tag - The String tag contents (the stuff inside the double brackets).
# tag - The String tag contents (the stuff inside the double brackets).
...
@@ -108,6 +123,12 @@ module Banzai
...
@@ -108,6 +123,12 @@ module Banzai
end
end
end
end
def
toc_tag?
(
node
)
node
.
content
==
'TOC'
&&
node
.
parent
.
name
==
'em'
&&
node
.
parent
.
parent
.
text
==
'[[TOC]]'
end
def
image?
(
path
)
def
image?
(
path
)
path
=~
ALLOWED_IMAGE_EXTENSIONS
path
=~
ALLOWED_IMAGE_EXTENSIONS
end
end
...
...
lib/banzai/pipeline/wiki_pipeline.rb
View file @
bc43ad71
...
@@ -4,7 +4,11 @@ module Banzai
...
@@ -4,7 +4,11 @@ module Banzai
module
Pipeline
module
Pipeline
class
WikiPipeline
<
FullPipeline
class
WikiPipeline
<
FullPipeline
def
self
.
filters
def
self
.
filters
super
.
insert
(
1
,
Filter
::
GollumTagsFilter
)
@filters
||=
begin
filters
=
super
toc
=
filters
.
index
(
Filter
::
TableOfContentsFilter
)
filters
.
insert
(
toc
+
1
,
Filter
::
GollumTagsFilter
)
end
end
end
end
end
end
end
...
...
spec/lib/banzai/filter/gollum_tags_filter_spec.rb
View file @
bc43ad71
...
@@ -86,4 +86,56 @@ describe Banzai::Filter::GollumTagsFilter, lib: true do
...
@@ -86,4 +86,56 @@ describe Banzai::Filter::GollumTagsFilter, lib: true do
expect
(
doc
.
at_css
(
'a'
)[
'href'
]).
to
eq
'wiki-slug'
expect
(
doc
.
at_css
(
'a'
)[
'href'
]).
to
eq
'wiki-slug'
end
end
end
end
context
'table of contents'
do
let
(
:pipeline
)
{
Banzai
::
Pipeline
[
:wiki
]
}
it
'replaces the tag with the TableOfContentsFilter result'
do
markdown
=
<<-
MD
.
strip_heredoc
[[_TOC_]]
## Header
Foo
MD
result
=
pipeline
.
call
(
markdown
,
project_wiki:
project_wiki
,
project:
project
)
aggregate_failures
do
expect
(
result
[
:output
].
text
).
not_to
include
'[[_TOC_]]'
expect
(
result
[
:output
].
text
).
not_to
include
'[['
expect
(
result
[
:output
].
to_html
).
to
include
(
result
[
:toc
])
end
end
it
'is case-sensitive'
do
markdown
=
<<-
MD
.
strip_heredoc
[[_toc_]]
# Header 1
Foo
MD
output
=
pipeline
.
to_html
(
markdown
,
project_wiki:
project_wiki
,
project:
project
)
expect
(
output
).
to
include
(
'[[<em>toc</em>]]'
)
end
it
'handles an empty pipeline result'
do
# No Markdown headers in this doc, so `result[:toc]` will be empty
markdown
=
<<-
MD
.
strip_heredoc
[[_TOC_]]
Foo
MD
output
=
pipeline
.
to_html
(
markdown
,
project_wiki:
project_wiki
,
project:
project
)
aggregate_failures
do
expect
(
output
).
not_to
include
(
'<ul>'
)
expect
(
output
).
to
include
(
'[[<em>TOC</em>]]'
)
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment