Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
pleroma
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
370
Issues
370
List
Boards
Labels
Service Desk
Milestones
Merge Requests
54
Merge Requests
54
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Pleroma
pleroma
Commits
27b9605d
Commit
27b9605d
authored
Jun 23, 2019
by
kaniini
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'rich_media_titles_two' into 'develop'
Rich media titles two See merge request
!1325
parents
8b7860f1
0e415921
Pipeline
#13730
passed with stages
in 44 minutes and 13 seconds
Changes
4
Pipelines
1
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
1551 additions
and
8 deletions
+1551
-8
lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex
lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex
+27
-8
test/fixtures/rich_media/non_ogp_embed.html
test/fixtures/rich_media/non_ogp_embed.html
+1479
-0
test/fixtures/rich_media/ogp-missing-title.html
test/fixtures/rich_media/ogp-missing-title.html
+12
-0
test/web/rich_media/parser_test.exs
test/web/rich_media/parser_test.exs
+33
-0
No files found.
lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex
View file @
27b9605d
defmodule
Pleroma
.
Web
.
RichMedia
.
Parsers
.
MetaTagsParser
do
def
parse
(
html
,
data
,
prefix
,
error_message
,
key_name
,
value_name
\\
"content"
)
do
with
elements
=
[
_
|
_
]
<-
get_elements
(
html
,
key_name
,
prefix
),
meta_data
=
Enum
.
reduce
(
elements
,
data
,
fn
el
,
acc
->
attributes
=
normalize_attributes
(
el
,
prefix
,
key_name
,
value_name
)
meta_data
=
html
|>
get_elements
(
key_name
,
prefix
)
|>
Enum
.
reduce
(
data
,
fn
el
,
acc
->
attributes
=
normalize_attributes
(
el
,
prefix
,
key_name
,
value_name
)
Map
.
merge
(
acc
,
attributes
)
end
)
do
{
:ok
,
meta_data
}
Map
.
merge
(
acc
,
attributes
)
end
)
|>
maybe_put_title
(
html
)
if
Enum
.
empty?
(
meta_data
)
do
{
:error
,
error_message
}
else
_e
->
{
:error
,
error_message
}
{
:ok
,
meta_data
}
end
end
...
...
@@ -27,4 +31,19 @@ defp normalize_attributes(html_node, prefix, key_name, value_name) do
%{
String
.
to_atom
(
data
[
key_name
])
=>
data
[
value_name
]}
end
defp
maybe_put_title
(%{
title:
_
}
=
meta
,
_
),
do
:
meta
defp
maybe_put_title
(
meta
,
html
)
when
meta
!=
%{}
do
case
get_page_title
(
html
)
do
""
->
meta
title
->
Map
.
put_new
(
meta
,
:title
,
title
)
end
end
defp
maybe_put_title
(
meta
,
_
),
do
:
meta
defp
get_page_title
(
html
)
do
Floki
.
find
(
html
,
"title"
)
|>
Floki
.
text
()
end
end
test/fixtures/rich_media/non_ogp_embed.html
0 → 100644
View file @
27b9605d
This diff is collapsed.
Click to expand it.
test/fixtures/rich_media/ogp-missing-title.html
0 → 100644
View file @
27b9605d
<html
prefix=
"og: http://ogp.me/ns#"
>
<head>
<title>
The Rock (1996)
</title>
<meta
property=
"og:type"
content=
"video.movie"
/>
<meta
property=
"og:url"
content=
"http://www.imdb.com/title/tt0117500/"
/>
<meta
property=
"og:image"
content=
"http://ia.media-imdb.com/images/rock.jpg"
/>
<meta
property=
"og:description"
content=
"Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
>
</head>
</html>
test/web/rich_media/parser_test.exs
View file @
27b9605d
...
...
@@ -9,6 +9,21 @@ defmodule Pleroma.Web.RichMedia.ParserTest do
}
->
%
Tesla
.
Env
{
status:
200
,
body:
File
.
read!
(
"test/fixtures/rich_media/ogp.html"
)}
%{
method:
:get
,
url:
"http://example.com/non-ogp"
}
->
%
Tesla
.
Env
{
status:
200
,
body:
File
.
read!
(
"test/fixtures/rich_media/non_ogp_embed.html"
)}
%{
method:
:get
,
url:
"http://example.com/ogp-missing-title"
}
->
%
Tesla
.
Env
{
status:
200
,
body:
File
.
read!
(
"test/fixtures/rich_media/ogp-missing-title.html"
)
}
%{
method:
:get
,
url:
"http://example.com/twitter-card"
...
...
@@ -38,6 +53,11 @@ test "returns error when no metadata present" do
assert
{
:error
,
_
}
=
Pleroma
.
Web
.
RichMedia
.
Parser
.
parse
(
"http://example.com/empty"
)
end
test
"doesn't just add a title"
do
assert
Pleroma
.
Web
.
RichMedia
.
Parser
.
parse
(
"http://example.com/non-ogp"
)
==
{
:error
,
"Found metadata was invalid or incomplete: %{}"
}
end
test
"parses ogp"
do
assert
Pleroma
.
Web
.
RichMedia
.
Parser
.
parse
(
"http://example.com/ogp"
)
==
{
:ok
,
...
...
@@ -51,6 +71,19 @@ test "parses ogp" do
}}
end
test
"falls back to <title> when ogp:title is missing"
do
assert
Pleroma
.
Web
.
RichMedia
.
Parser
.
parse
(
"http://example.com/ogp-missing-title"
)
==
{
:ok
,
%{
image:
"http://ia.media-imdb.com/images/rock.jpg"
,
title:
"The Rock (1996)"
,
description:
"Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
,
type:
"video.movie"
,
url:
"http://www.imdb.com/title/tt0117500/"
}}
end
test
"parses twitter card"
do
assert
Pleroma
.
Web
.
RichMedia
.
Parser
.
parse
(
"http://example.com/twitter-card"
)
==
{
:ok
,
...
...
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