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
|
//! GFM: Task list item check occurs in the [text][] content type.
//!
//! ## Grammar
//!
//! Checks form with the following BNF
//! (<small>see [construct][crate::construct] for character groups</small>):
//!
//! ```bnf
//! gfm_task_list_item_check ::= '[' (0x09 | ' ' | 'X' | 'x') ']'
//! ```
//!
//! The check is only allowed at the start of the first paragraph, optionally
//! following zero or more definitions or a blank line, in a list item.
//! The check must be followed by whitespace, which is in turn followed by
//! non-whitespace.
//!
//! ## HTML
//!
//! Checks relate to the `<input>` element, in the checkbox state
//! (`type=checkbox`), in HTML.
//! See [*§ 4.10.5.1.15 Checkbox state (`type=checkbox`)*][html-input-checkbox]
//! in the HTML spec for more info.
//!
//! ## Recommendation
//!
//! It is recommended to use lowercase `x` (instead of uppercase `X`), because
//! in markdown, it is more common to use lowercase in places where casing does
//! not matter.
//! It is also recommended to use a space (instead of a tab), as there is no
//! benefit of using tabs in this case.
//!
//! ## Tokens
//!
//! * [`GfmTaskListItemCheck`][Name::GfmTaskListItemCheck]
//! * [`GfmTaskListItemMarker`][Name::GfmTaskListItemMarker]
//! * [`GfmTaskListItemValueChecked`][Name::GfmTaskListItemValueChecked]
//! * [`GfmTaskListItemValueUnchecked`][Name::GfmTaskListItemValueUnchecked]
//!
//! ## References
//!
//! * [`micromark-extension-gfm-task-list-item`](https://github.com/micromark/micromark-extension-gfm-task-list-item)
//! * [*§ 5.3 Task list items (extension)* in `GFM`](https://github.github.com/gfm/#task-list-items-extension-)
//!
//! [text]: crate::construct::text
//! [html-input-checkbox]: https://html.spec.whatwg.org/multipage/input.html#checkbox-state-(type=checkbox)
use crate::construct::partial_space_or_tab::space_or_tab;
use crate::event::Name;
use crate::state::{Name as StateName, State};
use crate::tokenizer::Tokenizer;
/// At start of task list item check.
///
/// ```markdown
/// > | * [x] y.
/// ^
/// ```
pub fn start(tokenizer: &mut Tokenizer) -> State {
if tokenizer.parse_state.options.constructs.gfm_task_list_item
&& tokenizer
.tokenize_state
.document_at_first_paragraph_of_list_item
&& tokenizer.current == Some(b'[')
&& tokenizer.previous.is_none()
{
tokenizer.enter(Name::GfmTaskListItemCheck);
tokenizer.enter(Name::GfmTaskListItemMarker);
tokenizer.consume();
tokenizer.exit(Name::GfmTaskListItemMarker);
State::Next(StateName::GfmTaskListItemCheckInside)
} else {
State::Nok
}
}
/// In task list item check.
///
/// ```markdown
/// > | * [x] y.
/// ^
/// ```
pub fn inside(tokenizer: &mut Tokenizer) -> State {
match tokenizer.current {
Some(b'\t' | b'\n' | b' ') => {
tokenizer.enter(Name::GfmTaskListItemValueUnchecked);
tokenizer.consume();
tokenizer.exit(Name::GfmTaskListItemValueUnchecked);
State::Next(StateName::GfmTaskListItemCheckClose)
}
Some(b'X' | b'x') => {
tokenizer.enter(Name::GfmTaskListItemValueChecked);
tokenizer.consume();
tokenizer.exit(Name::GfmTaskListItemValueChecked);
State::Next(StateName::GfmTaskListItemCheckClose)
}
_ => State::Nok,
}
}
/// At close of task list item check.
///
/// ```markdown
/// > | * [x] y.
/// ^
/// ```
pub fn close(tokenizer: &mut Tokenizer) -> State {
match tokenizer.current {
Some(b']') => {
tokenizer.enter(Name::GfmTaskListItemMarker);
tokenizer.consume();
tokenizer.exit(Name::GfmTaskListItemMarker);
tokenizer.exit(Name::GfmTaskListItemCheck);
State::Next(StateName::GfmTaskListItemCheckAfter)
}
_ => State::Nok,
}
}
/// After task list item check.
///
/// ```markdown
/// > | * [x] y.
/// ^
/// ```
pub fn after(tokenizer: &mut Tokenizer) -> State {
match tokenizer.current {
// EOL in paragraph means there must be something else after it.
Some(b'\n') => State::Ok,
// Space or tab?
// Check what comes after.
Some(b'\t' | b' ') => {
tokenizer.check(State::Ok, State::Nok);
tokenizer.attempt(
State::Next(StateName::GfmTaskListItemCheckAfterSpaceOrTab),
State::Nok,
);
State::Retry(space_or_tab(tokenizer))
}
// EOF, or non-whitespace, both wrong.
_ => State::Nok,
}
}
/// After whitespace, after task list item check.
///
/// ```markdown
/// > | * [x] y.
/// ^
/// ```
pub fn after_space_or_tab(tokenizer: &mut Tokenizer) -> State {
// End of paragraph, after whitespace, after check, is not okay.
if tokenizer.current.is_none() {
State::Nok
} else {
State::Ok
}
}
|