Extract more metadata from HTML

And don't overwrite existing metadata properties
This commit is contained in:
Basti 2018-07-02 11:54:38 -05:00
parent 39ae311acb
commit 08bdd0a18c
2 changed files with 39 additions and 7 deletions

View File

@ -48,15 +48,44 @@ module Manifique
private
def parse_metadata_from_html
parse_title_from_html
parse_meta_elements_from_html
parse_display_mode_from_html
end
def parse_title_from_html
return if @metadata.name
if title = @html.at_css("title") and !title.text.empty?
@metadata.name = title.text
@metadata.from_html.push "name"
end
# TODO extract meta element parsing to seperate method
if desc = @html.at_css("meta[name=description]") and
!desc.attributes["content"].value.empty?
@metadata.description = desc.attributes["content"].value
@metadata.from_html.push "description"
end
def parse_meta_elements_from_html
{
description: "description",
theme_color: "theme-color"
}.each do |prop, name|
next if @metadata.send("#{prop}")
if value = get_meta_element_value(name)
@metadata.send "#{prop}=", value
@metadata.from_html.push prop.to_s
end
end
end
def parse_display_mode_from_html
return if @metadata.display
if get_meta_element_value("apple-mobile-web-app-capable") == "yes"
@metadata.display = "standalone"
@metadata.from_html.push "display"
end
end
def get_meta_element_value(name)
if el = @html.at_css("meta[name=#{name}]") and !el.attributes["content"].value.empty?
el.attributes["content"].value
end
end

View File

@ -164,11 +164,14 @@ RSpec.describe Manifique::WebClient do
expect(subject).to be_kind_of(Manifique::Metadata)
expect(subject.name).to eq("kosmos.social")
expect(subject.description).to eq("A friendly place for tooting")
expect(subject.theme_color).to eq("#282c37")
expect(subject.display).to eq("standalone")
end
it "knows which properties were loaded from HTML" do
expect(subject.from_html).to include("name")
expect(subject.from_html).to include("description")
%w{ name description theme_color display }.each do |property|
expect(subject.from_html).to include(property)
end
end
end
end