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

LibMarkdown: Parse paragraphs line-wise

This gets rid of the doubled-up checks in `Paragraph::parse()`, and
makes a paragraph the last possible kind of block to be parsed.
This commit is contained in:
AnotherTest 2020-09-20 20:42:23 +04:30 committed by Andreas Kling
parent 176a2f193c
commit eef794b8c6
3 changed files with 61 additions and 50 deletions

View file

@ -81,6 +81,7 @@ OwnPtr<Document> Document::parse(const StringView& str)
auto lines = lines_vec.begin();
auto document = make<Document>();
auto& blocks = document->m_blocks;
NonnullOwnPtrVector<Paragraph::Line> paragraph_lines;
while (true) {
if (lines.is_end())
@ -91,11 +92,30 @@ OwnPtr<Document> Document::parse(const StringView& str)
continue;
}
bool any = helper<Table>(lines, blocks) || helper<List>(lines, blocks) || helper<Paragraph>(lines, blocks)
|| helper<CodeBlock>(lines, blocks) || helper<Heading>(lines, blocks);
bool any = helper<Table>(lines, blocks) || helper<List>(lines, blocks) || helper<CodeBlock>(lines, blocks)
|| helper<Heading>(lines, blocks);
if (!any)
if (any) {
if (!paragraph_lines.is_empty()) {
auto last_block = document->m_blocks.take_last();
auto paragraph = make<Paragraph>(move(paragraph_lines));
document->m_blocks.append(move(paragraph));
document->m_blocks.append(move(last_block));
paragraph_lines.clear();
}
continue;
}
auto line = Paragraph::Line::parse(lines);
if (!line)
return nullptr;
paragraph_lines.append(line.release_nonnull());
}
if (!paragraph_lines.is_empty()) {
auto paragraph = make<Paragraph>(move(paragraph_lines));
document->m_blocks.append(move(paragraph));
}
return document;