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
4354bfab
Commit
4354bfab
authored
Mar 11, 2016
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add implementation of reference unfolder using banzai
parent
fd8394fa
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
8 deletions
+78
-8
lib/banzai/filter/reference_filter.rb
lib/banzai/filter/reference_filter.rb
+1
-0
lib/gitlab/gfm/reference_unfolder.rb
lib/gitlab/gfm/reference_unfolder.rb
+48
-5
spec/lib/gitlab/gfm/reference_unfolder_spec.rb
spec/lib/gitlab/gfm/reference_unfolder_spec.rb
+29
-3
No files found.
lib/banzai/filter/reference_filter.rb
View file @
4354bfab
...
...
@@ -47,6 +47,7 @@ module Banzai
# Returns a String
def
data_attribute
(
attributes
=
{})
attributes
[
:reference_filter
]
=
self
.
class
.
name
.
demodulize
attributes
.
delete
(
:original
)
if
context
[
:no_original_data
]
attributes
.
map
{
|
key
,
value
|
%Q(data-
#{
key
.
to_s
.
dasherize
}
="
#{
escape_once
(
value
)
}
")
}.
join
(
" "
)
end
...
...
lib/gitlab/gfm/reference_unfolder.rb
View file @
4354bfab
...
...
@@ -8,23 +8,66 @@ module Gitlab
def
initialize
(
text
,
project
)
@text
=
text
@project
=
project
@original
=
markdown
(
text
)
end
def
unfold
(
from_project
)
referables
.
each_with_object
(
@text
.
dup
)
do
|
referable
,
text
|
next
unless
referable
.
respond_to?
(
:to_reference
)
return
@text
unless
@text
=~
references_pattern
pattern
=
/
#{
Regexp
.
escape
(
referable
.
to_reference
)
}
/
text
.
gsub!
(
pattern
,
referable
.
to_reference
(
from_project
)
)
unfolded
=
@text
.
gsub
(
references_pattern
)
do
|
reference
|
unfold_reference
(
reference
,
Regexp
.
last_match
,
from_project
)
end
unless
substitution_valid?
(
unfolded
)
raise
StandardError
,
'Invalid references unfolding!'
end
unfolded
end
private
def
unfold_reference
(
reference
,
match
,
from_project
)
before
=
@text
[
0
...
match
.
begin
(
0
)]
after
=
@text
[
match
.
end
(
0
)
...
@text
.
length
]
referable
=
find_referable
(
reference
)
return
reference
unless
referable
cross_reference
=
referable
.
to_reference
(
from_project
)
new_text
=
before
+
cross_reference
+
after
substitution_valid?
(
new_text
)
?
cross_reference
:
reference
end
def
references_pattern
return
@pattern
if
@pattern
patterns
=
Gitlab
::
ReferenceExtractor
::
REFERABLES
.
map
do
|
ref
|
ref
.
to_s
.
classify
.
constantize
.
try
(
:reference_pattern
)
end
@pattern
=
Regexp
.
union
(
patterns
.
compact
)
end
def
referables
return
@referables
if
@referables
extractor
=
Gitlab
::
ReferenceExtractor
.
new
(
@project
)
extractor
.
analyze
(
@text
)
extractor
.
all
@referables
=
extractor
.
all
end
def
find_referable
(
reference
)
referables
.
find
{
|
ref
|
ref
.
to_reference
==
reference
}
end
def
substitution_valid?
(
substituted
)
@original
==
markdown
(
substituted
)
end
def
markdown
(
text
)
helper
=
Class
.
new
.
extend
(
GitlabMarkdownHelper
)
helper
.
markdown
(
text
,
project:
@project
,
no_original_data:
true
)
end
end
end
...
...
spec/lib/gitlab/gfm/reference_unfolder_spec.rb
View file @
4354bfab
...
...
@@ -33,10 +33,36 @@ describe Gitlab::Gfm::ReferenceUnfolder do
end
context
'description ambigous elements'
do
let
(
:url
)
{
'http://gitlab.com/#1'
}
let
(
:text
)
{
"This references #1, but not
#{
url
}
"
}
context
'url'
do
let
(
:url
)
{
'http://gitlab.com/#1'
}
let
(
:text
)
{
"This references #1, but not
#{
url
}
"
}
it
{
is_expected
.
to
include
url
}
it
{
is_expected
.
to
include
url
}
end
context
'code'
do
let
(
:text
)
{
"#1, but not `[#1]`"
}
it
{
is_expected
.
to
eq
"
#{
issue_first
.
to_reference
(
new_project
)
}
, but not `[#1]`"
}
end
context
'code reverse'
do
let
(
:text
)
{
"not `#1`, but #1"
}
it
{
is_expected
.
to
eq
"not `#1`, but
#{
issue_first
.
to_reference
(
new_project
)
}
"
}
end
context
'code in random order'
do
let
(
:text
)
{
"#1, `#1`, #1, `#1`"
}
let
(
:ref
)
{
issue_first
.
to_reference
(
new_project
)
}
it
{
is_expected
.
to
eq
"
#{
ref
}
, `#1`,
#{
ref
}
, `#1`"
}
end
end
context
'reference contains milestone'
do
let
(
:milestone
)
{
create
(
:milestone
)
}
let
(
:text
)
{
"milestone ref:
#{
milestone
.
to_reference
}
"
}
it
{
is_expected
.
to
eq
text
}
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