From 1d9625236ffd104854d1b9b68bf049b06851deff Mon Sep 17 00:00:00 2001 From: bokuweb Date: Thu, 5 Dec 2019 16:00:32 +0900 Subject: [PATCH] feat: Support comment for table --- .../documents/elements/comment_range_end.rs | 5 ++- docx-core/src/documents/elements/table.rs | 2 +- .../src/documents/elements/table_cell.rs | 2 +- docx-core/src/documents/elements/table_row.rs | 2 +- docx-core/src/documents/mod.rs | 25 +++++++++++-- docx-core/tests/lib.rs | 36 +++++++++++++++++++ docx-core/tests/output/[Content_Types].xml | 1 + docx-core/tests/output/docProps/app.xml | 11 +----- docx-core/tests/output/docProps/core.xml | 14 +++----- .../tests/output/word/_rels/document.xml.rels | 6 ++-- docx-core/tests/output/word/document.xml | 22 +++++++++--- docx-core/tests/output/word/styles.xml | 2 +- 12 files changed, 94 insertions(+), 34 deletions(-) diff --git a/docx-core/src/documents/elements/comment_range_end.rs b/docx-core/src/documents/elements/comment_range_end.rs index 56ebaf0..554b2be 100644 --- a/docx-core/src/documents/elements/comment_range_end.rs +++ b/docx-core/src/documents/elements/comment_range_end.rs @@ -41,7 +41,10 @@ mod tests { let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#" + r#" + + + "# diff --git a/docx-core/src/documents/elements/table.rs b/docx-core/src/documents/elements/table.rs index 697fd4a..efd2376 100644 --- a/docx-core/src/documents/elements/table.rs +++ b/docx-core/src/documents/elements/table.rs @@ -4,8 +4,8 @@ use crate::xml_builder::*; #[derive(Debug, Clone)] pub struct Table<'a> { + pub(crate) rows: Vec>, property: TableProperty, - rows: Vec>, grid: Vec, } diff --git a/docx-core/src/documents/elements/table_cell.rs b/docx-core/src/documents/elements/table_cell.rs index 2b98f13..ccf93b0 100644 --- a/docx-core/src/documents/elements/table_cell.rs +++ b/docx-core/src/documents/elements/table_cell.rs @@ -5,8 +5,8 @@ use crate::xml_builder::*; #[derive(Debug, Clone)] pub struct TableCell<'a> { + pub(crate) contents: Vec>, property: TableCellProperty, - contents: Vec>, } #[derive(Debug, Clone)] diff --git a/docx-core/src/documents/elements/table_row.rs b/docx-core/src/documents/elements/table_row.rs index bd30478..87fde2f 100644 --- a/docx-core/src/documents/elements/table_row.rs +++ b/docx-core/src/documents/elements/table_row.rs @@ -4,8 +4,8 @@ use crate::xml_builder::*; #[derive(Debug, Clone)] pub struct TableRow<'a> { + pub(crate) cells: Vec>, property: TableRowProperty, - cells: Vec>, } impl<'a> TableRow<'a> { diff --git a/docx-core/src/documents/mod.rs b/docx-core/src/documents/mod.rs index 9322355..d5a0453 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -95,12 +95,13 @@ impl<'a> Docx<'a> { } } + // Traverse and clone comments from document and add to comments node. fn update_comments(&mut self) { let mut comments: Vec> = vec![]; for child in &self.document.children { match child { - DocumentChild::Paragraph(p) => { - for child in &p.children { + DocumentChild::Paragraph(paragraph) => { + for child in ¶graph.children { match child { ParagraphChild::CommentStart(c) => { comments.push(c.comment()); @@ -109,6 +110,26 @@ impl<'a> Docx<'a> { } } } + DocumentChild::Table(table) => { + for row in &table.rows { + for cell in &row.cells { + for content in &cell.contents { + match content { + TableCellContent::Paragraph(paragraph) => { + for child in ¶graph.children { + match child { + ParagraphChild::CommentStart(c) => { + comments.push(c.comment()); + } + _ => {} + } + } + } + } + } + } + } + } _ => {} } } diff --git a/docx-core/tests/lib.rs b/docx-core/tests/lib.rs index c7ece58..af1ea6f 100644 --- a/docx-core/tests/lib.rs +++ b/docx-core/tests/lib.rs @@ -278,3 +278,39 @@ pub fn comments() -> Result<(), DocxError> { .pack(file)?; Ok(()) } + +#[test] +pub fn comments_to_table() -> Result<(), DocxError> { + let path = std::path::Path::new("./tests/output/comments_table.docx"); + let file = std::fs::File::create(&path).unwrap(); + let table = Table::new(vec![TableRow::new(vec![ + TableCell::new().add_paragraph( + Paragraph::new() + .add_comment_start( + Comment::new("1") + .author("bokuweb") + .date("2019-01-01T00:00:00Z") + .paragraph(Paragraph::new().add_run(Run::new().add_text("Hello"))), + ) + .add_run(Run::new().add_text("Hello")) + .add_comment_end("1"), + ), + TableCell::new().add_paragraph(Paragraph::new().add_run(Run::new().add_text("World"))), + ])]); + Docx::new() + .add_paragraph( + Paragraph::new() + .add_comment_start( + Comment::new("ABCD-1234") + .author("bokuweb") + .date("2019-01-01T00:00:00Z") + .paragraph(Paragraph::new().add_run(Run::new().add_text("Comment!!"))), + ) + .add_run(Run::new().add_text("Hello").highlight("cyan")) + .add_comment_end("ABCD-1234"), + ) + .add_table(table) + .build() + .pack(file)?; + Ok(()) +} diff --git a/docx-core/tests/output/[Content_Types].xml b/docx-core/tests/output/[Content_Types].xml index 20999db..4c07d60 100755 --- a/docx-core/tests/output/[Content_Types].xml +++ b/docx-core/tests/output/[Content_Types].xml @@ -8,4 +8,5 @@ + \ No newline at end of file diff --git a/docx-core/tests/output/docProps/app.xml b/docx-core/tests/output/docProps/app.xml index c577465..2130254 100755 --- a/docx-core/tests/output/docProps/app.xml +++ b/docx-core/tests/output/docProps/app.xml @@ -1,11 +1,2 @@ - - - - - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/docx-core/tests/output/docProps/core.xml b/docx-core/tests/output/docProps/core.xml index 3ecca83..953b4ec 100755 --- a/docx-core/tests/output/docProps/core.xml +++ b/docx-core/tests/output/docProps/core.xml @@ -1,12 +1,8 @@ - - - - - - - - - + 1970-01-01T00:00:00Z + unknown + unknown + 1970-01-01T00:00:00Z + 1 \ No newline at end of file diff --git a/docx-core/tests/output/word/_rels/document.xml.rels b/docx-core/tests/output/word/_rels/document.xml.rels index 2bee028..16a4293 100755 --- a/docx-core/tests/output/word/_rels/document.xml.rels +++ b/docx-core/tests/output/word/_rels/document.xml.rels @@ -1,6 +1,6 @@ - - - + + + \ No newline at end of file diff --git a/docx-core/tests/output/word/document.xml b/docx-core/tests/output/word/document.xml index af02188..3048d55 100755 --- a/docx-core/tests/output/word/document.xml +++ b/docx-core/tests/output/word/document.xml @@ -16,13 +16,25 @@ + + + + + + Hello + + + + + + World! + - Hello - - World - - Foo + + + + diff --git a/docx-core/tests/output/word/styles.xml b/docx-core/tests/output/word/styles.xml index 209712f..9cf26fa 100755 --- a/docx-core/tests/output/word/styles.xml +++ b/docx-core/tests/output/word/styles.xml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file