diff options
author | Peter Elliott <pelliott@ualberta.ca> | 2021-09-28 01:12:00 -0600 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-10-05 13:27:25 +0330 |
commit | 0a21c2bace9797f835718bf4ea45fb09a5a2297a (patch) | |
tree | a2056944cefc95c1753e08b542e205ffc8fe77d5 | |
parent | 5bb87c630c5599309f29977512d7719ff6cae2eb (diff) | |
download | serenity-0a21c2bace9797f835718bf4ea45fb09a5a2297a.zip |
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.)
-rw-r--r-- | Userland/Libraries/LibMarkdown/Block.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/CodeBlock.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/CodeBlock.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/ContainerBlock.cpp | 25 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/ContainerBlock.h | 13 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/Heading.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/Heading.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/HorizontalRule.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/HorizontalRule.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/List.cpp | 25 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/List.h | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/Paragraph.cpp | 14 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/Paragraph.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/Table.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibMarkdown/Table.h | 2 |
15 files changed, 75 insertions, 28 deletions
diff --git a/Userland/Libraries/LibMarkdown/Block.h b/Userland/Libraries/LibMarkdown/Block.h index 7e5fb393d5..bc5ec0e897 100644 --- a/Userland/Libraries/LibMarkdown/Block.h +++ b/Userland/Libraries/LibMarkdown/Block.h @@ -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; }; diff --git a/Userland/Libraries/LibMarkdown/CodeBlock.cpp b/Userland/Libraries/LibMarkdown/CodeBlock.cpp index e3e946dba0..d433d3c61e 100644 --- a/Userland/Libraries/LibMarkdown/CodeBlock.cpp +++ b/Userland/Libraries/LibMarkdown/CodeBlock.cpp @@ -11,7 +11,7 @@ namespace Markdown { -String CodeBlock::render_to_html() const +String CodeBlock::render_to_html(bool) const { StringBuilder builder; diff --git a/Userland/Libraries/LibMarkdown/CodeBlock.h b/Userland/Libraries/LibMarkdown/CodeBlock.h index 7b353956d2..c4b7ff7885 100644 --- a/Userland/Libraries/LibMarkdown/CodeBlock.h +++ b/Userland/Libraries/LibMarkdown/CodeBlock.h @@ -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); diff --git a/Userland/Libraries/LibMarkdown/ContainerBlock.cpp b/Userland/Libraries/LibMarkdown/ContainerBlock.cpp index 7585d635ee..9585264661 100644 --- a/Userland/Libraries/LibMarkdown/ContainerBlock.cpp +++ b/Userland/Libraries/LibMarkdown/ContainerBlock.cpp @@ -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); } } diff --git a/Userland/Libraries/LibMarkdown/ContainerBlock.h b/Userland/Libraries/LibMarkdown/ContainerBlock.h index 682f850c69..c737a6fc0e 100644 --- a/Userland/Libraries/LibMarkdown/ContainerBlock.h +++ b/Userland/Libraries/LibMarkdown/ContainerBlock.h @@ -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; }; } diff --git a/Userland/Libraries/LibMarkdown/Heading.cpp b/Userland/Libraries/LibMarkdown/Heading.cpp index 50fe6219f9..66c8d4b02d 100644 --- a/Userland/Libraries/LibMarkdown/Heading.cpp +++ b/Userland/Libraries/LibMarkdown/Heading.cpp @@ -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); } diff --git a/Userland/Libraries/LibMarkdown/Heading.h b/Userland/Libraries/LibMarkdown/Heading.h index 6378636475..4e7ffa1948 100644 --- a/Userland/Libraries/LibMarkdown/Heading.h +++ b/Userland/Libraries/LibMarkdown/Heading.h @@ -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); diff --git a/Userland/Libraries/LibMarkdown/HorizontalRule.cpp b/Userland/Libraries/LibMarkdown/HorizontalRule.cpp index 4f20d186ea..5d884df3db 100644 --- a/Userland/Libraries/LibMarkdown/HorizontalRule.cpp +++ b/Userland/Libraries/LibMarkdown/HorizontalRule.cpp @@ -10,7 +10,7 @@ namespace Markdown { -String HorizontalRule::render_to_html() const +String HorizontalRule::render_to_html(bool) const { return "<hr />\n"; } diff --git a/Userland/Libraries/LibMarkdown/HorizontalRule.h b/Userland/Libraries/LibMarkdown/HorizontalRule.h index f4568ce214..034b2233ea 100644 --- a/Userland/Libraries/LibMarkdown/HorizontalRule.h +++ b/Userland/Libraries/LibMarkdown/HorizontalRule.h @@ -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); }; diff --git a/Userland/Libraries/LibMarkdown/List.cpp b/Userland/Libraries/LibMarkdown/List.cpp index 393ff2899c..a75757b6e9 100644 --- a/Userland/Libraries/LibMarkdown/List.cpp +++ b/Userland/Libraries/LibMarkdown/List.cpp @@ -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); } } diff --git a/Userland/Libraries/LibMarkdown/List.h b/Userland/Libraries/LibMarkdown/List.h index ee484c50e8..90492f6654 100644 --- a/Userland/Libraries/LibMarkdown/List.h +++ b/Userland/Libraries/LibMarkdown/List.h @@ -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 }; }; } diff --git a/Userland/Libraries/LibMarkdown/Paragraph.cpp b/Userland/Libraries/LibMarkdown/Paragraph.cpp index c2c1111266..df0e3f2aea 100644 --- a/Userland/Libraries/LibMarkdown/Paragraph.cpp +++ b/Userland/Libraries/LibMarkdown/Paragraph.cpp @@ -9,12 +9,20 @@ namespace Markdown { -String Paragraph::render_to_html() const +String Paragraph::render_to_html(bool tight) const { StringBuilder builder; - builder.append("<p>"); + + 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(); } diff --git a/Userland/Libraries/LibMarkdown/Paragraph.h b/Userland/Libraries/LibMarkdown/Paragraph.h index 4b75e1cd67..e9c8db692e 100644 --- a/Userland/Libraries/LibMarkdown/Paragraph.h +++ b/Userland/Libraries/LibMarkdown/Paragraph.h @@ -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: diff --git a/Userland/Libraries/LibMarkdown/Table.cpp b/Userland/Libraries/LibMarkdown/Table.cpp index 692e2877bb..37f3c1ad5a 100644 --- a/Userland/Libraries/LibMarkdown/Table.cpp +++ b/Userland/Libraries/LibMarkdown/Table.cpp @@ -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; diff --git a/Userland/Libraries/LibMarkdown/Table.h b/Userland/Libraries/LibMarkdown/Table.h index 76e0143c21..a4f4d0e578 100644 --- a/Userland/Libraries/LibMarkdown/Table.h +++ b/Userland/Libraries/LibMarkdown/Table.h @@ -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); |