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
iv
gitlab-ce
Commits
765a2c73
Commit
765a2c73
authored
Jan 12, 2016
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring Banzai::Filter::GollumTagsFilter
parent
89e8b82b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
23 deletions
+28
-23
lib/banzai/filter/gollum_tags_filter.rb
lib/banzai/filter/gollum_tags_filter.rb
+28
-19
spec/lib/banzai/filter/gollum_tags_filter_spec.rb
spec/lib/banzai/filter/gollum_tags_filter_spec.rb
+0
-4
No files found.
lib/banzai/filter/gollum_tags_filter.rb
View file @
765a2c73
...
@@ -14,7 +14,10 @@ module Banzai
...
@@ -14,7 +14,10 @@ module Banzai
include
ActionView
::
Helpers
::
TagHelper
include
ActionView
::
Helpers
::
TagHelper
# Pattern to match tag contents.
# Pattern to match tag contents.
TAGS_PATTERN
=
%r{(.?)
\[\[
(.+?)
\]\]
([^
\[
]?)}
TAGS_PATTERN
=
%r{
\[\[
(.+?)
\]\]
}
# Pattern to match allowed image extensions
ALLOWED_IMAGE_EXTENSIONS
=
%r{.+(jpg|png|gif|svg|bmp)
\z
}i
def
call
def
call
search_text_nodes
(
doc
).
each
do
|
node
|
search_text_nodes
(
doc
).
each
do
|
node
|
...
@@ -22,9 +25,11 @@ module Banzai
...
@@ -22,9 +25,11 @@ module Banzai
next
unless
content
.
match
(
TAGS_PATTERN
)
next
unless
content
.
match
(
TAGS_PATTERN
)
html
=
process_tag
(
$
2
)
html
=
process_tag
(
$
1
)
node
.
replace
(
html
)
if
html
!=
node
.
content
if
html
&&
html
!=
node
.
content
node
.
replace
(
html
)
end
end
end
doc
doc
...
@@ -38,11 +43,11 @@ module Banzai
...
@@ -38,11 +43,11 @@ module Banzai
#
#
# Returns the String HTML version of the tag.
# Returns the String HTML version of the tag.
def
process_tag
(
tag
)
def
process_tag
(
tag
)
if
html
=
process_image_tag
(
tag
)
parts
=
tag
.
split
(
'|'
)
html
else
return
if
parts
.
size
.
zero?
process_page_link_tag
(
tag
)
end
process_image_tag
(
parts
)
||
process_page_link_tag
(
parts
)
end
end
# Attempt to process the tag as an image tag.
# Attempt to process the tag as an image tag.
...
@@ -51,16 +56,15 @@ module Banzai
...
@@ -51,16 +56,15 @@ module Banzai
#
#
# Returns the String HTML if the tag is a valid image tag or nil
# Returns the String HTML if the tag is a valid image tag or nil
# if it is not.
# if it is not.
def
process_image_tag
(
tag
)
def
process_image_tag
(
parts
)
parts
=
tag
.
split
(
'|'
)
content
=
parts
[
0
].
strip
return
if
parts
.
size
.
zero?
name
=
parts
[
0
].
strip
return
unless
image?
(
content
)
if
file
=
project_wiki
.
find_file
(
name
)
if
url?
(
content
)
path
=
content
elsif
file
=
project_wiki
.
find_file
(
content
)
path
=
::
File
.
join
project_wiki_base_path
,
file
.
path
path
=
::
File
.
join
project_wiki_base_path
,
file
.
path
elsif
name
=~
/^https?:\/\/.+(jpg|png|gif|svg|bmp)$/i
path
=
name
end
end
if
path
if
path
...
@@ -68,16 +72,21 @@ module Banzai
...
@@ -68,16 +72,21 @@ module Banzai
end
end
end
end
def
image?
(
path
)
path
=~
ALLOWED_IMAGE_EXTENSIONS
end
def
url?
(
path
)
path
.
start_with?
(
*
%w(http https)
)
end
# Attempt to process the tag as a page link tag.
# Attempt to process the tag as a page link tag.
#
#
# tag - The String tag contents (the stuff inside the double brackets).
# tag - The String tag contents (the stuff inside the double brackets).
#
#
# Returns the String HTML if the tag is a valid page link tag or nil
# Returns the String HTML if the tag is a valid page link tag or nil
# if it is not.
# if it is not.
def
process_page_link_tag
(
tag
)
def
process_page_link_tag
(
parts
)
parts
=
tag
.
split
(
'|'
)
return
if
parts
.
size
.
zero?
if
parts
.
size
==
1
if
parts
.
size
==
1
url
=
parts
[
0
].
strip
url
=
parts
[
0
].
strip
else
else
...
...
spec/lib/banzai/filter/gollum_tags_filter_spec.rb
View file @
765a2c73
...
@@ -37,8 +37,6 @@ describe Banzai::Filter::GollumTagsFilter, lib: true do
...
@@ -37,8 +37,6 @@ describe Banzai::Filter::GollumTagsFilter, lib: true do
context
'linking external images'
do
context
'linking external images'
do
it
'creates img tag for valid URL'
do
it
'creates img tag for valid URL'
do
expect
(
project_wiki
).
to
receive
(
:find_file
).
with
(
'http://example.com/image.jpg'
).
and_return
(
nil
)
tag
=
'[[http://example.com/image.jpg]]'
tag
=
'[[http://example.com/image.jpg]]'
doc
=
filter
(
"See
#{
tag
}
"
,
project_wiki:
project_wiki
)
doc
=
filter
(
"See
#{
tag
}
"
,
project_wiki:
project_wiki
)
...
@@ -46,8 +44,6 @@ describe Banzai::Filter::GollumTagsFilter, lib: true do
...
@@ -46,8 +44,6 @@ describe Banzai::Filter::GollumTagsFilter, lib: true do
end
end
it
'does not creates img tag for invalid URL'
do
it
'does not creates img tag for invalid URL'
do
expect
(
project_wiki
).
to
receive
(
:find_file
).
with
(
'http://example.com/image.pdf'
).
and_return
(
nil
)
tag
=
'[[http://example.com/image.pdf]]'
tag
=
'[[http://example.com/image.pdf]]'
doc
=
filter
(
"See
#{
tag
}
"
,
project_wiki:
project_wiki
)
doc
=
filter
(
"See
#{
tag
}
"
,
project_wiki:
project_wiki
)
...
...
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