summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibMarkdown/Paragraph.cpp
blob: d252fae4642cb8de40a2522017cc1fb4e0cac122 (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
/*
 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/StringBuilder.h>
#include <LibMarkdown/Paragraph.h>

namespace Markdown {

String Paragraph::render_to_html() const
{
    StringBuilder builder;
    builder.append("<p>");
    bool first = true;
    for (auto& line : m_lines) {
        if (!first)
            builder.append(' ');
        first = false;
        builder.append(line.text().render_to_html());
    }
    builder.append("</p>\n");
    return builder.build();
}

String Paragraph::render_for_terminal(size_t) const
{
    StringBuilder builder;
    bool first = true;
    for (auto& line : m_lines) {
        if (!first)
            builder.append(' ');
        first = false;
        builder.append(line.text().render_for_terminal());
    }
    builder.append("\n\n");
    return builder.build();
}

OwnPtr<Paragraph::Line> Paragraph::Line::parse(Vector<StringView>::ConstIterator& lines)
{
    if (lines.is_end())
        return {};

    auto text = Text::parse(*lines++);
    if (!text.has_value())
        return {};

    return make<Paragraph::Line>(text.release_value());
}
}