use anyhow::{bail, Context, Result};
use mdoc::{utils::write_file, DocumentBuilder};
use std::path::{Path, PathBuf};
pub fn build(path: Option<PathBuf>, output_type: Option<String>) -> Result<()> {
let builder = DocumentBuilder::new();
let mut doc = match path {
Some(path) => builder.source(path).build()?,
None => builder.build()?,
};
if let Some(output_type) = output_type {
doc.config.build.output = output_type;
}
let (data, extension) = match doc.config.build.output.as_str() {
"html" => (doc.html_bytes()?, "html"),
"latex" | "tex" => (doc.latex_bytes()?, "tex"),
"pdf" => (doc.pdf_bytes()?, "pdf"),
other => bail!("Unknown output type \"{}\"", other),
};
let filename = Path::new(&doc.config.filename()).with_extension(extension);
write_file(&filename, &data).context("Could not write to PDF file")?;
mdoc::success!("{:?}, built!", filename);
Ok(())
}