Change function replace_metadata_value
to replace_escaped_characters
This commit is contained in:
parent
5548ba4ba5
commit
8220cf29f7
1 changed files with 16 additions and 14 deletions
30
src/epub.rs
30
src/epub.rs
|
@ -71,7 +71,7 @@ pub fn generate_epubs(
|
||||||
let section_name = article.metadata().title();
|
let section_name = article.metadata().title();
|
||||||
let content_url = format!("article_{}.xhtml", idx);
|
let content_url = format!("article_{}.xhtml", idx);
|
||||||
let mut content = EpubContent::new(&content_url, xhtml_str.as_bytes())
|
let mut content = EpubContent::new(&content_url, xhtml_str.as_bytes())
|
||||||
.title(replace_metadata_value(section_name));
|
.title(replace_escaped_characters(section_name));
|
||||||
let header_level_tocs =
|
let header_level_tocs =
|
||||||
get_header_level_toc_vec(&content_url, article.article());
|
get_header_level_toc_vec(&content_url, article.article());
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ pub fn generate_epubs(
|
||||||
content = content.child(toc_element);
|
content = content.child(toc_element);
|
||||||
}
|
}
|
||||||
|
|
||||||
epub.metadata("title", replace_metadata_value(name))?;
|
epub.metadata("title", replace_escaped_characters(name))?;
|
||||||
epub.add_content(content)?;
|
epub.add_content(content)?;
|
||||||
info!("Adding images for {:?}", name);
|
info!("Adding images for {:?}", name);
|
||||||
article.img_urls.iter().for_each(|img| {
|
article.img_urls.iter().for_each(|img| {
|
||||||
|
@ -109,7 +109,7 @@ pub fn generate_epubs(
|
||||||
let appendix = generate_appendix(articles.iter().collect());
|
let appendix = generate_appendix(articles.iter().collect());
|
||||||
if let Err(err) = epub.add_content(
|
if let Err(err) = epub.add_content(
|
||||||
EpubContent::new("appendix.xhtml", appendix.as_bytes())
|
EpubContent::new("appendix.xhtml", appendix.as_bytes())
|
||||||
.title(replace_metadata_value("Article Sources")),
|
.title(replace_escaped_characters("Article Sources")),
|
||||||
) {
|
) {
|
||||||
let mut paperoni_err: PaperoniError = err.into();
|
let mut paperoni_err: PaperoniError = err.into();
|
||||||
paperoni_err.set_article_source(name);
|
paperoni_err.set_article_source(name);
|
||||||
|
@ -161,9 +161,9 @@ pub fn generate_epubs(
|
||||||
get_header_level_toc_vec("index.xhtml", article.article());
|
get_header_level_toc_vec("index.xhtml", article.article());
|
||||||
|
|
||||||
if let Some(author) = article.metadata().byline() {
|
if let Some(author) = article.metadata().byline() {
|
||||||
epub.metadata("author", replace_metadata_value(author))?;
|
epub.metadata("author", replace_escaped_characters(author))?;
|
||||||
}
|
}
|
||||||
let title = replace_metadata_value(article.metadata().title());
|
let title = replace_escaped_characters(article.metadata().title());
|
||||||
epub.metadata("title", &title)?;
|
epub.metadata("title", &title)?;
|
||||||
|
|
||||||
let mut content =
|
let mut content =
|
||||||
|
@ -189,7 +189,7 @@ pub fn generate_epubs(
|
||||||
let appendix = generate_appendix(vec![&article]);
|
let appendix = generate_appendix(vec![&article]);
|
||||||
epub.add_content(
|
epub.add_content(
|
||||||
EpubContent::new("appendix.xhtml", appendix.as_bytes())
|
EpubContent::new("appendix.xhtml", appendix.as_bytes())
|
||||||
.title(replace_metadata_value("Article Source")),
|
.title(replace_escaped_characters("Article Source")),
|
||||||
)?;
|
)?;
|
||||||
epub.generate(&mut out_file)?;
|
epub.generate(&mut out_file)?;
|
||||||
bar.inc(1);
|
bar.inc(1);
|
||||||
|
@ -216,7 +216,7 @@ pub fn generate_epubs(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Replaces characters that have to be escaped before adding to the epub's metadata
|
/// Replaces characters that have to be escaped before adding to the epub's metadata
|
||||||
fn replace_metadata_value(value: &str) -> String {
|
fn replace_escaped_characters(value: &str) -> String {
|
||||||
value
|
value
|
||||||
.replace("&", "&")
|
.replace("&", "&")
|
||||||
.replace("<", "<")
|
.replace("<", "<")
|
||||||
|
@ -235,8 +235,8 @@ fn generate_appendix(articles: Vec<&Extractor>) -> String {
|
||||||
};
|
};
|
||||||
format!(
|
format!(
|
||||||
"<a href=\"{}\">{}</a><br></br>",
|
"<a href=\"{}\">{}</a><br></br>",
|
||||||
replace_metadata_value(&article.url),
|
replace_escaped_characters(&article.url),
|
||||||
replace_metadata_value(article_name)
|
replace_escaped_characters(article_name)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
@ -300,20 +300,22 @@ fn get_header_level_toc_vec(content_url: &str, article: &NodeRef) -> Vec<TocElem
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::replace_metadata_value;
|
use kuchiki::traits::*;
|
||||||
|
|
||||||
|
use super::{get_header_level_toc_vec, replace_escaped_characters};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_replace_metadata_value() {
|
fn test_replace_escaped_characters() {
|
||||||
let mut value = "Lorem ipsum";
|
let mut value = "Lorem ipsum";
|
||||||
assert_eq!(replace_metadata_value(value), "Lorem ipsum");
|
assert_eq!(replace_escaped_characters(value), "Lorem ipsum");
|
||||||
value = "Memory safe > memory unsafe";
|
value = "Memory safe > memory unsafe";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
replace_metadata_value(value),
|
replace_escaped_characters(value),
|
||||||
"Memory safe > memory unsafe"
|
"Memory safe > memory unsafe"
|
||||||
);
|
);
|
||||||
value = "Author Name <author@mail.example>";
|
value = "Author Name <author@mail.example>";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
replace_metadata_value(value),
|
replace_escaped_characters(value),
|
||||||
"Author Name <author@mail.example>"
|
"Author Name <author@mail.example>"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue