diff --git a/nostarch/book.toml b/nostarch/book.toml index ce0abdc510..9ce0184ea2 100644 --- a/nostarch/book.toml +++ b/nostarch/book.toml @@ -14,5 +14,8 @@ build-dir = "../tmp" [preprocessor.trpl-listing] output-mode = "simple" +[preprocessor.trpl-figure] +output-mode = "simple" + [rust] edition = "2021" diff --git a/packages/mdbook_trpl/Cargo.toml b/packages/mdbook_trpl/Cargo.toml index 84c478abae..a28df11d6e 100644 --- a/packages/mdbook_trpl/Cargo.toml +++ b/packages/mdbook_trpl/Cargo.toml @@ -11,6 +11,10 @@ path = "src/bin/note/main.rs" name = "mdbook-trpl-listing" path = "src/bin/listing/main.rs" +[[bin]] +name = "mdbook-trpl-figure" +path = "src/bin/figure/main.rs" + [dependencies] clap = { version = "4", features = ["derive"] } html_parser = "0.7.0" diff --git a/packages/mdbook_trpl/src/bin/figure/main.rs b/packages/mdbook_trpl/src/bin/figure/main.rs new file mode 100644 index 0000000000..e780cbd4e7 --- /dev/null +++ b/packages/mdbook_trpl/src/bin/figure/main.rs @@ -0,0 +1,42 @@ +use std::io; + +use clap::{self, Parser, Subcommand}; + +use mdbook::preprocess::{CmdPreprocessor, Preprocessor}; +use mdbook_trpl::Figure; + +fn main() -> Result<(), String> { + match Cli::parse().command { + Some(Command::Supports { renderer }) => { + if Figure.supports_renderer(&renderer) { + Ok(()) + } else { + Err(format!("Renderer '{renderer}' is unsupported")) + } + } + None => { + let (ctx, book) = CmdPreprocessor::parse_input(io::stdin()) + .map_err(|e| format!("{e}"))?; + let processed = + Figure.run(&ctx, book).map_err(|e| format!("{e}"))?; + serde_json::to_writer(io::stdout(), &processed) + .map_err(|e| format!("{e}")) + } + } +} + +/// A simple preprocessor for handling figures with images in _The Rust +/// Programming Language_ book. +#[derive(Parser, Debug)] +struct Cli { + #[command(subcommand)] + command: Option, +} + +#[derive(Subcommand, Debug)] +enum Command { + /// Is the renderer supported? + /// + /// Supported renderers are `'html'`, `'markdown'`, and `'test'`. + Supports { renderer: String }, +} diff --git a/packages/mdbook_trpl/src/figure/mod.rs b/packages/mdbook_trpl/src/figure/mod.rs new file mode 100644 index 0000000000..72b38a18c1 --- /dev/null +++ b/packages/mdbook_trpl/src/figure/mod.rs @@ -0,0 +1,54 @@ +use mdbook::{book::Book, errors::Result, preprocess::Preprocessor}; + +use pulldown_cmark::Event::{self, *}; + +use crate::config::Mode; + +/// A simple preprocessor to rewrite `
`s with ``s. +/// +/// This is a no-op by default; it only operates on the book chapters when the +/// `[preprocessor.trpl-figure]` has `output-mode = "simple"`. +/// +/// Takes in Markdown containing like this: +/// +/// ```markdown +///
+/// +/// +/// +///
Figure 1-2: A description of the image
+/// +///
+/// ``` +/// +/// Spits out Markdown like this: +/// +/// ```markdown +/// +/// +/// Figure 1-2: A description of the image +/// ``` +pub struct TrplFigure; +impl TrplFigure { + pub fn supports_renderer(&self, renderer: &str) -> bool { + renderer == "html" || renderer == "markdown" || renderer == "test" + } +} + +impl Preprocessor for TrplFigure { + fn name(&self) -> &str { + "trpl-figure" + } + + fn run( + &self, + ctx: &mdbook::preprocess::PreprocessorContext, + book: Book, + ) -> Result { + if let Mode::Simple = Mode::from_context(ctx, self.name())? {} + todo!(); + Ok(book) + } +} + +mod tests; diff --git a/packages/mdbook_trpl/src/figure/tests.rs b/packages/mdbook_trpl/src/figure/tests.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/mdbook_trpl/src/lib.rs b/packages/mdbook_trpl/src/lib.rs index 806283e417..65611c0b21 100644 --- a/packages/mdbook_trpl/src/lib.rs +++ b/packages/mdbook_trpl/src/lib.rs @@ -1,6 +1,11 @@ -pub mod config; +mod config; +mod error; +mod figure; mod listing; mod note; +pub use config::Mode; +pub use error::Report; +pub use figure::TrplFigure as Figure; pub use listing::TrplListing as Listing; pub use note::TrplNote as Note;