0.4.2: ignore logos with search / menu in ancestor tree

This commit is contained in:
Sam Denty 2022-12-25 21:10:15 +00:00
parent b460b4415a
commit d95824a11d
No known key found for this signature in database
GPG key ID: 7B4EAF7B9E291B79
3 changed files with 35 additions and 16 deletions

2
Cargo.lock generated
View file

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

View file

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

View file

@ -187,25 +187,44 @@ impl Icons {
"img[alt*=logo], svg[alt*=logo]", "img[alt*=logo], svg[alt*=logo]",
"img[class*=logo], svg[class*=logo]", "img[class*=logo], svg[class*=logo]",
)) ))
.map(|elem_ref| { .filter_map(|elem_ref| {
let elem = elem_ref.value(); let mut ancestors = elem_ref
let mut weight = 0;
// if in the header
if elem_ref
.ancestors() .ancestors()
.map(ElementRef::wrap) .map(ElementRef::wrap)
.flatten() .flatten()
.any(|element| element.value().name() == "header") .map(|elem_ref| elem_ref.value());
{
let skip_classnames = regex!("menu|search");
let should_skip = ancestors.any(|ancestor| {
ancestor
.attr("class")
.map(|attr| skip_classnames.is_match(&attr.to_lowercase()))
.or_else(|| {
ancestor
.attr("id")
.map(|attr| skip_classnames.is_match(&attr.to_lowercase()))
})
.unwrap_or(false)
});
if should_skip {
return None;
}
let mut weight = 0;
// if in the header
if ancestors.any(|element| element.name() == "header") {
weight += 2; weight += 2;
} }
let mentions_logo = |attr_name| { let mut mentions_logo = |attr_name| {
elem ancestors.any(|ancestor| {
ancestor
.attr(attr_name) .attr(attr_name)
.map(|attr| regex!("logo([^s]|$)").is_match(&attr.to_lowercase())) .map(|attr| regex!("logo([^s]|$)").is_match(&attr.to_lowercase()))
.unwrap_or(false) .unwrap_or(false)
})
}; };
if mentions_logo("class") || mentions_logo("id") { if mentions_logo("class") || mentions_logo("id") {
weight += 3; weight += 3;
@ -217,7 +236,7 @@ impl Icons {
weight += 1; weight += 1;
} }
(elem_ref, weight) Some((elem_ref, weight))
}) })
.collect(); .collect();