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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#![doc = include_str!("../README.md")]
use logos::Logos;
use pulldown_cmark::{CodeBlockKind, Event, Tag};
pub mod languages;
pub trait Highlight: Sized {
const LANG: &'static str;
fn kind(tokens: &[Self; 2]) -> Kind;
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Kind {
None,
Glyph,
Literal,
Identifier,
SpecialIdentifier,
Keyword,
Comment,
}
static HIGHLIGHT_TAG: [Option<&'static str>; 7] = {
let mut tags = [None; 7];
tags[Kind::Glyph as usize] = Some("u");
tags[Kind::Literal as usize] = Some("span");
tags[Kind::Identifier as usize] = Some("var");
tags[Kind::SpecialIdentifier as usize] = Some("em");
tags[Kind::Keyword as usize] = Some("b");
tags[Kind::Comment as usize] = Some("i");
tags
};
#[derive(Debug, Default)]
pub struct SyntaxPreprocessor<'a, I: Iterator<Item = Event<'a>>> {
parent: I,
}
impl<'a, I: Iterator<Item = Event<'a>>> SyntaxPreprocessor<'a, I> {
pub fn new(parent: I) -> Self {
Self { parent }
}
}
impl<'a, I: Iterator<Item = Event<'a>>> Iterator for SyntaxPreprocessor<'a, I> {
type Item = Event<'a>;
fn next(&mut self) -> Option<Self::Item> {
let lang = match self.parent.next()? {
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(lang))) => lang,
other => return Some(other),
};
let mut code = String::new();
for event in &mut self.parent {
match event {
Event::Text(text) => code.push_str(&text),
Event::End(Tag::CodeBlock(CodeBlockKind::Fenced(ref l))) if *l == lang => break,
other => println!("Unexpected event {:#?}", other),
}
}
let mut html = String::with_capacity(code.len() + code.len() / 4 + 60);
html.push_str("<pre><code class=\"language-");
html.push_str(lang.as_ref());
html.push_str("\">");
match lang.as_ref() {
"rust" => highlight::<languages::Rust>(&code, &mut html),
"js" | "javascript" => highlight::<languages::JavaScript>(&code, &mut html),
"toml" => highlight::<languages::Toml>(&code, &mut html),
_ => write_escaped(&mut html, &code),
}
html.push_str("</code></pre>");
Some(Event::Html(html.into()))
}
}
#[inline]
fn write_escaped(s: &mut String, part: &str) {
let mut start = 0;
for (idx, byte) in part.bytes().enumerate() {
let replace = match byte {
b'<' => "<",
b'>' => ">",
b'&' => "&",
b'"' => """,
_ => continue,
};
s.push_str(&part[start..idx]);
s.push_str(replace);
start = idx + 1;
}
s.push_str(&part[start..]);
}
pub fn highlight<'a, Token>(source: &'a str, buf: &mut String)
where
Token: Highlight + Logos<'a, Source=str> + Eq + Copy + std::fmt::Debug,
Token::Extras: Default,
{
let mut lex = Token::lexer(source);
let mut open = Kind::None;
let mut last = 0usize;
let mut tokens = [Token::ERROR; 2];
while let Some(token) = lex.next() {
tokens[0] = tokens[1];
tokens[1] = dbg!(token);
dbg!(lex.slice());
let kind = Token::kind(&tokens);
if open != kind {
if let Some(tag) = HIGHLIGHT_TAG[open as usize] {
buf.push_str("</");
buf.push_str(tag);
buf.push('>');
}
write_escaped(buf, &source[last..lex.span().start]);
if let Some(tag) = HIGHLIGHT_TAG[kind as usize] {
buf.push('<');
buf.push_str(tag);
buf.push('>');
}
open = kind;
write_escaped(buf, lex.slice());
} else {
write_escaped(buf, &source[last..lex.span().end]);
}
last = lex.span().end;
}
if let Some(tag) = HIGHLIGHT_TAG[open as usize] {
buf.push_str("</");
buf.push_str(tag);
buf.push('>');
}
}