From 9730cd0aaaa57cf4c8aba2ee1fab7bb0646d380d Mon Sep 17 00:00:00 2001 From: carlocorradini Date: Wed, 13 Dec 2023 18:11:39 +0100 Subject: fix: additional-css UNIX style path normalization --- .github/workflows/check.yml | 10 +++++++--- CHANGELOG.md | 8 ++++++++ Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/bin/mdbook-admonish.rs | 42 ++++++++++++++++++++++++++++++++++++++---- 5 files changed, 61 insertions(+), 7 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index d4e411e..abcd2ac 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -72,20 +72,24 @@ jobs: matrix: os: - ubuntu-20.04 - # - windows-2019 rust: - stable - beta - 1.66.0 experimental: - false - # Run a canary test on nightly that's allowed to fail include: + # Run a canary test on nightly that's allowed to fail - os: ubuntu-20.04 rust: nightly experimental: true - # Don't bother retesting stable linux, we did it in the comprehensive test + # Test only stable on Windows, presume we'd get same result on other + # versions as Linux + - os: windows-2022 + rust: stable + experimental: false exclude: + # Don't bother retesting stable linux, we did it in the comprehensive test - os: ubuntu-20.04 rust: stable experimental: false diff --git a/CHANGELOG.md b/CHANGELOG.md index 8caaeaa..73225fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## Unreleased +### Changed + +- `additional-css` _UNIX_ style path normalization ([#161](https://github.com/tommilligan/mdbook-admonish/issues/161)) + +### Fixed + +- Typo from _prereprocessor_ to _preprocessor_ + ## 1.14.0 ### Changed diff --git a/Cargo.lock b/Cargo.lock index e8d2753..b2c8445 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -960,6 +960,7 @@ dependencies = [ "log", "mdbook", "once_cell", + "path-slash", "pretty_assertions", "pulldown-cmark", "regex", @@ -1126,6 +1127,12 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "path-slash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e91099d4268b0e11973f036e885d652fb0b21fedcf69738c627f94db6a44f42" + [[package]] name = "percent-encoding" version = "2.3.0" diff --git a/Cargo.toml b/Cargo.toml index 385b4f7..cd0b371 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ env_logger = { version = "0.10", default_features = false, optional = true } log = "0.4.20" mdbook = "0.4.35" once_cell = "1.18.0" +path-slash = "0.2.1" pulldown-cmark = "0.9.3" regex = "1.9.6" semver = "1.0.19" diff --git a/src/bin/mdbook-admonish.rs b/src/bin/mdbook-admonish.rs index c420abe..89a9419 100644 --- a/src/bin/mdbook-admonish.rs +++ b/src/bin/mdbook-admonish.rs @@ -97,6 +97,9 @@ fn handle_supports(renderer: String) -> ! { #[cfg(feature = "cli-install")] mod install { use anyhow::{Context, Result}; + use path_slash::PathExt; + use std::borrow::Cow; + use std::path::Path; use std::{ fs::{self, File}, io::Write, @@ -122,6 +125,16 @@ mod install { } } + // Normalize path to UNIX style. + // This avoids conflicts/rewriting files when projects are used under different + // operating systems (e.g. on Windows, after being used on Linux) + // + // https://github.com/tommilligan/mdbook-admonish/issues/161 + fn normalize_config_file_path(path: &Path) -> Result> { + path.to_slash() + .context("UNIX style path normalization error") + } + pub fn handle_install(proj_dir: PathBuf, css_dir: PathBuf) -> Result<()> { let config = proj_dir.join("book.toml"); log::info!("Reading configuration file '{}'", config.display()); @@ -139,7 +152,7 @@ mod install { ); preprocessor["assets_version"] = value; } else { - log::info!("Unexpected configuration, not updating prereprocessor configuration"); + log::info!("Unexpected configuration, not updating preprocessor configuration"); }; let mut additional_css = additional_css(&mut doc); @@ -148,12 +161,13 @@ mod install { // Normalize path to remove no-op components // https://github.com/tommilligan/mdbook-admonish/issues/47 let filepath: PathBuf = filepath.components().collect(); - let filepath_str = filepath.to_str().context("non-utf8 filepath")?; if let Ok(ref mut additional_css) = additional_css { - if !additional_css.contains_str(filepath_str) { + let filepath_str = normalize_config_file_path(&filepath)?; + + if !additional_css.contains_str(&filepath_str) { log::info!("Adding '{filepath_str}' to 'additional-css'"); - additional_css.push(filepath_str); + additional_css.push(filepath_str.as_ref()); } } else { log::warn!("Unexpected configuration, not updating 'additional-css'"); @@ -227,4 +241,24 @@ A beautifully styled message. item["command"] = toml_edit::value("mdbook-admonish"); Ok(item) } + + #[cfg(test)] + mod test { + use super::*; + + /// This test seems redundant, but would fail on Windows. + /// + /// We want to always convert to a fixed output string style, independant + /// of runtime platform, and forward slashes in relative paths are fine on + /// Windows. + #[test] + fn test_normalize_config_file_path() { + let input = PathBuf::from(".") + .join("css-dir") + .join("mdbook-admonish.css"); + let expected = "./css-dir/mdbook-admonish.css"; + let actual = normalize_config_file_path(&input).unwrap(); + assert_eq!(actual.as_ref(), expected); + } + } } -- cgit v1.2.3