use templates to create the feed entries
This commit is contained in:
parent
dc9b13c563
commit
75c5a47449
5 changed files with 33 additions and 22 deletions
|
@ -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>,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue