From 36c3eb65c646efa068cff78e6bde275d3c90970e Mon Sep 17 00:00:00 2001 From: Kenneth Gitere Date: Tue, 27 Apr 2021 20:34:26 +0300 Subject: [PATCH] Add appendix page for listing the source of the article --- src/epub.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/epub.rs b/src/epub.rs index 0b46277..87d6106 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -91,6 +91,17 @@ pub fn generate_epubs( successful_articles_table.add_row(vec![article.metadata().title()]); epub }); + let appendix = generate_appendix(articles.iter().collect()); + if let Err(err) = epub.add_content( + EpubContent::new("appendix.xhtml", appendix.as_bytes()) + .title(replace_metadata_value("Article Sources")), + ) { + let mut paperoni_err: PaperoniError = err.into(); + paperoni_err.set_article_source(name); + errors.push(paperoni_err); + return Err(errors); + } + let mut out_file = File::create(&name).unwrap(); match epub.generate(&mut out_file) { Ok(_) => (), @@ -147,6 +158,11 @@ pub fn generate_epubs( img.1.as_ref().unwrap(), )?; } + let appendix = generate_appendix(vec![&article]); + epub.add_content( + EpubContent::new("appendix.xhtml", appendix.as_bytes()) + .title(replace_metadata_value("Article Source")), + )?; epub.generate(&mut out_file)?; bar.inc(1); @@ -179,6 +195,37 @@ fn replace_metadata_value(value: &str) -> String { .replace(">", ">") } +//TODO: The type signature of the argument should change as it requires that merged articles create an entirely new Vec of references +fn generate_appendix(articles: Vec<&Extractor>) -> String { + let link_tags: String = articles + .iter() + .map(|article| { + let article_name = if !article.metadata().title().is_empty() { + article.metadata().title() + } else { + &article.url + }; + format!( + "{}

", + replace_metadata_value(&article.url), + replace_metadata_value(article_name) + ) + }) + .collect(); + let template = format!( + r#" + + + +

Appendix

Article sources

+ {} + +"#, + link_tags + ); + template +} + #[cfg(test)] mod test { use super::replace_metadata_value;