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

Implement replace_re filter #2163

Merged
merged 6 commits into from
Apr 20, 2023
Merged

Implement replace_re filter #2163

merged 6 commits into from
Apr 20, 2023

Conversation

cydave
Copy link
Contributor

@cydave cydave commented Apr 2, 2023

Sanity check:

  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

Code changes

(Delete or ignore this section for documentation changes)

  • Are you doing the PR on the next branch?

If the change is a new feature or adding to/changing an existing one:

  • Have you created/updated the relevant documentation page(s)?

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)
Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

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

Copy link
Contributor Author

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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably fine

let mut cache = self.re_cache.lock().expect("re_cache lock");
let pattern_re = {
match cache.get(&pattern) {
Some(pat) => pat.clone(),
Copy link
Collaborator

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

@@ -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());
Copy link
Collaborator

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, will do.

@cydave
Copy link
Contributor Author

cydave commented Apr 20, 2023

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.

Copy link
Collaborator

@Keats Keats left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@Keats Keats merged commit 6a5c241 into getzola:next Apr 20, 2023
@cydave cydave deleted the next branch April 20, 2023 17:53
peterprototypes pushed a commit to peterprototypes/zola that referenced this pull request Sep 12, 2023
* Implement replace_re filter

* Cargo fmt

* add regex caching

* cargo fmt

* update docs, update unit test

* rename replace_re -> regex_replace
technimad pushed a commit to technimad/zola that referenced this pull request Sep 30, 2023
* Implement replace_re filter

* Cargo fmt

* add regex caching

* cargo fmt

* update docs, update unit test

* rename replace_re -> regex_replace
Keats pushed a commit that referenced this pull request Dec 18, 2023
* Implement replace_re filter

* Cargo fmt

* add regex caching

* cargo fmt

* update docs, update unit test

* rename replace_re -> regex_replace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants