mod rustdoc;
mod toc;
use std::path::Path;
use anyhow::Result;
use string_sections::{prelude::Sections, SectionSpan};
use self::{rustdoc::RustDocFence, toc::TocFence};
pub trait Fence {
fn is_match(name: &str) -> bool
where
Self: Sized;
fn priority(self: &Self) -> u8;
fn create(document: &str, section: SectionSpan, template_dir: &Path) -> Result<Box<Self>>
where
Self: Sized;
fn run(self: &Self, document: &mut String) -> Result<()>;
}
fn create_fence(
document: &str,
section: SectionSpan,
template_dir: &Path,
) -> Result<Option<Box<dyn Fence>>> {
if TocFence::is_match(§ion.start_line) {
Ok(Some(TocFence::create(document, section, template_dir)?))
} else if RustDocFence::is_match(§ion.start_line) {
Ok(Some(RustDocFence::create(document, section, template_dir)?))
} else {
Ok(None)
}
}
pub fn find_fences(document: &str, template_dir: &Path) -> Result<Vec<Box<dyn Fence>>> {
let mut fences = Vec::new();
let section_iter = document.sections(
|line| line.starts_with("```"),
|sect| sect.end_line.starts_with("```"),
);
for section in section_iter {
if let Some(fence) = create_fence(document, section, template_dir)? {
fences.push(fence)
}
}
Ok(fences)
}