From 5a806218a6c66de4c6690106ce58f7b2add7712a Mon Sep 17 00:00:00 2001 From: Nicola Ricciardi Date: Wed, 24 Jul 2024 22:20:53 +0200 Subject: [PATCH] Bugs fixing --- Cargo.toml | 2 +- README.md | 8 +- src/cli.rs | 73 +++++++++++++++++-- src/compiler.rs | 14 ++-- src/compiler/compilation_configuration.rs | 4 +- .../parsing/parsing_rule/html_image_rule.rs | 11 ++- src/constants.rs | 2 +- src/resource/image_resource.rs | 7 ++ 8 files changed, 102 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e9768b4..60e4f34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nmd" -version = "0.36.0" +version = "0.36.1" authors = ["Nicola Ricciardi"] edition = "2021" description = "Official NMD CLI and compiler" diff --git a/README.md b/README.md index 58db072..db185d9 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ **New way to write in markdown** [![License](https://img.shields.io/badge/license-GPL3-green.svg)](LICENSE) -[![Version](https://img.shields.io/badge/version-v0.36.0-blue.svg)](CHANGELOG.md) +[![Version](https://img.shields.io/badge/version-v0.36.1-blue.svg)](CHANGELOG.md) NMD stands for **New MarkDown**, or for friends, *Nicola MarkDown* (if Stephen Bourne can name a shell, why can't I name mine NMD?) @@ -44,7 +44,7 @@ NMD is full compatible with CommonMark standard. #### Performance -Being developed in Rust, NMD has incredible performance. In single thread mode, NMD is 10 times faster than the compiler for VS Code, while in multi thread mode it is **20 times faster**! +Being developed in Rust, NMD has incredible performance. In single thread mode, NMD is 2 times faster than the compiler for VS Code, while in multi thread mode it is up to **10 times faster**, but having more features as more modifiers, integrated table of content and bibliography! #### Extended Syntax @@ -182,10 +182,14 @@ Moreover, if you watch dossier files and compile them if something changes, you `--fast-draft` to create a fast draft of dossier, generally compiler takes less time to generate it. +`--parallelization` to parallelize work (default is single thread). + `-s -s ` to compile only a subset of documents in dossier configuration list. In the end, if you are writing in NMD and you want a preview, you could compile with `-p` option. `-p` renders a preview in a web server on `127.0.0.1:1234` (`--preview-scraping-interval ` to set client scraping interval in *milliseconds*). +`--embed-local-image`, `--embed-remote-image`, `--strict-image-src-check` and `--embed-local-image` to manage images parsing. + ## Develop Develop [check list](DEVELOP.md) diff --git a/src/cli.rs b/src/cli.rs index ed50983..a9a5509 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,3 +1,4 @@ +use core::panic; use std::collections::HashSet; use std::num::ParseIntError; use std::sync::Arc; @@ -176,6 +177,36 @@ impl NmdCli { .help("compile only a documents subset") .action(ArgAction::Append) ) + .arg( + Arg::new("parallelization") + .long("parallelization") + .help("set parallelization") + .action(ArgAction::SetTrue) + ) + .arg( + Arg::new("embed-local-image") + .long("embed-local-image") + .help("set embed local image") + .action(ArgAction::SetTrue) + ) + .arg( + Arg::new("embed-remote-image") + .long("embed-remote-image") + .help("set embed remote image") + .action(ArgAction::SetTrue) + ) + .arg( + Arg::new("compress-embed-image") + .long("compress-embed-image") + .help("set compress embed image") + .action(ArgAction::SetTrue) + ) + .arg( + Arg::new("strict-image-src-check") + .long("strict-image-src-check") + .help("set strict image source check") + .action(ArgAction::SetTrue) + ) ) .subcommand( Command::new("generate") @@ -383,14 +414,24 @@ impl NmdCli { }, CompilableResourceType::File => { - compilation_configuration.set_output_location(compilation_configuration.input_location().clone()); // could be a dir or a file + let mut output_path = compilation_configuration.input_location().clone(); - if compilation_configuration.output_location().is_dir() { - compilation_configuration.set_output_location(compilation_configuration.output_location().join(file_utility::build_output_file_name( - compilation_configuration.input_location().file_stem().unwrap().to_string_lossy().to_string().as_str(), + if output_path.is_dir() { + + output_path = output_path.join(file_utility::build_output_file_name( + output_path.file_stem().unwrap().to_string_lossy().to_string().as_str(), Some(&compilation_configuration.format().get_extension()) - ))); + )); + + } else { + + output_path = output_path.parent().unwrap().join(file_utility::build_output_file_name( + output_path.file_stem().unwrap().to_string_lossy().to_string().as_str(), + Some(&compilation_configuration.format().get_extension()) + )); } + + compilation_configuration.set_output_location(output_path); }, CompilableResourceType::Unknown => (), } @@ -468,6 +509,28 @@ impl NmdCli { compilation_configuration.set_styles_raw_path(styles.map(|s| s.clone()).collect()); } + // PARALLELIZATION + if matches.get_flag("parallelization") { + compilation_configuration.set_parallelization(Some(true)); + } + + // IMAGEs + if matches.get_flag("embed-local-image") { + compilation_configuration.set_embed_local_image(Some(true)); + } + + if matches.get_flag("embed-remote-image") { + compilation_configuration.set_embed_remote_image(Some(true)); + } + + if matches.get_flag("compress-embed-image") { + compilation_configuration.set_compress_embed_image(Some(true)); + } + + if matches.get_flag("strict-image-src-check") { + compilation_configuration.set_strict_image_src_check(Some(true)); + } + // DOCUMENT SUBSET (only if dossier) if let Some(documents_subset) = matches.get_many::("documents-subset") { diff --git a/src/compiler.rs b/src/compiler.rs index f9ef8fc..762d4a7 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -83,7 +83,7 @@ impl Compiler { let loading_start = Instant::now(); - log::info!("compilation configuration (this will override dossier compilation configuration):\n\n{:#?}\n", compilation_configuration); + log::debug!("compilation configuration (this will override dossier compilation configuration):\n\n{:#?}\n", compilation_configuration); let codex = Arc::new(compilation_configuration.codex()); @@ -161,6 +161,8 @@ impl Compiler { dossier.parse(compilation_configuration.format(), Arc::clone(&codex), Arc::new(RwLock::new(parsing_configuration)), Arc::new(parsing_configuration_overlay))?; + log::info!("dossier parsed in {} ms", compilation_start.elapsed().as_millis()); + assembler_configuration.set_theme(compilation_configuration.theme().as_ref().unwrap_or(&dossier_theme).clone()); assembler_configuration.set_preview(compilation_configuration.preview()); assembler_configuration.set_watching(compilation_configuration.watching()); @@ -483,9 +485,9 @@ impl Compiler { log::info!("start to compile dossier"); - let compile_start = Instant::now(); + let compilation_start = Instant::now(); - log::info!("compilation configuration (this will override dossier compilation configuration):\n\n{:#?}\n", compilation_configuration); + log::debug!("compilation configuration (this will override dossier compilation configuration):\n\n{:#?}\n", compilation_configuration); let codex = compilation_configuration.codex(); @@ -493,7 +495,7 @@ impl Compiler { let mut document: Document = loader.load_document_from_path(&codex, compilation_configuration.input_location())?; - log::info!("document loaded in {} ms", compile_start.elapsed().as_millis()); + log::info!("document loaded in {} ms", compilation_start.elapsed().as_millis()); compilation_configuration.fill_with_default(); @@ -519,6 +521,8 @@ impl Compiler { document.parse(compilation_configuration.format(), Arc::clone(&codex), Arc::new(RwLock::new(parsing_configuration)), Arc::new(None))?; + log::info!("document parsed in {} ms", compilation_start.elapsed().as_millis()); + assembler_configuration.set_theme(compilation_configuration.theme().clone().unwrap_or(Theme::default())); assembler_configuration.set_preview(compilation_configuration.preview()); assembler_configuration.set_watching(compilation_configuration.watching()); @@ -539,7 +543,7 @@ impl Compiler { artifact.dump(&dump_configuration)?; - log::info!("end to compile dossier (compile time: {} ms)", compile_start.elapsed().as_millis()); + log::info!("end to compile dossier (compile time: {} ms)", compilation_start.elapsed().as_millis()); Ok(()) } diff --git a/src/compiler/compilation_configuration.rs b/src/compiler/compilation_configuration.rs index a083e79..1a72a99 100644 --- a/src/compiler/compilation_configuration.rs +++ b/src/compiler/compilation_configuration.rs @@ -140,7 +140,7 @@ impl CompilationConfiguration { pub fn fill_with_default(&mut self) { if self.embed_local_image().is_none() { - self.set_embed_local_image(Some(true)); + self.set_embed_local_image(Some(false)); } if self.embed_remote_image().is_none() { @@ -156,7 +156,7 @@ impl CompilationConfiguration { } if self.parallelization().is_none() { - self.set_parallelization(Some(true)); + self.set_parallelization(Some(false)); } if self.strict_image_src_check().is_none() { diff --git a/src/compiler/parsing/parsing_rule/html_image_rule.rs b/src/compiler/parsing/parsing_rule/html_image_rule.rs index 8a70a01..5d8a226 100644 --- a/src/compiler/parsing/parsing_rule/html_image_rule.rs +++ b/src/compiler/parsing/parsing_rule/html_image_rule.rs @@ -234,10 +234,15 @@ impl HtmlImageRule { let id = ResourceReference::of(label.as_str(), Some(document_name)).unwrap(); - let mut image: ImageResource = ImageResource::new(PathBuf::from(src.as_str()), Some(parsed_label.parsed_content()), Some(label.as_str().to_string())) + let image = ImageResource::new(PathBuf::from(src.as_str()), Some(parsed_label.parsed_content()), Some(label.as_str().to_string())) .elaborating_relative_path_as_dossier_assets(parsing_configuration.input_location()) - .inferring_mime_type() - .unwrap(); + .inferring_mime_type(); + + if let Err(err) = &image { + log::error!("error occurs during image '{}' loading: {}", src.as_str(), err.to_string()); + } + + let mut image = image.unwrap(); return Self::build_img_from_parsing_configuration(&mut image, Some(id), vec!["image"], style, &parsing_configuration).unwrap(); diff --git a/src/constants.rs b/src/constants.rs index 58df7d7..db0081f 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,6 +1,6 @@ /// NMD version -pub const VERSION: &str = "0.36.0"; +pub const VERSION: &str = "0.36.1"; pub const DOSSIER_CONFIGURATION_YAML_FILE_NAME: &str = "nmd.yml"; diff --git a/src/resource/image_resource.rs b/src/resource/image_resource.rs index 45978e2..ffd2b65 100644 --- a/src/resource/image_resource.rs +++ b/src/resource/image_resource.rs @@ -64,6 +64,13 @@ impl ImageResource { /// Elaborate `src` path if it is relative appending if it doesn't exist dossier `assets` directory pub fn elaborating_relative_path_as_dossier_assets(mut self, base_location: &PathBuf) -> Self { + + let mut base_location: PathBuf = base_location.clone(); + + if !base_location.is_dir() { + base_location = PathBuf::from(base_location.parent().unwrap()); + } + if self.src().is_relative() { self.set_src(base_location.join(self.src()));