Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: case_sensitive option for highlight and sanitize filter #81

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/filter/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub struct HighlightConfig {
#[serde(flatten)]
keywords: KeywordsOrPatterns,
/// Background color to use for highlighting. Default is yellow.
#[serde(default)]
bg_color: Option<String>,
#[serde(default)]
case_sensitive: Option<bool>,
}

#[derive(
Expand Down Expand Up @@ -55,7 +58,8 @@ impl FeedFilterConfig for HighlightConfig {
async fn build(self) -> Result<Self::Filter, ConfigError> {
let patterns = self.keywords.into_patterns()?;
let bg_color = self.bg_color.unwrap_or_else(|| "#ffff00".into());
Highlight::new(&patterns, bg_color)
let case_sensitive = self.case_sensitive.unwrap_or(false);
Highlight::new(&patterns, bg_color, case_sensitive)
}
}

Expand Down Expand Up @@ -104,13 +108,18 @@ impl Highlight {
fn new<T: AsRef<str>>(
patterns: &[T],
bg_color: String,
case_sensitive: bool,
) -> Result<Self, ConfigError> {
let regexset = RegexSetBuilder::new(patterns)
.case_insensitive(true)
.case_insensitive(!case_sensitive)
.build()?;
let patterns = patterns
.iter()
.map(|p| RegexBuilder::new(p.as_ref()).case_insensitive(true).build())
.map(|p| {
RegexBuilder::new(p.as_ref())
.case_insensitive(!case_sensitive)
.build()
})
.collect::<Result<Vec<Regex>, _>>()?;

Ok(Self {
Expand Down Expand Up @@ -241,7 +250,7 @@ mod test {
#[test]
fn test_highlighting() {
let keywords = vec!["foo", "bar"];
let highlight = Highlight::new(&keywords, "#ffff00".into())
let highlight = Highlight::new(&keywords, "#ffff00".into(), false)
.expect("failed to build highlighter");

let html = r#"<html><p class="foo">FOO<div><!-- bar -->foo<br> bar</div></p></html>
Expand Down Expand Up @@ -274,6 +283,7 @@ highlight:
keywords: vec!["foo".into(), "bar".into()],
},
bg_color: Some("#ffff00".into()),
case_sensitive: None,
},
);

Expand All @@ -289,6 +299,7 @@ highlight:
patterns: vec![r"\bfoo\b".into()],
},
bg_color: Some("#ffff00".into()),
case_sensitive: None,
},
);
}
Expand Down
33 changes: 24 additions & 9 deletions src/filter/sanitize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;

use regex::Regex;
use regex::{Regex, RegexBuilder};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand All @@ -12,9 +12,11 @@ use super::{FeedFilter, FeedFilterConfig, FilterContext};
#[derive(
JsonSchema, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash,
)]
struct SanitizeOpReplaceConfig {
struct ReplaceConfig {
from: String,
to: String,
#[serde(default)]
case_sensitive: Option<bool>,
}

#[derive(
Expand All @@ -26,9 +28,9 @@ pub struct SanitizeOpConfig {
/// Remove all matches of the regex
remove_regex: Option<String>,
/// Replace all occurrences of the string
replace: Option<SanitizeOpReplaceConfig>,
replace: Option<ReplaceConfig>,
/// Replace all matches of the regex
replace_regex: Option<SanitizeOpReplaceConfig>,
replace_regex: Option<ReplaceConfig>,
}

impl SanitizeOpConfig {
Expand All @@ -48,7 +50,16 @@ impl SanitizeOpConfig {

macro_rules! parse_regex {
($regex:expr) => {
Regex::new(&$regex).map_err(ConfigError::from)?
RegexBuilder::new(&$regex)
.case_insensitive(true)
.build()
.map_err(ConfigError::from)?
};
($regex:expr, $cs:expr) => {
RegexBuilder::new(&$regex)
.case_insensitive($cs.unwrap_or(true))
.build()
.map_err(ConfigError::from)?
};
}

Expand All @@ -62,13 +73,15 @@ impl SanitizeOpConfig {
}

if let Some(text) = self.replace {
let from = parse_regex!(regex::escape(&text.from));
let case_sensitive = text.case_sensitive;
let from = parse_regex!(regex::escape(&text.from), case_sensitive);
let to = text.to;
return Ok(SanitizeOp::Replace(from, to));
}

if let Some(repl) = self.replace_regex {
let from = parse_regex!(repl.from);
let case_sensitive = repl.case_sensitive;
let from = parse_regex!(repl.from, case_sensitive);
let to = repl.to;
return Ok(SanitizeOp::Replace(from, to));
}
Expand Down Expand Up @@ -182,19 +195,21 @@ mod test {
SanitizeOpConfig {
remove: None,
remove_regex: None,
replace: Some(SanitizeOpReplaceConfig {
replace: Some(ReplaceConfig {
from: "bar".into(),
to: "baz".into(),
case_sensitive: None,
}),
replace_regex: None,
},
SanitizeOpConfig {
remove: None,
remove_regex: None,
replace: None,
replace_regex: Some(SanitizeOpReplaceConfig {
replace_regex: Some(ReplaceConfig {
from: r"\w+".into(),
to: "qux".into(),
case_sensitive: None,
}),
},
],
Expand Down
Loading