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
|
//! Code (indented) is a construct that occurs in the [flow][] content type.
//!
//! It forms with the following BNF:
//!
//! ```bnf
//! code_indented ::= indented_filled_line *( eol *( blank_line eol ) indented_filled_line )
//!
//! ; Restriction: at least one `code` must not be whitespace.
//! indented_filled_line ::= 4space_or_tab *code
//! blank_line ::= *space_or_tab
//! eol ::= '\r' | '\r\n' | '\n'
//! code ::= . ; any unicode code point (other than line endings).
//! space_or_tab ::= ' ' | '\t'
//! ```
//!
//! Code (indented) relates to both the `<pre>` and the `<code>` elements in
//! HTML.
//! See [*§ 4.4.3 The `pre` element*][html-pre] and the [*§ 4.5.15 The `code`
//! element*][html-code] in the HTML spec for more info.
//!
//! In markdown, it is also possible to use [code (text)][code_text] in the
//! [text][] content type.
//! It is also possible to create code with the [code (fenced)][code_fenced]
//! construct.
//! That construct is more explicit, more similar to code (text), and has
//! support for specifying the programming language that the code is in, so it
//! is recommended to use that instead of indented code.
//!
//! ## Tokens
//!
//! * [`CodeIndented`][Token::CodeIndented]
//! * [`CodeFlowChunk`][Token::CodeFlowChunk]
//! * [`LineEnding`][Token::LineEnding]
//! * [`SpaceOrTab`][Token::SpaceOrTab]
//!
//! ## References
//!
//! * [`code-indented.js` in `micromark`](https://github.com/micromark/micromark/blob/main/packages/micromark-core-commonmark/dev/lib/code-indented.js)
//! * [*§ 4.4 Indented code blocks* in `CommonMark`](https://spec.commonmark.org/0.30/#indented-code-blocks)
//!
//! [flow]: crate::content::flow
//! [text]: crate::content::text
//! [code_text]: crate::construct::code_text
//! [code_fenced]: crate::construct::code_fenced
//! [html-pre]: https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element
//! [html-code]: https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-code-element
use super::partial_space_or_tab::{space_or_tab, space_or_tab_min_max};
use crate::constant::TAB_SIZE;
use crate::token::Token;
use crate::tokenizer::{Code, State, Tokenizer};
/// Start of code (indented).
///
/// > **Parsing note**: it is not needed to check if this first line is a
/// > filled line (that it has a non-whitespace character), because blank lines
/// > are parsed already, so we never run into that.
///
/// ```markdown
/// > | aaa
/// ^
/// ```
pub fn start(tokenizer: &mut Tokenizer) -> State {
// Do not interrupt paragraphs.
if tokenizer.interrupt || !tokenizer.parse_state.constructs.code_indented {
State::Nok
} else {
tokenizer.enter(Token::CodeIndented);
tokenizer.go(space_or_tab_min_max(TAB_SIZE, TAB_SIZE), at_break)(tokenizer)
}
}
/// At a break.
///
/// ```markdown
/// > | aaa
/// ^ ^
/// ```
fn at_break(tokenizer: &mut Tokenizer) -> State {
match tokenizer.current {
Code::None => after(tokenizer),
Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => tokenizer
.attempt(further_start, |ok| {
Box::new(if ok { at_break } else { after })
})(tokenizer),
_ => {
tokenizer.enter(Token::CodeFlowChunk);
content(tokenizer)
}
}
}
/// Inside code content.
///
/// ```markdown
/// > | aaa
/// ^^^^
/// ```
fn content(tokenizer: &mut Tokenizer) -> State {
match tokenizer.current {
Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => {
tokenizer.exit(Token::CodeFlowChunk);
at_break(tokenizer)
}
_ => {
tokenizer.consume();
State::Fn(Box::new(content))
}
}
}
/// After indented code.
///
/// ```markdown
/// > | aaa
/// ^
/// ```
fn after(tokenizer: &mut Tokenizer) -> State {
tokenizer.exit(Token::CodeIndented);
// Feel free to interrupt.
tokenizer.interrupt = false;
State::Ok
}
/// Right at a line ending, trying to parse another indent.
///
/// ```markdown
/// > | aaa
/// ^
/// | bbb
/// ```
fn further_start(tokenizer: &mut Tokenizer) -> State {
if tokenizer.lazy {
State::Nok
} else {
match tokenizer.current {
Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => {
tokenizer.enter(Token::LineEnding);
tokenizer.consume();
tokenizer.exit(Token::LineEnding);
State::Fn(Box::new(further_start))
}
_ => tokenizer.attempt(space_or_tab_min_max(TAB_SIZE, TAB_SIZE), |ok| {
Box::new(if ok { further_end } else { further_begin })
})(tokenizer),
}
}
}
/// After a proper indent.
///
/// ```markdown
/// | aaa
/// > | bbb
/// ^
/// ```
fn further_end(_tokenizer: &mut Tokenizer) -> State {
State::Ok
}
/// At the beginning of a line that is not indented enough.
///
/// ```markdown
/// | aaa
/// > | bbb
/// ^
/// ```
fn further_begin(tokenizer: &mut Tokenizer) -> State {
tokenizer.attempt_opt(space_or_tab(), further_after)(tokenizer)
}
/// After whitespace, not indented enough.
///
/// ```markdown
/// | aaa
/// > | bbb
/// ^
/// ```
fn further_after(tokenizer: &mut Tokenizer) -> State {
match tokenizer.current {
Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => further_start(tokenizer),
_ => State::Nok,
}
}
|