From c2d09621aa0efd41c10ed2560ea73eba46582528 Mon Sep 17 00:00:00 2001 From: Jacob Kiers Date: Tue, 13 Dec 2022 20:32:07 +0100 Subject: [PATCH] Add style sheet to the feed Based on the code proposed in PR 70 of the atom-syndication crate. Signed-off-by: Jacob Kiers --- Cargo.lock | 3 +-- Cargo.toml | 3 +++ resources/feed.xsl | 1 + src/feed.rs | 14 ++++++++++++++ src/main.rs | 25 ++++++++++++------------- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4517b5..6a7386e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,8 +29,7 @@ checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" [[package]] name = "atom_syndication" version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21fb6a0b39c6517edafe46f8137e53c51742425a4dae1c73ee12264a37ad7541" +source = "git+https://github.com/jacobkiers/atom-syndication?rev=add7083b56b9d737f0fa1d3383aa82789b6c38ad#add7083b56b9d737f0fa1d3383aa82789b6c38ad" dependencies = [ "chrono", "derive_builder", diff --git a/Cargo.toml b/Cargo.toml index 7a091be..c7928ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,6 @@ imap = { version = "^2.4.1", default-features = false } mail-parser = "^0.8.0" rustls-connector = { version = "^0.16.1", default-features = false, features = [ "webpki-roots-certs", "quic" ] } sha2 = "^0.10.2" + +[patch.crates-io] +atom_syndication = { git = "https://github.com/jacobkiers/atom-syndication", rev = "add7083b56b9d737f0fa1d3383aa82789b6c38ad" } diff --git a/resources/feed.xsl b/resources/feed.xsl index 7b47381..a10bb53 100644 --- a/resources/feed.xsl +++ b/resources/feed.xsl @@ -26,6 +26,7 @@

Recent Items

+

Last updated on

diff --git a/src/feed.rs b/src/feed.rs index 2f1f16d..d2ee9df 100644 --- a/src/feed.rs +++ b/src/feed.rs @@ -2,6 +2,7 @@ use crate::Message; use atom_syndication::{ ContentBuilder, Entry, EntryBuilder, Feed, FeedBuilder, Generator, LinkBuilder, Person, + WriteConfig, }; use chrono::{DateTime, TimeZone, Utc}; @@ -82,6 +83,19 @@ pub(crate) fn build_atom_feed(hostname: &String, feed_file: &str) -> Feed { .build() } +pub(crate) fn write_feed( + feed: Feed, + mut out: W, +) -> Result { + let _ = writeln!(out, r#""#); + let _ = writeln!(out, r#""#); + let config = WriteConfig { + write_document_declaration: false, + ..Default::default() + }; + feed.write(out, config) +} + //#[derive(Serialize, Deserialize, Debug)] pub(crate) struct Newsletter { id: String, diff --git a/src/main.rs b/src/main.rs index a1bd0e6..43769a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -113,14 +113,9 @@ fn build_feed(filename: &PathBuf, hostname: String, include_html: bool) -> Resul feed.set_updated(Utc::now()); println!("Writing feed to {}", filename.display()); - // TODO: Ugly hack because atom_syndication crate does not support style sheets. - let feed_str = feed.to_string().as_str().replace(">\n\n\n Result { Ok(input.replace("src", "data-source")) } -fn write_file, D: AsRef<[u8]>>(html_path: P, data: D) -> Result<(), std::io::Error> { - let path : PathBuf = html_path.into(); +fn open_file>(path: P) -> std::io::Result { OpenOptions::new() .write(true) .create(true) - .open(&path) - .expect(format!("Could not open file '{}' for writing", &path.display()).as_str()) + .open(path.into()) +} + +fn write_file, D: AsRef<[u8]>>(path: P, data: D) -> Result<(), std::io::Error> { + let path: PathBuf = path.into(); + open_file(path.clone()) + .unwrap_or_else(|_| panic!("Could not open file '{}' for writing", &path.display())) .write_all(data.as_ref()) -} \ No newline at end of file +}