1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:27:35 +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: public:
virtual ~Block() { } 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; virtual String render_for_terminal(size_t view_width = 0) const = 0;
}; };

View file

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

View file

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

View file

@ -14,15 +14,26 @@
namespace Markdown { namespace Markdown {
String ContainerBlock::render_to_html() const String ContainerBlock::render_to_html(bool tight) const
{ {
StringBuilder builder; StringBuilder builder;
for (auto& block : m_blocks) { for (size_t i = 0; i + 1 < m_blocks.size(); ++i) {
auto s = block.render_to_html(); auto s = m_blocks[i].render_to_html(tight);
builder.append(s); 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(); return builder.build();
} }
@ -62,15 +73,21 @@ OwnPtr<ContainerBlock> ContainerBlock::parse(LineIterator& lines)
paragraph_text.clear(); paragraph_text.clear();
}; };
bool has_blank_lines = false;
bool has_trailing_blank_lines = false;
while (true) { while (true) {
if (lines.is_end()) if (lines.is_end())
break; break;
if ((*lines).is_empty()) { if ((*lines).is_empty()) {
has_trailing_blank_lines = true;
++lines; ++lines;
flush_paragraph(); flush_paragraph();
continue; 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) 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(); 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 { class ContainerBlock final : public Block {
public: public:
ContainerBlock(NonnullOwnPtrVector<Block> blocks) ContainerBlock(NonnullOwnPtrVector<Block> blocks, bool has_blank_lines, bool has_trailing_blank_lines)
: m_blocks(move(blocks)) : m_blocks(move(blocks))
, m_has_blank_lines(has_blank_lines)
, m_has_trailing_blank_lines(has_trailing_blank_lines)
{ {
} }
virtual ~ContainerBlock() override { } 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; virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<ContainerBlock> parse(LineIterator& lines); 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: private:
NonnullOwnPtrVector<Block> m_blocks; NonnullOwnPtrVector<Block> m_blocks;
bool m_has_blank_lines;
bool m_has_trailing_blank_lines;
}; };
} }

View file

@ -9,7 +9,7 @@
namespace Markdown { 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); 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 ~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; virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<Heading> parse(LineIterator& lines); static OwnPtr<Heading> parse(LineIterator& lines);

View file

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

View file

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

View file

@ -7,10 +7,11 @@
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <LibMarkdown/List.h> #include <LibMarkdown/List.h>
#include <LibMarkdown/Paragraph.h>
namespace Markdown { namespace Markdown {
String List::render_to_html() const String List::render_to_html(bool) const
{ {
StringBuilder builder; StringBuilder builder;
@ -18,8 +19,10 @@ String List::render_to_html() const
builder.appendff("<{}>\n", tag); builder.appendff("<{}>\n", tag);
for (auto& item : m_items) { for (auto& item : m_items) {
builder.append("<li>\n"); builder.append("<li>");
builder.append(item->render_to_html()); 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"); builder.append("</li>\n");
} }
@ -53,12 +56,15 @@ OwnPtr<List> List::parse(LineIterator& lines)
bool first = true; bool first = true;
bool is_ordered = false; bool is_ordered = false;
bool is_tight = true;
bool has_trailing_blank_lines = false;
while (!lines.is_end()) { while (!lines.is_end()) {
size_t offset = 0; size_t offset = 0;
const StringView& line = *lines; const StringView& line = *lines;
if (line.is_empty())
break;
bool appears_unordered = false; bool appears_unordered = false;
if (line.length() > 2) { if (line.length() > 2) {
@ -98,18 +104,23 @@ OwnPtr<List> List::parse(LineIterator& lines)
break; break;
} }
is_tight = is_tight && !has_trailing_blank_lines;
size_t saved_indent = lines.indent(); size_t saved_indent = lines.indent();
lines.set_indent(saved_indent + offset); lines.set_indent(saved_indent + offset);
lines.ignore_next_prefix(); 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); lines.set_indent(saved_indent);
first = false; 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 { class List final : public Block {
public: 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_items(move(items))
, m_is_ordered(is_ordered) , m_is_ordered(is_ordered)
, m_is_tight(is_tight)
{ {
} }
virtual ~List() override { } 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; virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<List> parse(LineIterator& lines); static OwnPtr<List> parse(LineIterator& lines);
@ -30,6 +31,7 @@ public:
private: private:
Vector<OwnPtr<ContainerBlock>> m_items; Vector<OwnPtr<ContainerBlock>> m_items;
bool m_is_ordered { false }; bool m_is_ordered { false };
bool m_is_tight { false };
}; };
} }

View file

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

View file

@ -22,7 +22,7 @@ public:
virtual ~Paragraph() override { } 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; virtual String render_for_terminal(size_t view_width = 0) const override;
private: private:

View file

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

View file

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