1
Fork 0

Update dependencies and refactor mdbook-toc into a preprocessor

This commit is contained in:
Jan-Erik Rediger 2019-07-09 11:15:10 +02:00
parent 8c2a8c2d5c
commit 80faf141da
4 changed files with 869 additions and 702 deletions

1430
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -4,9 +4,10 @@ version = "0.1.0"
authors = ["Jan-Erik Rediger <janerik@fnordig.de>"] authors = ["Jan-Erik Rediger <janerik@fnordig.de>"]
[dependencies] [dependencies]
mdbook = { git = "https://github.com/badboy/mdBook", branch = "fix-section-numbering" } mdbook = "0.3.0"
pulldown-cmark = "0.1.2" pulldown-cmark = "0.5"
pulldown-cmark-to-cmark = "1.1.0" pulldown-cmark-to-cmark = "1.2"
env_logger = "0.5.10" env_logger = "0.6"
log = "0.4.3" log = "0.4"
clap = "2.32.0" clap = "2.33"
serde_json = "1.0"

64
src/bin/mdbook-toc.rs Normal file
View file

@ -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);
}
}

View file

@ -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);
}
}