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/CodeBlock.h>
|
|
|
|
|
#include <LibMarkdown/Document.h>
|
|
|
|
|
#include <LibMarkdown/Heading.h>
|
2021-01-01 22:39:18 -03:00
|
|
|
#include <LibMarkdown/HorizontalRule.h>
|
2020-04-28 16:04:25 -03:00
|
|
|
#include <LibMarkdown/List.h>
|
|
|
|
|
#include <LibMarkdown/Paragraph.h>
|
2020-09-20 09:14:03 -03:00
|
|
|
#include <LibMarkdown/Table.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;
|
|
|
|
|
|
2019-10-13 07:58:56 -03:00
|
|
|
builder.append("<!DOCTYPE html>\n");
|
|
|
|
|
builder.append("<html>\n");
|
2020-10-31 15:11:44 -03:00
|
|
|
builder.append("<head>\n");
|
|
|
|
|
builder.append("<style>\n");
|
|
|
|
|
builder.append("code { white-space: pre; }\n");
|
|
|
|
|
builder.append("</style>\n");
|
|
|
|
|
builder.append("</head>\n");
|
2019-10-13 07:58:56 -03:00
|
|
|
builder.append("<body>\n");
|
|
|
|
|
|
2021-08-29 17:14:48 -03:00
|
|
|
builder.append(render_to_inline_html());
|
|
|
|
|
|
|
|
|
|
builder.append("</body>\n");
|
|
|
|
|
builder.append("</html>\n");
|
|
|
|
|
return builder.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String Document::render_to_inline_html() const
|
|
|
|
|
{
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
|
2019-09-20 18:46:18 -03:00
|
|
|
for (auto& block : m_blocks) {
|
|
|
|
|
auto s = block.render_to_html();
|
|
|
|
|
builder.append(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder.build();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
|
|
|
|
|
for (auto& block : m_blocks) {
|
2020-09-20 09:11:04 -03:00
|
|
|
auto s = block.render_for_terminal(view_width);
|
2019-09-20 18:46:18 -03:00
|
|
|
builder.append(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder.build();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 16:04:25 -03:00
|
|
|
template<typename BlockType>
|
|
|
|
|
static bool helper(Vector<StringView>::ConstIterator& lines, NonnullOwnPtrVector<Block>& blocks)
|
2019-09-20 18:46:18 -03:00
|
|
|
{
|
2020-05-18 17:58:00 -03:00
|
|
|
OwnPtr<BlockType> block = BlockType::parse(lines);
|
|
|
|
|
if (!block)
|
2019-09-20 18:46:18 -03:00
|
|
|
return false;
|
2020-05-18 17:58:00 -03:00
|
|
|
blocks.append(block.release_nonnull());
|
2019-09-20 18:46:18 -03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 17:58:00 -03:00
|
|
|
OwnPtr<Document> Document::parse(const StringView& str)
|
2019-09-20 18:46:18 -03:00
|
|
|
{
|
2019-12-02 09:42:33 -03:00
|
|
|
const Vector<StringView> lines_vec = str.lines();
|
2019-09-20 18:46:18 -03:00
|
|
|
auto lines = lines_vec.begin();
|
2020-05-18 17:58:00 -03:00
|
|
|
auto document = make<Document>();
|
2020-05-11 14:55:31 -03:00
|
|
|
auto& blocks = document->m_blocks;
|
2021-09-06 22:11:46 -03:00
|
|
|
StringBuilder paragraph_text;
|
2019-09-20 18:46:18 -03:00
|
|
|
|
2020-10-25 22:52:07 -03:00
|
|
|
auto flush_paragraph = [&] {
|
2021-09-06 22:11:46 -03:00
|
|
|
if (paragraph_text.is_empty())
|
2020-10-25 22:52:07 -03:00
|
|
|
return;
|
2021-09-06 22:11:46 -03:00
|
|
|
auto paragraph = make<Paragraph>(Text::parse(paragraph_text.build()));
|
2020-10-25 22:52:07 -03:00
|
|
|
document->m_blocks.append(move(paragraph));
|
2021-09-06 22:11:46 -03:00
|
|
|
paragraph_text.clear();
|
2020-10-25 22:52:07 -03:00
|
|
|
};
|
2021-09-06 22:11:46 -03:00
|
|
|
|
2019-09-20 18:46:18 -03:00
|
|
|
while (true) {
|
|
|
|
|
if (lines.is_end())
|
2020-05-11 14:55:31 -03:00
|
|
|
break;
|
2019-09-20 18:46:18 -03:00
|
|
|
|
|
|
|
|
if ((*lines).is_empty()) {
|
|
|
|
|
++lines;
|
2021-09-10 03:45:45 -03:00
|
|
|
|
2020-10-25 22:52:07 -03:00
|
|
|
flush_paragraph();
|
2019-09-20 18:46:18 -03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 13:12:23 -03:00
|
|
|
bool any = helper<Table>(lines, blocks) || helper<List>(lines, blocks) || helper<CodeBlock>(lines, blocks)
|
2021-01-01 22:39:18 -03:00
|
|
|
|| helper<Heading>(lines, blocks) || helper<HorizontalRule>(lines, blocks);
|
2020-09-20 13:12:23 -03:00
|
|
|
|
|
|
|
|
if (any) {
|
2021-09-06 22:11:46 -03:00
|
|
|
if (!paragraph_text.is_empty()) {
|
2020-09-20 13:12:23 -03:00
|
|
|
auto last_block = document->m_blocks.take_last();
|
2020-10-25 22:52:07 -03:00
|
|
|
flush_paragraph();
|
2020-09-20 13:12:23 -03:00
|
|
|
document->m_blocks.append(move(last_block));
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-09-20 18:46:18 -03:00
|
|
|
|
2021-09-10 03:45:45 -03:00
|
|
|
if (!paragraph_text.is_empty())
|
|
|
|
|
paragraph_text.append("\n");
|
2021-09-06 22:11:46 -03:00
|
|
|
paragraph_text.append(*lines++);
|
2020-09-20 13:12:23 -03:00
|
|
|
}
|
|
|
|
|
|
2021-09-06 22:11:46 -03:00
|
|
|
flush_paragraph();
|
2020-05-11 14:55:31 -03:00
|
|
|
|
|
|
|
|
return document;
|
2019-09-20 18:46:18 -03:00
|
|
|
}
|
2020-04-28 16:04:25 -03:00
|
|
|
|
|
|
|
|
}
|