1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#![deny(
clippy::all,
missing_debug_implementations,
missing_copy_implementations,
missing_docs,
clippy::cargo
)]
#![warn(clippy::pedantic, clippy::nursery)]
use mdbook::book::{Book, Chapter};
use mdbook::errors::Error;
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
use mdbook::BookItem;
use pulldown_cmark::{CodeBlockKind, CowStr, Event, Options, Parser, Tag};
use pulldown_cmark_to_cmark::cmark;
mod backend;
use backend::{Backend, RenderContext};
mod config;
#[derive(Default, Clone, Copy, Debug)]
pub struct D2;
impl Preprocessor for D2 {
fn name(&self) -> &str {
"d2"
}
fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {
let backend = Backend::from_context(ctx);
book.for_each_mut(|section| {
if let BookItem::Chapter(chapter) = section {
let events = process_events(
&backend,
chapter,
Parser::new_ext(&chapter.content, Options::all()),
);
let mut buf = String::with_capacity(chapter.content.len() + 128);
cmark(events, &mut buf).unwrap();
chapter.content = buf;
}
});
Ok(book)
}
}
fn process_events<'a>(
backend: &'a Backend,
chapter: &'a Chapter,
events: impl Iterator<Item = Event<'a>> + 'a,
) -> impl Iterator<Item = Event<'a>> + 'a {
let mut in_block = false;
let mut diagram = String::new();
let mut diagram_index = 0;
events.flat_map(move |event| {
match (&event, in_block) {
(
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(CowStr::Borrowed("d2")))),
false,
) => {
in_block = true;
diagram.clear();
diagram_index += 1;
vec![]
}
(Event::Text(content), true) => {
diagram.push_str(content);
vec![]
}
(Event::End(Tag::CodeBlock(CodeBlockKind::Fenced(CowStr::Borrowed("d2")))), true) => {
in_block = false;
let render_context = RenderContext::new(
chapter.source_path.as_ref().unwrap(),
&chapter.name,
chapter.number.as_ref(),
diagram_index,
);
match backend.render(&render_context, &diagram) {
Ok(events) => events,
Err(e) => {
eprintln!("{e}");
vec![]
}
}
}
_ => vec![event],
}
})
}