2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2020-01-24 16:45:29 +03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2021-09-06 19:11:46 -06:00
|
|
|
* Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-09-21 00:46:18 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-04-28 21:04:25 +02:00
|
|
|
#include <LibMarkdown/Document.h>
|
2021-09-19 11:14:18 -06:00
|
|
|
#include <LibMarkdown/LineIterator.h>
|
2021-09-10 21:36:29 +02:00
|
|
|
#include <LibMarkdown/Visitor.h>
|
2019-09-21 00:46:18 +03:00
|
|
|
|
2020-04-28 21:04:25 +02:00
|
|
|
namespace Markdown {
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString Document::render_to_html(StringView extra_head_contents) const
|
2019-09-21 00:46:18 +03:00
|
|
|
{
|
|
|
|
|
StringBuilder builder;
|
2022-09-27 11:35:10 +01: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 12:58:56 +02:00
|
|
|
|
2021-08-29 13:14:48 -07:00
|
|
|
builder.append(render_to_inline_html());
|
|
|
|
|
|
2022-09-27 11:35:10 +01:00
|
|
|
builder.append(R"~~~(
|
|
|
|
|
</body>
|
|
|
|
|
</html>)~~~"sv);
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
return builder.to_byte_string();
|
2021-08-29 13:14:48 -07:00
|
|
|
}
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString Document::render_to_inline_html() const
|
2021-08-29 13:14:48 -07:00
|
|
|
{
|
2021-09-19 11:04:24 -06:00
|
|
|
return m_container->render_to_html();
|
2019-09-21 00:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2023-06-13 16:15:13 +01:00
|
|
|
ErrorOr<String> Document::render_for_terminal(size_t view_width) const
|
2019-09-21 00:46:18 +03:00
|
|
|
{
|
2022-12-23 12:25:00 +03:00
|
|
|
StringBuilder builder;
|
|
|
|
|
for (auto& line : m_container->render_lines_for_terminal(view_width)) {
|
2023-06-13 16:15:13 +01:00
|
|
|
TRY(builder.try_append(line));
|
|
|
|
|
TRY(builder.try_append("\n"sv));
|
2022-12-23 12:25:00 +03:00
|
|
|
}
|
|
|
|
|
|
2023-06-13 16:15:13 +01:00
|
|
|
return builder.to_string();
|
2019-09-21 00:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2021-09-10 21:36:29 +02: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-11 00:55:02 +01:00
|
|
|
OwnPtr<Document> Document::parse(StringView str)
|
2019-09-21 00:46:18 +03:00
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
Vector<StringView> const lines_vec = str.lines();
|
2021-09-19 11:14:18 -06:00
|
|
|
LineIterator lines(lines_vec.begin());
|
2021-09-19 11:04:24 -06:00
|
|
|
return make<Document>(ContainerBlock::parse(lines));
|
2019-09-21 00:46:18 +03:00
|
|
|
}
|
2020-04-28 21:04:25 +02:00
|
|
|
|
|
|
|
|
}
|