textlane-html 0.6.0

Textlane's HTML plugins: parsing, transforms, sanitizing and serializing
Documentation
use textlane_core::{Allocator, Attach, Error, File, Plugin, Transform, Tree, TreeKind};

use crate::actions::hast::autolink::link_headings;
use crate::actions::hast::slug::slug_hast;
use crate::models::options::{Autolink, AutolinkOptions, Slug};

impl Plugin for Slug {
    fn attach(&self, context: &mut Attach) -> Result<(), Error> {
        context.add_transform(Box::new(self.clone()));
        Ok(())
    }
}

impl Transform for Slug {
    fn input(&self) -> TreeKind {
        TreeKind::Hast
    }

    fn output(&self) -> TreeKind {
        TreeKind::Hast
    }

    fn run<'a>(
        &self,
        tree: Tree<'a>,
        _file: &mut File,
        alloc: &'a Allocator,
    ) -> Result<Tree<'a>, Error> {
        match tree {
            Tree::Hast(mut node) => {
                slug_hast(&mut node, self, alloc);
                match &self.autolink {
                    Some(Autolink::Enabled(true)) => {
                        link_headings(&mut node, &AutolinkOptions::default(), alloc)?;
                    }
                    Some(Autolink::Options(options)) => {
                        link_headings(&mut node, options, alloc)?
                    }
                    Some(Autolink::Enabled(false)) | None => {}
                }
                Ok(Tree::Hast(node))
            }
            _ => Err(Error::new("invalid-stage", "`html.slug` takes hast")),
        }
    }
}