use templates to create the feed entries
This commit is contained in:
parent
dc9b13c563
commit
75c5a47449
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -516,7 +516,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "findpenguins-feed"
|
||||
version = "0.2.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"askama",
|
||||
"axum",
|
||||
|
@ -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"
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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>,
|
||||
}
|
||||
|
7
templates/feed_entry.html
Normal file
7
templates/feed_entry.html
Normal 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 %}
|
Loading…
Reference in New Issue
Block a user