2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2020-01-24 16:45:29 +03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2021-09-26 20:14:58 -06:00
|
|
|
* Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2022-12-23 12:25:00 +03:00
|
|
|
#include <AK/Forward.h>
|
2019-09-21 00:46:18 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-04-28 21:04:25 +02:00
|
|
|
#include <LibMarkdown/List.h>
|
2021-09-28 01:12:00 -06:00
|
|
|
#include <LibMarkdown/Paragraph.h>
|
2021-09-10 21:36:29 +02:00
|
|
|
#include <LibMarkdown/Visitor.h>
|
2019-09-21 00:46:18 +03:00
|
|
|
|
2020-04-28 21:04:25 +02:00
|
|
|
namespace Markdown {
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString List::render_to_html(bool) const
|
2019-09-21 00:46:18 +03:00
|
|
|
{
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* tag = m_is_ordered ? "ol" : "ul";
|
2021-09-29 23:19:56 -06:00
|
|
|
builder.appendff("<{}", tag);
|
|
|
|
|
|
|
|
|
|
if (m_start_number != 1)
|
|
|
|
|
builder.appendff(" start=\"{}\"", m_start_number);
|
|
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append(">\n"sv);
|
2019-09-21 00:46:18 +03:00
|
|
|
|
|
|
|
|
for (auto& item : m_items) {
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("<li>"sv);
|
2023-03-06 17:16:25 +01:00
|
|
|
if (!m_is_tight || (item->blocks().size() != 0 && !dynamic_cast<Paragraph const*>(item->blocks()[0].ptr())))
|
2022-07-11 20:10:18 +00:00
|
|
|
builder.append('\n');
|
2021-09-28 01:12:00 -06:00
|
|
|
builder.append(item->render_to_html(m_is_tight));
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("</li>\n"sv);
|
2019-09-21 00:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2021-05-05 20:12:26 +02:00
|
|
|
builder.appendff("</{}>\n", tag);
|
2019-09-21 00:46:18 +03:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
return builder.to_byte_string();
|
2019-09-21 00:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
Vector<ByteString> List::render_lines_for_terminal(size_t view_width) const
|
2019-09-21 00:46:18 +03:00
|
|
|
{
|
2023-12-16 17:49:34 +03:30
|
|
|
Vector<ByteString> lines;
|
2019-09-21 00:46:18 +03:00
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (auto& item : m_items) {
|
2022-12-23 12:25:00 +03:00
|
|
|
auto item_lines = item->render_lines_for_terminal(view_width);
|
|
|
|
|
auto first_line = item_lines.take_first();
|
|
|
|
|
|
|
|
|
|
StringBuilder builder;
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append(" "sv);
|
2019-09-21 00:46:18 +03:00
|
|
|
if (m_is_ordered)
|
2022-02-16 23:12:30 +02:00
|
|
|
builder.appendff("{}.", ++i);
|
2019-09-21 00:46:18 +03:00
|
|
|
else
|
2022-07-11 20:10:18 +00:00
|
|
|
builder.append('*');
|
2022-12-23 12:25:00 +03:00
|
|
|
auto item_indentation = builder.length();
|
|
|
|
|
|
|
|
|
|
builder.append(first_line);
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
lines.append(builder.to_byte_string());
|
2022-12-23 12:25:00 +03:00
|
|
|
|
|
|
|
|
for (auto& line : item_lines) {
|
|
|
|
|
builder.clear();
|
2023-12-16 17:49:34 +03:30
|
|
|
builder.append(ByteString::repeated(' ', item_indentation));
|
2022-12-23 12:25:00 +03:00
|
|
|
builder.append(line);
|
2023-12-16 17:49:34 +03:30
|
|
|
lines.append(builder.to_byte_string());
|
2022-12-23 12:25:00 +03:00
|
|
|
}
|
2019-09-21 00:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2022-12-23 12:25:00 +03:00
|
|
|
return lines;
|
2019-09-21 00:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2021-09-10 21:36:29 +02:00
|
|
|
RecursionDecision List::walk(Visitor& visitor) const
|
|
|
|
|
{
|
|
|
|
|
RecursionDecision rd = visitor.visit(*this);
|
|
|
|
|
if (rd != RecursionDecision::Recurse)
|
|
|
|
|
return rd;
|
|
|
|
|
|
|
|
|
|
for (auto const& block : m_items) {
|
|
|
|
|
rd = block->walk(visitor);
|
|
|
|
|
if (rd == RecursionDecision::Break)
|
|
|
|
|
return rd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RecursionDecision::Continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 11:14:18 -06:00
|
|
|
OwnPtr<List> List::parse(LineIterator& lines)
|
2019-09-21 00:46:18 +03:00
|
|
|
{
|
2021-09-26 20:14:58 -06:00
|
|
|
Vector<OwnPtr<ContainerBlock>> items;
|
2020-05-18 16:58:00 -04:00
|
|
|
|
2019-09-21 00:46:18 +03:00
|
|
|
bool first = true;
|
2021-09-26 20:14:58 -06:00
|
|
|
bool is_ordered = false;
|
2021-09-28 01:12:00 -06:00
|
|
|
|
|
|
|
|
bool is_tight = true;
|
|
|
|
|
bool has_trailing_blank_lines = false;
|
2021-09-29 23:19:56 -06:00
|
|
|
size_t start_number = 1;
|
2021-09-28 01:12:00 -06:00
|
|
|
|
2021-09-26 20:14:58 -06:00
|
|
|
while (!lines.is_end()) {
|
2021-09-28 01:12:00 -06:00
|
|
|
|
2021-09-26 20:14:58 -06:00
|
|
|
size_t offset = 0;
|
2020-05-04 19:44:37 +03:00
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
StringView line = *lines;
|
2019-09-21 00:46:18 +03:00
|
|
|
|
|
|
|
|
bool appears_unordered = false;
|
2021-09-29 19:24:22 -06:00
|
|
|
|
|
|
|
|
while (offset < line.length() && line[offset] == ' ')
|
|
|
|
|
++offset;
|
|
|
|
|
|
|
|
|
|
if (offset + 2 <= line.length()) {
|
|
|
|
|
if (line[offset + 1] == ' ' && (line[offset] == '*' || line[offset] == '-' || line[offset] == '+')) {
|
2019-09-21 00:46:18 +03:00
|
|
|
appears_unordered = true;
|
2021-09-29 19:24:22 -06:00
|
|
|
offset++;
|
2019-09-21 00:46:18 +03:00
|
|
|
}
|
2020-05-04 19:44:37 +03:00
|
|
|
}
|
2019-09-21 00:46:18 +03:00
|
|
|
|
|
|
|
|
bool appears_ordered = false;
|
2021-09-29 19:24:22 -06:00
|
|
|
for (size_t i = offset; i < 10 && i < line.length(); i++) {
|
2019-09-21 00:46:18 +03:00
|
|
|
char ch = line[i];
|
|
|
|
|
if ('0' <= ch && ch <= '9')
|
|
|
|
|
continue;
|
|
|
|
|
if (ch == '.' || ch == ')')
|
|
|
|
|
if (i + 1 < line.length() && line[i + 1] == ' ') {
|
2023-12-23 15:59:14 +13:00
|
|
|
auto maybe_start_number = line.substring_view(offset, i - offset).to_number<size_t>();
|
2021-09-29 23:19:56 -06:00
|
|
|
if (!maybe_start_number.has_value())
|
|
|
|
|
break;
|
|
|
|
|
if (first)
|
|
|
|
|
start_number = maybe_start_number.value();
|
2019-09-21 00:46:18 +03:00
|
|
|
appears_ordered = true;
|
|
|
|
|
offset = i + 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!(appears_unordered && appears_ordered));
|
2021-09-26 20:14:58 -06:00
|
|
|
if (!appears_unordered && !appears_ordered) {
|
2020-05-04 19:44:37 +03:00
|
|
|
if (first)
|
2021-01-10 16:29:28 -07:00
|
|
|
return {};
|
2020-05-04 19:44:37 +03:00
|
|
|
|
2021-09-26 20:14:58 -06:00
|
|
|
break;
|
|
|
|
|
}
|
2020-05-04 19:44:37 +03:00
|
|
|
|
2021-09-26 20:14:58 -06:00
|
|
|
while (offset < line.length() && line[offset] == ' ')
|
|
|
|
|
offset++;
|
2020-05-04 19:44:37 +03:00
|
|
|
|
2021-09-26 20:14:58 -06:00
|
|
|
if (first) {
|
|
|
|
|
is_ordered = appears_ordered;
|
|
|
|
|
} else if (appears_ordered != is_ordered) {
|
|
|
|
|
break;
|
2020-05-04 19:44:37 +03:00
|
|
|
}
|
2019-09-21 00:46:18 +03:00
|
|
|
|
2021-09-28 01:12:00 -06:00
|
|
|
is_tight = is_tight && !has_trailing_blank_lines;
|
|
|
|
|
|
2021-10-03 21:45:10 -06:00
|
|
|
lines.push_context(LineIterator::Context::list_item(offset));
|
2021-09-26 20:14:58 -06:00
|
|
|
|
2021-09-28 01:12:00 -06:00
|
|
|
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));
|
2021-09-26 20:14:58 -06:00
|
|
|
|
2021-10-03 21:45:10 -06:00
|
|
|
lines.pop_context();
|
2021-09-26 20:14:58 -06:00
|
|
|
|
2019-09-21 00:46:18 +03:00
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-29 23:19:56 -06:00
|
|
|
return make<List>(move(items), is_ordered, is_tight, start_number);
|
2019-09-21 00:46:18 +03:00
|
|
|
}
|
2020-04-28 21:04:25 +02:00
|
|
|
|
|
|
|
|
}
|