diff options
author | Jan-Erik Rediger <janerik@fnordig.de> | 2019-07-09 11:15:10 +0200 |
---|---|---|
committer | Jan-Erik Rediger <janerik@fnordig.de> | 2019-07-09 11:15:10 +0200 |
commit | 80faf141da2515eb098ce051eaa155b50c6b6afe (patch) | |
tree | f9ca2e6c53e6db99d4671f8bf1cdfb3bdc486c9d /src | |
parent | 8c2a8c2d5c05b012e14f66208781738ea5b0400a (diff) |
Update dependencies and refactor mdbook-toc into a preprocessor
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/mdbook-toc.rs | 64 | ||||
-rw-r--r-- | src/main.rs | 64 |
2 files changed, 64 insertions, 64 deletions
diff --git a/src/bin/mdbook-toc.rs b/src/bin/mdbook-toc.rs new file mode 100644 index 0000000..a375261 --- /dev/null +++ b/src/bin/mdbook-toc.rs @@ -0,0 +1,64 @@ +extern crate clap; +extern crate mdbook; +extern crate mdbook_toc; +extern crate serde_json; + +use clap::{App, Arg, ArgMatches, SubCommand}; +use mdbook::errors::Error; +use mdbook::preprocess::{CmdPreprocessor, Preprocessor}; +use mdbook_toc::Toc; + +use std::io; +use std::process; + +pub fn make_app() -> App<'static, 'static> { + App::new("mdbook-toc") + .about("mdbook preprocessor to Table of Contents") + .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 { + eprintln!( + "Warning: The mdbook-toc preprocessor was built against version \ + {} of mdbook, but we're being called from version {}", + mdbook::MDBOOK_VERSION, + ctx.mdbook_version + ); + } + + let processed_book = Toc.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 = Toc.supports_renderer(&renderer); + + // Signal whether the renderer is supported by exiting with 1 or 0. + if supported { + process::exit(0); + } else { + process::exit(1); + } +} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 20fd5bb..0000000 --- a/src/main.rs +++ /dev/null @@ -1,64 +0,0 @@ -extern crate env_logger; -extern crate mdbook; -extern crate mdbook_toc; -extern crate clap; - -use mdbook::MDBook; -use mdbook::errors::Result; -use mdbook_toc::Toc; -use clap::{App, ArgMatches}; - -use std::env; -use std::process; -use std::path::{Path, PathBuf}; - -fn get_book_dir(args: &ArgMatches) -> PathBuf { - if let Some(dir) = args.value_of("dir") { - // Check if path is relative from current dir, or absolute... - let p = Path::new(dir); - if p.is_relative() { - env::current_dir().unwrap().join(dir) - } else { - p.to_path_buf() - } - } else { - env::current_dir().expect("Unable to determine the current directory") - } -} - -pub fn make_app<'a, 'b>() -> App<'a, 'b> { - App::new("mdbook-toc") - .about("Build the book from the markdown files with ToC support") - .arg_from_usage( - "-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book \ - when omitted)'", - ) - .arg_from_usage( - "[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'", - ) -} - -pub fn execute(args: &ArgMatches) -> Result<()> { - let book_dir = get_book_dir(args); - let mut book = MDBook::load(&book_dir)?; - - if let Some(dest_dir) = args.value_of("dest-dir") { - book.config.build.build_dir = PathBuf::from(dest_dir); - } - - book.with_preprecessor(Toc); - book.build()?; - - Ok(()) -} - -fn main() { - env_logger::init(); - let app = make_app(); - let matches = app.get_matches(); - - if let Err(e) = execute(&matches) { - eprintln!("{}", e); - process::exit(1); - } -} |