aboutsummaryrefslogtreecommitdiffstats
path: root/src/construct/hard_break_trailing.rs
blob: f0ef83bdd97cfd241b848f1404e0dbdd3be6f9a6 (plain) (blame)
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
//! Hard break (trailing) is a construct that occurs in the  [text][] content
//! type.
//!
//! They’re formed with the following BNF:
//!
//! ```bnf
//! ; Restriction: followed by a line ending  (that is part of the construct
//! ; instead of ending it).
//! hard_break_trailing ::= 2*' '
//! ```
//!
//! The minimum number of the spaces is defined in
//! [`HARD_BREAK_PREFIX_SIZE_MIN`][hard_break_prefix_size_min].
//!
//! Hard breaks in markdown relate to the HTML element `<br>`.
//! See [*§ 4.5.27 The `br` element* in the HTML spec][html] for more info.
//!
//! It is also possible to create a hard break with a similar construct: a
//! [hard break (escape)][hard_break_escape] is a backslash followed
//! by a line ending.
//! That construct is recommended because it is similar to a
//! [character escape][character_escape] and similar to how line endings can be
//! “escaped” in other languages.
//! Trailing spaces are typically invisible in editors, or even automatically
//! removed, making hard break (trailing) hard to use.
//!
//! ## Tokens
//!
//! *   [`HardBreakTrailing`][Token::HardBreakTrailing]
//! *   [`HardBreakTrailingSpace`][Token::HardBreakTrailingSpace]
//!
//! ## References
//!
//! *   [`lib/initialize/text.js` in `micromark`](https://github.com/micromark/micromark/blob/main/packages/micromark/dev/lib/initialize/text.js)
//! *   [*§ 6.7 Hard line breaks* in `CommonMark`](https://spec.commonmark.org/0.30/#hard-line-breaks)
//!
//! [text]: crate::content::text
//! [hard_break_escape]: crate::construct::hard_break_escape
//! [character_escape]: crate::construct::character_escape
//! [hard_break_prefix_size_min]: crate::constant::HARD_BREAK_PREFIX_SIZE_MIN
//! [html]: https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-br-element

use crate::constant::HARD_BREAK_PREFIX_SIZE_MIN;
use crate::token::Token;
use crate::tokenizer::{Code, State, Tokenizer};

/// Start of a hard break (trailing).
///
/// ```markdown
/// > | a␠␠
///      ^
///   | b
/// ```
pub fn start(tokenizer: &mut Tokenizer) -> State {
    match tokenizer.current {
        Code::Char(' ') if tokenizer.parse_state.constructs.hard_break_trailing => {
            tokenizer.enter(Token::HardBreakTrailing);
            tokenizer.enter(Token::HardBreakTrailingSpace);
            tokenizer.consume();
            State::Fn(Box::new(|t| inside(t, 1)))
        }
        _ => State::Nok,
    }
}

/// Inside the hard break (trailing).
///
/// ```markdown
/// > | a␠␠
///      ^
///   | b
/// ```
fn inside(tokenizer: &mut Tokenizer, size: usize) -> State {
    match tokenizer.current {
        Code::Char(' ') => {
            tokenizer.consume();
            State::Fn(Box::new(move |t| inside(t, size + 1)))
        }
        Code::CarriageReturnLineFeed | Code::Char('\n' | '\r')
            if size >= HARD_BREAK_PREFIX_SIZE_MIN =>
        {
            tokenizer.exit(Token::HardBreakTrailingSpace);
            tokenizer.exit(Token::HardBreakTrailing);
            State::Ok
        }
        _ => State::Nok,
    }
}