diff --git a/lib/manifique/metadata.rb b/lib/manifique/metadata.rb
index 5757628..b326230 100644
--- a/lib/manifique/metadata.rb
+++ b/lib/manifique/metadata.rb
@@ -10,15 +10,16 @@ module Manifique
self.url = data[:url]
self.from_web_manifest = []
self.from_html = []
+ self.icons = []
end
def load_from_web_manifest(manifest)
- [:name, :short_name, :description, :icons,
- :theme_color, :background_color, :display,
- :start_url, :scope, :share_target].map(&:to_s).each do |prop|
+ [ :name, :short_name, :description, :icons,
+ :theme_color, :background_color, :display,
+ :start_url, :scope, :share_target ].map(&:to_s).each do |prop|
self.send("#{prop}=", manifest[prop]) if manifest[prop]
self.from_web_manifest.push(prop)
- end
+ end
end
def to_json
diff --git a/lib/manifique/web_client.rb b/lib/manifique/web_client.rb
index f8b9156..5b6f72a 100644
--- a/lib/manifique/web_client.rb
+++ b/lib/manifique/web_client.rb
@@ -16,6 +16,7 @@ module Manifique
def fetch_metadata
fetch_website
+
if manifest = fetch_web_manifest
@metadata.load_from_web_manifest(manifest)
else
@@ -51,6 +52,7 @@ module Manifique
parse_title_from_html
parse_meta_elements_from_html
parse_display_mode_from_html
+ parse_icons_from_html
end
def parse_title_from_html
@@ -89,6 +91,27 @@ module Manifique
end
end
+ def parse_icons_from_html
+ icon_links = @html.css("link[rel=icon]")
+ if icon_links.any?
+ icon_links.each do |link|
+ icon = {}
+ icon["src"] = link.attributes["href"].value rescue nil
+ next if icon["src"].to_s.empty?
+ icon["sizes"] = link.attributes["sizes"].value rescue nil
+ icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
+ @metadata.icons.push icon
+ end
+ end
+
+ @metadata.from_html.push "icons" unless @metadata.icons.empty?
+ end
+
+ def get_icon_type(src)
+ extension = src.match(/\.([a-zA-Z]+)$/)[1]
+ return "image/#{extension}"
+ end
+
def do_get_request(url)
conn = Faraday.new do |b|
b.use FaradayMiddleware::FollowRedirects
diff --git a/spec/fixtures/mastodon-no-manifest.html b/spec/fixtures/mastodon-no-manifest.html
index 37be55d..fa7d0a5 100644
--- a/spec/fixtures/mastodon-no-manifest.html
+++ b/spec/fixtures/mastodon-no-manifest.html
@@ -21,6 +21,11 @@
+
+
+
+
+
diff --git a/spec/manifique/web_client_spec.rb b/spec/manifique/web_client_spec.rb
index 665c7fb..e62ab3a 100644
--- a/spec/manifique/web_client_spec.rb
+++ b/spec/manifique/web_client_spec.rb
@@ -166,10 +166,14 @@ RSpec.describe Manifique::WebClient do
expect(subject.description).to eq("A friendly place for tooting")
expect(subject.theme_color).to eq("#282c37")
expect(subject.display).to eq("standalone")
+
+ png_icons = subject.icons.select{|i| i["type"] == "image/png"}
+ expect(png_icons.length).to eq(5)
+ expect(subject.icons.find{|i| i["sizes"] == "512x512"}["src"]).to eq( "/application_icon_x512.png")
end
it "knows which properties were loaded from HTML" do
- %w{ name description theme_color display }.each do |property|
+ %w{ name description theme_color display icons }.each do |property|
expect(subject.from_html).to include(property)
end
end