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
use crate::{MarkdownIt, Node};
use crate::parser::inline::{InlineRule, InlineState, TextSpecial};
use crate::plugins::cmark::inline::newline::Hardbreak;
pub fn add(md: &mut MarkdownIt) {
md.inline.add_rule::<EscapeScanner>();
}
#[doc(hidden)]
pub struct EscapeScanner;
impl InlineRule for EscapeScanner {
const MARKER: char = '\\';
fn run(state: &mut InlineState, silent: bool) -> bool {
let mut chars = state.src[state.pos..state.pos_max].chars();
if chars.next().unwrap() != '\\' { return false; }
match chars.next() {
Some('\n') => {
let map_start = state.pos;
state.pos += 2;
let map_end = state.pos;
while let Some(' ' | '\t') = chars.next() {
state.pos += 1;
}
if !silent {
let mut node = Node::new(Hardbreak);
node.srcmap = state.get_map(map_start, map_end);
state.node.children.push(node);
}
true
}
Some(chr) => {
if !silent {
let mut orig_str = "\\".to_owned();
orig_str.push(chr);
let content_str = match chr {
'\\' | '!' | '"' | '#' | '$' | '%' | '&' | '\'' | '(' | ')' |
'*' | '+' | ',' | '.' | '/' | ':' | ';' | '<' | '=' | '>' | '?' |
'@' | '[' | ']' | '^' | '_' | '`' | '{' | '|' | '}' | '~' | '-' => chr.into(),
_ => orig_str.clone()
};
let mut node = Node::new(TextSpecial {
content: content_str,
markup: orig_str,
info: "escape",
});
node.srcmap = state.get_map(state.pos, state.pos + 1 + chr.len_utf8());
state.node.children.push(node);
}
state.pos += 1 + chr.len_utf8();
true
}
None => false
}
}
}