From 00b0dc309e3526a3d725b1dffe2b766715a0747f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABlle=20Huisman?= Date: Mon, 8 Jul 2024 09:41:53 +0200 Subject: [PATCH] Add cmark to processor --- book/mdbook-trunk/Cargo.toml | 2 + book/mdbook-trunk/src/preprocessor.rs | 91 ++++++++++++++++++++++++--- 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/book/mdbook-trunk/Cargo.toml b/book/mdbook-trunk/Cargo.toml index 5d7e5167..0bf7fa2f 100644 --- a/book/mdbook-trunk/Cargo.toml +++ b/book/mdbook-trunk/Cargo.toml @@ -12,5 +12,7 @@ version.workspace = true [dependencies] clap = { version = "4.5.8", features = ["derive"] } mdbook = "0.4.40" +pulldown-cmark = "0.11.0" +pulldown-cmark-to-cmark = "15.0.1" semver = "1.0.23" serde_json = "1.0.120" diff --git a/book/mdbook-trunk/src/preprocessor.rs b/book/mdbook-trunk/src/preprocessor.rs index 6cfa37cb..f4d97862 100644 --- a/book/mdbook-trunk/src/preprocessor.rs +++ b/book/mdbook-trunk/src/preprocessor.rs @@ -1,9 +1,10 @@ use mdbook::{ book::{Book, Chapter}, - errors::Error, preprocess::{Preprocessor, PreprocessorContext}, BookItem, }; +use pulldown_cmark::{Event, Parser}; +use pulldown_cmark_to_cmark::cmark; pub struct TrunkPreprocessor; @@ -12,7 +13,25 @@ impl TrunkPreprocessor { Self } - fn process_chapter(&self, _chapter: &mut Chapter) {} + fn process_chapter(&self, chapter: &mut Chapter) -> Result<(), mdbook::errors::Error> { + let mut buf = String::with_capacity(chapter.content.len()); + + let events = Parser::new(&chapter.content).map(|event| { + println!("{:?}", event); + + if let Event::Text(_text) = &event {} + + event + }); + + if let Err(err) = cmark(events, &mut buf) { + Err(err.into()) + } else { + chapter.content = buf; + + Ok(()) + } + } } impl Default for TrunkPreprocessor { @@ -26,14 +45,14 @@ impl Preprocessor for TrunkPreprocessor { "trunk" } - fn run(&self, _ctx: &PreprocessorContext, book: Book) -> Result { + fn run(&self, _ctx: &PreprocessorContext, book: Book) -> Result { let mut book = book.clone(); - book.for_each_mut(|section| match section { - BookItem::Chapter(chapter) => self.process_chapter(chapter), - BookItem::Separator => {} - BookItem::PartTitle(_) => {} - }); + for section in &mut book.sections { + if let BookItem::Chapter(chapter) = section { + self.process_chapter(chapter)?; + } + } Ok(book) } @@ -42,3 +61,59 @@ impl Preprocessor for TrunkPreprocessor { renderer == "html" } } + +#[cfg(test)] +mod test { + use mdbook::preprocess::CmdPreprocessor; + + use super::*; + + #[test] + fn test_run() { + let input_json = r##"[ + { + "root": "/path/to/book", + "config": { + "book": { + "authors": ["AUTHOR"], + "language": "en", + "multilingual": false, + "src": "src", + "title": "TITLE" + }, + "preprocessor": { + "nop": {} + } + }, + "renderer": "html", + "mdbook_version": "0.4.21" + }, + { + "sections": [ + { + "Chapter": { + "name": "Chapter 1", + "content": "# Chapter 1\n{{#trunk example.rs}}\n", + "number": [1], + "sub_items": [], + "path": "chapter_1.md", + "source_path": "chapter_1.md", + "parent_names": [] + } + } + ], + "__non_exhaustive": null + } + ]"## + .as_bytes(); + + let (ctx, book) = CmdPreprocessor::parse_input(input_json).unwrap(); + let expected_book = book.clone(); + + let result = TrunkPreprocessor::new().run(&ctx, book); + assert!(result.is_ok()); + + let actual_book = result.unwrap(); + assert_eq!(actual_book, expected_book); + } +}