185 lines
6.3 KiB
Rust
185 lines
6.3 KiB
Rust
//! Markdown of the generated French reports: the building blocks of
|
||
//! `packages/anatomy/src/markdown.ts` and the one change Prettier makes to those reports,
|
||
//! the alignment of the tables (measured on the three reports: no other line changes).
|
||
//! Pinned by `oracles/r5/markdown-tables.json` and the committed reports.
|
||
|
||
use super::PipelineError;
|
||
use super::text::display_width;
|
||
use crate::json::js_number;
|
||
|
||
/// A table cell value: text, a number (printed as `String(number)`) or nothing (`-`).
|
||
#[derive(Debug, Clone, PartialEq)]
|
||
pub enum Cell {
|
||
Text(String),
|
||
Number(f64),
|
||
Null,
|
||
}
|
||
|
||
impl From<&str> for Cell {
|
||
fn from(text: &str) -> Self {
|
||
Self::Text(text.to_owned())
|
||
}
|
||
}
|
||
|
||
impl From<String> for Cell {
|
||
fn from(text: String) -> Self {
|
||
Self::Text(text)
|
||
}
|
||
}
|
||
|
||
impl From<usize> for Cell {
|
||
fn from(count: usize) -> Self {
|
||
Self::Number(count as f64)
|
||
}
|
||
}
|
||
|
||
impl From<Option<String>> for Cell {
|
||
fn from(value: Option<String>) -> Self {
|
||
value.map_or(Self::Null, Self::Text)
|
||
}
|
||
}
|
||
|
||
/// `cell()`: `-` for null; pipes escaped, newlines turned into spaces.
|
||
pub fn cell(value: &Cell) -> String {
|
||
match value {
|
||
Cell::Null => "-".to_owned(),
|
||
Cell::Number(number) => js_number(*number),
|
||
Cell::Text(text) => text.replace('|', "\\|").replace('\n', " "),
|
||
}
|
||
}
|
||
|
||
/// `table()`: the raw table the TypeScript wrote before Prettier aligned it.
|
||
pub fn table(head: &[&str], rows: &[Vec<Cell>]) -> String {
|
||
let mut lines = vec![head.join(" | "), vec!["---"; head.len()].join(" | ")];
|
||
for row in rows {
|
||
let cells: Vec<String> = row.iter().map(cell).collect();
|
||
lines.push(cells.join(" | "));
|
||
}
|
||
lines
|
||
.iter()
|
||
.map(|line| format!("| {line} |"))
|
||
.collect::<Vec<_>>()
|
||
.join("\n")
|
||
}
|
||
|
||
/// `code()`: an inline code span, `-` for nothing.
|
||
pub fn code(value: Option<&str>) -> String {
|
||
value.map_or_else(|| "-".to_owned(), |text| format!("`{text}`"))
|
||
}
|
||
|
||
/// What `formatMarkdown` does to a report written by `table()`: every table aligned (cells
|
||
/// padded to the widest of their column, three at least, separator dashes to the same
|
||
/// width), every other line kept. Prettier's other rewrites of authored text are not
|
||
/// reproduced: the draft's reports go through `report_guard::format_report`, which refuses them.
|
||
pub fn format_markdown(raw: &str) -> Result<String, PipelineError> {
|
||
let lines: Vec<&str> = raw.split('\n').collect();
|
||
let mut out: Vec<String> = Vec::with_capacity(lines.len());
|
||
let mut index = 0;
|
||
while index < lines.len() {
|
||
if is_table_start(&lines, index) {
|
||
let end = table_end(&lines, index);
|
||
out.extend(align(&lines[index..end])?);
|
||
index = end;
|
||
} else {
|
||
out.push(lines[index].to_owned());
|
||
index += 1;
|
||
}
|
||
}
|
||
Ok(out.join("\n"))
|
||
}
|
||
|
||
fn is_table_start(lines: &[&str], index: usize) -> bool {
|
||
lines[index].starts_with('|')
|
||
&& lines
|
||
.get(index + 1)
|
||
.is_some_and(|next| split_row(next).iter().all(|c| is_separator(c)))
|
||
}
|
||
|
||
fn is_separator(cell: &str) -> bool {
|
||
cell.len() >= 3 && cell.bytes().all(|b| b == b'-')
|
||
}
|
||
|
||
fn table_end(lines: &[&str], start: usize) -> usize {
|
||
let mut end = start;
|
||
while end < lines.len() && lines[end].starts_with('|') {
|
||
end += 1;
|
||
}
|
||
end
|
||
}
|
||
|
||
/// The cells of a row: split on the pipes no backslash escapes, trimmed as GFM trims them.
|
||
pub(crate) fn split_row(line: &str) -> Vec<String> {
|
||
let inner = line.trim_end_matches([' ', '\t']);
|
||
let inner = inner.strip_prefix('|').unwrap_or(inner);
|
||
let inner = inner.strip_suffix('|').unwrap_or(inner);
|
||
let mut cells = Vec::new();
|
||
let mut current = String::new();
|
||
let mut escaped = false;
|
||
for c in inner.chars() {
|
||
if c == '|' && !escaped {
|
||
cells.push(std::mem::take(&mut current));
|
||
} else {
|
||
current.push(c);
|
||
}
|
||
escaped = c == '\\' && !escaped;
|
||
}
|
||
cells.push(current);
|
||
cells
|
||
.into_iter()
|
||
.map(|cell| cell.trim_matches([' ', '\t']).to_owned())
|
||
.collect()
|
||
}
|
||
|
||
/// The largest aligned table written, in display columns (rows × padded row width): every row
|
||
/// is padded to the widest cell of each column, so one long authored cell multiplies by the row
|
||
/// count (an 80 KB rationale over 14,000 records asked for 1 GB). The equine CHANGELOG's widest
|
||
/// table holds about 0.5 MB.
|
||
pub const MAX_TABLE_COLUMNS_TOTAL: usize = 32 * 1024 * 1024;
|
||
|
||
fn align(lines: &[&str]) -> Result<Vec<String>, PipelineError> {
|
||
let rows: Vec<Vec<String>> = lines.iter().map(|line| split_row(line)).collect();
|
||
let columns = rows.first().map_or(0, Vec::len);
|
||
// A row with another cell count would lose or invent a cell where Prettier keeps it.
|
||
if let Some(index) = rows.iter().position(|row| row.len() != columns) {
|
||
return Err(PipelineError::UnsupportedMarkdown(lines[index].to_owned()));
|
||
}
|
||
let mut widths = vec![3; columns];
|
||
let content = rows.iter().enumerate().filter(|&(index, _)| index != 1);
|
||
for (_, row) in content {
|
||
for (column, text) in row.iter().enumerate().take(columns) {
|
||
widths[column] = widths[column].max(display_width(text)?);
|
||
}
|
||
}
|
||
let row_width = widths.iter().sum::<usize>() + 3 * columns + 1;
|
||
if row_width.saturating_mul(rows.len()) > MAX_TABLE_COLUMNS_TOTAL {
|
||
return Err(PipelineError::Guard(format!(
|
||
"an aligned table of {} rows × {row_width} columns exceeds the {MAX_TABLE_COLUMNS_TOTAL} bound",
|
||
rows.len()
|
||
)));
|
||
}
|
||
let mut out = Vec::with_capacity(rows.len());
|
||
for (index, row) in rows.iter().enumerate() {
|
||
out.push(if index == 1 {
|
||
separator(&widths)
|
||
} else {
|
||
padded(row, &widths)?
|
||
});
|
||
}
|
||
Ok(out)
|
||
}
|
||
|
||
fn separator(widths: &[usize]) -> String {
|
||
let cells: Vec<String> = widths.iter().map(|&width| "-".repeat(width)).collect();
|
||
format!("| {} |", cells.join(" | "))
|
||
}
|
||
|
||
fn padded(row: &[String], widths: &[usize]) -> Result<String, PipelineError> {
|
||
let mut cells = Vec::with_capacity(widths.len());
|
||
for (column, &width) in widths.iter().enumerate() {
|
||
let text = row.get(column).map_or("", String::as_str);
|
||
let pad = width.saturating_sub(display_width(text)?);
|
||
cells.push(format!("{text}{}", " ".repeat(pad)));
|
||
}
|
||
Ok(format!("| {} |", cells.join(" | ")))
|
||
}
|