mod adoc;
pub mod hbs;
use {
anyhow::{Context, Result},
std::{fmt::Write, fs, path::Path},
};
pub use self::adoc::AdocRunContext;
use self::hbs::HbsContext;
pub fn convert_adoc(
src_file: &Path,
dummy_dst_name: &str,
acx: &AdocRunContext,
hcx: &HbsContext,
) -> Result<String> {
let mut buf = String::with_capacity(5 * 1024);
self::convert_adoc_buf(&mut buf, src_file, dummy_dst_name, acx, hcx)?;
Ok(buf)
}
pub fn convert_adoc_buf(
buf: &mut String,
src_file: &Path,
dummy_dst_name: &str,
acx: &AdocRunContext,
hcx: &HbsContext,
) -> Result<()> {
ensure!(
src_file.is_file(),
"Given invalid source file path: {}",
src_file.display()
);
let metadata = {
let adoc_text = fs::read_to_string(src_file).context("Unable to read source file")?;
adoc::AdocMetadata::extract_with_base(&adoc_text, &acx)
};
let mut acx = acx.clone();
if metadata.find_attr("hbs").is_some() {
acx.set_embedded_mode(true);
}
buf.clear();
adoc::run_asciidoctor_buf(buf, src_file, dummy_dst_name, &acx)?;
if let Some(hbs_attr) = metadata.find_attr("hbs") {
let src_name = format!("{}", src_file.display());
let hbs_file_path = {
let hbs_name = hbs_attr
.value()
.ok_or_else(|| anyhow!("`hbs` attribute without path"))?;
hcx.src_dir.join(hbs_name)
};
let output = {
let hbs_dir = hbs_file_path.parent().unwrap();
let mut hbs = hbs::init_hbs(&hbs_dir)?;
hbs::render_hbs(buf, &src_name, &metadata, &mut hbs, &hbs_file_path, hcx)?
};
buf.clear();
buf.write_str(&output)?;
}
Ok(())
}