summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibMarkdown/ContainerBlock.cpp
diff options
context:
space:
mode:
authorPeter Elliott <pelliott@ualberta.ca>2021-09-28 01:12:00 -0600
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-10-05 13:27:25 +0330
commit0a21c2bace9797f835718bf4ea45fb09a5a2297a (patch)
treea2056944cefc95c1753e08b542e205ffc8fe77d5 /Userland/Libraries/LibMarkdown/ContainerBlock.cpp
parent5bb87c630c5599309f29977512d7719ff6cae2eb (diff)
downloadserenity-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.)
Diffstat (limited to 'Userland/Libraries/LibMarkdown/ContainerBlock.cpp')
-rw-r--r--Userland/Libraries/LibMarkdown/ContainerBlock.cpp25
1 files changed, 21 insertions, 4 deletions
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);
}
}