diff --git a/Cargo.lock b/Cargo.lock index 01db9dc..f5ae5a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1638,7 +1638,7 @@ checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" [[package]] name = "site_icons" -version = "0.4.1" +version = "0.4.2" dependencies = [ "byteorder", "clap", diff --git a/Cargo.toml b/Cargo.toml index f05cd43..fec9568 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "site_icons" -version = "0.4.1" +version = "0.4.2" authors = ["Sam Denty "] edition = "2018" license = "GPL-3.0" diff --git a/src/icons.rs b/src/icons.rs index 642d90b..10c75b6 100644 --- a/src/icons.rs +++ b/src/icons.rs @@ -187,25 +187,44 @@ impl Icons { "img[alt*=logo], svg[alt*=logo]", "img[class*=logo], svg[class*=logo]", )) - .map(|elem_ref| { - let elem = elem_ref.value(); - let mut weight = 0; - - // if in the header - if elem_ref + .filter_map(|elem_ref| { + let mut ancestors = elem_ref .ancestors() .map(ElementRef::wrap) .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; } - let mentions_logo = |attr_name| { - elem - .attr(attr_name) - .map(|attr| regex!("logo([^s]|$)").is_match(&attr.to_lowercase())) - .unwrap_or(false) + let mut mentions_logo = |attr_name| { + ancestors.any(|ancestor| { + ancestor + .attr(attr_name) + .map(|attr| regex!("logo([^s]|$)").is_match(&attr.to_lowercase())) + .unwrap_or(false) + }) }; if mentions_logo("class") || mentions_logo("id") { weight += 3; @@ -217,7 +236,7 @@ impl Icons { weight += 1; } - (elem_ref, weight) + Some((elem_ref, weight)) }) .collect();