Skip to content

Commit

Permalink
Merge pull request #46 from Bennett-Petzold/main
Browse files Browse the repository at this point in the history
Update all dependencies to latest
  • Loading branch information
crowdagger authored Oct 5, 2024
2 parents 31815e7 + 6a1ee2c commit 6356fad
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 78 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "epub-builder"
version = "0.7.4"
version = "0.7.5"
authors = ["Elisabeth Henry <liz.henry@ouvaton.org>"]
description = "A Rust library for generating EPUB files"
readme = "README.md"
Expand All @@ -23,17 +23,17 @@ zip-library = ["libzip", "libzip/time"]
[dependencies]
thiserror = "1.0"
once_cell = "1"
upon = "0.7"
upon = "0.8"
chrono = { version = "0.4", default-features = false, features = ["clock", "std", "wasmbind"] }
uuid = { version = "1", features = ["v4"] }
tempfile = { version = "3", optional = true }
libzip = { version = "0.6", optional = true, default-features = false, features = ["deflate"], package = "zip" }
tempfile = { version = "3", optional = true }
libzip = { version = "2.1", optional = true, default-features = false, features = ["deflate"], package = "zip"}
html-escape = "0.2"
log = "0.4"

[dev-dependencies]
pretty_assertions = "1"
env_logger = "0.10"
env_logger = "0.11"

[[example]]
name = "builder"
Expand Down
25 changes: 12 additions & 13 deletions examples/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ use std::io;
use std::io::Write;
use std::path::Path;


// Try to print Zip file to stdout
fn create_book(outpath: &Path) -> Result<()> {
let writer = File::create(outpath).unwrap();

// Create a new EpubBuilder using the zip library
EpubBuilder::new(ZipLibrary::new()?)?
// Set some metadata
let mut builder = EpubBuilder::new(ZipLibrary::new()?)?;
// Set some metadata
builder
.metadata("author", "Wikipedia Contributors")?
.metadata("title", "Ada Lovelace: first programmer")?
// // Set the stylesheet (create a "stylesheet.css" file in EPUB that is used by some generated files)
.stylesheet(File::open("examples/book/book.css")?)?
// Add a image cover file
.add_cover_image("cover.png", File::open("examples/book/Ada_Lovelace_color.svg")?, "image/svg")?

.add_cover_image(
"cover.png",
File::open("examples/book/Ada_Lovelace_color.svg")?,
"image/svg",
)?
// Add a title page
// .add_content(
// EpubContent::new("title.xhtml", File::open("examples/book/title.html")?)
Expand All @@ -33,7 +36,7 @@ fn create_book(outpath: &Path) -> Result<()> {
// )?
// Generate a toc inside of the document, that will be part of the linear structure.
.inline_toc()
// add text of first chapter
// add text of first chapter
.add_content(
EpubContent::new("chapter_1.xhtml", File::open("examples/book/ch1.html")?)
.title("First Programmer")
Expand All @@ -43,8 +46,8 @@ fn create_book(outpath: &Path) -> Result<()> {
EpubContent::new("chapter_2.xhtml", File::open("examples/book/ch2.html")?)
.title("First computer program")
.reftype(ReferenceType::Text),
)?
.generate(&writer)?; // generate into file to see epub
)?;
builder.generate(writer)?; // generate into file to see epub

log::debug!("sample book generation is done");
Ok(())
Expand All @@ -59,11 +62,7 @@ fn main() {
log::debug!("write file to: {}", &outpath.display());

match create_book(&outpath) {
Ok(_) => writeln!(
&mut io::stderr(),
"Successfully wrote epub document"
)
.unwrap(),
Ok(_) => writeln!(&mut io::stderr(), "Successfully wrote epub document").unwrap(),
Err(err) => writeln!(&mut io::stderr(), "Error: {:?}", err).unwrap(),
};
}
13 changes: 7 additions & 6 deletions examples/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ fn run() -> Result<()> {
let _writer = File::create(_out_file).unwrap();

// Create a new EpubBuilder using the zip library
EpubBuilder::new(ZipLibrary::new()?)?
// Set some metadata
let mut builder = EpubBuilder::new(ZipLibrary::new()?)?;
// Set some metadata
builder
.metadata("author", "Joan Doe")?
.metadata("title", "Dummy Book <T>")?
// Set the stylesheet (create a "stylesheet.css" file in EPUB that is used by some generated files)
Expand Down Expand Up @@ -73,10 +74,10 @@ fn run() -> Result<()> {
// Add a chapter without a title, which will thus not appear in the TOC.
.add_content(EpubContent::new("notes.xhtml", dummy_content.as_bytes()))?
// Generate a toc inside of the document, that will be part of the linear structure.
.inline_toc()
// Finally, write the EPUB file to stdout
.generate(&mut io::stdout())?; // generate into stout
// .generate(&_writer)?; // generate into temp file to see epub internals
.inline_toc();
// Finally, write the EPUB file to stdout
builder.generate(&mut io::stdout())?; // generate into stout
// .generate(&_writer)?; // generate into temp file to see epub internals
log::debug!("dummy book generation is done");
Ok(())
}
Expand Down
14 changes: 7 additions & 7 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ pub fn indent<S: AsRef<str>>(s: S, level: usize) -> String {
}

/// conditionnaly encode HTML
pub fn encode_html<'a>(s: &'a str, encode: bool) -> Cow<'a, str> {
if encode {
html_escape::encode_text(s)
} else {
Cow::Borrowed(s)
}
pub fn encode_html(s: &str, encode: bool) -> Cow<'_, str> {
if encode {
html_escape::encode_text(s)
} else {
Cow::Borrowed(s)
}
}

#[test]
#[allow(clippy::blacklisted_name)]
#[allow(clippy::disallowed_names)]
fn test_indent() {
let foo = "Some string with only one line";
assert_eq!(indent(foo, 3), " Some string with only one line");
Expand Down
16 changes: 8 additions & 8 deletions src/epub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ impl<Z: Zip> EpubBuilder<Z> {
/// let mut epub: Vec<u8> = vec!();
/// builder.generate(&mut epub).unwrap();
/// ```
pub fn generate<W: io::Write>(&mut self, to: W) -> Result<()> {
pub fn generate<W: io::Write>(mut self, to: W) -> Result<()> {
// If no styleesheet was provided, generate a dummy one
if !self.stylesheet {
self.stylesheet(b"".as_ref())?;
Expand Down Expand Up @@ -692,7 +692,7 @@ impl<Z: Zip> EpubBuilder<Z> {
}

let data = {
let mut authors: Vec<_> = vec!{};
let mut authors: Vec<_> = vec![];
for (i, author) in self.metadata.author.iter().enumerate() {
let author = upon::value! {
id_attr: html_escape::encode_double_quoted_attribute(&i.to_string()),
Expand All @@ -718,7 +718,7 @@ impl<Z: Zip> EpubBuilder<Z> {
}
};

let mut res:Vec<u8> = vec![];
let mut res: Vec<u8> = vec![];
match self.version {
EpubVersion::V20 => templates::v2::CONTENT_OPF.render(&data).to_writer(&mut res),
EpubVersion::V30 => templates::v3::CONTENT_OPF.render(&data).to_writer(&mut res),
Expand All @@ -744,7 +744,7 @@ impl<Z: Zip> EpubBuilder<Z> {
};
let mut res: Vec<u8> = vec![];
templates::TOC_NCX
.render(&data)
.render(&Engine::new(), &data)
.to_writer(&mut res)
.map_err(|e| crate::Error::TemplateError {
msg: "error rendering toc.ncx template".to_string(),
Expand Down Expand Up @@ -793,7 +793,7 @@ impl<Z: Zip> EpubBuilder<Z> {
}
}

let data = upon::value!{
let data = upon::value! {
content: content, // Not escaped: XML content
toc_name: common::encode_html(&self.metadata.toc_name, self.escape_html),
generator_attr: html_escape::encode_double_quoted_attribute(&self.metadata.generator),
Expand Down Expand Up @@ -827,9 +827,9 @@ impl<Z: Zip> EpubBuilder<Z> {
// Ordering to to look as similar as possible to the W3 Recommendation ruleset
// Slightly more permissive, there are some that are invalid start chars, but this is ok.
fn is_id_char(c: char) -> bool {
('A'..='Z').contains(&c)
c.is_ascii_uppercase()
|| c == '_'
|| ('a'..='z').contains(&c)
|| c.is_ascii_lowercase()
|| ('\u{C0}'..='\u{D6}').contains(&c)
|| ('\u{D8}'..='\u{F6}').contains(&c)
|| ('\u{F8}'..='\u{2FF}').contains(&c)
Expand All @@ -844,7 +844,7 @@ fn is_id_char(c: char) -> bool {
|| ('\u{10000}'..='\u{EFFFF}').contains(&c)
|| c == '-'
|| c == '.'
|| ('0'..='9').contains(&c)
|| c.is_ascii_digit()
|| c == '\u{B7}'
|| ('\u{0300}'..='\u{036F}').contains(&c)
|| ('\u{203F}'..='\u{2040}').contains(&c)
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
//! let mut output = Vec::<u8>::new();
//!
//! // Create a new EpubBuilder using the zip library
//! epub_builder::EpubBuilder::new(epub_builder::ZipLibrary::new()?)?
//! let mut builder = epub_builder::EpubBuilder::new(epub_builder::ZipLibrary::new()?)?;
//! // Set some metadata
//! .metadata("author", "Joan Doe")?
//! builder.metadata("author", "Joan Doe")?
//! .metadata("title", "Dummy Book")?
//! // Set epub version to 3.0
//! .epub_version(epub_builder::EpubVersion::V30)
Expand Down Expand Up @@ -66,10 +66,10 @@
//! // Add a chapter without a title, which will thus not appear in the TOC.
//! .add_content(epub_builder::EpubContent::new("notes.xhtml", dummy_content.as_bytes()))?
//! // Generate a toc inside of the document, that will be part of the linear structure.
//! .inline_toc()
//! .inline_toc();
//! // Finally, write the EPUB file to a writer. It could be a `Vec<u8>`, a file,
//! // `stdout` or whatever you like, it just needs to implement the `std::io::Write` trait.
//! .generate(&mut output)?;
//! builder.generate(&mut output)?;
//! Ok(output)
//! }
//!
Expand Down
24 changes: 14 additions & 10 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ pub static IBOOKS: &[u8] = include_bytes!("../templates/ibooks.xml");
pub static CONTAINER: &[u8] = include_bytes!("../templates/container.xml");

static ENGINE: Lazy<::upon::Engine> = Lazy::new(|| {
let mut engine = ::upon::Engine::new();
engine.add_filter("eq", str::eq);
engine
}
);
let mut engine = ::upon::Engine::new();
engine.add_filter("eq", str::eq);
engine
});

pub static TOC_NCX: Lazy<::upon::Template> = Lazy::new(|| {
ENGINE.compile(include_str!("../templates/toc.ncx"))
ENGINE
.compile(include_str!("../templates/toc.ncx"))
.expect("error compiling 'toc.ncx' template'")
});

Expand All @@ -24,11 +24,13 @@ pub mod v2 {
use once_cell::sync::Lazy;

pub static CONTENT_OPF: Lazy<::upon::Template> = Lazy::new(|| {
ENGINE.compile(include_str!("../templates/v2/content.opf"))
ENGINE
.compile(include_str!("../templates/v2/content.opf"))
.expect("error compiling 'content.opf' (for EPUB 2.0) template")
});
pub static NAV_XHTML: Lazy<::upon::Template> = Lazy::new(|| {
ENGINE.compile(include_str!("../templates/v2/nav.xhtml"))
ENGINE
.compile(include_str!("../templates/v2/nav.xhtml"))
.expect("error compiling 'nav.xhtml' (for EPUB 2.0) template")
});
}
Expand All @@ -37,11 +39,13 @@ pub mod v3 {
use once_cell::sync::Lazy;

pub static CONTENT_OPF: Lazy<::upon::Template> = Lazy::new(|| {
ENGINE.compile(include_str!("../templates/v3/content.opf"))
ENGINE
.compile(include_str!("../templates/v3/content.opf"))
.expect("error compiling 'content.opf' (for EPUB 3.0) template")
});
pub static NAV_XHTML: Lazy<::upon::Template> = Lazy::new(|| {
ENGINE.compile(include_str!("../templates/v3/nav.xhtml"))
ENGINE
.compile(include_str!("../templates/v3/nav.xhtml"))
.expect("error compiling 'nav.xhtml' (for EPUB 3.0) template")
});
}
Loading

0 comments on commit 6356fad

Please sign in to comment.