feat: Add attr to paragraph

main
bokuweb 2019-11-13 17:52:02 +09:00
parent 41432fb748
commit 32c9bcee08
6 changed files with 66 additions and 4 deletions

View File

@ -7,6 +7,7 @@ use crate::xml_builder::*;
pub struct Paragraph { pub struct Paragraph {
runs: Vec<Run>, runs: Vec<Run>,
property: ParagraphProperty, property: ParagraphProperty,
attrs: Vec<(String, String)>,
} }
impl Default for Paragraph { impl Default for Paragraph {
@ -14,6 +15,7 @@ impl Default for Paragraph {
Self { Self {
runs: Vec::new(), runs: Vec::new(),
property: ParagraphProperty::new(), property: ParagraphProperty::new(),
attrs: Vec::new(),
} }
} }
} }
@ -28,6 +30,11 @@ impl Paragraph {
self self
} }
pub fn add_attr(mut self, key: impl Into<String>, val: impl Into<String>) -> Paragraph {
self.attrs.push((key.into(), val.into()));
self
}
pub fn align(mut self, alignment_type: AlignmentType) -> Paragraph { pub fn align(mut self, alignment_type: AlignmentType) -> Paragraph {
self.property = self.property.align(alignment_type); self.property = self.property.align(alignment_type);
self self
@ -52,7 +59,7 @@ impl Paragraph {
impl BuildXML for Paragraph { impl BuildXML for Paragraph {
fn build(&self) -> Vec<u8> { fn build(&self) -> Vec<u8> {
XMLBuilder::new() XMLBuilder::new()
.open_paragraph() .open_paragraph(&self.attrs)
.add_child(&self.property) .add_child(&self.property)
.add_children(&self.runs) .add_children(&self.runs)
.close() .close()
@ -90,4 +97,16 @@ mod tests {
r#"<w:p><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:r><w:rPr><w:sz w:val="60" /><w:szCs w:val="60" /></w:rPr><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"# r#"<w:p><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:r><w:rPr><w:sz w:val="60" /><w:szCs w:val="60" /></w:rPr><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"#
); );
} }
#[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#"<w:p customId="abcd-1234-567890"><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"#
);
}
} }

View File

@ -29,7 +29,8 @@ impl XMLBuilder {
// i.e. <w:qFormat ... > // i.e. <w:qFormat ... >
closed_el!(q_format, "w:qFormat"); closed_el!(q_format, "w:qFormat");
// i.e. <w:p ... > // i.e. <w:p ... >
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_paragraph_property, "w:pPr");
opened_el!(open_doc_defaults, "w:docDefaults"); opened_el!(open_doc_defaults, "w:docDefaults");
// i.e. <w:name ... > // i.e. <w:name ... >

View File

@ -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 { macro_rules! closed_el_with_child {
($name: ident, $el_name: expr) => { ($name: ident, $el_name: expr) => {
pub(crate) fn $name(mut self, child: &str) -> Self { pub(crate) fn $name(mut self, child: &str) -> Self {

View File

@ -193,3 +193,18 @@ pub fn tab_and_break() -> Result<(), DocxError> {
.pack(file)?; .pack(file)?;
Ok(()) 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(())
}

View File

@ -1,3 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships> </Relationships>

View File

@ -1,3 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
</Relationships> </Relationships>