feat: Add document relation ship
parent
32c9bcee08
commit
2bfdf9bc2b
|
@ -0,0 +1,66 @@
|
||||||
|
use crate::documents::BuildXML;
|
||||||
|
use crate::xml_builder::*;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct DocumentRels {}
|
||||||
|
|
||||||
|
impl DocumentRels {
|
||||||
|
pub fn new() -> DocumentRels {
|
||||||
|
DocumentRels {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BuildXML for DocumentRels {
|
||||||
|
fn build(&self) -> Vec<u8> {
|
||||||
|
let b = XMLBuilder::new();
|
||||||
|
b.declaration(None)
|
||||||
|
.open_relationships("http://schemas.openxmlformats.org/package/2006/relationships")
|
||||||
|
.relationship(
|
||||||
|
"rId1",
|
||||||
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
|
||||||
|
"styles.xml",
|
||||||
|
)
|
||||||
|
.relationship(
|
||||||
|
"rId2",
|
||||||
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable",
|
||||||
|
"fontTable.xml",
|
||||||
|
)
|
||||||
|
.relationship(
|
||||||
|
"rId3",
|
||||||
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings",
|
||||||
|
"settings.xml",
|
||||||
|
)
|
||||||
|
.relationship(
|
||||||
|
"rId4",
|
||||||
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/tag",
|
||||||
|
"tag.xml",
|
||||||
|
)
|
||||||
|
.close()
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
#[cfg(test)]
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
use std::str;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_build() {
|
||||||
|
let c = DocumentRels::new();
|
||||||
|
let b = c.build();
|
||||||
|
assert_eq!(
|
||||||
|
str::from_utf8(&b).unwrap(),
|
||||||
|
r#"<?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" />
|
||||||
|
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/tag" Target="tag.xml" />
|
||||||
|
</Relationships>"#
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ mod build_xml;
|
||||||
mod content_types;
|
mod content_types;
|
||||||
mod doc_props;
|
mod doc_props;
|
||||||
mod document;
|
mod document;
|
||||||
|
mod document_rels;
|
||||||
mod elements;
|
mod elements;
|
||||||
mod rels;
|
mod rels;
|
||||||
mod styles;
|
mod styles;
|
||||||
|
@ -12,6 +13,7 @@ pub(crate) use build_xml::*;
|
||||||
pub use content_types::*;
|
pub use content_types::*;
|
||||||
pub use doc_props::*;
|
pub use doc_props::*;
|
||||||
pub use document::*;
|
pub use document::*;
|
||||||
|
pub use document_rels::*;
|
||||||
pub use elements::*;
|
pub use elements::*;
|
||||||
pub use rels::*;
|
pub use rels::*;
|
||||||
pub use styles::*;
|
pub use styles::*;
|
||||||
|
@ -21,6 +23,7 @@ pub use xml_docx::*;
|
||||||
pub struct Docx {
|
pub struct Docx {
|
||||||
content_type: ContentTypes,
|
content_type: ContentTypes,
|
||||||
rels: Rels,
|
rels: Rels,
|
||||||
|
document_rels: DocumentRels,
|
||||||
doc_props: DocProps,
|
doc_props: DocProps,
|
||||||
styles: Styles,
|
styles: Styles,
|
||||||
document: Document,
|
document: Document,
|
||||||
|
@ -33,12 +36,14 @@ impl Default for Docx {
|
||||||
let doc_props = DocProps::new(None, None /* TODO: */);
|
let doc_props = DocProps::new(None, None /* TODO: */);
|
||||||
let styles = Styles::new();
|
let styles = Styles::new();
|
||||||
let document = Document::new();
|
let document = Document::new();
|
||||||
|
let document_rels = DocumentRels::new();
|
||||||
Docx {
|
Docx {
|
||||||
content_type,
|
content_type,
|
||||||
rels,
|
rels,
|
||||||
doc_props,
|
doc_props,
|
||||||
styles,
|
styles,
|
||||||
document,
|
document,
|
||||||
|
document_rels,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,6 +70,7 @@ impl Docx {
|
||||||
doc_props: self.doc_props.build(),
|
doc_props: self.doc_props.build(),
|
||||||
styles: self.styles.build(),
|
styles: self.styles.build(),
|
||||||
document: self.document.build(),
|
document: self.document.build(),
|
||||||
|
document_rels: self.document_rels.build(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ pub struct XMLDocx {
|
||||||
pub doc_props: XMLDocProps,
|
pub doc_props: XMLDocProps,
|
||||||
pub styles: Vec<u8>,
|
pub styles: Vec<u8>,
|
||||||
pub document: Vec<u8>,
|
pub document: Vec<u8>,
|
||||||
|
pub document_rels: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XMLDocx {
|
impl XMLDocx {
|
||||||
|
|
|
@ -28,7 +28,7 @@ where
|
||||||
zip.start_file("docProps/core.xml", options)?;
|
zip.start_file("docProps/core.xml", options)?;
|
||||||
zip.write_all(&xml.doc_props.core)?;
|
zip.write_all(&xml.doc_props.core)?;
|
||||||
zip.start_file("word/_rels/document.xml.rels", options)?;
|
zip.start_file("word/_rels/document.xml.rels", options)?;
|
||||||
zip.write_all(&xml.rels)?;
|
zip.write_all(&xml.document_rels)?;
|
||||||
zip.start_file("word/document.xml", options)?;
|
zip.start_file("word/document.xml", options)?;
|
||||||
zip.write_all(&xml.document)?;
|
zip.write_all(&xml.document)?;
|
||||||
zip.start_file("word/styles.xml", options)?;
|
zip.start_file("word/styles.xml", options)?;
|
||||||
|
|
|
@ -202,7 +202,7 @@ pub fn custom_attr_paragraph() -> Result<(), DocxError> {
|
||||||
.add_paragraph(
|
.add_paragraph(
|
||||||
Paragraph::new()
|
Paragraph::new()
|
||||||
.add_run(Run::new().add_text("Hello"))
|
.add_run(Run::new().add_text("Hello"))
|
||||||
.add_custom_attr("w:customId", "1234-5678"),
|
.add_attr("w:customId", "1234-5678"),
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
.pack(file)?;
|
.pack(file)?;
|
||||||
|
|
Loading…
Reference in New Issue