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]] [[package]]
name = "findpenguins-feed" name = "findpenguins-feed"
version = "0.2.0" version = "0.3.0"
dependencies = [ dependencies = [
"askama", "askama",
"axum", "axum",

View File

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

View File

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

View File

@ -7,3 +7,12 @@ use super::Feed;
pub struct FeedsTemplate { pub struct FeedsTemplate {
pub feeds: Vec<Feed>, 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 %}