Replies: 1 comment
-
|
Here is my solution, if somebody wants: rocket::build()
.attach(Template::custom(|engines| {
engines.tera.register_filter("url", {
use regex::{Captures, Regex};
use rocket_dyn_templates::tera::{Error, Value};
Box::new(
move |v: &Value,
_: &std::collections::HashMap<String, Value>|
-> Result<Value, Error> {
match v.as_str() {
Some(s) => match Regex::new(r"[A-z]+:\/\/(?:[^\s]+|$)") {
Ok(r) => Ok(r
.replace_all(s, |c: &Captures| {
format!("<a href=\"{}\">{}</a>", &c[0], &c[0])
})
.into()),
Err(e) => Err(Error::msg(e)),
},
None => Err(Error::msg("Expected a string")),
}
},
)
})
}))
...
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Should I really implement my own filter or it is already exist?
In other words, I want to replace
http://to<a href="http://entries in text.Beta Was this translation helpful? Give feedback.
All reactions