From 4356e328335eead2d4d9c9f615e5fd3ed22cf584 Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sun, 9 Jun 2024 19:54:18 +0300 Subject: [PATCH 1/4] upgrade mdbook to 0.4.50, align local dependencies with mdbook versions, code updates --- Cargo.lock | 91 ++++----------------------------------- Cargo.toml | 14 +++--- src/generator.rs | 29 +++++++------ src/resources/resource.rs | 6 +-- src/utils.rs | 2 +- 5 files changed, 37 insertions(+), 105 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 852cadac8..5f64a17d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -111,7 +111,7 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "hermit-abi 0.1.19", + "hermit-abi", "libc", "winapi", ] @@ -415,19 +415,6 @@ dependencies = [ "regex", ] -[[package]] -name = "env_logger" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" -dependencies = [ - "humantime", - "is-terminal", - "log", - "regex", - "termcolor", -] - [[package]] name = "env_logger" version = "0.11.3" @@ -644,20 +631,6 @@ dependencies = [ "wasi", ] -[[package]] -name = "handlebars" -version = "4.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "faa67bab9ff362228eb3d00bd024a4965d8231bbb7921167f0cfa66c6626b225" -dependencies = [ - "log", - "pest", - "pest_derive", - "serde", - "serde_json", - "thiserror", -] - [[package]] name = "handlebars" version = "5.1.2" @@ -696,12 +669,6 @@ dependencies = [ "libc", ] -[[package]] -name = "hermit-abi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" - [[package]] name = "html-escape" version = "0.2.13" @@ -781,17 +748,6 @@ dependencies = [ "hashbrown", ] -[[package]] -name = "is-terminal" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" -dependencies = [ - "hermit-abi 0.3.9", - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "is_terminal_polyfill" version = "1.70.0" @@ -876,13 +832,13 @@ dependencies = [ "chrono", "clap 4.5.4", "clap_complete", - "env_logger 0.11.3", - "handlebars 5.1.2", + "env_logger", + "handlebars", "log", "memchr", "once_cell", "opener", - "pulldown-cmark 0.10.3", + "pulldown-cmark", "regex", "serde", "serde_json", @@ -894,20 +850,20 @@ dependencies = [ [[package]] name = "mdbook-epub" -version = "0.4.37" +version = "0.4.40" dependencies = [ "const_format", - "env_logger 0.10.2", + "env_logger", "epub", "epub-builder", "eyre", - "handlebars 4.5.0", + "handlebars", "html_parser", "log", "mdbook", "mime_guess", "mockall", - "pulldown-cmark 0.9.6", + "pulldown-cmark", "semver", "serde", "serde_derive", @@ -1190,18 +1146,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "pulldown-cmark" -version = "0.9.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57206b407293d2bcd3af849ce869d52068623f19e1b5ff8e8778e3309439682b" -dependencies = [ - "bitflags 2.5.0", - "getopts", - "memchr", - "unicase", -] - [[package]] name = "pulldown-cmark" version = "0.10.3" @@ -1209,6 +1153,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76979bea66e7875e7509c4ec5300112b316af87fa7a252ca91c448b32dfe3993" dependencies = [ "bitflags 2.5.0", + "getopts", "memchr", "pulldown-cmark-escape", "unicase", @@ -1514,15 +1459,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "termcolor" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" -dependencies = [ - "winapi-util", -] - [[package]] name = "terminal_size" version = "0.3.0" @@ -1846,15 +1782,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" -dependencies = [ - "windows-sys 0.52.0", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index de3efd0ab..0a1bf34db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ authors = ["Michael Bryan "] description = "An EPUB renderer for mdbook." name = "mdbook-epub" -version = "0.4.37" +version = "0.4.40" readme = "README.md" license = "MIT" repository = "https://github.com/Michael-F-Bryan/mdbook-epub" @@ -25,17 +25,17 @@ doc = false eyre = "0.6" epub-builder = "0.7" thiserror = "1.0.49" -pulldown-cmark = "0.9.3" +pulldown-cmark = "0.10.0" semver = "1.0.17" serde = { version = "1.0.163", features = ["derive"] } serde_derive = "1.0" serde_json = "1.0.96" structopt = "0.3.26" mime_guess = "2.0" -env_logger = "0.10.0" +env_logger = "0.11.1" log = "0.4.17" -mdbook = { version = "0.4.37", default-features = false } -handlebars = "4.3.7" +mdbook = { version = "0.4.40", default-features = false } +handlebars = "5.0" toml = "0.5.11" # downgraded due to parent 'mdbook' dependency and error there html_parser = "0.7.0" url = "2.3" @@ -45,11 +45,11 @@ const_format = "0.2.31" uuid = "1.8" [dev-dependencies] -tempfile = "3" +tempfile = "3.4" epub = "2.1.1" serial_test = "2.0.0" mockall = "0.11" -env_logger = "0.10.0" +env_logger = "0.11.1" [profile.release] lto = true diff --git a/src/generator.rs b/src/generator.rs index 1512924e6..4c681e149 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -9,11 +9,11 @@ use std::{ }; use epub_builder::{EpubBuilder, EpubContent, ZipLibrary}; -use handlebars::{Handlebars, RenderError}; +use handlebars::{Handlebars, RenderError, RenderErrorReason}; use html_parser::{Dom, Node}; use mdbook::book::{BookItem, Chapter}; use mdbook::renderer::RenderContext; -use pulldown_cmark::{CowStr, Event, html, Tag}; +use pulldown_cmark::{CowStr, Event, html, Tag, TagEnd}; use url::Url; use urlencoding::encode; @@ -193,13 +193,18 @@ impl<'a> Generator<'a> { fn render_chapter(&self, ch: &Chapter) -> Result { let chapter_dir = if let Some(chapter_file_path) = &ch.path { chapter_file_path.parent().ok_or_else(|| { - RenderError::new(format!("No CSS found by a path = {:?}", ch.path)) + RenderError::from( + RenderErrorReason::Other(format!("No CSS found by a path = {:?}", ch.path)) + ) })? } else { - return Err(RenderError::new(format!( - "Draft chapter: '{}' could not be rendered.", - ch.name - ))); + return Err( + RenderError::from( + RenderErrorReason::Other(format!( + "Draft chapter: '{}' could not be rendered.", + ch.name + ))) + ); }; let mut body = String::new(); let parser = utils::create_new_pull_down_parser(&ch.content); @@ -418,12 +423,12 @@ impl<'a> AssetLinkFilter<'a> { fn apply(&self, event: Event<'a>) -> Event<'a> { trace!("AssetLinkFilter: Processing Event = {:?}", &event); match event { - Event::Start(Tag::Image(ty, ref url, ref title)) => { - if let Some(asset) = self.assets.get(&url.to_string()) { + Event::Start(Tag::Image{link_type, ref dest_url, ref title, ref id}) => { + if let Some(asset) = self.assets.get(&dest_url.to_string()) { // PREPARE info for replacing original REMOTE link by `.ext` value inside chapter content - debug!("Found URL '{}' by Event", &url); + debug!("Found URL '{}' by Event", &dest_url); let new = self.path_prefix(asset.filename.as_path()); - Event::Start(Tag::Image(ty, CowStr::from(new), title.to_owned())) + Event::Start(Tag::Image{link_type, dest_url: CowStr::from(new), title: title.to_owned(), id: id.to_owned()}) } else { event } @@ -528,7 +533,7 @@ impl EventQuoteConverter { self.convert_text = false; event } - Event::End(Tag::CodeBlock(_)) => { + Event::End(TagEnd::CodeBlock) => { self.convert_text = true; event } diff --git a/src/resources/resource.rs b/src/resources/resource.rs index a86b48fc1..9735fcaaf 100644 --- a/src/resources/resource.rs +++ b/src/resources/resource.rs @@ -137,10 +137,10 @@ fn find_assets_in_markdown(chapter_src_content: &str) -> Result, Err // that will process chapter content and find assets for event in pull_down_parser { match event { - Event::Start(Tag::Image(_, dest, _)) => { - found_asset.push(dest.to_string()); + Event::Start(Tag::Image{link_type: _, dest_url, title: _, id: _}) => { + found_asset.push(dest_url.to_string()); } - Event::Html(html) => { + Event::Html(html) | Event::InlineHtml(html) => { let content = html.into_string(); if let Ok(dom) = Dom::parse(&content) { diff --git a/src/utils.rs b/src/utils.rs index 55b062866..936e83dd1 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,7 +4,7 @@ use std::path::{Component, Path, PathBuf}; use url::Url; use uuid::Uuid; -pub(crate) fn create_new_pull_down_parser(text: &str) -> Parser<'_, '_> { +pub(crate) fn create_new_pull_down_parser(text: &str) -> Parser<'_> { let mut opts = Options::empty(); opts.insert(Options::ENABLE_TABLES); opts.insert(Options::ENABLE_FOOTNOTES); From 9aa350225edf143cb1064b474bdd3914374b64e4 Mon Sep 17 00:00:00 2001 From: d1t2 <507502+dieterplex@users.noreply.github.com> Date: Sun, 21 Jul 2024 16:58:45 +0800 Subject: [PATCH 2/4] Migrate dependency from structopt to clap derive (#118) * Migrate dependency from structopt to clap derive * fix: Downgrade yanked version of `zip`crate `cargo audit` shows that `epub` crate is depending on yanked version of `zip` crate and it requires ^1.1.3. Update to highest available version with command: `cargo update zip@1.3.0 --precise 1.1.4` --- Cargo.lock | 212 +++++++++++++++-------------------------- Cargo.toml | 2 +- src/bin/mdbook-epub.rs | 17 ++-- 3 files changed, 87 insertions(+), 144 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5f64a17d5..8e167f8b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,15 +32,6 @@ dependencies = [ "libc", ] -[[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi", -] - [[package]] name = "anstream" version = "0.6.14" @@ -105,17 +96,6 @@ dependencies = [ "derive_arbitrary", ] -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - [[package]] name = "autocfg" version = "1.3.0" @@ -128,12 +108,6 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - [[package]] name = "bitflags" version = "2.5.0" @@ -198,21 +172,6 @@ dependencies = [ "windows-targets 0.52.5", ] -[[package]] -name = "clap" -version = "2.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" -dependencies = [ - "ansi_term", - "atty", - "bitflags 1.3.2", - "strsim 0.8.0", - "textwrap", - "unicode-width", - "vec_map", -] - [[package]] name = "clap" version = "4.5.4" @@ -220,6 +179,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] @@ -231,7 +191,7 @@ dependencies = [ "anstream", "anstyle", "clap_lex", - "strsim 0.11.1", + "strsim", "terminal_size", ] @@ -241,7 +201,19 @@ version = "4.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd79504325bf38b10165b02e89b4347300f855f273c4cb30c4a3209e6583275e" dependencies = [ - "clap 4.5.4", + "clap", +] + +[[package]] +name = "clap_derive" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.65", ] [[package]] @@ -438,7 +410,7 @@ dependencies = [ "regex", "thiserror", "xml-rs", - "zip 1.3.0", + "zip 1.1.4", ] [[package]] @@ -653,21 +625,9 @@ checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" [[package]] name = "heck" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] - -[[package]] -name = "hermit-abi" -version = "0.1.19" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "html-escape" @@ -830,7 +790,7 @@ checksum = "b45a38e19bd200220ef07c892b0157ad3d2365e5b5a267ca01ad12182491eea5" dependencies = [ "anyhow", "chrono", - "clap 4.5.4", + "clap", "clap_complete", "env_logger", "handlebars", @@ -852,6 +812,7 @@ dependencies = [ name = "mdbook-epub" version = "0.4.40" dependencies = [ + "clap", "const_format", "env_logger", "epub", @@ -869,7 +830,6 @@ dependencies = [ "serde_derive", "serde_json", "serial_test", - "structopt", "tempfile", "thiserror", "toml", @@ -967,6 +927,27 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_enum" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02339744ee7253741199f897151b38e72257d13802d4ee837285cc2990a90845" +dependencies = [ + "num_enum_derive", +] + +[[package]] +name = "num_enum_derive" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "681030a937600a36906c185595136d26abfebb4aa9c65701cefcaf8578bb982b" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.65", +] + [[package]] name = "once_cell" version = "1.19.0" @@ -1114,27 +1095,12 @@ dependencies = [ ] [[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn 1.0.109", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" +name = "proc-macro-crate" +version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" dependencies = [ - "proc-macro2", - "quote", - "version_check", + "toml_edit", ] [[package]] @@ -1152,7 +1118,7 @@ version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76979bea66e7875e7509c4ec5300112b316af87fa7a252ca91c448b32dfe3993" dependencies = [ - "bitflags 2.5.0", + "bitflags", "getopts", "memchr", "pulldown-cmark-escape", @@ -1180,7 +1146,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "469052894dcb553421e483e4209ee581a45100d31b4018de03e5a7ad86374a7e" dependencies = [ - "bitflags 2.5.0", + "bitflags", ] [[package]] @@ -1233,7 +1199,7 @@ version = "0.38.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ - "bitflags 2.5.0", + "bitflags", "errno", "libc", "linux-raw-sys", @@ -1383,42 +1349,12 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" -[[package]] -name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - [[package]] name = "strsim" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" -[[package]] -name = "structopt" -version = "0.3.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10" -dependencies = [ - "clap 2.34.0", - "lazy_static", - "structopt-derive", -] - -[[package]] -name = "structopt-derive" -version = "0.4.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "subtle" version = "2.5.0" @@ -1475,15 +1411,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] - [[package]] name = "thiserror" version = "1.0.61" @@ -1547,6 +1474,23 @@ dependencies = [ "serde", ] +[[package]] +name = "toml_datetime" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf" + +[[package]] +name = "toml_edit" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + [[package]] name = "topological-sort" version = "0.2.2" @@ -1595,12 +1539,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-segmentation" -version = "1.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" - [[package]] name = "unicode-width" version = "0.1.12" @@ -1685,12 +1623,6 @@ dependencies = [ "getrandom", ] -[[package]] -name = "vec_map" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" - [[package]] name = "version_check" version = "0.9.4" @@ -1936,6 +1868,15 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] + [[package]] name = "xml-rs" version = "0.8.20" @@ -1963,9 +1904,9 @@ dependencies = [ [[package]] name = "zip" -version = "1.3.0" +version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1f4a27345eb6f7aa7bd015ba7eb4175fa4e1b462a29874b779e0bbcf96c6ac7" +checksum = "9cc23c04387f4da0374be4533ad1208cbb091d5c11d070dfef13676ad6497164" dependencies = [ "arbitrary", "crc32fast", @@ -1973,5 +1914,6 @@ dependencies = [ "displaydoc", "flate2", "indexmap", + "num_enum", "thiserror", ] diff --git a/Cargo.toml b/Cargo.toml index 0a1bf34db..b7e31ec56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ name = "mdbook-epub" doc = false [dependencies] +clap ={ version = "4.5", default-features = false, features = ["derive"]} eyre = "0.6" epub-builder = "0.7" thiserror = "1.0.49" @@ -30,7 +31,6 @@ semver = "1.0.17" serde = { version = "1.0.163", features = ["derive"] } serde_derive = "1.0" serde_json = "1.0.96" -structopt = "0.3.26" mime_guess = "2.0" env_logger = "0.11.1" log = "0.4.17" diff --git a/src/bin/mdbook-epub.rs b/src/bin/mdbook-epub.rs index 911d59017..81d433d15 100644 --- a/src/bin/mdbook-epub.rs +++ b/src/bin/mdbook-epub.rs @@ -8,10 +8,9 @@ use std::process; use ::env_logger; use ::mdbook; use ::serde_json; -use ::structopt; -use mdbook::MDBook; +use clap::{value_parser, Parser}; use mdbook::renderer::RenderContext; -use structopt::StructOpt; +use mdbook::MDBook; use ::mdbook_epub; use mdbook_epub::errors::Error; @@ -19,7 +18,7 @@ use mdbook_epub::errors::Error; fn main() { env_logger::init(); info!("Booting EPUB generator..."); - let args = Args::from_args(); + let args = Args::parse(); debug!("prepared generator args = {:?}", args); if let Err(e) = run(&args) { @@ -61,14 +60,16 @@ fn run(args: &Args) -> Result<(), Error> { Ok(()) } -#[derive(Debug, Clone, StructOpt)] +#[derive(Debug, Clone, Parser)] +#[command(version, about, long_about = None)] struct Args { - #[structopt( - short = "s", + #[arg( + short = 's', long = "standalone", help = "Run standalone (i.e. not as a mdbook plugin)" )] standalone: bool, - #[structopt(help = "The book to render.", parse(from_os_str), default_value = ".")] + + #[arg(help = "The book to render.", value_parser = value_parser!(PathBuf), default_value = ".")] root: PathBuf, } From 979cf68cebacc1ddf3907d3e73f8bfe091861281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Szab=C3=B3?= Date: Sun, 21 Jul 2024 11:59:32 +0300 Subject: [PATCH 3/4] turn on CI on all the branches (#111) So people working on pull-request in branches will be able to enable GitHub Actions and benefit from it. --- .github/workflows/rust.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 2ddf4943b..11e3b8a30 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -2,11 +2,7 @@ name: Rust on: push: - branches: - - master pull_request: - branches: - - master jobs: build: From 0a0bb27e9201680186b6f806b51ee46df4a7457d Mon Sep 17 00:00:00 2001 From: Yuriy Larin Date: Sun, 21 Jul 2024 20:33:03 +0300 Subject: [PATCH 4/4] added tests for clap Args --- src/bin/mdbook-epub.rs | 58 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/src/bin/mdbook-epub.rs b/src/bin/mdbook-epub.rs index 81d433d15..b615baee6 100644 --- a/src/bin/mdbook-epub.rs +++ b/src/bin/mdbook-epub.rs @@ -8,7 +8,7 @@ use std::process; use ::env_logger; use ::mdbook; use ::serde_json; -use clap::{value_parser, Parser}; +use clap::Parser; use mdbook::renderer::RenderContext; use mdbook::MDBook; @@ -47,12 +47,12 @@ fn run(args: &Args) -> Result<(), Error> { debug!("EPUB book config is : {:?}", md.config); RenderContext::new(md.root, md.book, md.config, destination) } else { - println!("Running mdbook-epub as plugin..."); + println!("Running mdbook-epub as plugin waiting on the STDIN input. If you wanted to process the files in the current folder, use the -s flag from documentation, See: mdbook-epub --help"); serde_json::from_reader(io::stdin()).map_err(|_| Error::RenderContext)? }; debug!("calling the main code for epub creation"); mdbook_epub::generate(&ctx)?; - info!( + println!( "Book is READY in directory: '{}'", ctx.destination.display() ); @@ -61,6 +61,10 @@ fn run(args: &Args) -> Result<(), Error> { } #[derive(Debug, Clone, Parser)] +#[clap( +name = "MDBook epub utility", +about = "MDBook epub utility makes EPUB file from MD source files described by book.toml", +)] #[command(version, about, long_about = None)] struct Args { #[arg( @@ -70,6 +74,52 @@ struct Args { )] standalone: bool, - #[arg(help = "The book to render.", value_parser = value_parser!(PathBuf), default_value = ".")] + #[arg( + help = "Root folder the book to render from", + value_parser = clap::value_parser!(PathBuf), + default_value = ".", + name = "root" + )] root: PathBuf, } + +#[cfg(test)] +mod tests { + use super::*; + use std::path::PathBuf; + + #[test] + fn test_standalone_only() { + let args = Args::try_parse_from(&["test", "--standalone"]).unwrap(); + debug_assert!(args.standalone); + debug_assert_eq!(args.root, PathBuf::from(".")); + } + + #[test] + fn test_standalone_with_root_path() { + let args = Args::try_parse_from(&["test", "--standalone", "/some/path"]).unwrap(); + debug_assert!(args.standalone); + debug_assert_eq!(args.root, PathBuf::from("/some/path")); + } + + #[test] + fn test_default_root_default_short() { + let args = Args::try_parse_from(&["test"]).unwrap(); + debug_assert!(!args.standalone); + debug_assert_eq!(args.root, PathBuf::from(".")); + } + + #[test] + fn test_short_flag() { + let args = Args::try_parse_from(&["test", "-s"]).unwrap(); + debug_assert!(args.standalone); + debug_assert_eq!(args.root, PathBuf::from(".")); + } + + #[test] + fn test_with_root_only() { + let args = Args::try_parse_from(&["test", "/another/path"]).unwrap(); + debug_assert!(!args.standalone); + debug_assert_eq!(args.root, PathBuf::from("/another/path")); + } +}