ladybird/Userland/Libraries/LibMarkdown/Paragraph.cpp
Peter Elliott dee84113ab LibMarkdown: Match HTML formatting of Commonmark tests
This patch changes the HTML formatting (where to put newlines, etc...)
to better match commonmark's test cases. This has minimal effect of the
correctness of our markdown implementation, but makes it easier to test.

Changes:
 - Use <em> instead of <i>.
 - Newline before end of code block.
 - <hr /> instead of <hr>.
 - Newline before first list item.
 - Newline between lines of a paragraph.
 - Trim whitespace on lines of paragraphs.

 Tests passed: 33/652 -> 87/652
2021-08-31 16:53:51 +02:00

52 lines
1.2 KiB
C++

/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/StringBuilder.h>
#include <LibMarkdown/Paragraph.h>
namespace Markdown {
String Paragraph::render_to_html() const
{
StringBuilder builder;
builder.append("<p>");
bool first = true;
for (auto& line : m_lines) {
if (!first)
builder.append('\n');
first = false;
builder.append(line.text().render_to_html().trim(" \t"));
}
builder.append("</p>\n");
return builder.build();
}
String Paragraph::render_for_terminal(size_t) const
{
StringBuilder builder;
bool first = true;
for (auto& line : m_lines) {
if (!first)
builder.append(' ');
first = false;
builder.append(line.text().render_for_terminal());
}
builder.append("\n\n");
return builder.build();
}
OwnPtr<Paragraph::Line> Paragraph::Line::parse(Vector<StringView>::ConstIterator& lines)
{
if (lines.is_end())
return {};
auto text = Text::parse(*lines++);
if (!text.has_value())
return {};
return make<Paragraph::Line>(text.release_value());
}
}