From 3f2624685d3caf94d030f34707b5822cdead891c Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Sat, 28 Sep 2024 21:42:33 -0400 Subject: [PATCH] Fix issue w/ logger, update to latest mdbook-preprocessor-utils --- Cargo.lock | 124 ++++++----------------- crates/mdbook-quiz-schema/Cargo.toml | 4 +- crates/mdbook-quiz/Cargo.toml | 8 +- crates/mdbook-quiz/build.rs | 11 +- crates/mdbook-quiz/src/main.rs | 46 ++++----- js/packages/quiz-embed/src/main.tsx | 14 ++- js/packages/quiz/src/components/quiz.tsx | 2 +- 7 files changed, 69 insertions(+), 140 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 83975f3..801918a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -138,11 +138,12 @@ dependencies = [ [[package]] name = "aquascope_workspace_utils" -version = "0.3.2" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4097d84b5e3e78c4df3ea06290f4e810d1db9beeac26cf3bca37ccc57913ec50" +checksum = "153aa8f8bd3803f9976cfac42300767b58f1b8a7970d174b266fd46330bb7d1e" dependencies = [ "anyhow", + "serde", "toml", ] @@ -278,20 +279,6 @@ dependencies = [ "windows-targets 0.52.6", ] -[[package]] -name = "clap" -version = "3.2.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" -dependencies = [ - "bitflags 1.3.2", - "clap_derive", - "clap_lex 0.2.4", - "indexmap 1.9.3", - "once_cell", - "textwrap 0.16.1", -] - [[package]] name = "clap" version = "4.3.24" @@ -299,6 +286,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fb690e81c7840c0d7aade59f242ea3b41b9bc27bcd5997890e7702ae4b32e487" dependencies = [ "clap_builder", + "clap_derive", + "once_cell", ] [[package]] @@ -309,7 +298,7 @@ checksum = "5ed2e96bc16d8d740f6f48d663eddf4b8a0983e79210fd55479b7bcd0a69860e" dependencies = [ "anstream", "anstyle", - "clap_lex 0.5.1", + "clap_lex", "once_cell", "strsim", "terminal_size 0.2.6", @@ -321,29 +310,19 @@ version = "4.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5a2d6eec27fce550d708b2be5d798797e5a55b246b323ef36924a0001996352" dependencies = [ - "clap 4.3.24", + "clap", ] [[package]] name = "clap_derive" -version = "3.2.25" +version = "4.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae6371b8bdc8b7d3959e9cf7b22d4435ef3e79e138688421ec654acf8c81b008" +checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050" dependencies = [ "heck", - "proc-macro-error", "proc-macro2", "quote", - "syn 1.0.109", -] - -[[package]] -name = "clap_lex" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" -dependencies = [ - "os_str_bytes", + "syn 2.0.76", ] [[package]] @@ -671,7 +650,7 @@ dependencies = [ "futures-sink", "futures-util", "http 0.2.12", - "indexmap 2.4.0", + "indexmap", "slab", "tokio", "tokio-util", @@ -692,12 +671,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "hashbrown" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" - [[package]] name = "hashbrown" version = "0.13.2" @@ -904,16 +877,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "indexmap" -version = "1.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" -dependencies = [ - "autocfg", - "hashbrown 0.12.3", -] - [[package]] name = "indexmap" version = "2.4.0" @@ -1111,7 +1074,7 @@ dependencies = [ "ammonia", "anyhow", "chrono", - "clap 4.3.24", + "clap", "clap_complete", "elasticlunr-rs", "env_logger", @@ -1138,15 +1101,14 @@ dependencies = [ [[package]] name = "mdbook-aquascope" -version = "0.3.2" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e731e41c9abdd458886a67c9933eb4dbe89bc0b3f609d9e846883dea99873bfd" +checksum = "415b2ea7ada2cd087916c918c0793475e00975815237adad5df6428bc0261812" dependencies = [ "anyhow", "aquascope_workspace_utils", - "clap 4.3.24", + "clap", "flate2", - "html-escape", "itertools", "lazy_static", "mdbook-preprocessor-utils", @@ -1163,18 +1125,20 @@ dependencies = [ [[package]] name = "mdbook-preprocessor-utils" -version = "0.1.6" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d8be89acb3f0a3ecb8d63b49a1301fdae2a2c234ae07b4138f7de26e770b950" +checksum = "db71dc8abd51a497c35f460401f649e5fac1168defa687dd7246a3d5e0c84a13" dependencies = [ "anyhow", "chrono", - "clap 3.2.25", + "clap", "env_logger", + "html-escape", "log", "mdbook", "rayon", "semver", + "serde", "serde_json", "tempfile", ] @@ -1184,6 +1148,7 @@ name = "mdbook-quiz" version = "0.3.8" dependencies = [ "anyhow", + "clap", "html-escape", "log", "mdbook", @@ -1417,12 +1382,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "os_str_bytes" -version = "6.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" - [[package]] name = "owo-colors" version = "3.5.0" @@ -1588,30 +1547,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" -[[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" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro2" version = "1.0.86" @@ -1821,22 +1756,22 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" [[package]] name = "serde" -version = "1.0.209" +version = "1.0.149" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" +checksum = "256b9932320c590e707b94576e3cc1f7c9024d0ee6612dfbcf1cb106cbe8e055" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.209" +version = "1.0.149" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" +checksum = "b4eae9b04cbffdfd550eb462ed33bc6a1b68c935127d008b27444d08380f94e4" dependencies = [ "proc-macro2", "quote", - "syn 2.0.76", + "syn 1.0.109", ] [[package]] @@ -1852,12 +1787,11 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.127" +version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad" +checksum = "46266871c240a00b8f503b877622fe33430b3c7d963bdc0f2adc511e54a1eae3" dependencies = [ "itoa", - "memchr", "ryu", "serde", ] @@ -2233,7 +2167,7 @@ version = "0.20.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70f427fce4d84c72b5b732388bf4a9f4531b53f74e2887e3ecb2481f68f66d81" dependencies = [ - "indexmap 2.4.0", + "indexmap", "toml_datetime", "winnow", ] diff --git a/crates/mdbook-quiz-schema/Cargo.toml b/crates/mdbook-quiz-schema/Cargo.toml index 405ffc1..301e4a7 100644 --- a/crates/mdbook-quiz-schema/Cargo.toml +++ b/crates/mdbook-quiz-schema/Cargo.toml @@ -15,10 +15,10 @@ json-schema = ["dep:schemars", "dep:serde_json"] ts = ["dep:ts-rs"] [dependencies] -serde = {version = "1.0.188", features = ["derive"]} +serde = {version = "1", features = ["derive"]} ts-rs = {version = "7.0.0", optional = true} schemars = {version = "0.8.15", optional = true} -serde_json = {version = "1.0.107", optional = true} +serde_json = {version = "1", optional = true} [dev-dependencies] toml = { workspace = true } diff --git a/crates/mdbook-quiz/Cargo.toml b/crates/mdbook-quiz/Cargo.toml index a0bd6a1..47e2055 100644 --- a/crates/mdbook-quiz/Cargo.toml +++ b/crates/mdbook-quiz/Cargo.toml @@ -21,16 +21,18 @@ regex = "1" html-escape = "0.2" toml = { workspace = true } mdbook = "= 0.4.25" -mdbook-preprocessor-utils = "0.1" -mdbook-aquascope = {version = "0.3", optional = true} +mdbook-preprocessor-utils = "0.2.0" +mdbook-aquascope = {version = "0.3.4", optional = true} mdbook-quiz-schema = {path = "../mdbook-quiz-schema", version = "0.3.4"} mdbook-quiz-validate = {path = "../mdbook-quiz-validate", version = "0.3.4"} toml_edit = "0.20.0" uuid = {version = "1.4.1", features = ["v4"]} log = "0.4.20" +clap = { version = "4", features = ["derive"] } [dev-dependencies] -mdbook-preprocessor-utils = { version = "0.1", features = ["testing"] } +mdbook-preprocessor-utils = { version = "0.2.0", features = ["testing"] } [build-dependencies] anyhow = { workspace = true } +mdbook-preprocessor-utils = "0.2.0" \ No newline at end of file diff --git a/crates/mdbook-quiz/build.rs b/crates/mdbook-quiz/build.rs index f548c1a..2805e7c 100644 --- a/crates/mdbook-quiz/build.rs +++ b/crates/mdbook-quiz/build.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use std::{fs, path::Path, process::Command}; +use std::{path::Path, process::Command}; const BINDINGS_DIR: &str = "../../js/packages/quiz/src/bindings"; const JS_DIST_DIR: &str = "../../js/packages/quiz-embed/dist"; @@ -24,14 +24,7 @@ fn main() -> Result<()> { } } - println!("cargo:rerun-if-changed={JS_DIST_DIR}"); - let entries = fs::read_dir(js_dist_dir)?; - let local_js_dist_dir = Path::new(LOCAL_JS_DIST_DIR); - fs::create_dir_all(local_js_dist_dir)?; - for entry in entries { - let path = entry?.path(); - fs::copy(&path, local_js_dist_dir.join(path.file_name().unwrap()))?; - } + mdbook_preprocessor_utils::copy_assets(LOCAL_JS_DIST_DIR, JS_DIST_DIR)?; Ok(()) } diff --git a/crates/mdbook-quiz/src/main.rs b/crates/mdbook-quiz/src/main.rs index c28aa82..9762b05 100644 --- a/crates/mdbook-quiz/src/main.rs +++ b/crates/mdbook-quiz/src/main.rs @@ -1,14 +1,12 @@ use anyhow::{Context, Result}; use mdbook_preprocessor_utils::{ - mdbook::preprocess::PreprocessorContext, Asset, SimplePreprocessor, + mdbook::preprocess::PreprocessorContext, Asset, HtmlElementBuilder, SimplePreprocessor, }; use mdbook_quiz_validate::Validated; use regex::Regex; use std::{ - env, - fmt::Write, - fs, + env, fs, path::{Path, PathBuf}, sync::OnceLock, }; @@ -148,42 +146,40 @@ impl QuizPreprocessor { self.add_aquascope_blocks(&mut content)?; let quiz_name = quiz_path_rel.file_stem().unwrap().to_string_lossy(); - let content_json = serde_json::to_string(&content)?; - let mut html = String::from("
"); - - Ok(html) + Ok(html.finish()) } } +#[derive(clap::Parser)] +#[clap(author, about, version)] +struct QuizArgs; + impl SimplePreprocessor for QuizPreprocessor { + type Args = QuizArgs; + fn name() -> &'static str { "quiz" } @@ -284,8 +280,8 @@ mod test { fs::write( &chapter_path, r#" - *Hello world!* - + *Hello world!* + {{#quiz ../quiz.toml}} "#, )?; diff --git a/js/packages/quiz-embed/src/main.tsx b/js/packages/quiz-embed/src/main.tsx index 3080ec6..a2ca33f 100644 --- a/js/packages/quiz-embed/src/main.tsx +++ b/js/packages/quiz-embed/src/main.tsx @@ -30,12 +30,16 @@ let initQuizzes = () => { document.querySelectorAll(".quiz-placeholder").forEach(el => { let divEl = el as HTMLDivElement; - let name = divEl.dataset.quizName!; - let quiz: Quiz = JSON.parse(divEl.dataset.quizQuestions!); + let name = JSON.parse(divEl.dataset.quizName!) as string; + let quiz = JSON.parse(divEl.dataset.quizQuestions!) as Quiz; let root = ReactDOM.createRoot(el); - let fullscreen = divEl.dataset.quizFullscreen !== undefined; - let cacheAnswers = divEl.dataset.quizCacheAnswers !== undefined; - let showBugReporter = divEl.dataset.quizShowBugReporter !== undefined; + + let maybeParseJson = (s: string | undefined): T | undefined => + s ? JSON.parse(s) : undefined; + let fullscreen = maybeParseJson(divEl.dataset.quizFullscreen) === true; + let cacheAnswers = maybeParseJson(divEl.dataset.quizCacheAnswers) === true; + let showBugReporter = maybeParseJson(divEl.dataset.quizShowBugReporter) === true; + root.render( = observer( } window.telemetry?.log("answers", { - quizName: name, + quizName: config.name, quizHash, answers: state.answers, attempt: state.attempt