use std::path::{Path, PathBuf};
use serde_json::Value;
use crate::error::DocError;
use crate::extension::MarkdownProcessor;
#[cfg(feature = "cite")]
pub struct CiteProcessor {
bibliography: mini_cite::Bibliography,
bib_dir: PathBuf,
}
#[cfg(feature = "cite")]
impl CiteProcessor {
pub fn new(bib_dir: impl AsRef<Path>) -> Result<Self, mini_cite::CiteError> {
let bib_dir = bib_dir.as_ref().to_path_buf();
Ok(Self {
bibliography: mini_cite::Bibliography::load_dir(&bib_dir)?,
bib_dir,
})
}
pub fn bib_dir(&self) -> &Path {
&self.bib_dir
}
pub fn len(&self) -> usize {
self.bibliography.len()
}
pub fn is_empty(&self) -> bool {
self.bibliography.is_empty()
}
}
#[cfg(feature = "cite")]
impl MarkdownProcessor for CiteProcessor {
fn process(&self, body: &str, _frontmatter: &Value) -> Result<String, DocError> {
mini_cite::process(body, &self.bibliography)
.map_err(|e| DocError::Extension(format!("cite: {e}")))
}
fn name(&self) -> &str {
"cite"
}
}