From 7925fc4e24582eeffdcf82054462cb3bfb44d709 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 5 Nov 2019 15:10:48 +0900 Subject: [PATCH] feat: Add contentType skeleton --- docx-core/fixtures/hello/[Content_Types].xml | 10 +- .../fixtures/hello_world/[Content_Types].xml | 18 ++- docx-core/fixtures/hello_world/_rels/.rels | 9 +- .../fixtures/hello_world/docProps/app.xml | 3 +- .../fixtures/hello_world/docProps/core.xml | 13 +- .../fixtures/hello_world/word/footer1.xml | 17 ++- .../fixtures/hello_world/word/footnotes.xml | 52 ++++++- .../fixtures/hello_world/word/header1.xml | 17 ++- .../fixtures/hello_world/word/numbering.xml | 114 +++++++++++++- .../fixtures/hello_world/word/styles.xml | 144 +++++++++++++++++- docx-core/src/documents/content_types.rs | 76 +++++++++ docx-core/src/documents/mod.rs | 16 ++ docx-core/src/documents/xml_builder.rs | 56 +++++++ docx-core/src/documents/xml_document.rs | 12 ++ docx-core/src/lib.rs | 30 ++-- 15 files changed, 565 insertions(+), 22 deletions(-) create mode 100644 docx-core/src/documents/content_types.rs create mode 100644 docx-core/src/documents/mod.rs create mode 100644 docx-core/src/documents/xml_builder.rs create mode 100644 docx-core/src/documents/xml_document.rs diff --git a/docx-core/fixtures/hello/[Content_Types].xml b/docx-core/fixtures/hello/[Content_Types].xml index dc111cb..f0bf1dd 100644 --- a/docx-core/fixtures/hello/[Content_Types].xml +++ b/docx-core/fixtures/hello/[Content_Types].xml @@ -1,3 +1,11 @@ - + + + + + + + + + \ No newline at end of file diff --git a/docx-core/fixtures/hello_world/[Content_Types].xml b/docx-core/fixtures/hello_world/[Content_Types].xml index 9c5c964..94fc644 100644 --- a/docx-core/fixtures/hello_world/[Content_Types].xml +++ b/docx-core/fixtures/hello_world/[Content_Types].xml @@ -1 +1,17 @@ - \ No newline at end of file + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docx-core/fixtures/hello_world/_rels/.rels b/docx-core/fixtures/hello_world/_rels/.rels index 1399e4d..a2d03c2 100644 --- a/docx-core/fixtures/hello_world/_rels/.rels +++ b/docx-core/fixtures/hello_world/_rels/.rels @@ -1 +1,8 @@ - \ No newline at end of file + + + + + + + + \ No newline at end of file diff --git a/docx-core/fixtures/hello_world/docProps/app.xml b/docx-core/fixtures/hello_world/docProps/app.xml index 8993710..c6a3665 100644 --- a/docx-core/fixtures/hello_world/docProps/app.xml +++ b/docx-core/fixtures/hello_world/docProps/app.xml @@ -1 +1,2 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/docx-core/fixtures/hello_world/docProps/core.xml b/docx-core/fixtures/hello_world/docProps/core.xml index 3f8b481..1de47b7 100644 --- a/docx-core/fixtures/hello_world/docProps/core.xml +++ b/docx-core/fixtures/hello_world/docProps/core.xml @@ -1 +1,12 @@ -Un-namedUn-named12019-09-13T19:25:28Z2019-09-13T19:25:28Z \ No newline at end of file + + + Un-named + Un-named + 1 + 2019-09-13T19:25:28Z + 2019-09-13T19:25:28Z + \ No newline at end of file diff --git a/docx-core/fixtures/hello_world/word/footer1.xml b/docx-core/fixtures/hello_world/word/footer1.xml index 05a9751..6679e73 100644 --- a/docx-core/fixtures/hello_world/word/footer1.xml +++ b/docx-core/fixtures/hello_world/word/footer1.xml @@ -1 +1,16 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/docx-core/fixtures/hello_world/word/footnotes.xml b/docx-core/fixtures/hello_world/word/footnotes.xml index edbd2f4..42a0057 100644 --- a/docx-core/fixtures/hello_world/word/footnotes.xml +++ b/docx-core/fixtures/hello_world/word/footnotes.xml @@ -1 +1,51 @@ - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docx-core/fixtures/hello_world/word/header1.xml b/docx-core/fixtures/hello_world/word/header1.xml index 08805f3..1fbc667 100644 --- a/docx-core/fixtures/hello_world/word/header1.xml +++ b/docx-core/fixtures/hello_world/word/header1.xml @@ -1 +1,16 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/docx-core/fixtures/hello_world/word/numbering.xml b/docx-core/fixtures/hello_world/word/numbering.xml index 0f46501..c809c7b 100644 --- a/docx-core/fixtures/hello_world/word/numbering.xml +++ b/docx-core/fixtures/hello_world/word/numbering.xml @@ -1 +1,113 @@ - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docx-core/fixtures/hello_world/word/styles.xml b/docx-core/fixtures/hello_world/word/styles.xml index c9a6343..fa16384 100644 --- a/docx-core/fixtures/hello_world/word/styles.xml +++ b/docx-core/fixtures/hello_world/word/styles.xml @@ -1 +1,143 @@ - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docx-core/src/documents/content_types.rs b/docx-core/src/documents/content_types.rs new file mode 100644 index 0000000..837ca2a --- /dev/null +++ b/docx-core/src/documents/content_types.rs @@ -0,0 +1,76 @@ +use super::xml_builder::*; + +pub struct ContentTypes {} + +impl ContentTypes { + pub fn new() -> ContentTypes { + ContentTypes {} + } + + pub fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.add_declaration() + .open_types("http://schemas.openxmlformats.org/package/2006/content-types") + .add_override( + "/_rels/.rels", + "application/vnd.openxmlformats-package.relationships+xml", + ) + .add_override( + "/docProps/app.xml", + "application/vnd.openxmlformats-officedocument.extended-properties+xml", + ) + .add_override( + "/docProps/core.xml", + "application/vnd.openxmlformats-package.core-properties+xml", + ) + .add_override( + "/word/_rels/document.xml.rels", + "application/vnd.openxmlformats-package.relationships+xml", + ) + .add_override( + "/word/settings.xml", + "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", + ) + .add_override( + "/word/fontTable.xml", + "application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml", + ) + .add_override( + "/word/document.xml", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", + ) + .add_override( + "/word/styles.xml", + "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", + ) + .close() + .build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + use std::str; + + #[test] + fn test_build() { + let c = ContentTypes::new(); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#" + + + + + + + + + +"# + ); + } +} diff --git a/docx-core/src/documents/mod.rs b/docx-core/src/documents/mod.rs new file mode 100644 index 0000000..d7e9c66 --- /dev/null +++ b/docx-core/src/documents/mod.rs @@ -0,0 +1,16 @@ +pub mod content_types; +mod xml_builder; +mod xml_document; + +use content_types::*; + +pub(crate) struct Document { + content_type: ContentTypes, +} + +impl Document { + pub fn new() -> Document { + let content_type = ContentTypes::new(); + Document { content_type } + } +} diff --git a/docx-core/src/documents/xml_builder.rs b/docx-core/src/documents/xml_builder.rs new file mode 100644 index 0000000..0e8c175 --- /dev/null +++ b/docx-core/src/documents/xml_builder.rs @@ -0,0 +1,56 @@ +use xml::writer::{EmitterConfig, EventWriter, XmlEvent}; + +pub struct XMLBuilder { + writer: EventWriter>, +} + +impl XMLBuilder { + pub(crate) fn new() -> XMLBuilder { + let buf = Vec::new(); + let writer = EmitterConfig::new() + .write_document_declaration(false) + .perform_indent(true) + .create_writer(buf); + XMLBuilder { writer } + } + + pub(crate) fn add_declaration(mut self) -> Self { + self.writer + .write( + XmlEvent::start_element("?xml") + .attr("version", "1.0") + .attr("encoding", "UTF-8"), + ) + .expect("should write to buf"); + self.close() + } + + pub(crate) fn open_types(mut self, uri: &str) -> Self { + self.writer + .write(XmlEvent::start_element("Types").attr("xmlns", uri)) + .expect("should write to buf"); + self + } + + pub(crate) fn add_override(mut self, name: &str, content_type: &str) -> Self { + self.writer + .write( + XmlEvent::start_element("Override") + .attr("PartName", &name) + .attr("ContentType", &content_type), + ) + .expect("should write to buf"); + self.close() + } + + pub(crate) fn close(mut self) -> Self { + self.writer + .write(XmlEvent::end_element()) + .expect("should end"); + self + } + + pub(crate) fn build(self) -> Vec { + self.writer.into_inner() + } +} diff --git a/docx-core/src/documents/xml_document.rs b/docx-core/src/documents/xml_document.rs new file mode 100644 index 0000000..5b05e4b --- /dev/null +++ b/docx-core/src/documents/xml_document.rs @@ -0,0 +1,12 @@ +use super::Document; + +pub(crate) struct XMLDocument { + content_type: Vec, +} + +impl From for XMLDocument { + fn from(doc: Document) -> XMLDocument { + let content_type = doc.content_type.build(); + XMLDocument { content_type } + } +} diff --git a/docx-core/src/lib.rs b/docx-core/src/lib.rs index 9aad3cf..c5a0a49 100644 --- a/docx-core/src/lib.rs +++ b/docx-core/src/lib.rs @@ -1,22 +1,28 @@ +mod documents; + +use documents::*; + use std::fs::File; use std::io::{self, Write}; use xml::writer::{EmitterConfig, EventWriter, Result, XmlEvent}; pub fn simple() { + let doc = Document::new(); let mut file = File::create("./dist/output.xml").unwrap(); - let mut b = Vec::new(); - let mut w = EmitterConfig::new() - .write_document_declaration(false) - .create_writer(&mut b); - - w.write(XmlEvent::start_element("h:hello").ns("h", "urn:hello-world")) - .unwrap(); - w.write("hello world").unwrap(); - w.write(XmlEvent::end_element()).unwrap(); - - file.write_all(&b).unwrap(); - file.flush().unwrap(); + // let mut b = Vec::new(); + // let mut w = EmitterConfig::new() + // .write_document_declaration(false) + // .create_writer(&mut b); + // w.write( + // XmlEvent::start_element("?xml") + // .attr("version", "1.0") + // .attr("encoding", "UTF-8"), + // ); + // // w.write("hello world").unwrap(); + // w.write(XmlEvent::end_element()).unwrap(); + // file.write_all(&b).unwrap(); + // file.flush().unwrap(); // assert_eq!( // str::from_utf8(&b).unwrap(), // r#"hello world"#