rushdown 0.18.0

A 100% CommonMark-compatible GitHub Flavored Markdown parser and renderer
Documentation
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Scanners for various patterns in text.

mod scanner_gen;

use memchr::memmem;

pub use self::scanner_gen::*;
use crate::{
    text::{self, Reader, Segment, EOS},
    util::is_space,
};

/// Trait for scanning input for specific patterns.
pub trait Scan {
    /// Scans the input and returns the position of the pattern if found.
    fn scan(&self, input: &[u8]) -> Option<usize>;
}

impl<F> Scan for F
where
    F: Fn(&[u8]) -> Option<usize>,
{
    fn scan(&self, input: &[u8]) -> Option<usize> {
        self(input)
    }
}

// re2c stuff {{{

const BUFSIZE: usize = 4096;

struct State<'a, T: Reader<'a>> {
    reader: &'a mut T,
    yyinput: [u8; BUFSIZE],
    yylimit: usize,
    yycursor: usize,
    yymarker: usize,
    token: usize,
    eof: bool,
}

impl<'a, T: Reader<'a>> State<'a, T> {
    fn new(reader: &'a mut T) -> Self {
        let yylimit = BUFSIZE - 1;
        State {
            reader,
            yyinput: [255; BUFSIZE],
            yylimit,
            yycursor: yylimit,
            yymarker: yylimit,
            token: yylimit,
            eof: false,
        }
    }
}

impl<'a, T: Reader<'a>> State<'a, T> {
    fn set_position(&mut self, line: usize, pos: Segment) {
        self.reader.set_position(line, pos);
    }
}

#[derive(PartialEq)]
enum Fill {
    Ok,
    Eof,
    LongLexeme,
}

fn fill<'a, T: Reader<'a>>(st: &mut State<'a, T>) -> Fill {
    if st.eof {
        return Fill::Eof;
    }
    if st.token < 1 {
        return Fill::LongLexeme;
    }

    st.yyinput.copy_within(st.token..st.yylimit, 0);
    st.yylimit -= st.token;
    st.yycursor -= st.token;
    st.yymarker = st.yymarker.overflowing_sub(st.token).0;
    st.token = 255;

    let Some((line, _)) = st.reader.peek_line_bytes() else {
        panic!("should not happen")
    };
    let bufsize = BUFSIZE - 1 - st.yylimit;
    if line.len() < bufsize {
        st.reader.advance_line();
        st.yyinput[st.yylimit..st.yylimit + line.len()].copy_from_slice(&line);
        st.yylimit += line.len();
    } else {
        st.reader.advance(bufsize);
        st.yyinput[st.yylimit..st.yylimit + bufsize].copy_from_slice(&line[..bufsize]);
        st.yylimit += bufsize;
    }
    st.eof = st.reader.peek_byte() == 255;
    st.yyinput[st.yylimit] = 255;
    Fill::Ok
}

// }}} re2c stuff

// private utilities for scanners {{{

fn starts_with(s: &[u8], pos: usize, prefix: &[&str], allow_eol: bool) -> Option<usize> {
    if pos >= s.len() {
        if allow_eol {
            return Some(0);
        }
        return None;
    }
    let s2 = &s[pos..];
    for p in prefix {
        let p_bytes = p.as_bytes();
        if s2.starts_with(p_bytes) {
            return Some(p_bytes.len());
        }
    }
    None
}

fn skip_while(s: &[u8], pos: usize, pred: impl Fn(u8) -> bool, allow_eol: bool) -> Option<usize> {
    if pos >= s.len() {
        if allow_eol {
            return Some(0);
        }
        return None;
    }
    let i = pos + s[pos..].iter().take_while(|&&c| pred(c)).count();
    if i > pos {
        return Some(i - pos);
    }
    None
}

fn contains_any_i(s: &[u8], needles: &[&str]) -> bool {
    for n in needles {
        let n_bytes = n.as_bytes();
        if s.windows(n_bytes.len())
            .any(|window| window.eq_ignore_ascii_case(n_bytes))
        {
            return true;
        }
    }
    false
}

fn not_indented(line: &[u8]) -> Option<usize> {
    let i = line.iter().take_while(|&&c| c == b' ').count();
    if i > 3 {
        return None;
    }
    Some(i)
}

fn skip_spaces(line: &[u8], pos: usize) -> usize {
    let mut i = pos;
    while i < line.len() && is_space(line[i]) {
        i += 1;
    }
    i - pos
}

fn scan_open_close<'a>(
    reader: &mut impl text::Reader<'a>,
    open: &[u8],
    close: &[u8],
) -> Option<()> {
    let (line, _) = reader.peek_line_bytes()?;
    let (l, p) = reader.position();
    if line.starts_with(open) {
        reader.advance(open.len());
        loop {
            let Some((line, _)) = reader.peek_line_bytes() else {
                reader.set_position(l, p);
                return None;
            };
            if let Some(pos) = memmem::find(&line, close) {
                reader.advance(pos + close.len());
                return Some(());
            }
            reader.advance(line.len());
        }
    }
    reader.set_position(l, p);
    None
}

// }}}

// HTML scanners {{{

include!(concat!(env!("OUT_DIR"), "/allowed_block_tags.rs"));

pub(crate) fn scan_html_block_open_1(line: &[u8]) -> Option<()> {
    let mut i = not_indented(line)?;
    i += starts_with(line, i, &["<textarea", "<script", "<pre", "<style"], false)?;
    starts_with(line, i, &["/>", "\r\n", " ", "\t", ">", "\n"], true)?;
    Some(())
}

pub(crate) fn scan_html_block_close_1(line: &[u8]) -> Option<()> {
    contains_any_i(line, &["</script>", "</pre>", "</style>", "</textarea>"]).then_some(())
}

pub(crate) fn scan_html_block_open_2(line: &[u8]) -> Option<()> {
    let i = not_indented(line)?;
    starts_with(line, i, &["<!--"], false)?;
    Some(())
}

pub(crate) fn scan_html_block_close_2(line: &[u8]) -> Option<()> {
    contains_any_i(line, &["-->"]).then_some(())
}

pub(crate) fn scan_html_block_open_3(line: &[u8]) -> Option<()> {
    let i = not_indented(line)?;
    starts_with(line, i, &["<?"], false)?;
    Some(())
}

pub(crate) fn scan_html_block_close_3(line: &[u8]) -> Option<()> {
    contains_any_i(line, &["?>"]).then_some(())
}

pub(crate) fn scan_html_block_open_4(line: &[u8]) -> Option<()> {
    let mut i = not_indented(line)?;
    i += starts_with(line, i, &["<!"], false)?;
    skip_while(line, i, |c| c.is_ascii_alphabetic(), false)?;
    Some(())
}

pub(crate) fn scan_html_block_close_4(line: &[u8]) -> Option<()> {
    contains_any_i(line, &[">"]).then_some(())
}

pub(crate) fn scan_html_block_open_5(line: &[u8]) -> Option<()> {
    let i = not_indented(line)?;
    starts_with(line, i, &["<![CDATA["], false)?;
    Some(())
}

pub(crate) fn scan_html_block_close_5(line: &[u8]) -> Option<()> {
    contains_any_i(line, &["]]>"]).then_some(())
}

pub(crate) fn scan_html_block_open_6(line: &[u8]) -> Option<()> {
    let mut i = not_indented(line)?;
    i += starts_with(line, i, &["</", "<"], false)?;
    let tag_name_start = i;
    i += skip_while(line, i, |c| c.is_ascii_alphabetic(), false)?;
    let tag_name =
        unsafe { core::str::from_utf8_unchecked(&line[tag_name_start..i]).to_ascii_lowercase() };
    if !ALLOWED_BLOCK_TAGS.contains_key(&tag_name) {
        return None;
    }
    starts_with(line, i, &["/>", "\r\n", " ", "\t", ">", "\n"], true)?;
    Some(())
}

pub(crate) fn scan_html_block_open_7(line: &[u8]) -> Option<()> {
    let mut i = not_indented(line)?;
    let n = starts_with(line, i, &["</", "<"], false)?;
    let closing = n == 2;
    i += n;
    let tag_name_start = i;
    i += skip_while(line, i, |c| c.is_ascii_alphabetic(), false)?;
    let tag_name =
        unsafe { core::str::from_utf8_unchecked(&line[tag_name_start..i]).to_ascii_lowercase() };
    if ALLOWED_BLOCK_TAGS.contains_key(&tag_name)
        && tag_name != "pre"
        && tag_name != "script"
        && tag_name != "style"
        && tag_name != "textarea"
    {
        return None;
    }

    if let Some(m) = skip_while(line, i, |c| c.is_ascii_alphanumeric() || c == b'-', false) {
        i += m;
    }
    if !closing {
        if let Some(m) = scan_html_attributes(&line[i..]) {
            i += m;
        }
    }
    i += skip_spaces(line, i);
    i += starts_with(line, i, &["/>", ">"], false)?;
    i += skip_spaces(line, i);
    (i == line.len()).then_some(())
}

pub(crate) fn scan_html_attributes(s: &[u8]) -> Option<usize> {
    let mut r = unsafe { text::BasicReader::new_unchecked(s) };
    if scan_html_attributes_reader(&mut r).is_some() {
        Some(r.position().1.start())
    } else {
        None
    }
}

pub(crate) fn scan_html_attributes_reader<'a>(reader: &mut impl text::Reader<'a>) -> Option<()> {
    let (sline, spos) = reader.position();
    let mut has_space = false;
    loop {
        if !has_space && !is_space(reader.peek_byte()) {
            // no more attributes
            return Some(());
        }
        reader.skip_spaces();

        let c = reader.peek_byte();
        if c == EOS || !(c.is_ascii_alphabetic() || c == b'_' || c == b':') {
            return Some(());
        }
        reader.advance(1);
        reader.skip_while(|c| {
            c.is_ascii_alphanumeric() || c == b':' || c == b'.' || c == b'_' || c == b'-'
        });
        has_space = reader.skip_spaces() > 0;
        if reader.peek_byte() == b'=' {
            reader.advance(1);
            reader.skip_spaces();
            let open = reader.peek_byte();
            if open == b'"' || open == b'\'' {
                reader.advance(1);
                reader.skip_while(|c| c != open && c != EOS);
                if reader.peek_byte() == open {
                    reader.advance(1);
                } else {
                    // no closing quote
                    reader.set_position(sline, spos);
                    return None;
                }
            } else if unquoted_html_attr_value_char(open) {
                reader.advance(1);
                reader.skip_while(unquoted_html_attr_value_char);
            } else {
                // invalid value start
                reader.set_position(sline, spos);
                return None;
            }
            continue;
        }
        // name only attribute
    }
}

#[inline]
pub(crate) fn unquoted_html_attr_value_char(c: u8) -> bool {
    !(c <= 0x20 || c == b'"' || c == b'\'' || c == b'=' || c == b'<' || c == b'>' || c == b'`')
}

pub(crate) fn scan_html_tag_name_reader<'a>(reader: &mut impl text::Reader<'a>) -> Option<()> {
    let c = reader.peek_byte();
    if !c.is_ascii_alphabetic() {
        return None;
    }
    reader.advance(1);
    reader.skip_while(|c| c.is_ascii_alphanumeric() || c == b'-');
    Some(())
}

pub(crate) fn scan_html_tag_reader<'a>(reader: &mut impl text::Reader<'a>) -> Option<()> {
    let c = reader.peek_byte();
    if c != b'<' {
        return None;
    }
    let (line, pos) = reader.position();
    let mut f = || {
        reader.advance(1);
        let c = reader.peek_byte();
        let closing = c == b'/';
        if closing {
            reader.advance(1);
        }
        scan_html_tag_name_reader(reader)?;
        if !closing {
            scan_html_attributes_reader(reader)?;
        }
        reader.skip_spaces();
        let c = reader.peek_byte();
        if c == b'/' {
            reader.advance(1);
            let c = reader.peek_byte();
            if c != b'>' {
                return None;
            }
        } else if c != b'>' {
            return None;
        }
        reader.advance(1);
        Some(())
    };
    f().or_else(|| {
        reader.set_position(line, pos);
        None
    })
}

pub(crate) fn scan_html_comment_reader<'a>(reader: &mut impl text::Reader<'a>) -> Option<()> {
    let (line, _) = reader.peek_line_bytes()?;
    if line.starts_with(b"<!-->") {
        reader.advance(5);
        return Some(());
    }
    if line.starts_with(b"<!--->") {
        reader.advance(6);
        return Some(());
    }
    scan_open_close(reader, b"<!--", b"-->")
}

pub(crate) fn scan_html_processing_instruction_reader<'a>(
    reader: &mut impl text::Reader<'a>,
) -> Option<()> {
    scan_open_close(reader, b"<?", b"?>")
}

pub(crate) fn scan_html_declaration_reader<'a>(reader: &mut impl text::Reader<'a>) -> Option<()> {
    let (line, _) = reader.peek_line_bytes()?;
    if !line.starts_with(b"<!") {
        return None;
    }
    let (l, p) = reader.position();
    reader.advance(2);
    if !reader.peek_byte().is_ascii_alphabetic() {
        reader.set_position(l, p);
        return None;
    }
    reader.advance(1);
    reader.skip_while(|c| c != b'>');
    if reader.peek_byte() != b'>' {
        reader.set_position(l, p);
        return None;
    }
    reader.advance(1);
    Some(())
}

pub(crate) fn scan_html_cdata_reader<'a>(reader: &mut impl text::Reader<'a>) -> Option<()> {
    scan_open_close(reader, b"<![CDATA[", b"]]>")
}

// }}} HTML_block scanners

// URL scanners {{{

pub(crate) fn scan_url_www(buffer: &[u8]) -> Option<usize> {
    let mut i = 0;
    i += scan_url_www_domain(buffer)?;
    match scan_url_path(&buffer[i..]) {
        Some(m) => i += m,
        None => return None,
    }
    Some(i)
}

pub(crate) fn scan_url_strict(buffer: &[u8]) -> Option<usize> {
    let mut i = 0;
    i += skip_while(buffer, 0, |c| c.is_ascii_lowercase(), false)?;
    i += starts_with(buffer, i, &["://"], false)?;
    i += scan_url_domain(&buffer[i..])?;
    match scan_url_path(&buffer[i..]) {
        Some(m) => i += m,
        None => return None,
    }
    Some(i)
}

fn scan_url_www_domain(buffer: &[u8]) -> Option<usize> {
    let mut i = 0;
    i += starts_with(buffer, 0, &["www"], false)?;
    let mut last_start = 0usize;
    let mut last_stop = 0usize;
    let mut n = 0;
    while i < buffer.len() {
        if buffer[i] != b'.'
            && (!is_domain_safe(buffer[i])
                || buffer[i] != b'/' && buffer[i] != b'#' && buffer[i] != b'?')
        {
            break;
        }
        i += starts_with(buffer, i, &["."], false)?;
        match skip_while(buffer, i, is_domain_safe, false) {
            Some(m) => {
                last_start = i;
                i += m;
                n += 1;
                last_stop = i;
            }
            None => break,
        }
    }
    if n < 2 || matches_tld_and_port(&buffer[last_start..last_stop]).is_none() {
        return None;
    }
    Some(i)
}

fn is_domain_safe(c: u8) -> bool {
    c == b'-'
        || c == b'@'
        || c == b':'
        || c == b'%'
        || c == b'_'
        || c == b'+'
        || c == b'~'
        || c == b'='
        || c.is_ascii_alphanumeric()
}

fn scan_url_domain(buffer: &[u8]) -> Option<usize> {
    let mut i = 0;
    i += skip_while(buffer, 0, is_domain_safe, false)?;
    let mut last_start = 0usize;
    let mut last_stop = 0usize;
    let mut n = 0;
    while i < buffer.len() {
        if buffer[i] != b'.'
            && (!is_domain_safe(buffer[i])
                || buffer[i] != b'/' && buffer[i] != b'#' && buffer[i] != b'?')
        {
            break;
        }
        i += starts_with(buffer, i, &["."], false)?;
        match skip_while(buffer, i, is_domain_safe, false) {
            Some(m) => {
                last_start = i;
                i += m;
                n += 1;
                last_stop = i;
            }
            None => break,
        }
    }

    if n < 1 || matches_tld_and_port(&buffer[last_start..last_stop]).is_none() {
        return None;
    }
    Some(i)
}

fn matches_tld_and_port(buffer: &[u8]) -> Option<()> {
    let mut i = 0;
    skip_while(buffer, i, |c| c.is_ascii_lowercase(), false)?;
    if i < buffer.len() && buffer[i] == b':' {
        i += 1;
        skip_while(buffer, i, |c| c.is_ascii_digit(), false)?;
    }
    Some(())
}

fn scan_url_path(buffer: &[u8]) -> Option<usize> {
    let mut i = 0;
    match starts_with(buffer, 0, &["/", "#", "?"], true) {
        Some(m) => i += m,
        None => return Some(0),
    }

    match skip_while(
        buffer,
        i,
        |c| {
            is_domain_safe(c)
                || c == b'('
                || c == b')'
                || c == b';'
                || c == b','
                || c == b'\''
                || c == b'"'
                || c == b'>'
                || c == b'^'
                || c == b'{'
                || c == b'}'
                || c == b'['
                || c == b']'
                || c == b'`'
                || c == b'!'
                || c == b'?'
                || c == b'$'
                || c == b'&'
                || c == b'~'
                || c == b'/'
        },
        true,
    ) {
        Some(m) => i += m,
        None => i += 0,
    }
    Some(i)
}

// }}}

// Other scanners {{{
pub(crate) fn scan_task_list_item(buffer: &[u8]) -> Option<usize> {
    let mut i = 0;
    i += starts_with(buffer, i, &["[X]", "[x]", "[ ]"], false)?;
    Some(i)
}
// }}}