Skip to content
GitLab
Explorer
Connexion
S'inscrire
Navigation principale
Rechercher ou aller à…
Projet
P
parlote-facil
Gestion
Activité
Membres
Labels
Programmation
Tickets
Tableaux des tickets
Jalons
Wiki
Code
Requêtes de fusion
Dépôt
Branches
Validations
Étiquettes
Graphe du dépôt
Comparer les révisions
Extraits de code
Compilation
Pipelines
Jobs
Planifications de pipeline
Artéfacts
Déploiement
Releases
Registre de paquets
Registre de conteneur
Registre de modèles
Opération
Environnements
Modules Terraform
Surveillance
Incidents
Analyse
Données d'analyse des chaînes de valeur
Analyse des contributeurs
Données d'analyse CI/CD
Données d'analyse du dépôt
Expériences du modèle
Aide
Aide
Support
Documentation de GitLab
Comparer les forfaits GitLab
Forum de la communauté
Contribuer à GitLab
Donner votre avis
Raccourcis clavier
?
Extraits de code
Groupes
Projets
Afficher davantage de fils d'Ariane
facil
parlote-facil
Validations
c4a7745c
Valider
c4a7745c
rédigé
14 years ago
par
Dan Hansen & Sarah Mei
Parcourir les fichiers
Options
Téléchargements
Correctifs
Plain Diff
Refactored the markdownify method.
parent
1d1faea9
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Modifications
2
Masquer les modifications d'espaces
En ligne
Côte à côte
Affichage de
2 fichiers modifiés
app/helpers/application_helper.rb
+88
-74
88 ajouts, 74 suppressions
app/helpers/application_helper.rb
spec/helpers/application_helper_spec.rb
+21
-45
21 ajouts, 45 suppressions
spec/helpers/application_helper_spec.rb
avec
109 ajouts
et
119 suppressions
app/helpers/application_helper.rb
+
88
−
74
Voir le fichier @
c4a7745c
...
...
@@ -24,21 +24,24 @@ module ApplicationHelper
post
.
aspect_ids
.
include?
(
a
.
id
)
end
end
def
aspects_without_post
aspects
,
post
aspects
.
reject
do
|
a
|
post
.
aspect_ids
.
include?
(
a
.
id
)
end
end
def
aspect_badge
aspects
str
=
''
if
aspects
.
count
>
1
str
=
"<span class='aspect_badge all'>
#{
I18n
.
t
(
'application.helper.aspect_badge.all_aspects'
)
}
</span>"
elsif
aspects
.
count
==
1
aspect
=
aspects
.
first
str
=
"<span class='aspect_badge single'><a href=
#{
aspect_path
(
aspect
)
}
>
#{
aspect
.
name
}
</a></span>"
str
=
"<span class='aspect_badge single'><a href=
#{
aspect_path
(
aspect
)
}
>
#{
aspect
.
name
}
</a></span>"
end
str
.
html_safe
end
def
aspect_links
aspects
,
opts
=
{}
str
=
""
aspects
.
each
do
|
a
|
...
...
@@ -46,15 +49,16 @@ module ApplicationHelper
end
str
.
html_safe
end
def
aspect_li
aspect
,
opts
=
{}
param_string
=
""
if
opts
.
size
>
0
param_string
<<
'?'
opts
.
each_pair
do
|
k
,
v
|
opts
.
each_pair
do
|
k
,
v
|
param_string
<<
"
#{
k
}
=
#{
v
}
"
end
end
"<li>
"<li>
<a href='/aspects/
#{
aspect
.
id
}#{
param_string
}
'>
#{
aspect
.
name
}
</a>
...
...
@@ -98,12 +102,12 @@ module ApplicationHelper
def
person_url
(
person
)
case
person
.
class
.
to_s
when
"User"
user_path
(
person
)
when
"Person"
person_path
(
person
)
else
I18n
.
t
(
'application.helper.unknown_person'
)
when
"User"
user_path
(
person
)
when
"Person"
person_path
(
person
)
else
I18n
.
t
(
'application.helper.unknown_person'
)
end
end
...
...
@@ -124,28 +128,28 @@ module ApplicationHelper
end
def
person_link
(
person
)
"<a href='/people/
#{
person
.
id
}
'>
"<a href='/people/
#{
person
.
id
}
'>
#{
person
.
name
}
</a>"
.
html_safe
end
def
image_or_default
(
person
,
size
=
:thumb_large
)
image_location
=
person
.
profile
.
image_url
(
size
)
if
person
.
profile
image_location
||=
person
.
profile
.
image_url
(
:thumb_large
)
if
person
.
profile
#backwards compatability for old profile pictures
image_location
||=
person
.
profile
.
image_url
(
:thumb_large
)
if
person
.
profile
#backwards compatability for old profile pictures
image_location
||=
"/images/user/default.png"
image_location
end
def
hard_link
(
string
,
path
)
link_to
string
,
path
,
:rel
=>
'external'
link_to
string
,
path
,
:rel
=>
'external'
end
def
person_image_link
(
person
,
opts
=
{})
return
""
if
person
.
nil?
if
opts
[
:to
]
==
:photos
link_to
person_image_tag
(
person
,
opts
[
:size
]),
person_photos_path
(
person
)
link_to
person_image_tag
(
person
,
opts
[
:size
]),
person_photos_path
(
person
)
else
"<a href='/people/
#{
person
.
id
}
'>
"<a href='/people/
#{
person
.
id
}
'>
#{
person_image_tag
(
person
)
}
</a>"
.
html_safe
end
...
...
@@ -158,83 +162,93 @@ module ApplicationHelper
def
person_photos_path
person
person_id
=
person
.
id
if
person
.
respond_to?
(
:id
)
person_id
||=
person
"
#{
photos_path
}
?person_id=
#{
person_id
}
"
end
def
markdownify
(
message
,
options
=
{})
message
=
h
(
message
).
html_safe
[
:autolinks
,
:youtube
,
:emphasis
,
:links
,
:newlines
].
each
do
|
k
|
if
!
options
.
has_key?
(
k
)
options
[
k
]
=
true
end
if
!
options
.
has_key?
(
:newlines
)
options
[
:newlines
]
=
true
end
if
options
[
:links
]
message
.
gsub!
(
/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\)/
)
do
|
m
|
escape
=
(
options
[
:emphasis
])
?
"
\\
"
:
""
res
=
"<a target=
\"
#{
escape
}
_blank
\"
href=
\"
#{
$2
}
\"
title=
\"
#{
$3
}
\"
>
#{
$1
}
</a>"
res
end
message
.
gsub!
(
/\[([^\[]+)\]\(([^ ]+)\)/
)
do
|
m
|
escape
=
(
options
[
:emphasis
])
?
"
\\
"
:
""
res
=
"<a target=
\"
#{
escape
}
_blank
\"
href=
\"
#{
$2
}
\"
>
#{
$1
}
</a>"
res
end
message
=
process_links
(
message
)
message
=
process_youtube
(
message
)
message
=
process_autolinks
(
message
)
message
=
process_emphasis
(
message
)
message
=
process_youtube_again
(
message
,
options
[
:youtube_maps
])
if
options
[
:newlines
]
message
.
gsub!
(
/\n+/
,
'<br />'
)
end
if
options
[
:youtube
]
message
.
gsub!
(
/( |^)(http:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/
)
do
|
m
|
res
=
"
#{
$1
}
youtube.com::
#{
$3
}
"
res
.
gsub!
(
/(\*|_)/
)
{
|
m
|
"
\\
#{
$1
}
"
}
if
options
[
:emphasis
]
res
end
return
message
end
def
process_links
(
message
)
message
.
gsub!
(
/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\)/
)
do
|
m
|
escape
=
"
\\
"
res
=
"<a target=
\"
#{
escape
}
_blank
\"
href=
\"
#{
$2
}
\"
title=
\"
#{
$3
}
\"
>
#{
$1
}
</a>"
res
end
message
.
gsub!
(
/\[([^\[]+)\]\(([^ ]+)\)/
)
do
|
m
|
escape
=
"
\\
"
res
=
"<a target=
\"
#{
escape
}
_blank
\"
href=
\"
#{
$2
}
\"
>
#{
$1
}
</a>"
res
end
if
options
[
:autolinks
]
message
.
gsub!
(
/( |^)(www\.[^\s]+\.[^\s])/
,
'\1http://\2'
)
message
.
gsub!
(
/(<a target="\\?_blank" href=")?(https|http|ftp):\/\/([^\s]+)/
)
do
|
m
|
if
!
$1
.
nil?
m
else
res
=
%{<a target="_blank" href="
#{
$2
}
://
#{
$3
}
">
#{
$3
}
</a>}
res
.
gsub!
(
/(\*|_)/
)
{
|
m
|
"
\\
#{
$1
}
"
}
if
options
[
:emphasis
]
res
end
end
return
message
end
def
process_youtube
(
message
)
message
.
gsub!
(
/( |^)(http:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/
)
do
|
m
|
res
=
"
#{
$1
}
youtube.com::
#{
$3
}
"
res
.
gsub!
(
/(\*|_)/
)
{
|
m
|
"
\\
#{
$1
}
"
}
res
end
return
message
end
if
options
[
:emphasis
]
message
.
gsub!
(
"
\\
**"
,
"-^doublestar^-"
)
message
.
gsub!
(
"
\\
__"
,
"-^doublescore^-"
)
message
.
gsub!
(
"
\\
*"
,
"-^star^-"
)
message
.
gsub!
(
"
\\
_"
,
"-^score^-"
)
message
.
gsub!
(
/(\*\*\*|___)(.+?)\1/m
,
'<em><strong>\2</strong></em>'
)
message
.
gsub!
(
/(\*\*|__)(.+?)\1/m
,
'<strong>\2</strong>'
)
message
.
gsub!
(
/(\*|_)(.+?)\1/m
,
'<em>\2</em>'
)
message
.
gsub!
(
"-^doublestar^-"
,
"**"
)
message
.
gsub!
(
"-^doublescore^-"
,
"__"
)
message
.
gsub!
(
"-^star^-"
,
"*"
)
message
.
gsub!
(
"-^score^-"
,
"_"
)
end
if
options
[
:youtube
]
while
youtube
=
message
.
match
(
/youtube\.com::([A-Za-z0-9_\\\-]+)/
)
video_id
=
youtube
[
1
]
if
options
[
:youtube_maps
]
&&
options
[
:youtube_maps
][
video_id
]
title
=
options
[
:youtube_maps
][
video_id
]
else
title
=
I18n
.
t
'application.helper.youtube_title.unknown'
end
message
.
gsub!
(
'youtube.com::'
+
video_id
,
'<a class="video-link" data-host="youtube.com" data-video-id="'
+
video_id
+
'" href="#video">Youtube: '
+
title
+
'</a>'
)
def
process_autolinks
(
message
)
message
.
gsub!
(
/( |^)(www\.[^\s]+\.[^\s])/
,
'\1http://\2'
)
message
.
gsub!
(
/(<a target="\\?_blank" href=")?(https|http|ftp):\/\/([^\s]+)/
)
do
|
m
|
if
!
$1
.
nil?
m
else
res
=
%{<a target="_blank" href="
#{
$2
}
://
#{
$3
}
">
#{
$3
}
</a>}
res
.
gsub!
(
/(\*|_)/
)
{
|
m
|
"
\\
#{
$1
}
"
}
res
end
end
return
message
end
if
options
[
:newlines
]
message
.
gsub!
(
/\n+/
,
'<br />'
)
end
def
process_emphasis
(
message
)
message
.
gsub!
(
"
\\
**"
,
"-^doublestar^-"
)
message
.
gsub!
(
"
\\
__"
,
"-^doublescore^-"
)
message
.
gsub!
(
"
\\
*"
,
"-^star^-"
)
message
.
gsub!
(
"
\\
_"
,
"-^score^-"
)
message
.
gsub!
(
/(\*\*\*|___)(.+?)\1/m
,
'<em><strong>\2</strong></em>'
)
message
.
gsub!
(
/(\*\*|__)(.+?)\1/m
,
'<strong>\2</strong>'
)
message
.
gsub!
(
/(\*|_)(.+?)\1/m
,
'<em>\2</em>'
)
message
.
gsub!
(
"-^doublestar^-"
,
"**"
)
message
.
gsub!
(
"-^doublescore^-"
,
"__"
)
message
.
gsub!
(
"-^star^-"
,
"*"
)
message
.
gsub!
(
"-^score^-"
,
"_"
)
return
message
end
def
process_youtube_again
(
message
,
youtube_maps
)
while
youtube
=
message
.
match
(
/youtube\.com::([A-Za-z0-9_\\\-]+)/
)
video_id
=
youtube
[
1
]
if
youtube_maps
&&
youtube_maps
[
video_id
]
title
=
youtube_maps
[
video_id
]
else
title
=
I18n
.
t
'application.helper.youtube_title.unknown'
end
message
.
gsub!
(
'youtube.com::'
+
video_id
,
'<a class="video-link" data-host="youtube.com" data-video-id="'
+
video_id
+
'" href="#video">Youtube: '
+
title
+
'</a>'
)
end
return
message
end
...
...
Ce diff est replié.
Cliquez pour l'agrandir.
spec/helpers/application_helper_spec.rb
+
21
−
45
Voir le fichier @
c4a7745c
...
...
@@ -72,24 +72,22 @@ describe ApplicationHelper do
markdownify
(
proto
+
"://"
+
url
).
should
==
"<a target=
\"
_blank
\"
href=
\"
"
+
proto
+
"://"
+
url
+
"
\"
>"
+
url
+
"</a>"
end
it
"recognizes youtube links"
do
proto
=
"http"
videoid
=
"0x__dDWdf23"
url
=
"www.youtube.com/watch?v="
+
videoid
+
"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
title
=
"UP & down & UP & down &"
res
=
markdownify
(
proto
+
'://'
+
url
)
res
.
should
=~
/data-host="youtube.com"/
res
.
should
=~
/data-video-id="
#{
videoid
}
"/
end
it
"recognizes youtube links with hyphens"
do
proto
=
"http"
videoid
=
"ABYnqp-bxvg"
url
=
"www.youtube.com/watch?v="
+
videoid
+
"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
title
=
"UP & down & UP & down &"
res
=
markdownify
(
proto
+
'://'
+
url
)
res
.
should
=~
/data-host="youtube.com"/
res
.
should
=~
/data-video-id="
#{
videoid
}
"/
describe
"video links"
do
it
"recognizes youtube links"
do
video_id
=
"0x__dDWdf23"
url
=
"http://www.youtube.com/watch?v="
+
video_id
+
"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
res
=
markdownify
(
url
)
res
.
should
=~
/data-host="youtube.com"/
res
.
should
=~
/data-video-id="
#{
video_id
}
"/
end
it
"recognizes youtube links with hyphens"
do
video_id
=
"ABYnqp-bxvg"
url
=
"http://www.youtube.com/watch?v="
+
video_id
+
"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
res
=
markdownify
(
url
)
res
.
should
=~
/data-host="youtube.com"/
res
.
should
=~
/data-video-id="
#{
video_id
}
"/
end
end
it
"recognizes multiple links of different types"
do
...
...
@@ -174,35 +172,13 @@ describe ApplicationHelper do
markdownify
(
message
).
should
==
"<em>some text</em> *some text<em> **some text</em> <em>some text</em> _some text<em> __some text</em>"
end
describe
"options"
do
before
do
@message
=
"http://url.com www.url.com www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related *emphasis* __emphasis__ [link](www.url.com) [link](url.com
\"
title
\"
)"
end
it
"can render only autolinks"
do
res
=
markdownify
(
@message
,
:youtube
=>
false
,
:emphasis
=>
false
,
:links
=>
false
)
res
.
should
==
"<a target=
\"
_blank
\"
href=
\"
http://url.com
\"
>url.com</a> <a target=
\"
_blank
\"
href=
\"
http://www.url.com
\"
>www.url.com</a> <a target=
\"
_blank
\"
href=
\"
http://www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related
\"
>www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related</a> *emphasis* __emphasis__ [link](www.url.com) [link](url.com "title")"
end
it
"can render only youtube"
do
res
=
markdownify
(
@message
,
:autolinks
=>
false
,
:emphasis
=>
false
,
:links
=>
false
)
res
.
should_not
=~
/a href="http:\/\/url.com"/
res
.
should_not
=~
/a href="http:\/\/www.url.com"/
res
.
should_not
=~
/<strong>emphasis<\/strong>/
end
it
"can render only emphasis tags"
do
res
=
markdownify
(
@message
,
:autolinks
=>
false
,
:youtube
=>
false
,
:links
=>
false
)
res
.
should
==
"http://url.com www.url.com www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related <em>emphasis</em> <strong>emphasis</strong> [link](www.url.com) [link](url.com "title")"
end
it
"can render only links tags"
do
res
=
markdownify
(
@message
,
:autolinks
=>
false
,
:youtube
=>
false
,
:emphasis
=>
false
)
res
.
should
==
"http://url.com www.url.com www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related *emphasis* __emphasis__ <a target=
\"
_blank
\"
href=
\"
www.url.com
\"
>link</a> <a target=
\"
_blank
\"
href=
\"
url.com
\"
title=
\"
title
\"
>link</a>"
describe
"newlines"
do
it
'skips inserting newlines if you pass the newlines option'
do
message
=
"These
\n
are
\n\s
ome
\n
new
\l
ines"
res
=
markdownify
(
message
,
:newlines
=>
false
)
res
.
should
==
message
end
end
describe
"newlines"
do
it
'generates breaklines'
do
message
=
"These
\n
are
\n
some
\n
new
\n
lines"
res
=
markdownify
(
message
)
...
...
Ce diff est replié.
Cliquez pour l'agrandir.
Aperçu
0%
Chargement en cours
Veuillez réessayer
ou
joindre un nouveau fichier
.
Annuler
You are about to add
0
people
to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Enregistrer le commentaire
Annuler
Veuillez vous
inscrire
ou vous
se connecter
pour commenter