From 32c9bcee086ea5862626bdb294032ea54355032f Mon Sep 17 00:00:00 2001 From: bokuweb Date: Wed, 13 Nov 2019 17:52:02 +0900 Subject: [PATCH] feat: Add attr to paragraph --- docx-core/src/documents/elements/paragraph.rs | 21 ++++++++++++++++++- docx-core/src/xml_builder/elements.rs | 3 ++- docx-core/src/xml_builder/macros.rs | 21 +++++++++++++++++++ docx-core/tests/lib.rs | 15 +++++++++++++ fixtures/tab_and_break/_rels/.rels | 5 ++++- .../word/_rels/document.xml.rels | 5 ++++- 6 files changed, 66 insertions(+), 4 deletions(-) diff --git a/docx-core/src/documents/elements/paragraph.rs b/docx-core/src/documents/elements/paragraph.rs index 4006cc8..5589ee5 100644 --- a/docx-core/src/documents/elements/paragraph.rs +++ b/docx-core/src/documents/elements/paragraph.rs @@ -7,6 +7,7 @@ use crate::xml_builder::*; pub struct Paragraph { runs: Vec, property: ParagraphProperty, + attrs: Vec<(String, String)>, } impl Default for Paragraph { @@ -14,6 +15,7 @@ impl Default for Paragraph { Self { runs: Vec::new(), property: ParagraphProperty::new(), + attrs: Vec::new(), } } } @@ -28,6 +30,11 @@ impl Paragraph { self } + pub fn add_attr(mut self, key: impl Into, val: impl Into) -> Paragraph { + self.attrs.push((key.into(), val.into())); + self + } + pub fn align(mut self, alignment_type: AlignmentType) -> Paragraph { self.property = self.property.align(alignment_type); self @@ -52,7 +59,7 @@ impl Paragraph { impl BuildXML for Paragraph { fn build(&self) -> Vec { XMLBuilder::new() - .open_paragraph() + .open_paragraph(&self.attrs) .add_child(&self.property) .add_children(&self.runs) .close() @@ -90,4 +97,16 @@ mod tests { r#"Hello"# ); } + + #[test] + fn test_custom_attr() { + let b = Paragraph::new() + .add_run(Run::new().add_text("Hello")) + .add_attr("customId", "abcd-1234-567890") + .build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#"Hello"# + ); + } } diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index 03abcef..af3f82d 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -29,7 +29,8 @@ impl XMLBuilder { // i.e. closed_el!(q_format, "w:qFormat"); // i.e. - opened_el!(open_paragraph, "w:p"); + // opened_el!(open_paragraph, "w:p"); + opened_el_with_attrs!(open_paragraph, "w:p"); opened_el!(open_paragraph_property, "w:pPr"); opened_el!(open_doc_defaults, "w:docDefaults"); // i.e. diff --git a/docx-core/src/xml_builder/macros.rs b/docx-core/src/xml_builder/macros.rs index e1b6bfe..56a582b 100644 --- a/docx-core/src/xml_builder/macros.rs +++ b/docx-core/src/xml_builder/macros.rs @@ -49,6 +49,27 @@ macro_rules! opened_el { }; } +macro_rules! opened_el_with_attrs { + ($name: ident, $el_name: expr) => { + pub(crate) fn $name(mut self, attrs: &[(String, String)]) -> Self { + let mut e = XmlEvent::start_element($el_name); + #[allow(unused)] + let mut key: &str = ""; + #[allow(unused)] + let mut val: &str = ""; + for attr in attrs { + key = &attr.0; + val = &attr.1; + e = e.attr(key, val); + } + self.writer + .write(e) + .expect("should write to buf"); + self + } + }; +} + macro_rules! closed_el_with_child { ($name: ident, $el_name: expr) => { pub(crate) fn $name(mut self, child: &str) -> Self { diff --git a/docx-core/tests/lib.rs b/docx-core/tests/lib.rs index 3156267..0964f08 100644 --- a/docx-core/tests/lib.rs +++ b/docx-core/tests/lib.rs @@ -193,3 +193,18 @@ pub fn tab_and_break() -> Result<(), DocxError> { .pack(file)?; Ok(()) } + +#[test] +pub fn custom_attr_paragraph() -> Result<(), DocxError> { + let path = std::path::Path::new("./tests/output/custom_attr_paragraph.docx"); + let file = std::fs::File::create(&path).unwrap(); + Docx::new() + .add_paragraph( + Paragraph::new() + .add_run(Run::new().add_text("Hello")) + .add_custom_attr("w:customId", "1234-5678"), + ) + .build() + .pack(file)?; + Ok(()) +} diff --git a/fixtures/tab_and_break/_rels/.rels b/fixtures/tab_and_break/_rels/.rels index f0b72e7..e13b32e 100644 --- a/fixtures/tab_and_break/_rels/.rels +++ b/fixtures/tab_and_break/_rels/.rels @@ -1,3 +1,6 @@ - + + + + \ No newline at end of file diff --git a/fixtures/tab_and_break/word/_rels/document.xml.rels b/fixtures/tab_and_break/word/_rels/document.xml.rels index 8a2db8a..6ac87fd 100644 --- a/fixtures/tab_and_break/word/_rels/document.xml.rels +++ b/fixtures/tab_and_break/word/_rels/document.xml.rels @@ -1,3 +1,6 @@ - + + + + \ No newline at end of file