-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
infra: create mdbook-trpl-figure preprocessor
- Loading branch information
1 parent
f4e1321
commit 3f5764a
Showing
6 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |