subscript_compiler/
cli.rs

1use std::path::PathBuf;
2use structopt::StructOpt;
3
4#[derive(StructOpt, Debug)]
5#[structopt(
6    name="subscript",
7    about = "compile subscript markup into HTML, or PDF (WIP)",
8)]
9enum Cli {
10    Compile {
11        #[structopt(short, long, parse(from_os_str))]
12        source: PathBuf,
13        #[structopt(short, long, parse(from_os_str))]
14        output: Option<PathBuf>,
15    },
16}
17
18pub fn run_cli() {
19    match Cli::from_args() {
20        Cli::Compile{source: source_path, output} => {
21            let source = std::fs::read_to_string(&source_path).unwrap();
22            let output_path = output.unwrap_or_else(|| {
23                let source_path = source_path.clone();
24                let default = std::ffi::OsStr::new("html");
25                let ext = source_path.extension().unwrap_or(default);
26                let mut path = source_path.clone();
27                assert!(path.set_extension(ext));
28                path
29            });
30            if let Some(parent) = output_path.parent() {
31                let _ = std::fs::create_dir_all(parent);
32            }
33            // let output = crate::frontend::pass::to_html::compile_to_html(&source);
34            let output = crate::codegen::html::Document::from_source(&source);
35            let output = output.render_to_string();
36            std::fs::write(&output_path, output).unwrap();
37        }
38    }
39}
40
41