Skip to content

Commit

Permalink
infra: create mdbook-trpl-figure preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskrycho committed Nov 8, 2024
1 parent f4e1321 commit 3f5764a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
3 changes: 3 additions & 0 deletions nostarch/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ build-dir = "../tmp"
[preprocessor.trpl-listing]
output-mode = "simple"

[preprocessor.trpl-figure]
output-mode = "simple"

[rust]
edition = "2021"
4 changes: 4 additions & 0 deletions packages/mdbook_trpl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
42 changes: 42 additions & 0 deletions packages/mdbook_trpl/src/bin/figure/main.rs
Original file line number Diff line number Diff line change
@@ -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<Command>,
}

#[derive(Subcommand, Debug)]
enum Command {
/// Is the renderer supported?
///
/// Supported renderers are `'html'`, `'markdown'`, and `'test'`.
Supports { renderer: String },
}
54 changes: 54 additions & 0 deletions packages/mdbook_trpl/src/figure/mod.rs
Original file line number Diff line number Diff line change
@@ -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 `<figure>`s with `<img>`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>
///
/// <img src="http://www.example.com/some-cool-image.jpg">
///
/// <figcaption>Figure 1-2: A description of the image</figcaption>
///
/// </figure>
/// ```
///
/// Spits out Markdown like this:
///
/// ```markdown
/// <img src="http://www.example.com/some-cool-image.jpg">
///
/// 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<Book> {
if let Mode::Simple = Mode::from_context(ctx, self.name())? {}
todo!();
Ok(book)
}
}

mod tests;
Empty file.
7 changes: 6 additions & 1 deletion packages/mdbook_trpl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 3f5764a

Please sign in to comment.