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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
//! Thematic breaks, sometimes called horizontal rules, are a construct that
//! occurs in the [flow][] content type.
//!
//! They’re formed with the following BNF:
//!
//! ```bnf
//! ; Restriction: all markers must be identical.
//! ; Restriction: at least 3 markers must be used.
//! thematic_break ::= *space_or_tab 1*(1*marker *space_or_tab)
//!
//! space_or_tab ::= ' ' | '\t'
//! marker ::= '*' | '-' | '_'
//! ```
//!
//! Thematic breaks in markdown typically relate to the HTML element `<hr>`.
//! See [*§ 4.4.2 The `hr` element* in the HTML spec][html] for more info.
//!
//! It is recommended to use exactly three asterisks without whitespace when
//! writing markdown.
//! As using more than three markers has no effect other than wasting space,
//! it is recommended to use exactly three markers.
//! Thematic breaks formed with asterisks or dashes can interfere with
//! [list][]s if there is whitespace between them: `* * *` and `- - -`.
//! For these reasons, it is recommend to not use spaces or tabs between the
//! markers.
//! Thematic breaks formed with dashes (without whitespace) can also form
//! [heading (setext)][heading_setext].
//! As dashes and underscores frequently occur in natural language and URLs, it
//! is recommended to use asterisks for thematic breaks to distinguish from
//! such use.
//! Because asterisks can be used to form the most markdown constructs, using
//! them has the added benefit of making it easier to gloss over markdown: you
//! can look for asterisks to find syntax while not worrying about other
//! characters.
//!
//! ## Tokens
//!
//! * [`ThematicBreak`][Token::ThematicBreak]
//! * [`ThematicBreakSequence`][Token::ThematicBreakSequence]
//!
//! ## References
//!
//! * [`thematic-break.js` in `micromark`](https://github.com/micromark/micromark/blob/main/packages/micromark-core-commonmark/dev/lib/thematic-break.js)
//! * [*§ 4.1 Thematic breaks* in `CommonMark`](https://spec.commonmark.org/0.30/#thematic-breaks)
//!
//! [flow]: crate::content::flow
//! [heading_setext]: crate::construct::heading_setext
//! [list]: crate::construct::list
//! [html]: https://html.spec.whatwg.org/multipage/grouping-content.html#the-hr-element
use super::partial_space_or_tab::{space_or_tab, space_or_tab_min_max};
use crate::constant::{TAB_SIZE, THEMATIC_BREAK_MARKER_COUNT_MIN};
use crate::token::Token;
use crate::tokenizer::{Code, State, Tokenizer};
/// Type of thematic break.
#[derive(Debug, PartialEq)]
enum Kind {
/// In a thematic break using asterisks (`*`).
///
/// ## Example
///
/// ```markdown
/// ***
/// ```
Asterisk,
/// In a thematic break using dashes (`-`).
///
/// ## Example
///
/// ```markdown
/// ---
/// ```
Dash,
/// In a thematic break using underscores (`_`).
///
/// ## Example
///
/// ```markdown
/// ___
/// ```
Underscore,
}
impl Kind {
/// Turn the kind into a [char].
fn as_char(&self) -> char {
match self {
Kind::Asterisk => '*',
Kind::Dash => '-',
Kind::Underscore => '_',
}
}
/// Turn a [char] into a kind.
///
/// ## Panics
///
/// Panics if `char` is not `*`, `-`, or `_`.
fn from_char(char: char) -> Kind {
match char {
'*' => Kind::Asterisk,
'-' => Kind::Dash,
'_' => Kind::Underscore,
_ => unreachable!("invalid char"),
}
}
/// Turn [Code] into a kind.
///
/// > 👉 **Note**: an opening paren must be used for `Kind::Paren`.
///
/// ## Panics
///
/// Panics if `code` is not `Code::Char('*' | '-' | '_')`.
fn from_code(code: Code) -> Kind {
match code {
Code::Char(char) => Kind::from_char(char),
_ => unreachable!("invalid code"),
}
}
}
/// State needed to parse thematic breaks.
#[derive(Debug)]
struct Info {
/// Kind of marker.
kind: Kind,
/// Number of markers.
size: usize,
}
/// Start of a thematic break.
///
/// ```markdown
/// > | ***
/// ^
/// ```
pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State {
let max = if tokenizer.parse_state.constructs.code_indented {
TAB_SIZE - 1
} else {
usize::MAX
};
if tokenizer.parse_state.constructs.thematic_break {
tokenizer.enter(Token::ThematicBreak);
tokenizer.go(space_or_tab_min_max(0, max), before)(tokenizer, code)
} else {
State::Nok
}
}
/// Start of a thematic break, after whitespace.
///
/// ```markdown
/// > | ***
/// ^
/// ```
fn before(tokenizer: &mut Tokenizer, code: Code) -> State {
match code {
Code::Char('*' | '-' | '_') => at_break(
tokenizer,
code,
Info {
kind: Kind::from_code(code),
size: 0,
},
),
_ => State::Nok,
}
}
/// After something but before something else.
///
/// ```markdown
/// > | ***
/// ^
/// ```
fn at_break(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State {
match code {
Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r')
if info.size >= THEMATIC_BREAK_MARKER_COUNT_MIN =>
{
tokenizer.exit(Token::ThematicBreak);
// Feel free to interrupt.
tokenizer.interrupt = false;
State::Ok
}
Code::Char(char) if char == info.kind.as_char() => {
tokenizer.enter(Token::ThematicBreakSequence);
sequence(tokenizer, code, info)
}
_ => State::Nok,
}
}
/// In a sequence of markers.
///
/// ```markdown
/// > | ***
/// ^
/// ```
fn sequence(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State {
match code {
Code::Char(char) if char == info.kind.as_char() => {
tokenizer.consume(code);
info.size += 1;
State::Fn(Box::new(|t, c| sequence(t, c, info)))
}
_ => {
tokenizer.exit(Token::ThematicBreakSequence);
tokenizer.attempt_opt(space_or_tab(), |t, c| at_break(t, c, info))(tokenizer, code)
}
}
}
|