Add replace_metadata_value
function
This commit is contained in:
parent
b402472ba6
commit
6b62051942
1 changed files with 35 additions and 3 deletions
36
src/epub.rs
36
src/epub.rs
|
@ -20,10 +20,13 @@ pub fn generate_epub(extractor: Extractor) {
|
|||
let html_buf = std::str::from_utf8(&html_buf).unwrap();
|
||||
let mut epub = EpubBuilder::new(ZipLibrary::new().unwrap()).unwrap();
|
||||
if let Some(author) = extractor.metadata().byline() {
|
||||
epub.metadata("author", author.replace("&", "&"))
|
||||
epub.metadata("author", replace_metadata_value(author))
|
||||
.unwrap();
|
||||
}
|
||||
epub.metadata("title", extractor.metadata().title().replace("&", "&"))
|
||||
epub.metadata(
|
||||
"title",
|
||||
replace_metadata_value(extractor.metadata().title()),
|
||||
)
|
||||
.unwrap();
|
||||
epub.add_content(EpubContent::new("index.xhtml", html_buf.as_bytes()))
|
||||
.unwrap();
|
||||
|
@ -38,3 +41,32 @@ pub fn generate_epub(extractor: Extractor) {
|
|||
epub.generate(&mut out_file).unwrap();
|
||||
println!("Created {:?}", file_name);
|
||||
}
|
||||
|
||||
/// Replaces characters that have to be escaped before adding to the epub's metadata
|
||||
fn replace_metadata_value(value: &str) -> String {
|
||||
value
|
||||
.replace("&", "&")
|
||||
.replace("<", "<")
|
||||
.replace(">", ">")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::replace_metadata_value;
|
||||
|
||||
#[test]
|
||||
fn test_replace_metadata_value() {
|
||||
let mut value = "Lorem ipsum";
|
||||
assert_eq!(replace_metadata_value(value), "Lorem ipsum");
|
||||
value = "Memory safe > memory unsafe";
|
||||
assert_eq!(
|
||||
replace_metadata_value(value),
|
||||
"Memory safe > memory unsafe"
|
||||
);
|
||||
value = "Author Name <author@mail.example>";
|
||||
assert_eq!(
|
||||
replace_metadata_value(value),
|
||||
"Author Name <author@mail.example>"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue