Skip to content

World's first open-source enterprise world model.

A slide table keeps its rows and columns (#1023) (#1042)

Coderfu62e49ed72f6a
diff --git a/crates/utopia-ingest/src/parsers.rs b/crates/utopia-ingest/src/parsers.rs
index c60e7fa9..1194aac6 100644
--- a/crates/utopia-ingest/src/parsers.rs
+++ b/crates/utopia-ingest/src/parsers.rs
@@ -480,7 +480,8 @@ pub(crate) fn docx_xml_to_text(
Ok(out)
}
-/// PPTX: extract a:t text in the presentation's logical slide order.
+/// PPTX: extract a:t text in the presentation's logical slide order. Tables under a:tbl
+/// become Markdown grids through the same renderer as DOCX and spreadsheet tables.
pub fn pptx(bytes: &[u8]) -> anyhow::Result<String> {
let mut archive =
zip::ZipArchive::new(Cursor::new(bytes.to_vec())).context("Failed to unzip pptx")?;
@@ -510,7 +511,7 @@ pub fn pptx(bytes: &[u8]) -> anyhow::Result<String> {
let mut out = String::new();
for (num, name) in slides {
let xml = pptx_part(&mut archive, &name)?;
- let text = extract_xml_text(&xml, "a:t", "a:p", "a:br")?;
+ let text = pptx_xml_to_text(&xml)?;
if !text.trim().is_empty() {
out.push_str(&format!("\n## Slide {num}\n{text}\n"));
}
@@ -1015,45 +1016,159 @@ fn read_zip_entry(
Ok(content)
}
-/// 从 OOXML 里抽取 `text_tag`(如 a:t)内的文本,遇 `para_tag`(如 a:p)结束换行,
-/// 遇 `break_tag`(如 a:br)也换行。
-fn extract_xml_text(
- xml: &str,
- text_tag: &str,
- para_tag: &str,
- break_tag: &str,
-) -> anyhow::Result<String> {
+/// 从 PPTX 的 slide XML 里读文字。表外的 `a:p`/`a:br` 仍按原来的换行规则;
+/// `a:tbl` 里的每个 `a:tr` 收成一行,每个 `a:tc` 收成一格,格内段落用空格连接。
+fn pptx_xml_to_text(xml: &str) -> anyhow::Result<String> {
+ #[derive(Default)]
+ struct TableCell {
+ text: String,
+ span: usize,
+ horizontal_merge: bool,
+ vertical_merge: bool,
+ }
+
+ fn attr(e: &quick_xml::events::BytesStart<'_>, name: &str) -> Option<String> {
+ e.attributes()
+ .flatten()
+ .find(|a| a.key.as_ref() == name)
+ .map(|a| a.value.to_string())
+ }
+
+ fn truthy(e: &quick_xml::events::BytesStart<'_>, name: &str) -> bool {
+ matches!(attr(e, name).as_deref(), Some("1" | "true" | "on"))
+ }
+
let mut reader = Reader::from_str(xml);
let mut out = String::new();
let mut in_text = false;
+ let mut table_depth = 0usize;
+ let mut rows: Vec<GridRow> = Vec::new();
+ let mut row: GridRow = Vec::new();
+ let mut cell: Option<TableCell> = None;
+ let mut first_is_header = false;
loop {
match reader.read_event() {
- Ok(Event::Start(e)) if e.name().as_ref() == text_tag => in_text = true,
- // 段落里换的行(Shift+Enter)不是新段落,是两个 run 之间的一个 `<a:br>`。丢了它,
- // 标题「Q1」换行「2024」读成「Q12024」,季度和年份一起没了(Word 格子里的同一件事
- // 见 #813)
- Ok(Event::Start(e) | Event::Empty(e)) if e.name().as_ref() == break_tag => {
- out.push('\n');
- }
- Ok(Event::End(e)) => {
- let name = e.name();
- if name.as_ref() == text_tag {
- in_text = false;
- } else if name.as_ref() == para_tag {
- out.push('\n');
+ Ok(Event::Start(e)) => match e.name().as_ref() {
+ "a:tbl" => {
+ table_depth += 1;
+ if table_depth == 1 {
+ rows.clear();
+ row.clear();
+ cell = None;
+ first_is_header = false;
+ }
}
- }
+ "a:tblPr" if table_depth == 1 => {
+ first_is_header = truthy(&e, "firstRow");
+ }
+ "a:tr" if table_depth == 1 => row.clear(),
+ "a:tc" if table_depth == 1 => {
+ cell = Some(TableCell {
+ span: 1,
+ ..TableCell::default()
+ });
+ }
+ "a:tcPr" if table_depth == 1 => {
+ if let Some(c) = cell.as_mut() {
+ if let Some(span) = attr(&e, "gridSpan").and_then(|v| v.parse().ok()) {
+ c.span = span;
+ }
+ c.horizontal_merge = truthy(&e, "hMerge");
+ c.vertical_merge = truthy(&e, "vMerge");
+ }
+ }
+ "a:t" => {
+ if cell.is_some() || table_depth == 0 {
+ in_text = true;
+ }
+ }
+ "a:br" => match cell.as_mut() {
+ Some(c) => c.text.push(' '),
+ None if table_depth == 0 => out.push('\n'),
+ _ => {}
+ },
+ _ => {}
+ },
+ Ok(Event::Empty(e)) => match e.name().as_ref() {
+ "a:tblPr" if table_depth == 1 => {
+ first_is_header = truthy(&e, "firstRow");
+ }
+ "a:tcPr" if table_depth == 1 => {
+ if let Some(c) = cell.as_mut() {
+ if let Some(span) = attr(&e, "gridSpan").and_then(|v| v.parse().ok()) {
+ c.span = span;
+ }
+ c.horizontal_merge = truthy(&e, "hMerge");
+ c.vertical_merge = truthy(&e, "vMerge");
+ }
+ }
+ "a:br" => match cell.as_mut() {
+ Some(c) => c.text.push(' '),
+ None if table_depth == 0 => out.push('\n'),
+ _ => {}
+ },
+ "a:tab" => {
+ if let Some(c) = cell.as_mut() {
+ c.text.push(' ');
+ }
+ }
+ _ => {}
+ },
+ Ok(Event::End(e)) => match e.name().as_ref() {
+ "a:t" => in_text = false,
+ "a:p" => match cell.as_mut() {
+ Some(c) => c.text.push(' '),
+ None if table_depth == 0 => out.push('\n'),
+ _ => {}
+ },
+ "a:tc" if table_depth == 1 => {
+ if let Some(mut c) = cell.take() {
+ if !c.horizontal_merge {
+ if c.vertical_merge {
+ c.text.clear();
+ }
+ row.push((c.text, c.span.max(1), 0));
+ }
+ }
+ }
+ "a:tr" if table_depth == 1 => rows.push(std::mem::take(&mut row)),
+ "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) {
+ out.push('\n');
+ out.push_str(&md);
+ out.push_str("\n\n");
+ }
+ rows.clear();
+ row.clear();
+ cell = None;
+ first_is_header = false;
+ }
+ }
+ _ => {}
+ },
Ok(Event::Text(t)) if in_text => {
- out.push_str(&t.xml_content(quick_xml::XmlVersion::Implicit1_0));
+ let text = t.xml_content(quick_xml::XmlVersion::Implicit1_0);
+ match cell.as_mut() {
+ Some(c) => c.text.push_str(&text),
+ None => out.push_str(&text),
+ }
}
Ok(Event::CData(t)) if in_text => {
- out.push_str(&t.xml_content(quick_xml::XmlVersion::Implicit1_0));
+ let text = t.xml_content(quick_xml::XmlVersion::Implicit1_0);
+ match cell.as_mut() {
+ Some(c) => c.text.push_str(&text),
+ None => out.push_str(&text),
+ }
}
Ok(Event::GeneralRef(e)) if in_text => {
let reference = format!("&{};", e.into_inner());
- match quick_xml::escape::unescape(&reference) {
- Ok(s) => out.push_str(&s),
- Err(_) => out.push_str(&reference),
+ let text = quick_xml::escape::unescape(&reference)
+ .unwrap_or(std::borrow::Cow::Borrowed(&reference));
+ match cell.as_mut() {
+ Some(c) => c.text.push_str(&text),
+ None => out.push_str(&text),
}
}
Ok(Event::Eof) => break,
diff --git a/crates/utopia-ingest/tests/pptx_tables.rs b/crates/utopia-ingest/tests/pptx_tables.rs
new file mode 100644
index 00000000..2f7eadbb
--- /dev/null
+++ b/crates/utopia-ingest/tests/pptx_tables.rs
@@ -0,0 +1,217 @@
+use std::io::{Cursor, Write};
+
+fn package(parts: &[(String, String)]) -> Vec<u8> {
+ let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
+ for (name, xml) in parts {
+ zip.start_file(name, zip::write::SimpleFileOptions::default())
+ .unwrap();
+ zip.write_all(xml.as_bytes()).unwrap();
+ }
+ zip.finish().unwrap().into_inner()
+}
+
+fn deck(body: &str) -> Vec<u8> {
+ package(&[
+ ("[Content_Types].xml".into(), r#"<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Override PartName="/ppt/presentation.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"/><Override PartName="/ppt/slides/slide1.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml"/></Types>"#.into()),
+ ("_rels/.rels".into(), r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="ppt/presentation.xml"/></Relationships>"#.into()),
+ ("ppt/presentation.xml".into(), r#"<p:presentation xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:sldIdLst><p:sldId id="256" r:id="rId2"/></p:sldIdLst></p:presentation>"#.into()),
+ ("ppt/_rels/presentation.xml.rels".into(), r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide1.xml"/></Relationships>"#.into()),
+ ("ppt/slides/slide1.xml".into(), format!(r#"<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><p:cSld><p:spTree>{body}</p:spTree></p:cSld></p:sld>"#)),
+ ])
+}
+
+fn parse_slide(body: &str) -> String {
+ utopia_ingest::parse("deck.pptx", &deck(body)).unwrap().text
+}
+
+fn text_box(text: &str) -> String {
+ format!(
+ r#"<p:sp><p:txBody><a:bodyPr/><a:lstStyle/><a:p><a:r><a:t>{text}</a:t></a:r></a:p></p:txBody></p:sp>"#
+ )
+}
+
+fn cell(text: &str) -> String {
+ cell_with_props(text, "")
+}
+
+fn cell_with_props(text: &str, props: &str) -> String {
+ format!(
+ r#"<a:tc><a:txBody><a:bodyPr/><a:lstStyle/><a:p><a:r><a:t>{text}</a:t></a:r></a:p></a:txBody><a:tcPr {props}/></a:tc>"#
+ )
+}
+
+fn cell_with_paragraphs(paragraphs: &[&str]) -> String {
+ let paragraphs = paragraphs
+ .iter()
+ .map(|text| format!(r#"<a:p><a:r><a:t>{text}</a:t></a:r></a:p>"#))
+ .collect::<String>();
+ format!(r#"<a:tc><a:txBody><a:bodyPr/><a:lstStyle/>{paragraphs}</a:txBody><a:tcPr/></a:tc>"#)
+}
+
+fn empty_cell(props: &str) -> String {
+ format!(r#"<a:tc><a:txBody><a:bodyPr/><a:lstStyle/><a:p/></a:txBody><a:tcPr {props}/></a:tc>"#)
+}
+
+fn row(cells: &[String]) -> String {
+ format!("<a:tr>{}</a:tr>", cells.concat())
+}
+
+fn table(first_row: bool, rows: &[String]) -> String {
+ let first_row = if first_row { r#" firstRow="1""# } else { "" };
+ format!(
+ r#"<p:graphicFrame><a:graphic><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/table"><a:tbl><a:tblPr{first_row}/><a:tblGrid/><rows/></a:tbl></a:graphicData></a:graphic></p:graphicFrame>"#
+ )
+ .replace("<rows/>", &rows.concat())
+}
+
+#[test]
+fn a_three_by_four_slide_table_keeps_its_header_and_rows() {
+ let text = parse_slide(&table(
+ true,
+ &[
+ row(&[cell("Quarter"), cell("Revenue"), cell("YoY")]),
+ row(&[cell("Q1"), cell("1,200"), cell("+12%")]),
+ row(&[cell("Q2"), cell("1,350"), cell("+9%")]),
+ row(&[cell("Q3"), cell("1,410"), cell("+7%")]),
+ ],
+ ));
+
+ assert!(
+ text.contains(
+ "| Quarter | Revenue | YoY |\n| --- | --- | --- |\n| Q1 | 1,200 | +12% |\n| Q2 | 1,350 | +9% |\n| Q3 | 1,410 | +7% |"
+ ),
+ "{text}"
+ );
+}
+
+#[test]
+fn a_title_and_text_around_a_slide_table_keep_their_lines() {
+ let text = parse_slide(&format!(
+ "{}{}{}",
+ text_box("2024 年各季度营收"),
+ table(
+ true,
+ &[
+ row(&[cell("Quarter"), cell("Revenue")]),
+ row(&[cell("Q1"), cell("1,200")]),
+ ],
+ ),
+ text_box("来源:财务部"),
+ ));
+
+ let title = text.find("2024 年各季度营收").expect(&text);
+ let table_start = text.find("| Quarter | Revenue |").expect(&text);
+ let source = text.find("来源:财务部").expect(&text);
+ assert!(title < table_start && table_start < source, "{text}");
+}
+
+#[test]
+fn grid_span_and_horizontal_merge_keep_the_grid_shape() {
+ let text = parse_slide(&table(
+ true,
+ &[
+ row(&[
+ cell_with_props("Summary", r#"gridSpan="2""#),
+ cell_with_props("COVERED", r#"hMerge="1""#),
+ cell("Total"),
+ ]),
+ row(&[cell("Q1"), cell("1,200"), cell("1,350")]),
+ ],
+ ));
+
+ assert!(
+ text.contains("| | Summary | Total |\n| --- | --- | --- |\n| Q1 | 1,200 | 1,350 |"),
+ "{text}"
+ );
+ assert!(!text.contains("COVERED"), "{text}");
+}
+
+#[test]
+fn a_vertical_merge_continuation_stays_an_empty_cell() {
+ let text = parse_slide(&table(
+ true,
+ &[
+ row(&[cell("Quarter"), cell("Revenue"), cell("YoY")]),
+ row(&[cell("Q1"), cell("1,200"), cell("+12%")]),
+ row(&[cell("Q2"), cell("1,350"), empty_cell(r#"vMerge="1""#)]),
+ row(&[cell("Q3"), cell("1,410"), cell("+7%")]),
+ ],
+ ));
+
+ assert!(
+ text.contains(
+ "| Quarter | Revenue | YoY |\n| --- | --- | --- |\n| Q1 | 1,200 | +12% |\n| Q2 | 1,350 | |\n| Q3 | 1,410 | +7% |"
+ ),
+ "{text}"
+ );
+}
+
+#[test]
+fn first_row_marks_the_first_row_as_a_header() {
+ let text = parse_slide(&table(
+ true,
+ &[
+ row(&[cell("2024"), cell("2025")]),
+ row(&[cell("Revenue"), cell("1,200")]),
+ ],
+ ));
+
+ assert!(
+ text.contains("| 2024 | 2025 |\n| --- | --- |\n| Revenue | 1,200 |"),
+ "{text}"
+ );
+}
+
+#[test]
+fn without_first_row_rows_are_classified_by_their_content() {
+ let text = parse_slide(&table(
+ false,
+ &[
+ row(&[cell("2024"), cell("2025")]),
+ row(&[cell("Revenue"), cell("1,200")]),
+ ],
+ ));
+
+ assert!(
+ text.contains("| | |\n| --- | --- |\n| 2024 | 2025 |\n| Revenue | 1,200 |"),
+ "{text}"
+ );
+}
+
+#[test]
+fn a_slide_can_hold_two_tables_without_joining_them() {
+ let text = parse_slide(&format!(
+ "{}{}{}",
+ table(
+ true,
+ &[row(&[cell("A"), cell("B")]), row(&[cell("1"), cell("2")]),],
+ ),
+ text_box("Between"),
+ table(
+ true,
+ &[row(&[cell("C"), cell("D")]), row(&[cell("3"), cell("4")]),],
+ ),
+ ));
+
+ assert_eq!(text.matches("| --- | --- |").count(), 2, "{text}");
+ let first = text.find("| A | B |").expect(&text);
+ let between = text.find("Between").expect(&text);
+ let second = text.find("| C | D |").expect(&text);
+ assert!(first < between && between < second, "{text}");
+}
+
+#[test]
+fn paragraphs_in_a_cell_are_joined_by_spaces() {
+ let text = parse_slide(&table(
+ true,
+ &[
+ row(&[cell("Region"), cell("Sales")]),
+ row(&[cell_with_paragraphs(&["North", "America"]), cell("1,200")]),
+ ],
+ ));
+
+ assert!(
+ text.contains("| Region | Sales |\n| --- | --- |\n| North America | 1,200 |"),
+ "{text}"
+ );
+}