Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Upgrade to the new custom preprocessor system #2

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ version = "0.1.0"
authors = ["Jan-Erik Rediger <[email protected]>"]

[dependencies]
mdbook = { git = "https://github.com/badboy/mdBook", branch = "fix-section-numbering" }
mdbook = { git = "https://github.com/rust-lang-nursery/mdBook", branch = "custom-preprocessor" }
pulldown-cmark = "0.1.2"
pulldown-cmark-to-cmark = "1.1.0"
env_logger = "0.5.10"
log = "0.4.3"
clap = "2.32.0"
serde_json = "1.0"
66 changes: 66 additions & 0 deletions src/bin/mdbook-mermaid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
extern crate clap;
extern crate mdbook;
extern crate mdbook_mermaid;
extern crate serde_json;

use clap::{App, Arg, ArgMatches, SubCommand};
use mdbook::errors::Error;
use mdbook::preprocess::{CmdPreprocessor, Preprocessor};
use mdbook_mermaid::Mermaid;

use std::io;
use std::process;

pub fn make_app() -> App<'static, 'static> {
App::new("mdbook-mermaid")
.about("Build the book from the markdown files with mermaid support")
.subcommand(
SubCommand::with_name("supports")
.arg(Arg::with_name("renderer").required(true))
.about("Check whether a renderer is supported by this preprocessor"))
}

fn main() {
let matches = make_app().get_matches();

if let Some(sub_args) = matches.subcommand_matches("supports") {
handle_supports(sub_args);
} else {
if let Err(e) = handle_preprocessing() {
eprintln!("{}", e);
process::exit(1);
}
}
}

fn handle_preprocessing() -> Result<(), Error> {
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;

if ctx.mdbook_version != mdbook::MDBOOK_VERSION {
// We should probably use the `semver` crate to check compatibility
// here...
eprintln!(
"Warning: The mdbook-mermaid plugin was built against version \
{} of mdbook, but we're being called from version {}",
mdbook::MDBOOK_VERSION,
ctx.mdbook_version
);
}

let processed_book = Mermaid.run(&ctx, book)?;
serde_json::to_writer(io::stdout(), &processed_book)?;

Ok(())
}

fn handle_supports(sub_args: &ArgMatches) -> ! {
let renderer = sub_args.value_of("renderer").expect("Required argument");
let supported = Mermaid.supports_renderer(&renderer);

// Signal whether the renderer is supported by exiting with 1 or 0.
if supported {
process::exit(0);
} else {
process::exit(1);
}
}
64 changes: 0 additions & 64 deletions src/main.rs

This file was deleted.