Skip to content

World's first open-source enterprise world model.

A single-column text table on a slide keeps its cells (#1044)

Daniel0582f2f52a3b
diff --git a/crates/utopia-ingest/src/parsers.rs b/crates/utopia-ingest/src/parsers.rs
index 8070665b..5da4462e 100644
--- a/crates/utopia-ingest/src/parsers.rs
+++ b/crates/utopia-ingest/src/parsers.rs
@@ -137,8 +137,8 @@ fn pdf_with_poppler(bytes: &[u8]) -> anyhow::Result<String> {
}
/// docx:解压 word/document.xml。正文 w:t 取字;不在表格里的 w:p 是一段,段末留空行;
-/// 段内的 w:br/w:cr 仍是单换行。表格(w:tbl)收成网格交给 `table::render_grid`,和 HTML 表
-/// 走同一套渲染:w:gridSpan 跨列,上下合并(w:vMerge)的续格留空,格内段落的左缩进 w:ind
+/// 段内的 w:br/w:cr 仍是单换行。表格(w:tbl)收成网格交给 `table::grid_or_lines`,和 HTML 表
+/// 走同一套渲染(不成表的网格按行写出字来):w:gridSpan 跨列,上下合并(w:vMerge)的续格留空,格内段落的左缩进 w:ind
/// 当内边距(小节行靠它折进标签)。套在格子里的表按格子文字处理。标题(有大纲级别的段落,
/// 见 [`docx_heading_styles`])写成 Markdown 标题。
pub fn docx(bytes: &[u8]) -> anyhow::Result<String> {
@@ -436,7 +436,7 @@ pub(crate) fn docx_xml_to_text(
"w:tbl" => {
depth = depth.saturating_sub(1);
if depth == 0 {
- if let Some(md) = crate::table::render_grid(&rows, false) {
+ if let Some(md) = crate::table::grid_or_lines(&rows, false) {
if !out.is_empty() {
let end = out.trim_end().len();
out.truncate(end);
@@ -1166,7 +1166,7 @@ fn pptx_xml_to_text(xml: &str) -> anyhow::Result<String> {
"a:tbl" => {
table_depth = table_depth.saturating_sub(1);
if table_depth == 0 {
- if let Some(md) = crate::table::render_grid(&rows, first_is_header) {
+ if let Some(md) = crate::table::grid_or_lines(&rows, first_is_header) {
out.push('\n');
out.push_str(&md);
out.push_str("\n\n");
diff --git a/crates/utopia-ingest/src/table.rs b/crates/utopia-ingest/src/table.rs
index 3640241f..2ab1b9cd 100644
--- a/crates/utopia-ingest/src/table.rs
+++ b/crates/utopia-ingest/src/table.rs
@@ -341,6 +341,29 @@ pub(crate) fn render_grid(
render_rows(rows, &[], forced).map(|(md, _)| md)
}
+/// docx 和 pptx 用的入口:[`render_grid`] 不认作表的网格也不能丢字。没有列头也没有数据行的
+/// 网格(单列的文字、每排只有一格有字、一格跨整排的告示)按行写出来:一排一段,有字的格用
+/// ` | ` 连起来。HTML 的表有 htmd 接着,电子表格和 csv 各有退路,所以这一步不放在渲染器里。
+pub(crate) fn grid_or_lines(
+ rows: &[Vec<(String, usize, u32)>],
+ first_is_header: bool,
+) -> Option<String> {
+ render_grid(rows, first_is_header).or_else(|| {
+ let lines: Vec<String> = rows
+ .iter()
+ .filter_map(|row| {
+ let cells: Vec<String> = row
+ .iter()
+ .map(|(text, _, _)| clean(text))
+ .filter(|text| !text.is_empty())
+ .collect();
+ (!cells.is_empty()).then(|| cells.join(" | "))
+ })
+ .collect();
+ (!lines.is_empty()).then(|| lines.join("\n\n"))
+ })
+}
+
/// 电子表格的 merged cells 可以给出不止一行的表头。调用方已经把行投影成网格,
/// 这里只负责把明确的表头区间按表头处理。
pub(crate) fn render_grid_with_headers(
@@ -632,6 +655,57 @@ mod tests {
tables.join("\n=====\n")
}
+ #[test]
+ fn a_grid_that_is_not_a_table_keeps_its_words_as_lines() {
+ let cell = |text: &str, span: usize| (text.to_string(), span, 0);
+ // 单列的文字,中间夹一排空行
+ let one_column = vec![
+ vec![cell("Action", 1)],
+ vec![cell("", 1)],
+ vec![cell("Approve contract", 1)],
+ vec![cell("Renew license", 1)],
+ ];
+ for first_is_header in [false, true] {
+ assert_eq!(render_grid(&one_column, first_is_header), None);
+ assert_eq!(
+ grid_or_lines(&one_column, first_is_header).as_deref(),
+ Some("Action\n\nApprove contract\n\nRenew license")
+ );
+ }
+ // 两列,只有左边一列有字;一格跨整排的告示
+ let left_only = vec![
+ vec![cell("Action", 1), cell("", 1)],
+ vec![cell("Approve contract", 1), cell("", 1)],
+ ];
+ assert_eq!(
+ grid_or_lines(&left_only, false).as_deref(),
+ Some("Action\n\nApprove contract")
+ );
+ assert_eq!(
+ grid_or_lines(&[vec![cell("Notice for the board", 2)]], false).as_deref(),
+ Some("Notice for the board")
+ );
+ // 一个字都没有的网格什么都不写;真正的表照旧是表
+ assert_eq!(grid_or_lines(&[vec![cell("", 1)]], false), None);
+ let table = vec![
+ vec![cell("Item", 1), cell("2025", 1)],
+ vec![cell("Revenue", 1), cell("10", 1)],
+ ];
+ assert_eq!(grid_or_lines(&table, true), render_grid(&table, true));
+ assert!(grid_or_lines(&table, true)
+ .unwrap()
+ .starts_with("| Item | 2025 |"));
+ }
+
+ /// 单列的 HTML 表多半是排版用的:留给 htmd,格子里的标题、列表和链接才留得住
+ #[test]
+ fn a_single_column_html_table_is_left_to_the_page_converter() {
+ let html = "<table><tr><td><h2>To our shareholders</h2><p>Revenue grew.</p></td></tr><tr><td><p>We opened three offices.</p></td></tr></table>";
+ let (out, tables) = lift_tables(html);
+ assert!(tables.is_empty(), "{tables:?}");
+ assert_eq!(out, html);
+ }
+
#[test]
fn a_filing_table_gets_its_real_headers_and_its_section_path() {
let html = r#"<table>
diff --git a/crates/utopia-ingest/tests/docx_breaks.rs b/crates/utopia-ingest/tests/docx_breaks.rs
index ba97fed6..dc33dd1a 100644
--- a/crates/utopia-ingest/tests/docx_breaks.rs
+++ b/crates/utopia-ingest/tests/docx_breaks.rs
@@ -142,3 +142,27 @@ fn a_non_breaking_hyphen_keeps_the_numbers_on_either_side_apart() {
)));
assert!(text.contains("| Sample | 2024-01-15 |"), "{text}");
}
+
+/// A table with one column of words, or with words in only one cell of each row, is not a
+/// table with headers and data. Its words are still read, one row to a paragraph.
+#[test]
+fn a_word_table_that_is_not_a_grid_of_data_keeps_its_words() {
+ let cell = |text: &str| format!("<w:tc><w:p><w:r><w:t>{text}</w:t></w:r></w:p></w:tc>");
+ let one_column: String = ["Action", "Approve contract", "Renew license"]
+ .iter()
+ .map(|text| format!("<w:tr>{}</w:tr>", cell(text)))
+ .collect();
+ let text = read(&format!(
+ "<w:p><w:r><w:t>Before.</w:t></w:r></w:p><w:tbl>{one_column}</w:tbl><w:p><w:r><w:t>After.</w:t></w:r></w:p>"
+ ));
+ assert_eq!(
+ text.trim(),
+ "Before.\n\nAction\n\nApprove contract\n\nRenew license\n\nAfter."
+ );
+ let left_only: String = ["Action", "Approve contract"]
+ .iter()
+ .map(|text| format!("<w:tr>{}{}</w:tr>", cell(text), cell("")))
+ .collect();
+ let text = read(&format!("<w:tbl>{left_only}</w:tbl>"));
+ assert_eq!(text.trim(), "Action\n\nApprove contract");
+}
diff --git a/crates/utopia-ingest/tests/pptx_tables.rs b/crates/utopia-ingest/tests/pptx_tables.rs
index 2f7eadbb..a4c81662 100644
--- a/crates/utopia-ingest/tests/pptx_tables.rs
+++ b/crates/utopia-ingest/tests/pptx_tables.rs
@@ -64,6 +64,85 @@ fn table(first_row: bool, rows: &[String]) -> String {
.replace("<rows/>", &rows.concat())
}
+fn single_column_text_table(first_row: bool) -> String {
+ let first_row = if first_row { r#" firstRow="1""# } else { "" };
+ let rows = ["Action", "Approve contract", "Renew license"]
+ .iter()
+ .map(|text| format!(r#"<a:tr h="370840">{}</a:tr>"#, cell(text)))
+ .collect::<String>();
+ format!(
+ r#"<p:graphicFrame>
+<p:nvGraphicFramePr><p:cNvPr id="3" name="Actions"/><p:cNvGraphicFramePr/><p:nvPr/></p:nvGraphicFramePr>
+<p:xfrm><a:off x="914400" y="914400"/><a:ext cx="3657600" cy="1112520"/></p:xfrm>
+<a:graphic><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/table">
+<a:tbl><a:tblPr{first_row}/><a:tblGrid><a:gridCol w="3657600"/></a:tblGrid>{rows}</a:tbl>
+</a:graphicData></a:graphic></p:graphicFrame>"#
+ )
+}
+
+#[test]
+fn a_single_column_text_table_keeps_every_cell_beside_a_slide_title() {
+ let title = r#"<p:sp>
+<p:nvSpPr><p:cNvPr id="2" name="Title"/><p:cNvSpPr/><p:nvPr><p:ph type="title"/></p:nvPr></p:nvSpPr>
+<p:spPr/><p:txBody><a:bodyPr/><a:lstStyle/><a:p><a:r><a:t>Agenda</a:t></a:r></a:p></p:txBody>
+</p:sp>"#;
+ for first_row in [false, true] {
+ let body = format!("{title}{}", single_column_text_table(first_row));
+ let parsed = utopia_ingest::parse("deck.pptx", &deck(&body)).unwrap();
+ assert!(parsed.text.contains("Agenda"), "{}", parsed.text);
+ for cell in ["Action", "Approve contract", "Renew license"] {
+ assert!(
+ parsed.text.contains(cell),
+ "missing {cell:?} (firstRow={first_row}): text={:?}, warnings={:?}",
+ parsed.text,
+ parsed.warnings
+ );
+ }
+ }
+}
+
+#[test]
+fn a_slide_with_only_a_single_column_text_table_keeps_every_cell() {
+ for first_row in [false, true] {
+ let parsed = utopia_ingest::parse("deck.pptx", &deck(&single_column_text_table(first_row)))
+ .expect("a valid slide with text cells must be readable");
+ for cell in ["Action", "Approve contract", "Renew license"] {
+ assert!(
+ parsed.text.contains(cell),
+ "missing {cell:?}: {}",
+ parsed.text
+ );
+ }
+ }
+}
+
+/// Two columns where only one cell in each row has words, and a notice in one cell
+/// that spans the row: neither is a table with headers and data, and both keep their words.
+#[test]
+fn a_slide_table_that_is_not_a_grid_of_data_keeps_its_words() {
+ for first_row in [false, true] {
+ let text = parse_slide(&table(
+ first_row,
+ &[
+ row(&[cell("Action"), empty_cell("")]),
+ row(&[cell("Approve contract"), empty_cell("")]),
+ row(&[empty_cell(""), cell("Renew license")]),
+ ],
+ ));
+ for words in ["Action", "Approve contract", "Renew license"] {
+ assert!(text.contains(words), "missing {words:?}: {text}");
+ }
+ let text = parse_slide(&table(
+ first_row,
+ &[row(&[
+ cell_with_props("Notice for the board", r#"gridSpan="2""#),
+ empty_cell(r#"hMerge="1""#),
+ ])],
+ ));
+ assert!(text.contains("Notice for the board"), "{text}");
+ }
+}
+
#[test]
fn a_three_by_four_slide_table_keeps_its_header_and_rows() {
let text = parse_slide(&table(