1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
//! Main functions doing actual work. //! //! First, use `parse_descriptor()` to get an element list from a descriptor file, //! then construct an `EPubBook::from_elements()`, validate and absolutise paths via //! `EPubBook::normalise_paths()` and write the book with `EPubBook::write_zip()`. mod book; mod element; mod include_dir; use regex::Regex; use std::path::Path; use self::super::Error; use std::iter::FromIterator; use std::io::{BufReader, BufRead, Read}; pub use self::element::BookElement; pub use self::include_dir::IncludeDirectory; pub use self::book::{EPubContentType, EPubData, EPubBook}; /// Parse the whole descriptor with a specified /// [separator](https://nabijaczleweli.xyz/content/gen-epub-book/programmer.html#features-custom-separator) /// with the specified /// [rigidness](https://nabijaczleweli.xyz/content/gen-epub-book/programmer.html#features-free-date-format), /// stopping at the first encountered error. /// /// Uses `BookElement::parse()`, so it inherits all errors from there, /// adding only the error from splitting lines. /// /// Filters out non-describing lines. /// /// # Examples /// /// ``` /// # extern crate gen_epub_book; /// # extern crate chrono; /// # fn main() { /// # use chrono::DateTime; /// # use std::path::PathBuf; /// # use gen_epub_book::ops::{BookElement, parse_descriptor}; /// assert_eq!(parse_descriptor("string input", &mut &b"\ /// This is a very simple thing that should prove unproblematic to parse\n\ /// \n\ /// Name: Simple ePub demonstration\n\ /// Cover: cover.png\n\ /// \n\ /// Image-Content: simple/chapter_image.png\n\ /// Content: simple/ctnt.html\n\ /// \n\ /// Author: nabijaczleweli\n\ /// Date: 2017-02-08T15:30:18+01:00\n\ /// Language: en-GB\n"[..], ":", false), /// Ok(vec![ /// BookElement::Name("Simple ePub demonstration".to_string()), /// BookElement::Cover(PathBuf::from("cover.png")), /// BookElement::ImageContent(PathBuf::from("simple/chapter_image.png")), /// BookElement::Content(PathBuf::from("simple/ctnt.html")), /// BookElement::Author("nabijaczleweli".to_string()), /// BookElement::Date(DateTime::parse_from_rfc3339("2017-02-08T15:30:18+01:00").unwrap()), /// BookElement::Language("en-GB".to_string())])); /// # } /// ``` pub fn parse_descriptor<R: Read>(desc: &'static str, from: &mut R, separator: &str, free_date: bool) -> Result<Vec<BookElement>, Error> { let elems: Vec<Option<BookElement>> = try!(Result::from_iter(BufReader::new(from) .lines() .map(|r| { r.map_err(|_| { Error::Io { desc: desc, op: "read", more: Some("line split"), } }) }) .map(|r| r.and_then(|l| BookElement::parse(&l, separator, free_date))) .collect::<Vec<_>>())); Ok(elems.into_iter().flat_map(|o| o).collect()) } /// Find an ePub title line in the specified input stream. /// /// The title line contains `<!-- ePub title: "TOC_NAME" -->`, where `TOC_NAME` is any string not containing the `"` character. /// /// # Examples /// /// /// /// /// ``` /// # use gen_epub_book::ops::find_title; /// assert_eq!(find_title(&mut &br#"L1\nL <!-- ePub title: "TTL" -->2\nL3"#[..]), /// Some("TTL".to_string())); /// ``` pub fn find_title<R: Read>(i: &mut R) -> Option<String> { lazy_static! { static ref TITLE_RGX: Regex = Regex::new(r#"<!-- ePub title: "([^"]+)" -->"#).unwrap(); } BufReader::new(i) .lines() .find(|l| if l.is_ok() { TITLE_RGX.is_match(l.as_ref().unwrap()) } else { false }) .map(|l| TITLE_RGX.captures(&l.unwrap()).unwrap().get(1).unwrap().as_str().to_string()) } /// Find an appropriate `IncludeDirectory` for the specified file, or `None` otherwise /// /// # Examples /// /// Given: /// /// ```text /// special_book /// ├── rendered /// │ └── output /// │ ├── intro.html /// │ ├── main.html /// │ └── ending.html /// ├── previews /// │ └── generated /// │ └── out /// │ ├── intro.html /// │ └── main.html /// └── gep /// └── special /// ├── intro.html /// └── book.epupp /// ``` /// /// The following holds: /// /// ``` /// # use gen_epub_book::ops::{IncludeDirectory, find_file}; /// # use std::fs::{self, File}; /// # use std::env::temp_dir; /// # use std::path::Path; /// # let special_book = temp_dir().join("gen-epub-book.rs-doctest").join("ops-find-file-0").join("special_book"); /// # fs::create_dir_all(special_book.join("rendered").join("output")).unwrap(); /// # fs::create_dir_all(special_book.join("previews").join("generated").join("out")).unwrap(); /// # fs::create_dir_all(special_book.join("gep").join("special")).unwrap(); /// # for f in &["intro.html", "main.html", "ending.html"] { /// # File::create(special_book.join("rendered").join("output").join(f)).unwrap(); /// # } /// # for f in &["intro.html", "main.html"] { /// # File::create(special_book.join("previews").join("generated").join("out").join(f)).unwrap(); /// # } /// # for f in &["intro.html", "book.epupp"] { /// # File::create(special_book.join("gep").join("special").join(f)).unwrap(); /// # } /// let default = IncludeDirectory::Unnamed { /// dir: ("".to_string(), /// special_book.join("gep").join("special")), /// }; /// let previews = IncludeDirectory::Named { /// name: "previews".to_string(), /// dir: ("../../previews/generated/out".to_string(), /// special_book.join("previews").join("generated").join("out")), /// }; /// let rendered = IncludeDirectory::Unnamed { /// dir: ("../../rendered/output".to_string(), /// special_book.join("rendered").join("output")), /// }; /// /// let include_order = [default.clone(), previews.clone(), rendered.clone()]; /// assert_eq!(find_file(&Path::new("intro.html"), &include_order), Some(&default)); /// assert_eq!(find_file(&Path::new("main.html"), &include_order), Some(&previews)); /// assert_eq!(find_file(&Path::new("ending.html"), &include_order), Some(&rendered)); /// assert_eq!(find_file(&Path::new("cover.png"), &include_order), None); /// ``` pub fn find_file<P: AsRef<Path>>(file: P, include_order: &[IncludeDirectory]) -> Option<&IncludeDirectory> { let file = file.as_ref(); include_order.iter().find(|i| i.resolve(file).is_some()) }