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
//! Module containing various utility functions.


use std::path::{PathBuf, Path};
use std::io::{self, Write};
use self::super::Error;
use url::Url;
use reqwest;


/// Contents of the the container file
///
/// Points at `/content.opf` for root file.
///
/// Container file resides in `META-INF/container.xml`.
pub static CONTAINER: &'static str = include_str!("../assets/container.xml");

/// Contents of the the mimetype specifier.
///
/// Mimetype specifier resides in `/mimetype`.
pub static MIME_TYPE: &'static str = "application/epub+zip";

/// Constant header for the root file/content table.
///
/// Mimetype specifier resides in `/content.opf`.
pub static CONTENT_TABLE_HEADER: &'static str = include_str!("../assets/content.opf.header");


/// Uppercase the first character of the supplied string.
///
/// Based on http://stackoverflow.com/a/38406885/2851815
///
/// # Examples
///
/// ```
/// # use gen_epub_book::util::uppercase_first;
/// assert_eq!(uppercase_first("abolish"), "Abolish".to_string());
/// ```
pub fn uppercase_first(s: &str) -> String {
    let mut c = s.chars();
    match c.next() {
        None => String::new(),
        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
    }
}

/// Get the (X)HTML ID from a path.
///
/// Replaces slashes with `-`s, `.`s with `_` and removes all `../`s, `./`s and the extension.
///
/// # Examples
///
/// ```
/// # use gen_epub_book::util::xhtml_path_id;
/// assert_eq!(&xhtml_path_id("./abolish/the/../burgeoisie.html"), "abolish-the-burgeoisie_html");
/// ```
pub fn xhtml_path_id<P: AsRef<Path>>(p: P) -> String {
    book_filename(p).to_string_lossy().replace('.', "_")
}

/// Get filename to use for file specified by path.
///
/// # Examples
///
/// ```
/// # use gen_epub_book::util::book_filename;
/// # use std::path::Path;
/// assert_eq!(&book_filename("./abolish/the/../burgeoisie.html"), Path::new("abolish-the-burgeoisie.html"));
/// ```
pub fn book_filename<P: AsRef<Path>>(p: P) -> PathBuf {
    p.as_ref()
        .to_string_lossy()
        .replace('\u{FFFD}', "")
        .replace('\\', "/")
        .replace("../", "")
        .replace("./", "")
        .replace('/', "-")
        .into()
}

/// Get the (X)HTML ID from a URL.
///
/// # Examples
///
/// ```
/// # extern crate gen_epub_book;
/// # extern crate url;
/// # fn main() {
/// # use gen_epub_book::util::xhtml_url_id;
/// # use url::Url;
/// assert_eq!(xhtml_url_id(
///   &Url::parse("https://upload.wikimedia.org/2000px-Recycle_symbol_Taiwan.svg.png").unwrap()),
///   "2000px-Recycle_symbol_Taiwan_svg_png");
/// # }
/// ```
pub fn xhtml_url_id(url: &Url) -> String {
    url.path_segments().unwrap().last().unwrap().replace('.', "_")
}

/// Write the string content in an acceptable form.
///
/// # Examples
///
/// ```
/// # use gen_epub_book::util::write_string_content;
/// let mut buf = vec![];
/// assert_eq!(write_string_content(&mut buf, "<i>String content</i>"), Ok(()));
/// assert_eq!(&buf.iter().map(|&i| i as char).collect::<String>(),
/// r#"<html xmlns="http://www.w3.org/1999/xhtml">
///   <head></head>
///   <body>
///     <i>String content</i>
///   </body>
/// </html>
/// "#);
/// ```
pub fn write_string_content<W: Write>(to: &mut W, ctnt: &str) -> Result<(), Error> {
    fn e(more: &'static str) -> Error {
        Error::Io {
            desc: "string content",
            op: "write",
            more: Some(more),
        }
    }

    try!(writeln!(to, r#"<html xmlns="http://www.w3.org/1999/xhtml">"#).map_err(|_| e("string content html start")));
    try!(writeln!(to, r#"  <head></head>"#).map_err(|_| e("string content head start end")));
    try!(writeln!(to, r#"  <body>"#).map_err(|_| e("string content body start")));
    try!(writeln!(to, r#"    {}"#, ctnt).map_err(|_| e("string content string")));
    try!(writeln!(to, r#"  </body>"#).map_err(|_| e("string content body end")));
    try!(writeln!(to, r#"</html>"#).map_err(|_| e("string content html end")));

    Ok(())
}

/// Download the contents of the specified URL to the specified output stream.
///
/// # Examples
///
/// ```
/// # extern crate gen_epub_book;
/// # extern crate url;
/// # fn main() {
/// # use self::gen_epub_book::util::download_to;
/// # use self::url::Url;
/// let mut buf = vec![];
/// assert_eq!(download_to(&mut buf,
///                        &Url::parse("https://www.uuidgenerator.net/api/version4").unwrap()),
///            Ok(()));
/// assert_eq!(buf.len(), "90772bc3-c7fd-4d3a-b88b-57d3122d3712\r\n".len());
/// # }
/// ```
pub fn download_to<W: Write>(w: &mut W, what: &Url) -> Result<(), Error> {
    let mut resp = try!(reqwest::get(what.as_str()).map_err(|_| {
        Error::Io {
            desc: "network content",
            op: "request",
            more: None,
        }
    }));

    if !resp.status().is_success() {
        Err(Error::Io {
            desc: "network content",
            op: "inspect",
            more: resp.status().canonical_reason(),
        })
    } else {
        try!(io::copy(&mut resp, w).map_err(|_| {
            Error::Io {
                desc: "network content",
                op: "read",
                more: None,
            }
        }));
        Ok(())
    }
}