use markdown_that::parser::block::{BlockRule, BlockState};
use markdown_that::{MarkdownThat, Node, NodeValue, Renderer};
const CRAB_CLAW: &str = r#"(\/)"#;
const CRAB_URL: &str = "https://upload.wikimedia.org/wikipedia/commons/0/0f/Original_Ferris.svg";
#[derive(Debug)]
pub struct BlockFerris;
impl NodeValue for BlockFerris {
fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
let mut attrs_div = node.attrs.clone();
attrs_div.push(("class", "ferris-block".into()));
let attrs_img = vec![("src", CRAB_URL.into())];
fmt.cr(); fmt.open("div", &attrs_div); fmt.self_close("img", &attrs_img); fmt.close("div"); fmt.cr();
}
}
struct FerrisBlockScanner;
impl BlockRule for FerrisBlockScanner {
fn run(state: &mut BlockState) -> Option<(Node, usize)> {
let line = state.get_line(state.line).trim();
if !line.starts_with(CRAB_CLAW) {
return None;
}
if !line.ends_with(CRAB_CLAW) {
return None;
}
if line.len() < CRAB_CLAW.len() * 2 + 4 {
return None;
}
let dashes = &line[CRAB_CLAW.len()..line.len() - CRAB_CLAW.len()];
if dashes.chars().any(|c| c != '-') {
return None;
}
Some((Node::new(BlockFerris), 1))
}
}
pub fn add(md: &mut MarkdownThat) {
md.block.add_rule::<FerrisBlockScanner>();
}