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
182
183
184
185
186
187
188
189
//! Port of parser_block.ts: the block-level tokenizer.
use super::ruler::Ruler;
use super::rules_block;
use super::rules_block::state_block::StateBlock;
use super::token::Token;
use super::{Env, JsError, MarkdownIt};
/// A block rule: `(state, startLine, endLine, silent) => boolean`.
pub type BlockRule = fn(&mut StateBlock, usize, usize, bool) -> bool;
// First 2 params - rule name & source. Secondary array - list of rules,
// which can be terminated by this one.
const RULES: [(&str, BlockRule, &[&str]); 11] = [
(
"table",
rules_block::table::table,
&["paragraph", "reference"],
),
("code", rules_block::code::code, &[]),
(
"fence",
rules_block::fence::fence,
&["paragraph", "reference", "blockquote", "list"],
),
(
"blockquote",
rules_block::blockquote::blockquote,
&["paragraph", "reference", "blockquote", "list"],
),
(
"hr",
rules_block::hr::hr,
&["paragraph", "reference", "blockquote", "list"],
),
(
"list",
rules_block::list::list,
&["paragraph", "reference", "blockquote"],
),
("reference", rules_block::reference::reference, &[]),
(
"html_block",
rules_block::html_block::html_block,
&["paragraph", "reference", "blockquote"],
),
(
"heading",
rules_block::heading::heading,
&["paragraph", "reference", "blockquote"],
),
("lheading", rules_block::lheading::lheading, &[]),
("paragraph", rules_block::paragraph::paragraph, &[]),
];
/// `ParserBlock` (`md.block`).
#[derive(Debug, Clone)]
pub struct ParserBlock {
/// Keeps the configuration of block rules.
pub ruler: Ruler<BlockRule>,
}
impl Default for ParserBlock {
fn default() -> Self {
Self::new()
}
}
impl ParserBlock {
pub fn new() -> Self {
let mut ruler = Ruler::new();
for (name, rule, alt) in RULES {
ruler.push(name, rule, alt);
}
ParserBlock { ruler }
}
/// `tokenize(state, startLine, endLine)`: generate tokens for input range.
///
/// Throws (see [`crate::markdown_it::JsError`]) where JS throws: a rule that matched
/// without moving `state.line`, no rule matching at all (only possible when the paragraph
/// rule is disabled), and nesting past [`crate::markdown_it::MAX_DEPTH`]. Stops once
/// anything has thrown.
///
/// Lines past `state.line_max` (where JS reads `undefined` marks and goes on with NaN)
/// end the range.
pub fn tokenize(&self, state: &mut StateBlock, start_line: usize, end_line: usize) {
if !state.env.enter() {
return;
}
let end_line = end_line.min(state.line_max);
self.tokenize_lines(state, start_line, end_line);
state.env.leave();
}
fn tokenize_lines(&self, state: &mut StateBlock, start_line: usize, end_line: usize) {
let rules = self.ruler.get_rules("");
let len = rules.len();
let max_nesting = state.md.options.max_nesting;
let mut line = start_line;
let mut has_empty_lines = false;
while line < end_line {
line = state.skip_empty_lines(line);
state.line = line;
if line >= end_line {
break;
}
// Termination condition for nested calls.
// Nested calls currently used for blockquotes & lists
if state.s_count[line] < state.blk_indent {
break;
}
// If nesting level exceeded - skip tail to the end. That's not ordinary
// situation and we should not care about content.
if f64::from(state.level) >= max_nesting {
state.line = end_line;
break;
}
// Try all possible rules.
// On success, rule should:
//
// - update `state.line`
// - update `state.tokens`
// - return true
let prev_line = state.line;
let mut ok = false;
for i in 0..len {
ok = rules[i](state, line, end_line, false);
if state.env.has_thrown() {
return;
}
if ok {
if prev_line >= state.line {
state
.env
.throw(JsError::error("block rule didn't increment state.line"));
return;
}
break;
}
}
// this can only happen if user disables paragraph rule
if !ok {
state
.env
.throw(JsError::error("none of the block rules matched"));
return;
}
// set state.tight if we had an empty line before current tag
// i.e. latest empty line should not count
state.tight = !has_empty_lines;
// paragraph might "eat" one newline after it in nested lists
if state.is_empty(state.line - 1) {
has_empty_lines = true;
}
line = state.line;
if line < end_line && state.is_empty(line) {
has_empty_lines = true;
line += 1;
state.line = line;
}
}
}
/// `parse(src, md, env, outTokens)`: process input string and push block tokens into
/// `out_tokens`.
pub fn parse(&self, src: &[u16], md: &MarkdownIt, env: &mut Env, out_tokens: &mut Vec<Token>) {
if src.is_empty() {
return;
}
let mut state = StateBlock::new(src.to_vec(), md, env, std::mem::take(out_tokens));
let (line, line_max) = (state.line, state.line_max);
self.tokenize(&mut state, line, line_max);
*out_tokens = state.tokens;
}
}