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

#include <AK/StringBuilder.h>
#include <LibMarkdown/Document.h>
#include <LibMarkdown/LineIterator.h>
#include <LibMarkdown/Visitor.h>

namespace Markdown {

String Document::render_to_html() const
{
    StringBuilder builder;

    builder.append("<!DOCTYPE html>\n");
    builder.append("<html>\n");
    builder.append("<head>\n");
    builder.append("<style>\n");
    builder.append("code { white-space: pre; }\n");
    builder.append("</style>\n");
    builder.append("</head>\n");
    builder.append("<body>\n");

    builder.append(render_to_inline_html());

    builder.append("</body>\n");
    builder.append("</html>\n");
    return builder.build();
}

String Document::render_to_inline_html() const
{
    return m_container->render_to_html();
}

String Document::render_for_terminal(size_t view_width) const
{
    return m_container->render_for_terminal(view_width);
}

RecursionDecision Document::walk(Visitor& visitor) const
{
    RecursionDecision rd = visitor.visit(*this);
    if (rd != RecursionDecision::Recurse)
        return rd;

    return m_container->walk(visitor);
}

OwnPtr<Document> Document::parse(const StringView& str)
{
    const Vector<StringView> lines_vec = str.lines();
    LineIterator lines(lines_vec.begin());
    return make<Document>(ContainerBlock::parse(lines));
}

}