tread 0.4.3

A terminal reader for markdown, CSV, JSON and source code. Zero dependencies.
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
//! Line-level scanners shared by the block parser: indentation, leaf-block
//! recognizers, and link-reference / footnote definition parsing.
//! Split out of `block.rs` to keep both files under the size limit.
#![deny(unsafe_code)]

use super::ast::LinkRefs;
use super::list;

/// A source line paired with its 1-based line number. Container parsers hand
/// dedented copies down to the recursive call, so `num` survives nesting.
#[derive(Debug, Clone)]
pub(crate) struct Ln {
    pub text: String,
    pub num: usize,
}

impl Ln {
    pub(crate) fn new(text: String, num: usize) -> Ln {
        Ln { text, num }
    }
}

/// Indentation of a line in columns; tabs advance to the next multiple of 4.
pub(crate) fn indent_of(s: &str) -> usize {
    let mut col = 0usize;
    for c in s.chars() {
        match c {
            ' ' => col += 1,
            '\t' => col += 4 - col % 4,
            _ => break,
        }
    }
    col
}

pub(crate) fn is_blank(s: &str) -> bool {
    s.chars().all(|c| c == ' ' || c == '\t')
}

/// Remove up to `n` columns of leading whitespace, re-padding with spaces if
/// a tab straddles the boundary.
pub(crate) fn strip_indent(s: &str, n: usize) -> String {
    let mut col = 0usize;
    let mut idx = 0usize;
    for (i, c) in s.char_indices() {
        if col >= n {
            idx = i;
            break;
        }
        match c {
            ' ' => col += 1,
            '\t' => col += 4 - col % 4,
            _ => {
                idx = i;
                break;
            }
        }
        idx = i + c.len_utf8();
    }
    let mut out = String::new();
    for _ in n..col {
        out.push(' ');
    }
    out.push_str(&s[idx..]);
    out
}

pub(crate) fn is_thematic_break(s: &str) -> bool {
    if indent_of(s) >= 4 {
        return false;
    }
    let t = s.trim();
    let c = match t.chars().next() {
        Some(c @ ('-' | '_' | '*')) => c,
        _ => return false,
    };
    let mut n = 0;
    for ch in t.chars() {
        if ch == c {
            n += 1;
        } else if ch != ' ' && ch != '\t' {
            return false;
        }
    }
    n >= 3
}

/// ATX heading -> (level, text with any closing hash run removed).
pub(crate) fn atx(s: &str) -> Option<(u8, String)> {
    if indent_of(s) >= 4 {
        return None;
    }
    let t = s.trim_start();
    let level = t.chars().take_while(|&c| c == '#').count();
    if level == 0 || level > 6 {
        return None;
    }
    let rest = &t[level..];
    if !(rest.is_empty() || rest.starts_with(' ') || rest.starts_with('\t')) {
        return None;
    }
    let mut body = rest.trim();
    // A closing hash run is only a closing run when it is preceded by a space
    // (or is the whole body). `trim_end_matches` keeps us on char boundaries;
    // byte arithmetic here used to panic on multi-byte headings like `# テスト`.
    let stripped = body.trim_end_matches('#');
    if stripped.len() < body.len() {
        if stripped.is_empty() {
            body = "";
        } else if stripped.ends_with(' ') || stripped.ends_with('\t') {
            body = stripped.trim_end();
        }
    }
    Some((level as u8, body.to_string()))
}

pub(crate) struct Fence {
    pub ch: char,
    pub len: usize,
    pub indent: usize,
    pub info: String,
}

pub(crate) fn fence_at(s: &str) -> Option<Fence> {
    let indent = indent_of(s);
    if indent >= 4 {
        return None;
    }
    let t = s.trim_start();
    let ch = match t.chars().next() {
        Some(c @ ('`' | '~')) => c,
        _ => return None,
    };
    let len = t.chars().take_while(|&c| c == ch).count();
    if len < 3 {
        return None;
    }
    let info = t[len..].trim().to_string();
    if ch == '`' && info.contains('`') {
        return None;
    }
    Some(Fence {
        ch,
        len,
        indent,
        info,
    })
}

/// A closing fence for an open fence of `(ch, len)`.
pub(crate) fn closes_fence(s: &str, ch: char, len: usize) -> bool {
    match fence_at(s) {
        Some(f) => f.ch == ch && f.len >= len && f.info.is_empty(),
        None => false,
    }
}

/// Setext underline -> heading level.
pub(crate) fn setext(s: &str) -> Option<u8> {
    if indent_of(s) >= 4 {
        return None;
    }
    let t = s.trim();
    let c = t.chars().next()?;
    if (c == '=' || c == '-') && t.chars().all(|x| x == c) {
        Some(if c == '=' { 1 } else { 2 })
    } else {
        None
    }
}

pub(crate) fn quote_start(s: &str) -> bool {
    indent_of(s) < 4 && s.trim_start().starts_with('>')
}

/// Strip one `>` marker plus at most one following space from a quote line.
pub(crate) fn strip_quote(s: &str) -> String {
    let rest = &s.trim_start()[1..];
    if let Some(r) = rest.strip_prefix(' ') {
        r.to_string()
    } else if let Some(r) = rest.strip_prefix('\t') {
        format!("  {}", r)
    } else {
        rest.to_string()
    }
}

pub(crate) fn html_start(s: &str) -> bool {
    if indent_of(s) >= 4 {
        return false;
    }
    let mut c = s.trim_start().chars();
    if c.next() != Some('<') {
        return false;
    }
    match c.next() {
        Some('!') | Some('?') | Some('/') => true,
        Some(x) => x.is_ascii_alphabetic(),
        None => false,
    }
}

/// True when `s` cannot be a lazy continuation line of a paragraph.
pub(crate) fn interrupts_paragraph(s: &str) -> bool {
    is_blank(s)
        || is_thematic_break(s)
        || atx(s).is_some()
        || fence_at(s).is_some()
        || quote_start(s)
        || html_start(s)
        || list::interrupting_marker(s)
}

/// `[label]: destination "title"` on a single line. Footnote definitions
/// (`[^x]:`) are deliberately excluded.
pub(crate) fn link_ref_at(s: &str) -> Option<(String, String, Option<String>)> {
    if indent_of(s) >= 4 {
        return None;
    }
    let inner = s.trim_start().strip_prefix('[')?;
    let end = close_bracket(inner)?;
    let label = &inner[..end];
    if label.is_empty() || label.starts_with('^') {
        return None;
    }
    let rest = inner[end + 1..].strip_prefix(':')?.trim();
    if rest.is_empty() {
        return None;
    }
    let (dest, tail) = split_destination(rest)?;
    Some((normalize_label(label), dest, ref_title(tail)?))
}

fn close_bracket(s: &str) -> Option<usize> {
    let mut esc = false;
    for (i, c) in s.char_indices() {
        if esc {
            esc = false;
        } else if c == '\\' {
            esc = true;
        } else if c == ']' {
            return Some(i);
        }
    }
    None
}

fn split_destination(rest: &str) -> Option<(String, &str)> {
    if let Some(r) = rest.strip_prefix('<') {
        let e = r.find('>')?;
        Some((r[..e].to_string(), r[e + 1..].trim()))
    } else {
        match rest.find(char::is_whitespace) {
            Some(p) => Some((rest[..p].to_string(), rest[p..].trim())),
            None => Some((rest.to_string(), "")),
        }
    }
}

/// `Some(None)` = no title; `None` = trailing junk, so not a definition.
fn ref_title(tail: &str) -> Option<Option<String>> {
    match tail.chars().next() {
        None => Some(None),
        Some(q @ ('"' | '\'' | '(')) => {
            let close = if q == '(' { ')' } else { q };
            if tail.chars().count() >= 2 && tail.ends_with(close) {
                Some(Some(tail[1..tail.len() - close.len_utf8()].to_string()))
            } else {
                None
            }
        }
        Some(_) => None,
    }
}

/// Reference labels match case-insensitively with whitespace collapsed.
pub(crate) fn normalize_label(label: &str) -> String {
    let mut out = String::new();
    let mut space = false;
    for c in label.trim().chars() {
        if c.is_whitespace() {
            space = true;
        } else {
            if space && !out.is_empty() {
                out.push(' ');
            }
            space = false;
            out.extend(c.to_lowercase());
        }
    }
    out
}

/// Fence-aware pre-scan: the inline parser needs every definition up front,
/// and definitions inside code fences are not definitions.
pub(crate) fn collect_refs(lines: &[Ln]) -> LinkRefs {
    let mut refs = LinkRefs::new();
    let mut open: Option<(char, usize)> = None;
    for l in lines {
        match open {
            Some((ch, len)) => {
                if closes_fence(&l.text, ch, len) {
                    open = None;
                }
            }
            None => match fence_at(&l.text) {
                Some(f) => open = Some((f.ch, f.len)),
                None => {
                    if let Some((k, u, t)) = link_ref_at(&l.text) {
                        refs.entry(k).or_insert((u, t));
                    }
                }
            },
        }
    }
    refs
}

/// `[^label]: rest-of-line`.
pub(crate) fn footnote_def_at(s: &str) -> Option<(String, String)> {
    if indent_of(s) >= 4 {
        return None;
    }
    let inner = s.trim_start().strip_prefix("[^")?;
    let end = inner.find("]:")?;
    let label = inner[..end].trim();
    if label.is_empty() {
        return None;
    }
    Some((label.to_string(), inner[end + 2..].trim_start().to_string()))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn indent_and_strip_are_tab_aware() {
        assert_eq!(indent_of("    x"), 4);
        assert_eq!(indent_of("\tx"), 4);
        assert_eq!(indent_of("  \tx"), 4);
        assert_eq!(strip_indent("    code", 4), "code");
        assert_eq!(strip_indent("\tcode", 2), "  code");
        assert_eq!(strip_indent("no indent", 4), "no indent");
    }

    #[test]
    fn atx_variants() {
        assert_eq!(atx("## Two"), Some((2, "Two".into())));
        assert_eq!(atx("### Three ###"), Some((3, "Three".into())));
        assert_eq!(atx("# foo#"), Some((1, "foo#".into())));
        assert_eq!(atx("#"), Some((1, String::new())));
        assert_eq!(atx("#hash"), None);
        assert_eq!(atx("####### 7"), None);
        assert_eq!(atx("    # indented"), None);
    }

    #[test]
    fn fences_and_breaks() {
        let f = fence_at("  ```rust,no_run").unwrap();
        assert_eq!(
            (f.ch, f.len, f.indent, f.info.as_str()),
            ('`', 3, 2, "rust,no_run")
        );
        assert!(fence_at("``` a ` b").is_none());
        assert!(closes_fence("````", '`', 3));
        assert!(!closes_fence("```", '`', 4));
        assert!(!closes_fence("~~~", '`', 3));
        assert!(is_thematic_break("- - -") && is_thematic_break("***"));
        assert!(!is_thematic_break("-- "));
        assert_eq!(setext("==="), Some(1));
        assert_eq!(setext("---"), Some(2));
        assert_eq!(setext("- -"), None);
    }

    #[test]
    fn link_ref_shapes() {
        assert_eq!(
            link_ref_at("[Foo Bar]: /a/b \"T\""),
            Some(("foo bar".into(), "/a/b".into(), Some("T".into())))
        );
        assert_eq!(
            link_ref_at("  [x]: <a b.md>"),
            Some(("x".into(), "a b.md".into(), None))
        );
        assert_eq!(link_ref_at("[^1]: a footnote"), None);
        assert_eq!(link_ref_at("[x]: /a trailing junk"), None);
        assert_eq!(link_ref_at("[x] not a def"), None);
        assert_eq!(
            footnote_def_at("[^note]: body"),
            Some(("note".into(), "body".into()))
        );
    }

    #[test]
    fn quote_and_html_scanners() {
        assert!(quote_start("> a") && quote_start("  >a"));
        assert!(!quote_start("    > a"));
        assert_eq!(strip_quote("> a"), "a");
        assert_eq!(strip_quote(">"), "");
        assert_eq!(strip_quote(">> b"), "> b");
        assert!(html_start("<div>") && html_start("<!-- c -->") && html_start("</p>"));
        assert!(!html_start("< div") && !html_start("a<div>"));
    }
}