summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibMarkdown/Text.h
blob: e8c289e55cd6526ba29c76c87d6122fcbe611693 (plain)
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
/*
 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
 * Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/DeprecatedString.h>
#include <AK/Noncopyable.h>
#include <AK/OwnPtr.h>
#include <AK/RecursionDecision.h>
#include <AK/Vector.h>
#include <LibMarkdown/Forward.h>

namespace Markdown {

class Text final {
public:
    class Node {
    public:
        virtual void render_to_html(StringBuilder& builder) const = 0;
        virtual void render_for_terminal(StringBuilder& builder) const = 0;
        virtual size_t terminal_length() const = 0;
        virtual RecursionDecision walk(Visitor&) const = 0;

        virtual ~Node() = default;
    };

    class EmphasisNode : public Node {
    public:
        bool strong;
        NonnullOwnPtr<Node> child;

        EmphasisNode(bool strong, NonnullOwnPtr<Node> child)
            : strong(strong)
            , child(move(child))
        {
        }

        virtual void render_to_html(StringBuilder& builder) const override;
        virtual void render_for_terminal(StringBuilder& builder) const override;
        virtual size_t terminal_length() const override;
        virtual RecursionDecision walk(Visitor&) const override;
    };

    class CodeNode : public Node {
    public:
        NonnullOwnPtr<Node> code;

        CodeNode(NonnullOwnPtr<Node> code)
            : code(move(code))
        {
        }

        virtual void render_to_html(StringBuilder& builder) const override;
        virtual void render_for_terminal(StringBuilder& builder) const override;
        virtual size_t terminal_length() const override;
        virtual RecursionDecision walk(Visitor&) const override;
    };

    class BreakNode : public Node {
    public:
        virtual void render_to_html(StringBuilder& builder) const override;
        virtual void render_for_terminal(StringBuilder& builder) const override;
        virtual size_t terminal_length() const override;
        virtual RecursionDecision walk(Visitor&) const override;
    };

    class TextNode : public Node {
    public:
        DeprecatedString text;
        bool collapsible;

        TextNode(StringView text)
            : text(text)
            , collapsible(true)
        {
        }

        TextNode(StringView text, bool collapsible)
            : text(text)
            , collapsible(collapsible)
        {
        }

        virtual void render_to_html(StringBuilder& builder) const override;
        virtual void render_for_terminal(StringBuilder& builder) const override;
        virtual size_t terminal_length() const override;
        virtual RecursionDecision walk(Visitor&) const override;
    };

    class LinkNode : public Node {
    public:
        bool is_image;
        NonnullOwnPtr<Node> text;
        DeprecatedString href;
        Optional<int> image_width;
        Optional<int> image_height;

        LinkNode(bool is_image, NonnullOwnPtr<Node> text, DeprecatedString href, Optional<int> image_width, Optional<int> image_height)
            : is_image(is_image)
            , text(move(text))
            , href(move(href))
            , image_width(image_width)
            , image_height(image_height)
        {
        }

        bool has_image_dimensions() const
        {
            return image_width.has_value() || image_height.has_value();
        }
        virtual void render_to_html(StringBuilder& builder) const override;
        virtual void render_for_terminal(StringBuilder& builder) const override;
        virtual size_t terminal_length() const override;
        virtual RecursionDecision walk(Visitor&) const override;
    };

    class MultiNode : public Node {
    public:
        Vector<NonnullOwnPtr<Node>> children;

        virtual void render_to_html(StringBuilder& builder) const override;
        virtual void render_for_terminal(StringBuilder& builder) const override;
        virtual size_t terminal_length() const override;
        virtual RecursionDecision walk(Visitor&) const override;
    };

    class StrikeThroughNode : public Node {
    public:
        NonnullOwnPtr<Node> striked_text;

        StrikeThroughNode(NonnullOwnPtr<Node> striked_text)
            : striked_text(move(striked_text))
        {
        }

        virtual void render_to_html(StringBuilder& builder) const override;
        virtual void render_for_terminal(StringBuilder& builder) const override;
        virtual size_t terminal_length() const override;
        virtual RecursionDecision walk(Visitor&) const override;
    };

    size_t terminal_length() const;

    DeprecatedString render_to_html() const;
    DeprecatedString render_for_terminal() const;
    RecursionDecision walk(Visitor&) const;

    static Text parse(StringView);

private:
    struct Token {
        DeprecatedString data;
        // Flanking basically means that a delimiter run has a non-whitespace,
        // non-punctuation character on the corresponding side. For a more exact
        // definition, see the CommonMark spec.
        bool left_flanking;
        bool right_flanking;
        bool punct_before;
        bool punct_after;
        // is_run indicates that this token is a 'delimiter run'. A delimiter
        // run occurs when several of the same syntactical character ('`', '_',
        // or '*') occur in a row.
        bool is_run;

        char run_char() const
        {
            VERIFY(is_run);
            return data[0];
        }
        char run_length() const
        {
            VERIFY(is_run);
            return data.length();
        }
        bool is_space() const
        {
            return data[0] == ' ';
        }
        bool operator==(StringView str) const { return str == data; }
    };

    static Vector<Token> tokenize(StringView);

    static bool can_open(Token const& opening);
    static bool can_close_for(Token const& opening, Token const& closing);

    static NonnullOwnPtr<MultiNode> parse_sequence(Vector<Token>::ConstIterator& tokens, bool in_link);
    static NonnullOwnPtr<Node> parse_break(Vector<Token>::ConstIterator& tokens);
    static NonnullOwnPtr<Node> parse_newline(Vector<Token>::ConstIterator& tokens);
    static NonnullOwnPtr<Node> parse_emph(Vector<Token>::ConstIterator& tokens, bool in_link);
    static NonnullOwnPtr<Node> parse_code(Vector<Token>::ConstIterator& tokens);
    static NonnullOwnPtr<Node> parse_link(Vector<Token>::ConstIterator& tokens);
    static NonnullOwnPtr<Node> parse_strike_through(Vector<Token>::ConstIterator& tokens);

    OwnPtr<Node> m_node;
};

}