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
4f2d6b3e
Commit
4f2d6b3e
authored
Apr 21, 2017
by
Douwe Maan
Committed by
Toon Claes
Apr 27, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor MarkupHelper
parent
05e0f504
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
67 additions
and
99 deletions
+67
-99
app/helpers/markup_helper.rb
app/helpers/markup_helper.rb
+48
-68
app/models/repository.rb
app/models/repository.rb
+1
-1
app/views/projects/_readme.html.haml
app/views/projects/_readme.html.haml
+1
-1
app/views/projects/blob/_markup.html.haml
app/views/projects/blob/_markup.html.haml
+1
-1
app/views/projects/blob/preview.html.haml
app/views/projects/blob/preview.html.haml
+1
-1
app/views/projects/tree/_readme.html.haml
app/views/projects/tree/_readme.html.haml
+1
-1
app/views/search/results/_snippet_blob.html.haml
app/views/search/results/_snippet_blob.html.haml
+1
-1
app/views/shared/snippets/_blob.html.haml
app/views/shared/snippets/_blob.html.haml
+1
-1
lib/gitlab/asciidoc.rb
lib/gitlab/asciidoc.rb
+1
-7
lib/gitlab/other_markup.rb
lib/gitlab/other_markup.rb
+1
-7
spec/helpers/markup_helper_spec.rb
spec/helpers/markup_helper_spec.rb
+10
-10
No files found.
app/helpers/markup_helper.rb
View file @
4f2d6b3e
...
@@ -59,18 +59,22 @@ module MarkupHelper
...
@@ -59,18 +59,22 @@ module MarkupHelper
fragment
.
to_html
.
html_safe
fragment
.
to_html
.
html_safe
end
end
def
markdown
(
text
,
context
=
{})
# Return the first line of +text+, up to +max_chars+, after parsing the line
html
=
markdown_render
(
text
,
context
)
# as Markdown. HTML tags in the parsed output are not counted toward the
# +max_chars+ limit. If the length limit falls within a tag's contents, then
# the tag contents are truncated without removing the closing tag.
def
first_line_in_markdown
(
text
,
max_chars
=
nil
,
options
=
{})
md
=
markdown
(
text
,
options
).
strip
markup_postprocess
(
html
,
context
)
truncate_visible
(
md
,
max_chars
||
md
.
length
)
if
md
.
present?
end
end
def
markdown
_render
(
text
,
context
=
{})
def
markdown
(
text
,
context
=
{})
return
""
unless
text
.
present?
return
""
unless
text
.
present?
context
[
:project
]
||=
@project
context
[
:project
]
||=
@project
html
=
context
.
delete
(
:rendered
)
||
markdown_unsafe
(
text
,
context
)
Banzai
.
render
(
text
,
context
)
banzai_postprocess
(
html
,
context
)
end
end
def
markdown_field
(
object
,
field
)
def
markdown_field
(
object
,
field
)
...
@@ -81,85 +85,59 @@ module MarkupHelper
...
@@ -81,85 +85,59 @@ module MarkupHelper
banzai_postprocess
(
html
,
object
.
banzai_render_context
(
field
))
banzai_postprocess
(
html
,
object
.
banzai_render_context
(
field
))
end
end
def
asciidoc_render
(
text
)
def
markup
(
file_name
,
text
,
context
=
{})
Gitlab
::
Asciidoc
.
render
(
text
,
project:
@project
,
current_user:
(
current_user
if
defined?
(
current_user
)),
# RelativeLinkFilter
project_wiki:
@project_wiki
,
requested_path:
@path
,
ref:
@ref
,
commit:
@commit
)
end
def
other_markup_render
(
file_name
,
text
)
Gitlab
::
OtherMarkup
.
render
(
file_name
,
text
,
project:
@project
,
current_user:
(
current_user
if
defined?
(
current_user
)),
# RelativeLinkFilter
project_wiki:
@project_wiki
,
requested_path:
@path
,
ref:
@ref
,
commit:
@commit
)
end
def
markup_postprocess
(
html
,
context
=
{})
return
""
unless
html
.
present?
context
[
:project
]
||=
@project
context
[
:project
]
||=
@project
html
=
context
.
delete
(
:rendered
)
||
markup_unsafe
(
file_name
,
text
,
context
)
banzai_postprocess
(
html
,
context
)
banzai_postprocess
(
html
,
context
)
end
end
# Return the first line of +text+, up to +max_chars+, after parsing the line
def
render_wiki_content
(
wiki_page
)
# as Markdown. HTML tags in the parsed output are not counted toward the
text
=
wiki_page
.
content
# +max_chars+ limit. If the length limit falls within a tag's contents, then
return
""
unless
text
.
present?
# the tag contents are truncated without removing the closing tag.
def
first_line_in_markdown
(
text
,
max_chars
=
nil
,
options
=
{})
md
=
markdown
(
text
,
options
).
strip
truncate_visible
(
md
,
max_chars
||
md
.
length
)
if
md
.
present?
context
=
{
pipeline: :wiki
,
project:
@project
,
project_wiki:
@project_wiki
,
page_slug:
wiki_page
.
slug
}
end
def
render_wiki_content
(
wiki_page
)
html
=
context
=
{
pipeline: :wiki
,
project_wiki:
@project_wiki
,
page_slug:
wiki_page
.
slug
}
case
wiki_page
.
format
case
wiki_page
.
format
when
:markdown
when
:markdown
html
=
markdown_render
(
wiki_page
.
conten
t
,
context
)
markdown_unsafe
(
tex
t
,
context
)
when
:asciidoc
when
:asciidoc
html
=
asciidoc_render
(
wiki_page
.
conten
t
)
asciidoc_unsafe
(
tex
t
)
else
else
return
wiki_page
.
formatted_content
.
html_safe
wiki_page
.
formatted_content
.
html_safe
end
markup_postprocess
(
html
,
context
)
end
end
def
render_markup
(
file_name
,
file_content
)
banzai_postprocess
(
html
,
context
)
html
=
markup_render
(
file_name
,
file_content
)
markup_postprocess
(
html
)
end
end
def
markup_render
(
file_name
,
file_content
)
def
markup_unsafe
(
file_name
,
text
,
context
=
{})
return
""
unless
text
.
present?
if
gitlab_markdown?
(
file_name
)
if
gitlab_markdown?
(
file_name
)
Hamlit
::
RailsHelpers
.
preserve
(
markdown_
render
(
file_conten
t
))
Hamlit
::
RailsHelpers
.
preserve
(
markdown_
unsafe
(
text
,
contex
t
))
elsif
asciidoc?
(
file_name
)
elsif
asciidoc?
(
file_name
)
asciidoc_
render
(
file_conten
t
)
asciidoc_
unsafe
(
tex
t
)
elsif
plain?
(
file_name
)
elsif
plain?
(
file_name
)
content_tag
:pre
,
class:
'plain-readme'
do
content_tag
:pre
,
class:
'plain-readme'
do
file_conten
t
tex
t
end
end
else
else
other_markup_
render
(
file_name
,
file_conten
t
)
other_markup_
unsafe
(
file_name
,
tex
t
)
end
end
rescue
RuntimeError
rescue
RuntimeError
simple_format
(
file_content
)
simple_format
(
text
)
end
def
markdown_unsafe
(
text
,
context
=
{})
Banzai
.
render
(
text
,
context
)
end
def
asciidoc_unsafe
(
text
)
Gitlab
::
Asciidoc
.
render
(
text
)
end
def
other_markup_unsafe
(
file_name
,
text
)
Gitlab
::
OtherMarkup
.
render
(
file_name
,
text
)
end
end
# Returns the text necessary to reference `entity` across projects
# Returns the text necessary to reference `entity` across projects
...
@@ -249,6 +227,8 @@ module MarkupHelper
...
@@ -249,6 +227,8 @@ module MarkupHelper
# Calls Banzai.post_process with some common context options
# Calls Banzai.post_process with some common context options
def
banzai_postprocess
(
html
,
context
=
{})
def
banzai_postprocess
(
html
,
context
=
{})
return
""
unless
html
.
present?
context
.
merge!
(
context
.
merge!
(
current_user:
(
current_user
if
defined?
(
current_user
)),
current_user:
(
current_user
if
defined?
(
current_user
)),
...
...
app/models/repository.rb
View file @
4f2d6b3e
...
@@ -530,7 +530,7 @@ class Repository
...
@@ -530,7 +530,7 @@ class Repository
end
end
def
rendered_readme
def
rendered_readme
markup_
render
(
readme
.
name
,
readme
.
data
)
if
readme
markup_
unsafe
(
readme
.
name
,
readme
.
data
,
project:
project
)
if
readme
end
end
cache_method
:rendered_readme
cache_method
:rendered_readme
...
...
app/views/projects/_readme.html.haml
View file @
4f2d6b3e
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
-
if
can?
(
current_user
,
:push_code
,
@project
)
-
if
can?
(
current_user
,
:push_code
,
@project
)
=
link_to
icon
(
'pencil'
),
namespace_project_edit_blob_path
(
@project
.
namespace
,
@project
,
tree_join
(
@repository
.
root_ref
,
readme
.
name
)),
class:
'light edit-project-readme'
=
link_to
icon
(
'pencil'
),
namespace_project_edit_blob_path
(
@project
.
namespace
,
@project
,
tree_join
(
@repository
.
root_ref
,
readme
.
name
)),
class:
'light edit-project-readme'
.file-content.wiki
.file-content.wiki
=
markup
_postprocess
(
@repository
.
rendered_readme
)
=
markup
(
readme
.
name
,
readme
.
data
,
rendered:
@repository
.
rendered_readme
)
-
else
-
else
.row-content-block.second-block.center
.row-content-block.second-block.center
%h3
.page-title
%h3
.page-title
...
...
app/views/projects/blob/_markup.html.haml
View file @
4f2d6b3e
-
blob
.
load_all_data!
(
@repository
)
-
blob
.
load_all_data!
(
@repository
)
.file-content.wiki
.file-content.wiki
=
render_
markup
(
blob
.
name
,
blob
.
data
)
=
markup
(
blob
.
name
,
blob
.
data
)
app/views/projects/blob/preview.html.haml
View file @
4f2d6b3e
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
=
markdown
(
@content
)
=
markdown
(
@content
)
-
elsif
markup?
(
@blob
.
name
)
-
elsif
markup?
(
@blob
.
name
)
.file-content.wiki
.file-content.wiki
=
raw
render_
markup
(
@blob
.
name
,
@content
)
=
raw
markup
(
@blob
.
name
,
@content
)
-
else
-
else
.file-content.code.js-syntax-highlight
.file-content.code.js-syntax-highlight
-
unless
@diff_lines
.
empty?
-
unless
@diff_lines
.
empty?
...
...
app/views/projects/tree/_readme.html.haml
View file @
4f2d6b3e
...
@@ -5,4 +5,4 @@
...
@@ -5,4 +5,4 @@
%strong
%strong
=
readme
.
name
=
readme
.
name
.file-content.wiki
.file-content.wiki
=
render_
markup
(
readme
.
name
,
readme
.
data
)
=
markup
(
readme
.
name
,
readme
.
data
)
app/views/search/results/_snippet_blob.html.haml
View file @
4f2d6b3e
...
@@ -21,7 +21,7 @@
...
@@ -21,7 +21,7 @@
.file-content.wiki
.file-content.wiki
-
snippet_chunks
.
each
do
|
chunk
|
-
snippet_chunks
.
each
do
|
chunk
|
-
unless
chunk
[
:data
].
empty?
-
unless
chunk
[
:data
].
empty?
=
render_
markup
(
snippet
.
file_name
,
chunk
[
:data
])
=
markup
(
snippet
.
file_name
,
chunk
[
:data
])
-
else
-
else
.file-content.code
.file-content.code
.nothing-here-block
Empty file
.nothing-here-block
Empty file
...
...
app/views/shared/snippets/_blob.html.haml
View file @
4f2d6b3e
...
@@ -24,6 +24,6 @@
...
@@ -24,6 +24,6 @@
-
if
gitlab_markdown?
(
@snippet
.
file_name
)
-
if
gitlab_markdown?
(
@snippet
.
file_name
)
=
preserve
(
markdown_field
(
@snippet
,
:content
))
=
preserve
(
markdown_field
(
@snippet
,
:content
))
-
else
-
else
=
render_
markup
(
@snippet
.
file_name
,
@snippet
.
content
)
=
markup
(
@snippet
.
file_name
,
@snippet
.
content
)
-
else
-
else
=
render
'shared/file_highlight'
,
blob:
@snippet
=
render
'shared/file_highlight'
,
blob:
@snippet
lib/gitlab/asciidoc.rb
View file @
4f2d6b3e
...
@@ -14,15 +14,9 @@ module Gitlab
...
@@ -14,15 +14,9 @@ module Gitlab
# Public: Converts the provided Asciidoc markup into HTML.
# Public: Converts the provided Asciidoc markup into HTML.
#
#
# input - the source text in Asciidoc format
# input - the source text in Asciidoc format
# context - a Hash with the template context:
# :commit
# :project
# :project_wiki
# :requested_path
# :ref
# asciidoc_opts - a Hash of options to pass to the Asciidoctor converter
# asciidoc_opts - a Hash of options to pass to the Asciidoctor converter
#
#
def
self
.
render
(
input
,
context
,
asciidoc_opts
=
{})
def
self
.
render
(
input
,
asciidoc_opts
=
{})
asciidoc_opts
.
reverse_merge!
(
asciidoc_opts
.
reverse_merge!
(
safe: :secure
,
safe: :secure
,
backend: :gitlab_html5
,
backend: :gitlab_html5
,
...
...
lib/gitlab/other_markup.rb
View file @
4f2d6b3e
...
@@ -4,14 +4,8 @@ module Gitlab
...
@@ -4,14 +4,8 @@ module Gitlab
# Public: Converts the provided markup into HTML.
# Public: Converts the provided markup into HTML.
#
#
# input - the source text in a markup format
# input - the source text in a markup format
# context - a Hash with the template context:
# :commit
# :project
# :project_wiki
# :requested_path
# :ref
#
#
def
self
.
render
(
file_name
,
input
,
context
)
def
self
.
render
(
file_name
,
input
)
html
=
GitHub
::
Markup
.
render
(
file_name
,
input
).
html
=
GitHub
::
Markup
.
render
(
file_name
,
input
).
force_encoding
(
input
.
encoding
)
force_encoding
(
input
.
encoding
)
...
...
spec/helpers/markup_helper_spec.rb
View file @
4f2d6b3e
...
@@ -126,7 +126,7 @@ describe MarkupHelper do
...
@@ -126,7 +126,7 @@ describe MarkupHelper do
it
"uses Wiki pipeline for markdown files"
do
it
"uses Wiki pipeline for markdown files"
do
allow
(
@wiki
).
to
receive
(
:format
).
and_return
(
:markdown
)
allow
(
@wiki
).
to
receive
(
:format
).
and_return
(
:markdown
)
expect
(
helper
).
to
receive
(
:markdown_
render
).
with
(
'wiki content'
,
pipeline: :wiki
,
project_wiki:
@wiki
,
page_slug:
"nested/page"
)
expect
(
helper
).
to
receive
(
:markdown_
unsafe
).
with
(
'wiki content'
,
pipeline: :wiki
,
project_wiki:
@wiki
,
page_slug:
"nested/page"
)
helper
.
render_wiki_content
(
@wiki
)
helper
.
render_wiki_content
(
@wiki
)
end
end
...
@@ -135,7 +135,7 @@ describe MarkupHelper do
...
@@ -135,7 +135,7 @@ describe MarkupHelper do
allow_any_instance_of
(
ApplicationSetting
).
to
receive
(
:current
).
and_return
(
::
ApplicationSetting
.
create_from_defaults
)
allow_any_instance_of
(
ApplicationSetting
).
to
receive
(
:current
).
and_return
(
::
ApplicationSetting
.
create_from_defaults
)
allow
(
@wiki
).
to
receive
(
:format
).
and_return
(
:asciidoc
)
allow
(
@wiki
).
to
receive
(
:format
).
and_return
(
:asciidoc
)
expect
(
helper
).
to
receive
(
:asciidoc_
render
).
with
(
'wiki content'
)
expect
(
helper
).
to
receive
(
:asciidoc_
unsafe
).
with
(
'wiki content'
)
helper
.
render_wiki_content
(
@wiki
)
helper
.
render_wiki_content
(
@wiki
)
end
end
...
@@ -150,26 +150,26 @@ describe MarkupHelper do
...
@@ -150,26 +150,26 @@ describe MarkupHelper do
end
end
end
end
describe
'
render_
markup'
do
describe
'markup'
do
let
(
:content
)
{
'Noël'
}
let
(
:content
)
{
'Noël'
}
it
'preserves encoding'
do
it
'preserves encoding'
do
expect
(
content
.
encoding
.
name
).
to
eq
(
'UTF-8'
)
expect
(
content
.
encoding
.
name
).
to
eq
(
'UTF-8'
)
expect
(
helper
.
render_
markup
(
'foo.rst'
,
content
).
encoding
.
name
).
to
eq
(
'UTF-8'
)
expect
(
helper
.
markup
(
'foo.rst'
,
content
).
encoding
.
name
).
to
eq
(
'UTF-8'
)
end
end
it
"delegates to #markdown_
render
when file name corresponds to Markdown"
do
it
"delegates to #markdown_
unsafe
when file name corresponds to Markdown"
do
expect
(
helper
).
to
receive
(
:gitlab_markdown?
).
with
(
'foo.md'
).
and_return
(
true
)
expect
(
helper
).
to
receive
(
:gitlab_markdown?
).
with
(
'foo.md'
).
and_return
(
true
)
expect
(
helper
).
to
receive
(
:markdown_
render
).
and_return
(
'NOEL'
)
expect
(
helper
).
to
receive
(
:markdown_
unsafe
).
and_return
(
'NOEL'
)
expect
(
helper
.
render_
markup
(
'foo.md'
,
content
)).
to
eq
(
'NOEL'
)
expect
(
helper
.
markup
(
'foo.md'
,
content
)).
to
eq
(
'NOEL'
)
end
end
it
"delegates to #asciidoc_
render
when file name corresponds to AsciiDoc"
do
it
"delegates to #asciidoc_
unsafe
when file name corresponds to AsciiDoc"
do
expect
(
helper
).
to
receive
(
:asciidoc?
).
with
(
'foo.adoc'
).
and_return
(
true
)
expect
(
helper
).
to
receive
(
:asciidoc?
).
with
(
'foo.adoc'
).
and_return
(
true
)
expect
(
helper
).
to
receive
(
:asciidoc_
render
).
and_return
(
'NOEL'
)
expect
(
helper
).
to
receive
(
:asciidoc_
unsafe
).
and_return
(
'NOEL'
)
expect
(
helper
.
render_
markup
(
'foo.adoc'
,
content
)).
to
eq
(
'NOEL'
)
expect
(
helper
.
markup
(
'foo.adoc'
,
content
)).
to
eq
(
'NOEL'
)
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