Add style sheet to the feed
Based on the code proposed in PR 70 of the atom-syndication crate. Signed-off-by: Jacob Kiers <jacob@jacobkiers.net>
This commit is contained in:
parent
9129f7e11b
commit
c2d09621aa
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -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",
|
||||
|
@ -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" }
|
||||
|
@ -26,6 +26,7 @@
|
||||
</section>
|
||||
<section>
|
||||
<h2>Recent Items</h2>
|
||||
<p>Last updated on <xsl:apply-templates select="atom:feed/atom:updated" /></p>
|
||||
<xsl:apply-templates select="atom:feed/atom:entry" />
|
||||
</section>
|
||||
</body>
|
||||
|
14
src/feed.rs
14
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<W: std::io::Write>(
|
||||
feed: Feed,
|
||||
mut out: W,
|
||||
) -> Result<W, atom_syndication::Error> {
|
||||
let _ = writeln!(out, r#"<?xml version="1.0"?>"#);
|
||||
let _ = writeln!(out, r#"<?xml-stylesheet href="feed.xsl" type="text/xsl"?>"#);
|
||||
let config = WriteConfig {
|
||||
write_document_declaration: false,
|
||||
..Default::default()
|
||||
};
|
||||
feed.write(out, config)
|
||||
}
|
||||
|
||||
//#[derive(Serialize, Deserialize, Debug)]
|
||||
pub(crate) struct Newsletter {
|
||||
id: String,
|
||||
|
25
src/main.rs
25
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<feed", ">\n<?xml-stylesheet href=\"feed.xsl\" type=\"text/xsl\"?>\n<feed");
|
||||
let _ = write_file(filename, feed_str)?;
|
||||
let _ = write_file(dir.join("feed.xsl"), FEED_STYLESHEET)?;
|
||||
|
||||
// Another ugly hack, but I don't know how to do this better...
|
||||
let file_name = format!("{:?}", filename.file_name().unwrap()).replace('"', "");
|
||||
write_file(dir.join("index.html"), INDEX_HTML.replace("{FEED}", file_name.as_str()))?;
|
||||
feed::write_feed(feed, open_file(filename).unwrap())?;
|
||||
write_file(dir.join("feed.xsl"), FEED_STYLESHEET)?;
|
||||
write_file(dir.join("index.html"), INDEX_HTML.replace("{FEED}", feed_file))?;
|
||||
}
|
||||
|
||||
println!("Finished building the feed.");
|
||||
@ -209,12 +204,16 @@ fn process_html(input: &str) -> Result<String, ()> {
|
||||
Ok(input.replace("src", "data-source"))
|
||||
}
|
||||
|
||||
fn write_file<P: Into<PathBuf>, D: AsRef<[u8]>>(html_path: P, data: D) -> Result<(), std::io::Error> {
|
||||
let path : PathBuf = html_path.into();
|
||||
fn open_file<P: Into<PathBuf>>(path: P) -> std::io::Result<std::fs::File> {
|
||||
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<P: Into<PathBuf>, 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())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user