2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2020-01-24 10:45:29 -03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2021-09-06 22:11:46 -03:00
|
|
|
* Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-09-20 18:46:18 -03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-04-28 16:04:25 -03:00
|
|
|
#include <LibMarkdown/Document.h>
|
2021-09-19 14:14:18 -03:00
|
|
|
#include <LibMarkdown/LineIterator.h>
|
2021-09-10 16:36:29 -03:00
|
|
|
#include <LibMarkdown/Visitor.h>
|
2019-09-20 18:46:18 -03:00
|
|
|
|
2020-04-28 16:04:25 -03:00
|
|
|
namespace Markdown {
|
|
|
|
|
|
2022-12-04 15:02:33 -03:00
|
|
|
DeprecatedString Document::render_to_html(StringView extra_head_contents) const
|
2019-09-20 18:46:18 -03:00
|
|
|
{
|
|
|
|
|
StringBuilder builder;
|
2022-09-27 07:35:10 -03:00
|
|
|
builder.append(R"~~~(<!DOCTYPE html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<style>
|
|
|
|
|
code { white-space: pre; }
|
|
|
|
|
</style>
|
|
|
|
|
)~~~"sv);
|
|
|
|
|
if (!extra_head_contents.is_empty())
|
|
|
|
|
builder.append(extra_head_contents);
|
|
|
|
|
builder.append(R"~~~(
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
)~~~"sv);
|
2019-10-13 07:58:56 -03:00
|
|
|
|
2021-08-29 17:14:48 -03:00
|
|
|
builder.append(render_to_inline_html());
|
|
|
|
|
|
2022-09-27 07:35:10 -03:00
|
|
|
builder.append(R"~~~(
|
|
|
|
|
</body>
|
|
|
|
|
</html>)~~~"sv);
|
|
|
|
|
|
2023-01-26 15:58:09 -03:00
|
|
|
return builder.to_deprecated_string();
|
2021-08-29 17:14:48 -03:00
|
|
|
}
|
|
|
|
|
|
2022-12-04 15:02:33 -03:00
|
|
|
DeprecatedString Document::render_to_inline_html() const
|
2021-08-29 17:14:48 -03:00
|
|
|
{
|
2021-09-19 14:04:24 -03:00
|
|
|
return m_container->render_to_html();
|
2019-09-20 18:46:18 -03:00
|
|
|
}
|
|
|
|
|
|
2023-06-13 12:15:13 -03:00
|
|
|
ErrorOr<String> Document::render_for_terminal(size_t view_width) const
|
2019-09-20 18:46:18 -03:00
|
|
|
{
|
2022-12-23 06:25:00 -03:00
|
|
|
StringBuilder builder;
|
|
|
|
|
for (auto& line : m_container->render_lines_for_terminal(view_width)) {
|
2023-06-13 12:15:13 -03:00
|
|
|
TRY(builder.try_append(line));
|
|
|
|
|
TRY(builder.try_append("\n"sv));
|
2022-12-23 06:25:00 -03:00
|
|
|
}
|
|
|
|
|
|
2023-06-13 12:15:13 -03:00
|
|
|
return builder.to_string();
|
2019-09-20 18:46:18 -03:00
|
|
|
}
|
|
|
|
|
|
2021-09-10 16:36:29 -03:00
|
|
|
RecursionDecision Document::walk(Visitor& visitor) const
|
|
|
|
|
{
|
|
|
|
|
RecursionDecision rd = visitor.visit(*this);
|
|
|
|
|
if (rd != RecursionDecision::Recurse)
|
|
|
|
|
return rd;
|
|
|
|
|
|
|
|
|
|
return m_container->walk(visitor);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 20:55:02 -03:00
|
|
|
OwnPtr<Document> Document::parse(StringView str)
|
2019-09-20 18:46:18 -03:00
|
|
|
{
|
2022-04-01 14:58:27 -03:00
|
|
|
Vector<StringView> const lines_vec = str.lines();
|
2021-09-19 14:14:18 -03:00
|
|
|
LineIterator lines(lines_vec.begin());
|
2021-09-19 14:04:24 -03:00
|
|
|
return make<Document>(ContainerBlock::parse(lines));
|
2019-09-20 18:46:18 -03:00
|
|
|
}
|
2020-04-28 16:04:25 -03:00
|
|
|
|
|
|
|
|
}
|