implement clickable links filter

This commit is contained in:
yggverse 2025-08-21 14:41:18 +03:00
parent 0e897db723
commit def8f72ece
4 changed files with 26 additions and 3 deletions

View file

@ -15,6 +15,7 @@ anyhow = "1.0"
chrono = { version = "0.4.41", features = ["serde"] }
clap = { version = "4.5", features = ["derive"] }
redb = "3.0"
regex = "1.11"
rocket = "0.5"
rocket_dyn_templates = { version = "0.2", features = ["tera"] }
url = { version = "2.5", features = ["serde"] }

View file

@ -219,7 +219,29 @@ fn rocket() -> _ {
warn!("Canonical URL option is required for the RSS feed by the specification!") // @TODO
}
rocket::build()
.attach(Template::fairing())
.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")),
}
},
)
})
}))
.configure(rocket::Config {
port: config.port,
address: config.host,

View file

@ -11,7 +11,7 @@
{% for post in posts %}
<div>
<a name="{{ post.id }}"></a>
<p>{{ post.message | linebreaksbr | safe }}</p>
<p>{{ post.message | url | linebreaksbr | safe }}</p>
<div>
<ul>
<li><a href="{{ post.href.post }}" title="Created">{{ post.time }}</a></li>

View file

@ -2,7 +2,7 @@
{% block content %}
<div>
<a name="{{ post.id }}"></a>
<p>{{ post.message | linebreaksbr | safe }}</p>
<p>{{ post.message | url | linebreaksbr | safe }}</p>
<div>
<ul>
<li><a href="{{ post.href.post }}" title="Created">{{ post.time }}</a></li>