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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
c2502887
Commit
c2502887
authored
Feb 13, 2020
by
Francisco Javier López
Committed by
a_luna
Mar 02, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
First try rendering the whole view content
parent
e80ae90b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
79 additions
and
33 deletions
+79
-33
app/presenters/snippet_blob_presenter.rb
app/presenters/snippet_blob_presenter.rb
+25
-10
spec/presenters/snippet_blob_presenter_spec.rb
spec/presenters/snippet_blob_presenter_spec.rb
+54
-23
No files found.
app/presenters/snippet_blob_presenter.rb
View file @
c2502887
# frozen_string_literal: true
# frozen_string_literal: true
class
SnippetBlobPresenter
<
BlobPresenter
class
SnippetBlobPresenter
<
BlobPresenter
include
Gitlab
::
Routing
def
rich_data
def
rich_data
return
if
blob
.
binary?
return
if
blob
.
binary?
return
unless
blob
.
rich_viewer
if
markup?
render_rich_partial
blob
.
rendered_markup
else
highlight
(
plain:
false
)
end
end
end
def
plain_data
def
plain_data
return
if
blob
.
binary?
return
if
blob
.
binary?
highlight
(
plain:
!
markup?
)
highlight
(
plain:
false
)
end
end
def
raw_path
def
raw_path
...
@@ -27,10 +26,6 @@ class SnippetBlobPresenter < BlobPresenter
...
@@ -27,10 +26,6 @@ class SnippetBlobPresenter < BlobPresenter
private
private
def
markup?
blob
.
rich_viewer
&
.
partial_name
==
'markup'
end
def
snippet
def
snippet
blob
.
container
blob
.
container
end
end
...
@@ -38,4 +33,24 @@ class SnippetBlobPresenter < BlobPresenter
...
@@ -38,4 +33,24 @@ class SnippetBlobPresenter < BlobPresenter
def
language
def
language
nil
nil
end
end
def
render_rich_partial
renderer
.
render
(
"projects/blob/viewers/
#{
blob
.
rich_viewer
.
partial_name
}
"
,
viewer:
blob
.
rich_viewer
,
blob:
blob
,
blow_raw_path:
raw_path
)
end
def
renderer
ActionView
::
Base
.
new
(
build_lookup_context
,
{
snippet:
snippet
},
ActionController
::
Base
.
new
).
tap
do
|
renderer
|
renderer
.
extend
ApplicationController
.
_helpers
renderer
.
class_eval
do
include
Rails
.
application
.
routes
.
url_helpers
end
end
end
def
build_lookup_context
ActionView
::
Base
.
build_lookup_context
(
ActionController
::
Base
.
view_paths
)
end
end
end
spec/presenters/snippet_blob_presenter_spec.rb
View file @
c2502887
...
@@ -4,36 +4,67 @@ require 'spec_helper'
...
@@ -4,36 +4,67 @@ require 'spec_helper'
describe
SnippetBlobPresenter
do
describe
SnippetBlobPresenter
do
describe
'#rich_data'
do
describe
'#rich_data'
do
let
(
:snippet
)
{
build
(
:personal_snippet
)
}
let_it_be
(
:snippet
)
{
create
(
:personal_snippet
)
}
before
do
allow_next_instance_of
(
ActionController
::
Base
)
do
|
instance
|
allow
(
instance
).
to
receive
(
:current_user
).
and_return
(
nil
)
end
end
subject
{
described_class
.
new
(
snippet
.
blob
).
rich_data
}
subject
{
described_class
.
new
(
snippet
.
blob
).
rich_data
}
it
'returns nil when the snippet blob is binary'
do
context
'with PersonalSnippet'
do
allow
(
snippet
.
blob
).
to
receive
(
:binary?
).
and_return
(
true
)
let
(
:raw_url
)
{
"http://127.0.0.1:3000/snippets/
#{
snippet
.
id
}
/raw"
}
let
(
:snippet
)
{
build
(
:personal_snippet
)
}
expect
(
subject
).
to
be_nil
it
'returns nil when the snippet blob is binary'
do
end
allow
(
snippet
.
blob
).
to
receive
(
:binary?
).
and_return
(
true
)
it
'returns markdown content when snippet file is markup'
do
expect
(
subject
).
to
be_nil
snippet
.
file_name
=
'test.md'
end
snippet
.
content
=
'*foo*'
expect
(
subject
).
to
eq
'<p data-sourcepos="1:1-1:5" dir="auto"><em>foo</em></p>'
context
'with markdown format'
do
end
let
(
:snippet
)
{
create
(
:personal_snippet
,
file_name:
'test.md'
,
content:
'*foo*'
)
}
it
'returns syntax highlighted content'
do
it
'returns rich markdown content'
do
snippet
.
file_name
=
'test.rb'
expect
(
subject
).
to
eq
'<div class="file-content md md-file"><p data-sourcepos="1:1-1:5" dir="auto"><em>foo</em></p></div>'
snippet
.
content
=
'class Foo;end'
end
end
expect
(
subject
)
context
'with notebook format'
do
.
to
eq
'<span id="LC1" class="line" lang="ruby"><span class="k">class</span> <span class="nc">Foo</span><span class="p">;</span><span class="k">end</span></span>'
let
(
:snippet
)
{
create
(
:personal_snippet
,
file_name:
'test.ipynb'
)
}
end
it
'returns plain text highlighted content'
do
it
'returns rich notebook content'
do
snippet
.
file_name
=
'test'
expect
(
subject
.
strip
).
to
eq
'<div class="file-content" data-endpoint="http://127.0.0.1:3000/snippets/'
+
snippet
.
id
.
to_s
+
'/raw" id="js-notebook-viewer"></div>'
snippet
.
content
=
'foo'
end
end
expect
(
subject
).
to
eq
'<span id="LC1" class="line" lang="plaintext">foo</span>'
context
'with openapi format'
do
let
(
:snippet
)
{
create
(
:personal_snippet
,
file_name:
'openapi.yml'
)
}
it
'returns rich openapi content'
do
expect
(
subject
).
to
eq
'<div class="file-content" data-endpoint="http://127.0.0.1:3000/snippets/'
+
snippet
.
id
.
to_s
+
'/raw" id="js-openapi-viewer"></div>'
end
end
context
'with svg format'
do
let
(
:snippet
)
{
create
(
:personal_snippet
,
file_name:
'test.svg'
)
}
it
'returns rich svg content'
do
snippet
.
file_name
=
'test.svg'
expect
(
subject
).
to
eq
'<div class="file-content" data-endpoint="http://127.0.0.1:3000/snippets/'
+
snippet
.
id
.
to_s
+
'/raw" id="js-openapi-viewer"></div>'
end
end
context
'with other format'
do
let
(
:snippet
)
{
create
(
:personal_snippet
,
file_name:
'test'
)
}
it
'does not return no rich content'
do
expect
(
subject
).
to
be_nil
end
end
end
end
end
end
...
@@ -55,19 +86,19 @@ describe SnippetBlobPresenter do
...
@@ -55,19 +86,19 @@ describe SnippetBlobPresenter do
expect
(
subject
).
to
eq
'<span id="LC1" class="line" lang="markdown"><span class="ge">*foo*</span></span>'
expect
(
subject
).
to
eq
'<span id="LC1" class="line" lang="markdown"><span class="ge">*foo*</span></span>'
end
end
it
'returns
plain
syntax content'
do
it
'returns
highlighted
syntax content'
do
snippet
.
file_name
=
'test.rb'
snippet
.
file_name
=
'test.rb'
snippet
.
content
=
'class Foo;end'
snippet
.
content
=
'class Foo;end'
expect
(
subject
)
expect
(
subject
)
.
to
eq
'<span id="LC1" class="line" lang="
">class Foo;end
</span>'
.
to
eq
'<span id="LC1" class="line" lang="
ruby"><span class="k">class</span> <span class="nc">Foo</span><span class="p">;</span><span class="k">end</span>
</span>'
end
end
it
'returns plain text highlighted content'
do
it
'returns plain text highlighted content'
do
snippet
.
file_name
=
'test'
snippet
.
file_name
=
'test'
snippet
.
content
=
'foo'
snippet
.
content
=
'foo'
expect
(
subject
).
to
eq
'<span id="LC1" class="line" lang="">foo</span>'
expect
(
subject
).
to
eq
'<span id="LC1" class="line" lang="
plaintext
">foo</span>'
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