2020-09-20 15:14:03 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-20 15:14:03 +03:00
|
|
|
*/
|
|
|
|
|
2021-01-17 18:06:30 +03:00
|
|
|
#include <AK/Debug.h>
|
2020-09-20 15:14:03 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <LibMarkdown/Table.h>
|
|
|
|
|
|
|
|
namespace Markdown {
|
|
|
|
|
|
|
|
String Table::render_for_terminal(size_t view_width) const
|
|
|
|
{
|
|
|
|
auto unit_width_length = view_width == 0 ? 4 : ((float)(view_width - m_columns.size()) / (float)m_total_width);
|
|
|
|
StringBuilder builder;
|
|
|
|
|
|
|
|
auto write_aligned = [&](const auto& text, auto width, auto alignment) {
|
|
|
|
size_t original_length = 0;
|
|
|
|
for (auto& span : text.spans())
|
|
|
|
original_length += span.text.length();
|
|
|
|
auto string = text.render_for_terminal();
|
|
|
|
if (alignment == Alignment::Center) {
|
|
|
|
auto padding_length = (width - original_length) / 2;
|
2021-01-11 17:01:33 +03:00
|
|
|
builder.appendf("%*s%s%*s", (int)padding_length, "", string.characters(), (int)padding_length, "");
|
2020-09-20 15:14:03 +03:00
|
|
|
if ((width - original_length) % 2)
|
|
|
|
builder.append(' ');
|
|
|
|
} else {
|
2021-01-11 17:01:33 +03:00
|
|
|
builder.appendf(alignment == Alignment::Left ? "%-*s" : "%*s", (int)(width + (string.length() - original_length)), string.characters());
|
2020-09-20 15:14:03 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
for (auto& col : m_columns) {
|
|
|
|
if (!first)
|
|
|
|
builder.append('|');
|
|
|
|
first = false;
|
|
|
|
size_t width = col.relative_width * unit_width_length;
|
|
|
|
write_aligned(col.header, width, col.alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.append("\n");
|
|
|
|
for (size_t i = 0; i < view_width; ++i)
|
|
|
|
builder.append('-');
|
|
|
|
builder.append("\n");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_row_count; ++i) {
|
|
|
|
bool first = true;
|
|
|
|
for (auto& col : m_columns) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(i < col.rows.size());
|
2020-09-20 15:14:03 +03:00
|
|
|
auto& cell = col.rows[i];
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
builder.append('|');
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
size_t width = col.relative_width * unit_width_length;
|
|
|
|
write_aligned(cell, width, col.alignment);
|
|
|
|
}
|
|
|
|
builder.append("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
String Table::render_to_html() const
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
|
|
|
|
builder.append("<table>");
|
|
|
|
builder.append("<thead>");
|
|
|
|
builder.append("<tr>");
|
|
|
|
for (auto& column : m_columns) {
|
|
|
|
builder.append("<th>");
|
|
|
|
builder.append(column.header.render_to_html());
|
|
|
|
builder.append("</th>");
|
|
|
|
}
|
|
|
|
builder.append("</tr>");
|
|
|
|
builder.append("</thead>");
|
|
|
|
builder.append("<tbody>");
|
|
|
|
for (size_t i = 0; i < m_row_count; ++i) {
|
|
|
|
builder.append("<tr>");
|
|
|
|
for (auto& column : m_columns) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(i < column.rows.size());
|
2020-09-20 15:14:03 +03:00
|
|
|
builder.append("<td>");
|
|
|
|
builder.append(column.rows[i].render_to_html());
|
|
|
|
builder.append("</td>");
|
|
|
|
}
|
|
|
|
builder.append("</tr>");
|
|
|
|
}
|
|
|
|
builder.append("</tbody>");
|
|
|
|
builder.append("</table>");
|
|
|
|
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
OwnPtr<Table> Table::parse(Vector<StringView>::ConstIterator& lines)
|
|
|
|
{
|
|
|
|
auto peek_it = lines;
|
|
|
|
auto first_line = *peek_it;
|
|
|
|
if (!first_line.starts_with('|'))
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2020-09-20 15:14:03 +03:00
|
|
|
|
|
|
|
++peek_it;
|
|
|
|
|
|
|
|
if (peek_it.is_end())
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2020-09-20 15:14:03 +03:00
|
|
|
|
|
|
|
auto header_segments = first_line.split_view('|', true);
|
|
|
|
auto header_delimiters = peek_it->split_view('|', true);
|
|
|
|
|
|
|
|
if (!header_segments.is_empty())
|
|
|
|
header_segments.take_first();
|
|
|
|
if (!header_segments.is_empty() && header_segments.last().is_empty())
|
|
|
|
header_segments.take_last();
|
|
|
|
|
|
|
|
if (!header_delimiters.is_empty())
|
|
|
|
header_delimiters.take_first();
|
|
|
|
if (!header_delimiters.is_empty() && header_delimiters.last().is_empty())
|
|
|
|
header_delimiters.take_last();
|
|
|
|
|
|
|
|
++peek_it;
|
|
|
|
|
|
|
|
if (header_delimiters.size() != header_segments.size())
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2020-09-20 15:14:03 +03:00
|
|
|
|
|
|
|
if (header_delimiters.is_empty())
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2020-09-20 15:14:03 +03:00
|
|
|
|
|
|
|
size_t total_width = 0;
|
|
|
|
|
|
|
|
auto table = make<Table>();
|
|
|
|
table->m_columns.resize(header_delimiters.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < header_segments.size(); ++i) {
|
|
|
|
auto text_option = Text::parse(header_segments[i]);
|
|
|
|
if (!text_option.has_value())
|
2021-01-11 02:29:28 +03:00
|
|
|
return {}; // An invalid 'text' in the header should just fail the table parse.
|
2020-09-20 15:14:03 +03:00
|
|
|
|
|
|
|
auto text = text_option.release_value();
|
|
|
|
auto& column = table->m_columns[i];
|
|
|
|
|
|
|
|
column.header = move(text);
|
|
|
|
|
|
|
|
auto delimiter = header_delimiters[i].trim_whitespace();
|
|
|
|
|
|
|
|
auto align_left = delimiter.starts_with(':');
|
|
|
|
auto align_right = delimiter != ":" && delimiter.ends_with(':');
|
|
|
|
|
|
|
|
if (align_left)
|
|
|
|
delimiter = delimiter.substring_view(1, delimiter.length() - 1);
|
|
|
|
if (align_right)
|
|
|
|
delimiter = delimiter.substring_view(0, delimiter.length() - 1);
|
|
|
|
|
|
|
|
if (align_left && align_right)
|
|
|
|
column.alignment = Alignment::Center;
|
|
|
|
else if (align_right)
|
|
|
|
column.alignment = Alignment::Right;
|
|
|
|
else
|
|
|
|
column.alignment = Alignment::Left;
|
|
|
|
|
|
|
|
size_t relative_width = delimiter.length();
|
|
|
|
for (auto ch : delimiter) {
|
|
|
|
if (ch != '-') {
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(MARKDOWN_DEBUG, "Invalid character _{}_ in table heading delimiter (ignored)", ch);
|
2020-09-20 15:14:03 +03:00
|
|
|
--relative_width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
column.relative_width = relative_width;
|
|
|
|
total_width += relative_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
table->m_total_width = total_width;
|
|
|
|
|
|
|
|
for (off_t i = 0; i < peek_it - lines; ++i)
|
|
|
|
++lines;
|
|
|
|
|
|
|
|
size_t row_count = 0;
|
|
|
|
++lines;
|
|
|
|
while (!lines.is_end()) {
|
|
|
|
auto& line = *lines;
|
|
|
|
if (!line.starts_with('|'))
|
|
|
|
break;
|
|
|
|
|
|
|
|
++lines;
|
|
|
|
|
|
|
|
auto segments = line.split_view('|', true);
|
|
|
|
segments.take_first();
|
|
|
|
if (!segments.is_empty() && segments.last().is_empty())
|
|
|
|
segments.take_last();
|
|
|
|
++row_count;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < header_segments.size(); ++i) {
|
|
|
|
if (i >= segments.size()) {
|
|
|
|
// Ran out of segments, but still have headers.
|
|
|
|
// Just make an empty cell.
|
|
|
|
table->m_columns[i].rows.append(Text { "" });
|
|
|
|
} else {
|
|
|
|
auto text_option = Text::parse(segments[i]);
|
|
|
|
// We treat an invalid 'text' as a literal.
|
|
|
|
if (text_option.has_value()) {
|
|
|
|
auto text = text_option.release_value();
|
|
|
|
table->m_columns[i].rows.append(move(text));
|
|
|
|
} else {
|
|
|
|
table->m_columns[i].rows.append(Text { segments[i] });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
table->m_row_count = row_count;
|
|
|
|
|
2021-03-17 18:30:02 +03:00
|
|
|
return table;
|
2020-09-20 15:14:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|