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 {
|
|
|
|
|
|
|
|
|
|
String Document::render_to_html() const
|
2019-09-20 18:46:18 -03:00
|
|
|
{
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
builder.append("<!DOCTYPE html>\n"sv);
|
|
|
|
|
builder.append("<html>\n"sv);
|
|
|
|
|
builder.append("<head>\n"sv);
|
|
|
|
|
builder.append("<style>\n"sv);
|
|
|
|
|
builder.append("code { white-space: pre; }\n"sv);
|
|
|
|
|
builder.append("</style>\n"sv);
|
|
|
|
|
builder.append("</head>\n"sv);
|
|
|
|
|
builder.append("<body>\n"sv);
|
2019-10-13 07:58:56 -03:00
|
|
|
|
2021-08-29 17:14:48 -03:00
|
|
|
builder.append(render_to_inline_html());
|
|
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
builder.append("</body>\n"sv);
|
|
|
|
|
builder.append("</html>\n"sv);
|
2021-08-29 17:14:48 -03:00
|
|
|
return builder.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String Document::render_to_inline_html() const
|
|
|
|
|
{
|
2021-09-19 14:04:24 -03:00
|
|
|
return m_container->render_to_html();
|
2019-09-20 18:46:18 -03:00
|
|
|
}
|
|
|
|
|
|
2020-09-20 09:11:04 -03:00
|
|
|
String Document::render_for_terminal(size_t view_width) const
|
2019-09-20 18:46:18 -03:00
|
|
|
{
|
2021-09-19 14:04:24 -03:00
|
|
|
return m_container->render_for_terminal(view_width);
|
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
|
|
|
|
|
|
|
|
}
|