0.4.3: fix bug & increase SiteLogo weight for first match

This commit is contained in:
Sam Denty 2022-12-26 15:15:01 +00:00
parent d95824a11d
commit 76e84100a4
No known key found for this signature in database
GPG key ID: 7B4EAF7B9E291B79
3 changed files with 15 additions and 9 deletions

2
Cargo.lock generated
View file

@ -1638,7 +1638,7 @@ checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de"
[[package]]
name = "site_icons"
version = "0.4.2"
version = "0.4.3"
dependencies = [
"byteorder",
"clap",

View file

@ -1,6 +1,6 @@
[package]
name = "site_icons"
version = "0.4.2"
version = "0.4.3"
authors = ["Sam Denty <sam@samdenty.com>"]
edition = "2018"
license = "GPL-3.0"

View file

@ -187,15 +187,17 @@ impl Icons {
"img[alt*=logo], svg[alt*=logo]",
"img[class*=logo], svg[class*=logo]",
))
.filter_map(|elem_ref| {
let mut ancestors = elem_ref
.enumerate()
.filter_map(|(i, elem_ref)| {
let ancestors = elem_ref
.ancestors()
.map(ElementRef::wrap)
.flatten()
.map(|elem_ref| elem_ref.value());
.map(|elem_ref| elem_ref.value())
.collect::<Vec<_>>();
let skip_classnames = regex!("menu|search");
let should_skip = ancestors.any(|ancestor| {
let should_skip = ancestors.iter().any(|ancestor| {
ancestor
.attr("class")
.map(|attr| skip_classnames.is_match(&attr.to_lowercase()))
@ -214,12 +216,16 @@ impl Icons {
let mut weight = 0;
// if in the header
if ancestors.any(|element| element.name() == "header") {
if ancestors.iter().any(|element| element.name() == "header") {
weight += 2;
}
let mut mentions_logo = |attr_name| {
ancestors.any(|ancestor| {
if i == 0 {
weight += 1;
}
let mentions_logo = |attr_name| {
ancestors.iter().any(|ancestor| {
ancestor
.attr(attr_name)
.map(|attr| regex!("logo([^s]|$)").is_match(&attr.to_lowercase()))