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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
use crate::legacy::{Fill, Markup, intra};
pub(crate) struct State {
active_id: Option<String>,
active_fill: Option<FillComand>,
paragraphs: bool,
markup: bool,
processed: String,
current: String,
fills: Vec<Fill>,
escaped_html: bool,
last_blank: bool,
}
impl Default for State {
fn default() -> Self {
Self {
active_id: None,
active_fill: None,
paragraphs: true,
markup: true,
processed: String::new(),
current: String::new(),
fills: vec![],
escaped_html: false,
last_blank: true,
}
}
}
impl State {
/// Get an opening paragraph tag with any attributes currently set.
fn get_open_paragraph(&mut self) -> String {
match self.active_id.take() {
None => String::from("<p>"),
Some(id) => format!(r#"<p id="{id}">"#),
}
}
fn paragraph(&mut self) {
let current = self.take_current();
let content = self.make_paragraph(current);
if let Some(fill) = self.active_fill.as_mut() {
// linebreak if there is already text pushed to fill.content
if !fill.content.is_empty() {
fill.content.push('\n');
}
fill.content.push_str(&content);
} else {
// linebreak if there is already text pushed to final
if !self.processed.is_empty() {
self.processed.push('\n');
}
self.processed.push_str(&content);
}
}
fn make_paragraph(&mut self, content: String) -> String {
if self.should_paragraph_string(&content) {
let open = self.get_open_paragraph();
format!("{open}\n{}\n</p>", self.markup(content))
} else {
self.markup(content)
}
}
fn markup(&self, raw: String) -> String {
if self.markup {
intra::process(raw)
} else {
raw
}
}
fn should_paragraph_string(&self, string: &str) -> bool {
// wrap paragraphs if all of these are true:
// - we're supposed to be wrapping paragraphs
// - either of these is true:
// - the line does not start with <
// OR
// - the line starts with < AND it's been escaped
self.paragraphs
&& (!string.starts_with('<') || (string.starts_with('<') && self.escaped_html))
}
pub fn process_line(&mut self, line: &str) {
if self.parse_command(line) {
// Don't set last_blank; a command does not break up a text block
return;
}
if !line.is_empty() {
self.push_current(line);
self.last_blank = false;
} else {
if !self.paragraphs {
// It's a blank line, but we're not doing paragraphs,
// so leave it in the document
self.current.push('\n');
self.last_blank = false;
return;
}
// line is empty, close the textblock
self.finish_current();
}
}
pub fn done(mut self) -> Markup {
self.finish_current();
self.finish_active_fill();
Markup {
content: self.processed,
fill: self.fills,
}
}
fn escape_line<'a>(&mut self, line: &'a str) -> &'a str {
if let Some(strip) = line.strip_prefix('\\') {
match strip.chars().next() {
Some('[') => strip,
Some('<') => {
if self.last_blank {
self.escaped_html = true;
}
strip
}
Some('\\') => strip,
_ => line,
}
} else {
line
}
}
/// Possibly parses a line as a command and mutates internal state.
/// # Returns
/// true if the line was a command, false otherwise.
fn parse_command(&mut self, line: &str) -> bool {
match line.strip_prefix('[') {
Some(line) => match line.strip_suffix(']') {
Some(cmd) => self.run_command(cmd),
None => false,
},
None => false,
}
}
fn run_command(&mut self, cmd: &str) -> bool {
match cmd.trim() {
"@paragraphs off" => {
self.finish_current();
self.paragraphs = false;
true
}
"@paragraphs on" => {
self.finish_current();
self.paragraphs = true;
true
}
"@markup on" => {
self.finish_current();
self.markup = true;
true
}
"@markup off" => {
self.finish_current();
self.markup = false;
true
}
"@fill-end" => {
self.finish_current();
self.finish_active_fill();
true
}
template_key if cmd.starts_with("@fill ") => {
self.finish_current();
let key = template_key.strip_prefix("@fill ").unwrap().to_owned();
self.active_fill = Some(FillComand {
paragraphs: self.paragraphs,
key,
content: String::new(),
});
self.paragraphs = false;
true
}
annotation if cmd.starts_with('#') => {
self.active_id = Some(annotation[1..].to_owned());
true
}
_ => false,
}
}
fn take_current(&mut self) -> String {
self.current.split_off(0)
}
fn push_current(&mut self, text: &str) {
if !self.current.is_empty() {
self.current.push('\n');
}
let escaped = self.escape_line(text);
self.current.push_str(escaped);
}
fn finish_current(&mut self) {
if !self.current.is_empty() {
// Calling paragraph clears self.current
self.paragraph();
// reset block dependant state
self.last_blank = true;
self.escaped_html = false;
}
}
fn finish_active_fill(&mut self) {
if let Some(fill) = self.active_fill.take() {
self.fills.push(Fill {
key: fill.key,
content: fill.content,
});
self.paragraphs = fill.paragraphs;
}
}
}
struct FillComand {
/// Storage for the previous paragraphs variable, as @fill unsets it
paragraphs: bool,
key: String,
content: String,
}
#[cfg(test)]
mod test {
use crate::legacy::{Fill, process};
#[test]
fn parses_no_commands() {
let blk1 = "line one\nline two";
let blk2 = "block two";
assert_eq!(*process(blk1), format!("<p>\n{blk1}\n</p>"));
let tst = format!("{blk1}\n\n{blk2}");
assert_eq!(
*process(&tst),
format!("<p>\n{blk1}\n</p>\n<p>\n{blk2}\n</p>")
)
}
#[test]
fn parses_paragraph_off() {
let str = "[@paragraphs off]\none two\n\nthree\nfour";
assert_eq!(*process(str), "one two\n\nthree\nfour")
}
#[test]
fn parses_adds_annotation() {
let str = "[#greeting]\nHello!";
assert_eq!(*process(str), "<p id=\"greeting\">\nHello!\n</p>")
}
#[test]
fn doesnt_wrap_html() {
let str = "hello!\n\n<i>hi, how are you?</i>";
assert_eq!(*process(str), "<p>\nhello!\n</p>\n<i>hi, how are you?</i>")
}
#[test]
fn correctly_escapes() {
let str = "\\[@paragraph on]\n\\<i>Hello!</i>\n\\\\Goodbye!";
let correct = "<p>\n[@paragraph on]\n<i>Hello!</i>\n\\Goodbye!\n</p>";
assert_eq!(*process(str), correct)
}
#[test]
fn wraps_escaped_html() {
let str = "\\<i>test</i>";
let correct = "<p>\n<i>test</i>\n</p>";
assert_eq!(*process(str), correct)
}
#[test]
fn parses_extracts_fill() {
let str = "[@fill key]\nfilled stuff!\n[@fill-end]\nmeow";
let correct = "<p>\nmeow\n</p>";
let marked = process(str);
assert_eq!(
marked.fill,
vec![Fill {
key: "key".into(),
content: "filled stuff!".into()
}]
);
assert_eq!(marked.content, correct)
}
const BASE: &str = "test/markup";
fn test_files(test: &str) {
let input_path = format!("{BASE}/{test}/input.html");
let output_path = format!("{BASE}/{test}/output.html");
let input = std::fs::read_to_string(input_path).unwrap();
let output = std::fs::read_to_string(output_path).unwrap();
assert_eq!(*process(&input), output)
}
#[test]
fn parses_onoff() {
test_files("paragraph toggle")
}
}
|