-
Notifications
You must be signed in to change notification settings - Fork 984
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
Implement replace_re filter #2163
Conversation
components/templates/src/filters.rs
Outdated
Some(val) => try_get_value!("replace_re", "rep", String, val), | ||
None => return Err(TeraError::msg("Filter `replace_re` expected an arg called `rep`")), | ||
}; | ||
let pattern_re = Regex::new(&pattern) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you cache the compiled regex? If someone puts that filter in their base template and they have a lot of pages, re-compiling it could take a significant amount of time wrt the build time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm rather new to rust (and zola) but I'll try and give it a shot.
Is there a place in zola which does caching already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can have a struct that contains a hashmap regex str -> compiled_regex and implement the filter trait on it. It's the same as https://github.com/getzola/zola/blob/master/components/templates/src/global_fns/load_data.rs#L213
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure if I needed the same sync primitives for this filter as the one you have linked to, so I just included it. I hope something along those lines in commit
185856d should work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably fine
components/templates/src/filters.rs
Outdated
let mut cache = self.re_cache.lock().expect("re_cache lock"); | ||
let pattern_re = { | ||
match cache.get(&pattern) { | ||
Some(pat) => pat.clone(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to clone it, you can call replace_all here and return replace in L112
components/templates/src/lib.rs
Outdated
@@ -28,7 +28,7 @@ pub static ZOLA_TERA: Lazy<Tera> = Lazy::new(|| { | |||
.unwrap(); | |||
tera.register_filter("base64_encode", filters::base64_encode); | |||
tera.register_filter("base64_decode", filters::base64_decode); | |||
tera.register_filter("replace_re", filters::replace_re); | |||
tera.register_filter("replace_re", filters::ReplaceReFilter::new()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rename it to regex_replace
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing, will do.
I've renamed the filter to regex_replace and updated the documentation as well as tried removing calls to Regex.clone(). I hope it looks better now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!
* Implement replace_re filter * Cargo fmt * add regex caching * cargo fmt * update docs, update unit test * rename replace_re -> regex_replace
* Implement replace_re filter * Cargo fmt * add regex caching * cargo fmt * update docs, update unit test * rename replace_re -> regex_replace
* Implement replace_re filter * Cargo fmt * add regex caching * cargo fmt * update docs, update unit test * rename replace_re -> regex_replace
Sanity check:
Code changes
(Delete or ignore this section for documentation changes)
next
branch?If the change is a new feature or adding to/changing an existing one: