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

LibMarkdown: Implement "tightness" for lists

From the commonmark spec:
A list is loose if any of its constituent list items are separated by
blank lines, or if any of its constituent list items directly contain
two block-level elements with a blank line between them. Otherwise a
list is tight. (The difference in HTML output is that paragraphs in a
loose list are wrapped in <p> tags, while paragraphs in a tight list are
not.)
This commit is contained in:
Peter Elliott 2021-09-28 01:12:00 -06:00 committed by Ali Mohammad Pur
parent 5bb87c630c
commit 0a21c2bace
15 changed files with 75 additions and 28 deletions

View file

@ -15,7 +15,7 @@ class Block {
public:
virtual ~Block() { }
virtual String render_to_html() const = 0;
virtual String render_to_html(bool tight = false) const = 0;
virtual String render_for_terminal(size_t view_width = 0) const = 0;
};

View file

@ -11,7 +11,7 @@
namespace Markdown {
String CodeBlock::render_to_html() const
String CodeBlock::render_to_html(bool) const
{
StringBuilder builder;

View file

@ -23,7 +23,7 @@ public:
}
virtual ~CodeBlock() override { }
virtual String render_to_html() const override;
virtual String render_to_html(bool tight = false) const override;
virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<CodeBlock> parse(LineIterator& lines);

View file

@ -14,15 +14,26 @@
namespace Markdown {
String ContainerBlock::render_to_html() const
String ContainerBlock::render_to_html(bool tight) const
{
StringBuilder builder;
for (auto& block : m_blocks) {
auto s = block.render_to_html();
for (size_t i = 0; i + 1 < m_blocks.size(); ++i) {
auto s = m_blocks[i].render_to_html(tight);
builder.append(s);
}
// I don't like this edge case.
if (m_blocks.size() != 0) {
auto& block = m_blocks[m_blocks.size() - 1];
auto s = block.render_to_html(tight);
if (tight && dynamic_cast<Paragraph const*>(&block)) {
builder.append(s.substring_view(0, s.length() - 1));
} else {
builder.append(s);
}
}
return builder.build();
}
@ -62,15 +73,21 @@ OwnPtr<ContainerBlock> ContainerBlock::parse(LineIterator& lines)
paragraph_text.clear();
};
bool has_blank_lines = false;
bool has_trailing_blank_lines = false;
while (true) {
if (lines.is_end())
break;
if ((*lines).is_empty()) {
has_trailing_blank_lines = true;
++lines;
flush_paragraph();
continue;
} else {
has_blank_lines = has_blank_lines || has_trailing_blank_lines;
}
bool any = try_parse_block<Table>(lines, blocks) || try_parse_block<List>(lines, blocks) || try_parse_block<CodeBlock>(lines, blocks)
@ -92,7 +109,7 @@ OwnPtr<ContainerBlock> ContainerBlock::parse(LineIterator& lines)
flush_paragraph();
return make<ContainerBlock>(move(blocks));
return make<ContainerBlock>(move(blocks), has_blank_lines, has_trailing_blank_lines);
}
}

View file

@ -16,20 +16,29 @@ namespace Markdown {
class ContainerBlock final : public Block {
public:
ContainerBlock(NonnullOwnPtrVector<Block> blocks)
ContainerBlock(NonnullOwnPtrVector<Block> blocks, bool has_blank_lines, bool has_trailing_blank_lines)
: m_blocks(move(blocks))
, m_has_blank_lines(has_blank_lines)
, m_has_trailing_blank_lines(has_trailing_blank_lines)
{
}
virtual ~ContainerBlock() override { }
virtual String render_to_html() const override;
virtual String render_to_html(bool tight = false) const override;
virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<ContainerBlock> parse(LineIterator& lines);
bool has_blank_lines() const { return m_has_blank_lines; }
bool has_trailing_blank_lines() const { return m_has_trailing_blank_lines; }
NonnullOwnPtrVector<Block> const& blocks() const { return m_blocks; }
private:
NonnullOwnPtrVector<Block> m_blocks;
bool m_has_blank_lines;
bool m_has_trailing_blank_lines;
};
}

View file

@ -9,7 +9,7 @@
namespace Markdown {
String Heading::render_to_html() const
String Heading::render_to_html(bool) const
{
return String::formatted("<h{}>{}</h{}>\n", m_level, m_text.render_to_html(), m_level);
}

View file

@ -25,7 +25,7 @@ public:
}
virtual ~Heading() override { }
virtual String render_to_html() const override;
virtual String render_to_html(bool tight = false) const override;
virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<Heading> parse(LineIterator& lines);

View file

@ -10,7 +10,7 @@
namespace Markdown {
String HorizontalRule::render_to_html() const
String HorizontalRule::render_to_html(bool) const
{
return "<hr />\n";
}

View file

@ -21,7 +21,7 @@ public:
}
virtual ~HorizontalRule() override { }
virtual String render_to_html() const override;
virtual String render_to_html(bool tight = false) const override;
virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<HorizontalRule> parse(LineIterator& lines);
};

View file

@ -7,10 +7,11 @@
#include <AK/StringBuilder.h>
#include <LibMarkdown/List.h>
#include <LibMarkdown/Paragraph.h>
namespace Markdown {
String List::render_to_html() const
String List::render_to_html(bool) const
{
StringBuilder builder;
@ -18,8 +19,10 @@ String List::render_to_html() const
builder.appendff("<{}>\n", tag);
for (auto& item : m_items) {
builder.append("<li>\n");
builder.append(item->render_to_html());
builder.append("<li>");
if (!m_is_tight || (item->blocks().size() != 0 && !dynamic_cast<Paragraph const*>(&(item->blocks()[0]))))
builder.append("\n");
builder.append(item->render_to_html(m_is_tight));
builder.append("</li>\n");
}
@ -53,12 +56,15 @@ OwnPtr<List> List::parse(LineIterator& lines)
bool first = true;
bool is_ordered = false;
bool is_tight = true;
bool has_trailing_blank_lines = false;
while (!lines.is_end()) {
size_t offset = 0;
const StringView& line = *lines;
if (line.is_empty())
break;
bool appears_unordered = false;
if (line.length() > 2) {
@ -98,18 +104,23 @@ OwnPtr<List> List::parse(LineIterator& lines)
break;
}
is_tight = is_tight && !has_trailing_blank_lines;
size_t saved_indent = lines.indent();
lines.set_indent(saved_indent + offset);
lines.ignore_next_prefix();
items.append(ContainerBlock::parse(lines));
auto list_item = ContainerBlock::parse(lines);
is_tight = is_tight && !list_item->has_blank_lines();
has_trailing_blank_lines = has_trailing_blank_lines || list_item->has_trailing_blank_lines();
items.append(move(list_item));
lines.set_indent(saved_indent);
first = false;
}
return make<List>(move(items), is_ordered);
return make<List>(move(items), is_ordered, is_tight);
}
}

View file

@ -15,14 +15,15 @@ namespace Markdown {
class List final : public Block {
public:
List(Vector<OwnPtr<ContainerBlock>> items, bool is_ordered)
List(Vector<OwnPtr<ContainerBlock>> items, bool is_ordered, bool is_tight)
: m_items(move(items))
, m_is_ordered(is_ordered)
, m_is_tight(is_tight)
{
}
virtual ~List() override { }
virtual String render_to_html() const override;
virtual String render_to_html(bool tight = false) const override;
virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<List> parse(LineIterator& lines);
@ -30,6 +31,7 @@ public:
private:
Vector<OwnPtr<ContainerBlock>> m_items;
bool m_is_ordered { false };
bool m_is_tight { false };
};
}

View file

@ -9,12 +9,20 @@
namespace Markdown {
String Paragraph::render_to_html() const
String Paragraph::render_to_html(bool tight) const
{
StringBuilder builder;
if (!tight)
builder.append("<p>");
builder.append(m_text.render_to_html());
builder.append("</p>\n");
if (!tight)
builder.append("</p>");
builder.append('\n');
return builder.build();
}

View file

@ -22,7 +22,7 @@ public:
virtual ~Paragraph() override { }
virtual String render_to_html() const override;
virtual String render_to_html(bool tight = false) const override;
virtual String render_for_terminal(size_t view_width = 0) const override;
private:

View file

@ -65,7 +65,7 @@ String Table::render_for_terminal(size_t view_width) const
return builder.to_string();
}
String Table::render_to_html() const
String Table::render_to_html(bool) const
{
StringBuilder builder;

View file

@ -32,7 +32,7 @@ public:
Table() { }
virtual ~Table() override { }
virtual String render_to_html() const override;
virtual String render_to_html(bool tight = false) const override;
virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<Table> parse(LineIterator& lines);