Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Pleroma
pleroma
Commits
dcfd494e
Commit
dcfd494e
authored
May 17, 2017
by
lain
Browse files
Add Formatter.
parent
70024632
Changes
2
Hide whitespace changes
Inline
Side-by-side
lib/pleroma/formatter.ex
0 → 100644
View file @
dcfd494e
defmodule
Pleroma
.
Formatter
do
@link_regex
~r/https?:\/
\
/
[\
w
\
.
\
/
?=
\
-
#]+[\w]/
def
linkify
(
text
)
do
Regex
.
replace
(
@link_regex
,
text
,
"<a href='
\\
0'>
\\
0</a>"
)
end
@tag_regex
~r/\#\w+/u
def
parse_tags
(
text
)
do
Regex
.
scan
(
@tag_regex
,
text
)
|>
Enum
.
map
(
fn
([
"#"
<>
tag
=
full_tag
])
->
{
full_tag
,
tag
}
end
)
end
end
test/formatter_test.exs
0 → 100644
View file @
dcfd494e
defmodule
Pleroma
.
FormatterTest
do
alias
Pleroma
.
Formatter
use
Pleroma
.
DataCase
describe
".linkify"
do
test
"turning urls into links"
do
text
=
"Hey, check out https://www.youtube.com/watch?v=8Zg1-TufFzY."
expected
=
"Hey, check out <a href='https://www.youtube.com/watch?v=8Zg1-TufFzY'>https://www.youtube.com/watch?v=8Zg1-TufFzY</a>."
assert
Formatter
.
linkify
(
text
)
==
expected
end
end
describe
".parse_tags"
do
test
"parses tags in the text"
do
text
=
"Here's a #test. Maybe these are #working or not. What about #漢字? And #は。"
expected
=
[
{
"#test"
,
"test"
},
{
"#working"
,
"working"
},
{
"#漢字"
,
"漢字"
},
{
"#は"
,
"は"
}
]
assert
Formatter
.
parse_tags
(
text
)
==
expected
end
end
end
Write
Preview
Supports
Markdown
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