Serenity Operating System
1/*
2 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
3 * Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <AK/StringBuilder.h>
9#include <LibMarkdown/Document.h>
10#include <LibMarkdown/LineIterator.h>
11#include <LibMarkdown/Visitor.h>
12
13namespace Markdown {
14
15DeprecatedString Document::render_to_html(StringView extra_head_contents) const
16{
17 StringBuilder builder;
18 builder.append(R"~~~(<!DOCTYPE html>
19<html>
20<head>
21 <style>
22 code { white-space: pre; }
23 </style>
24)~~~"sv);
25 if (!extra_head_contents.is_empty())
26 builder.append(extra_head_contents);
27 builder.append(R"~~~(
28</head>
29<body>
30)~~~"sv);
31
32 builder.append(render_to_inline_html());
33
34 builder.append(R"~~~(
35</body>
36</html>)~~~"sv);
37
38 return builder.to_deprecated_string();
39}
40
41DeprecatedString Document::render_to_inline_html() const
42{
43 return m_container->render_to_html();
44}
45
46DeprecatedString Document::render_for_terminal(size_t view_width) const
47{
48 StringBuilder builder;
49 for (auto& line : m_container->render_lines_for_terminal(view_width)) {
50 builder.append(line);
51 builder.append("\n"sv);
52 }
53
54 return builder.to_deprecated_string();
55}
56
57RecursionDecision Document::walk(Visitor& visitor) const
58{
59 RecursionDecision rd = visitor.visit(*this);
60 if (rd != RecursionDecision::Recurse)
61 return rd;
62
63 return m_container->walk(visitor);
64}
65
66OwnPtr<Document> Document::parse(StringView str)
67{
68 Vector<StringView> const lines_vec = str.lines();
69 LineIterator lines(lines_vec.begin());
70 return make<Document>(ContainerBlock::parse(lines));
71}
72
73}