1use anyhow::{Context as _, Result};
2use clap::{Arg, ArgMatches, Command};
3use docs_preprocessor::ZedDocsPreprocessor;
4use mdbook::preprocess::{CmdPreprocessor, Preprocessor};
5use std::io::{self, Read};
6use std::process;
7
8pub fn make_app() -> Command {
9 Command::new("zed-docs-preprocessor")
10 .about("Preprocesses Zed Docs content to provide rich action & keybinding support and more")
11 .subcommand(
12 Command::new("supports")
13 .arg(Arg::new("renderer").required(true))
14 .about("Check whether a renderer is supported by this preprocessor"),
15 )
16}
17
18fn main() -> Result<()> {
19 let matches = make_app().get_matches();
20
21 let preprocessor =
22 ZedDocsPreprocessor::new().context("Failed to create ZedDocsPreprocessor")?;
23
24 if let Some(sub_args) = matches.subcommand_matches("supports") {
25 handle_supports(&preprocessor, sub_args);
26 } else {
27 handle_preprocessing(&preprocessor)?;
28 }
29
30 Ok(())
31}
32
33fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<()> {
34 let mut stdin = io::stdin();
35 let mut input = String::new();
36 stdin.read_to_string(&mut input)?;
37
38 let (ctx, book) = CmdPreprocessor::parse_input(input.as_bytes())?;
39
40 let processed_book = pre.run(&ctx, book)?;
41
42 serde_json::to_writer(io::stdout(), &processed_book)?;
43
44 Ok(())
45}
46
47fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! {
48 let renderer = sub_args
49 .get_one::<String>("renderer")
50 .expect("Required argument");
51 let supported = pre.supports_renderer(renderer);
52
53 if supported {
54 process::exit(0);
55 } else {
56 process::exit(1);
57 }
58}