use templates to create the feed entries

This commit is contained in:
Sebastian Hugentobler 2023-11-27 10:49:50 +01:00
parent dc9b13c563
commit 75c5a47449
Signed by: shu
GPG Key ID: BB32CF3CA052C2F0
5 changed files with 33 additions and 22 deletions

2
Cargo.lock generated
View File

@ -516,7 +516,7 @@ dependencies = [
[[package]]
name = "findpenguins-feed"
version = "0.2.0"
version = "0.3.0"
dependencies = [
"askama",
"axum",

View File

@ -1,6 +1,6 @@
[package]
name = "findpenguins-feed"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
authors = ["Sebastian Hugentobler <shu@vanwa.ch>"]
license = "AGPL-3.0-or-later"

View File

@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::fmt;
use askama::Template;
use futures::future::join_all;
use rss::{GuidBuilder, Item, ItemBuilder};
use serde::Deserialize;
@ -11,6 +12,8 @@ use time::Date;
use crate::scrapers::page_url;
use crate::{hash, scrapers};
use self::template::FeedEntryTemplate;
pub mod route;
pub mod template;
@ -44,34 +47,26 @@ const ISO8601_DATE: u128 = iso8601::Config::DEFAULT
impl Footprint {
pub fn into_rss_item(self, root_url: &str) -> Item {
let text = if String::is_empty(&self.text) {
"No description"
"No text"
} else {
&self.text
};
// injection, I know
let desc = format!(
"{} <br /><br /> --- <br /><br /> from <a href=\"{}\">page {}</a>",
text,
page_url(root_url, self.page),
self.page
);
// injection again
let content = self
.images
.iter()
.map(|x| format!("<img src=\"{}\" /><br />", x))
.fold(String::new(), |mut a, b| {
a.reserve(b.len());
a.push_str(&b);
a
});
let page_url = page_url(root_url, self.page);
let template = FeedEntryTemplate {
text: text.to_string(),
page_url,
page: self.page,
images: self.images,
};
let content = template.render().ok();
ItemBuilder::default()
.title(Some(self.title))
.pub_date(self.date.format(&Iso8601::<ISO8601_DATE>).ok())
.link(Some(self.url.clone()))
.description(Some(desc))
.content(Some(content))
.content(content)
.guid(Some(GuidBuilder::default().value(self.url).build()))
.build()
}

View File

@ -7,3 +7,12 @@ use super::Feed;
pub struct FeedsTemplate {
pub feeds: Vec<Feed>,
}
#[derive(Template)]
#[template(path = "feed_entry.html")]
pub struct FeedEntryTemplate {
pub text: String,
pub page_url: String,
pub page: u8,
pub images: Vec<String>,
}

View File

@ -0,0 +1,7 @@
{{ text }} <br /><br />
--- <br /><br />
from <a href="{{ page_url }}">page {{ page }}</a>
{% for img in images %}
<img src="{{ img }}" /><br />
{% endfor %}