mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:57:47 +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:
parent
176a2f193c
commit
eef794b8c6
3 changed files with 61 additions and 50 deletions
|
@ -81,6 +81,7 @@ OwnPtr<Document> Document::parse(const StringView& str)
|
||||||
auto lines = lines_vec.begin();
|
auto lines = lines_vec.begin();
|
||||||
auto document = make<Document>();
|
auto document = make<Document>();
|
||||||
auto& blocks = document->m_blocks;
|
auto& blocks = document->m_blocks;
|
||||||
|
NonnullOwnPtrVector<Paragraph::Line> paragraph_lines;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (lines.is_end())
|
if (lines.is_end())
|
||||||
|
@ -91,11 +92,30 @@ OwnPtr<Document> Document::parse(const StringView& str)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool any = helper<Table>(lines, blocks) || helper<List>(lines, blocks) || helper<Paragraph>(lines, blocks)
|
bool any = helper<Table>(lines, blocks) || helper<List>(lines, blocks) || helper<CodeBlock>(lines, blocks)
|
||||||
|| helper<CodeBlock>(lines, blocks) || helper<Heading>(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;
|
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;
|
return document;
|
||||||
|
|
|
@ -33,7 +33,13 @@ String Paragraph::render_to_html() const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.appendf("<p>");
|
builder.appendf("<p>");
|
||||||
builder.append(m_text.render_to_html());
|
bool first = true;
|
||||||
|
for (auto& line : m_lines) {
|
||||||
|
if (!first)
|
||||||
|
builder.append(' ');
|
||||||
|
first = false;
|
||||||
|
builder.append(line.text().render_to_html());
|
||||||
|
}
|
||||||
builder.appendf("</p>\n");
|
builder.appendf("</p>\n");
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
@ -41,58 +47,26 @@ String Paragraph::render_to_html() const
|
||||||
String Paragraph::render_for_terminal(size_t) const
|
String Paragraph::render_for_terminal(size_t) const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.append(m_text.render_for_terminal());
|
bool first = true;
|
||||||
|
for (auto& line : m_lines) {
|
||||||
|
if (!first)
|
||||||
|
builder.append(' ');
|
||||||
|
first = false;
|
||||||
|
builder.append(line.text().render_for_terminal());
|
||||||
|
}
|
||||||
builder.appendf("\n\n");
|
builder.appendf("\n\n");
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
OwnPtr<Paragraph> Paragraph::parse(Vector<StringView>::ConstIterator& lines)
|
OwnPtr<Paragraph::Line> Paragraph::Line::parse(Vector<StringView>::ConstIterator& lines)
|
||||||
{
|
{
|
||||||
if (lines.is_end())
|
if (lines.is_end())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
bool first = true;
|
auto text = Text::parse(*lines++);
|
||||||
StringBuilder builder;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
if (lines.is_end())
|
|
||||||
break;
|
|
||||||
StringView line = *lines;
|
|
||||||
if (line.is_empty())
|
|
||||||
break;
|
|
||||||
char ch = line[0];
|
|
||||||
// See if it looks like a blockquote
|
|
||||||
// or like an indented block.
|
|
||||||
if (ch == '>' || ch == ' ')
|
|
||||||
break;
|
|
||||||
if (line.length() > 1) {
|
|
||||||
// See if it looks like a heading.
|
|
||||||
if (ch == '#' && (line[1] == '#' || line[1] == ' '))
|
|
||||||
break;
|
|
||||||
// See if it looks like a code block.
|
|
||||||
if (ch == '`' && line[1] == '`')
|
|
||||||
break;
|
|
||||||
// See if it looks like a list.
|
|
||||||
if (ch == '*' || ch == '-')
|
|
||||||
if (line[1] == ' ')
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!first)
|
|
||||||
builder.append(' ');
|
|
||||||
builder.append(line);
|
|
||||||
first = false;
|
|
||||||
++lines;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (first)
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
auto text = Text::parse(builder.build());
|
|
||||||
if (!text.has_value())
|
if (!text.has_value())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
return make<Paragraph>(move(text.value()));
|
return make<Paragraph::Line>(text.release_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/NonnullOwnPtrVector.h>
|
||||||
#include <AK/OwnPtr.h>
|
#include <AK/OwnPtr.h>
|
||||||
#include <LibMarkdown/Block.h>
|
#include <LibMarkdown/Block.h>
|
||||||
#include <LibMarkdown/Text.h>
|
#include <LibMarkdown/Text.h>
|
||||||
|
@ -34,18 +35,34 @@ namespace Markdown {
|
||||||
|
|
||||||
class Paragraph final : public Block {
|
class Paragraph final : public Block {
|
||||||
public:
|
public:
|
||||||
explicit Paragraph(Text&& text)
|
class Line {
|
||||||
|
public:
|
||||||
|
explicit Line(Text&& text)
|
||||||
: m_text(move(text))
|
: m_text(move(text))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static OwnPtr<Line> parse(Vector<StringView>::ConstIterator& lines);
|
||||||
|
const Text& text() const { return m_text; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
Text m_text;
|
||||||
|
};
|
||||||
|
|
||||||
|
Paragraph(NonnullOwnPtrVector<Line>&& lines)
|
||||||
|
: m_lines(move(lines))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
virtual ~Paragraph() override { }
|
virtual ~Paragraph() override { }
|
||||||
|
|
||||||
virtual String render_to_html() const override;
|
virtual String render_to_html() const override;
|
||||||
virtual String render_for_terminal(size_t view_width = 0) const override;
|
virtual String render_for_terminal(size_t view_width = 0) const override;
|
||||||
static OwnPtr<Paragraph> parse(Vector<StringView>::ConstIterator& lines);
|
|
||||||
|
void add_line(NonnullOwnPtr<Line>&& line);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Text m_text;
|
NonnullOwnPtrVector<Line> m_lines;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue