1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:17:46 +00:00

LibMarkdown: Rewrite Inline text parser to be more forgiving

The previous Text::parse was not able to give up on parsing a textual
element, and just leave it as plain text. Because this is a very
important part of markdown, I fully rewrote the parser to support this
without having to backtrack. Also the parser now some other little
features, such ast delimiter runs and flanking.
This commit is contained in:
Peter Elliott 2021-09-06 19:11:46 -06:00 committed by Andreas Kling
parent 80e58dab9a
commit ec9f892899
10 changed files with 462 additions and 397 deletions

View file

@ -13,13 +13,7 @@ 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(m_text.render_to_html());
builder.append("</p>\n");
return builder.build();
}
@ -27,26 +21,9 @@ String Paragraph::render_to_html() const
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(m_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());
}
}