1use super::block_rule::BlockFerris;
4use super::inline_rule::InlineFerris;
5use markdown_that::parser::core::CoreRule;
6use markdown_that::{MarkdownThat, Node, NodeValue, Renderer};
7
8#[derive(Debug)]
9pub struct FerrisCounter(usize);
12
13impl NodeValue for FerrisCounter {
15 fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
16 let mut attrs = node.attrs.clone();
19
20 attrs.push(("class", "ferris-counter".into()));
22
23 fmt.cr(); fmt.open("footer", &attrs);
25 #[allow(clippy::useless_format)] fmt.text(&match self.0 {
27 0 => format!("No crabs around here."),
28 1 => format!("There is a crab lurking in this document."),
29 _ => format!("There are {} crabs lurking in this document.", self.0),
30 });
31 fmt.close("footer");
32 fmt.cr();
33 }
34}
35
36struct FerrisCounterRule;
38
39impl CoreRule for FerrisCounterRule {
40 fn run(root: &mut Node, _: &MarkdownThat) {
46 let mut counter = 0;
47
48 root.walk(|node, _| {
51 if node.is::<InlineFerris>() || node.is::<BlockFerris>() {
52 counter += 1;
53 }
54 });
55
56 root.children.push(Node::new(FerrisCounter(counter)))
58 }
59}
60
61pub fn add(md: &mut MarkdownThat) {
62 md.add_rule::<FerrisCounterRule>();
64}