textlane_html/plugins/stringify/
plugin.rs1use textlane_core::{Attach, Compiler, Error, File, HtmlTarget, Plugin, Tree, TreeKind};
2
3use crate::actions::hast::stringify::stringify_hast;
4use crate::models::options::Stringify;
5
6impl Plugin for Stringify {
7 fn attach(&self, context: &mut Attach) -> Result<(), Error> {
8 let compiler = self.clone();
9 context.set_compiler(move |_| Ok(Box::new(compiler)));
10 Ok(())
11 }
12}
13
14impl Compiler for Stringify {
15 fn input(&self) -> TreeKind {
16 TreeKind::Hast
17 }
18
19 fn extension(&self) -> &'static str {
20 "html"
21 }
22
23 fn compile(&self, tree: &Tree<'_>, _file: &mut File) -> Result<String, Error> {
24 match tree {
25 Tree::Hast(node) => stringify_hast(node, self),
26 _ => Err(Error::new("invalid-stage", "`html.stringify` takes hast")),
27 }
28 }
29
30 fn html_target(&self) -> Option<HtmlTarget> {
31 let defaults = Stringify {
32 allow_dangerous_html: self.allow_dangerous_html,
33 ..Stringify::default()
34 };
35 (*self == defaults).then_some(HtmlTarget {
36 allow_dangerous_html: self.allow_dangerous_html,
37 })
38 }
39}