From 84972af477ea3b8ff10a8198629b4b181f94b357 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Thu, 14 Nov 2019 15:54:39 +0900 Subject: [PATCH] feat: Add settings --- .../documents/elements/default_tab_stop.rs | 39 ++++++++ docx-core/src/documents/elements/mod.rs | 4 + docx-core/src/documents/elements/zoom.rs | 36 +++++++ docx-core/src/documents/mod.rs | 6 ++ docx-core/src/documents/settings.rs | 56 +++++++++++ docx-core/src/documents/xml_docx.rs | 1 + docx-core/src/xml_builder/elements.rs | 2 + docx-core/src/xml_builder/mod.rs | 1 + docx-core/src/xml_builder/settings.rs | 33 +++++++ docx-core/src/zipper/mod.rs | 2 + .../hello_libre_office/[Content_Types].xml | 10 +- fixtures/hello_libre_office/_rels/.rels | 5 +- fixtures/hello_libre_office/docProps/app.xml | 12 +-- fixtures/hello_libre_office/docProps/core.xml | 16 +--- .../word/_rels/document.xml.rels | 5 +- fixtures/hello_libre_office/word/document.xml | 33 +------ fixtures/hello_libre_office/word/settings.xml | 8 ++ fixtures/hello_libre_office/word/styles.xml | 94 +------------------ 18 files changed, 195 insertions(+), 168 deletions(-) create mode 100644 docx-core/src/documents/elements/default_tab_stop.rs create mode 100644 docx-core/src/documents/elements/zoom.rs create mode 100644 docx-core/src/documents/settings.rs create mode 100644 docx-core/src/xml_builder/settings.rs create mode 100644 fixtures/hello_libre_office/word/settings.xml diff --git a/docx-core/src/documents/elements/default_tab_stop.rs b/docx-core/src/documents/elements/default_tab_stop.rs new file mode 100644 index 0000000..97ff0f0 --- /dev/null +++ b/docx-core/src/documents/elements/default_tab_stop.rs @@ -0,0 +1,39 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct DefaultTabStop { + val: usize, +} + +impl DefaultTabStop { + pub fn new(val: usize) -> DefaultTabStop { + DefaultTabStop { val } + } +} + +impl BuildXML for DefaultTabStop { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.default_tab_stop(self.val).build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_zoom() { + let c = DefaultTabStop::new(20); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } +} diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index 4596a20..20b12af 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -3,6 +3,7 @@ mod bold; mod bold_cs; mod br; mod color; +mod default_tab_stop; mod doc_defaults; mod grid_span; mod highlight; @@ -38,12 +39,14 @@ mod table_row_property; mod table_width; mod text; mod vertical_merge; +mod zoom; pub use based_on::*; pub use bold::*; pub use bold_cs::*; pub use br::*; pub use color::*; +pub use default_tab_stop::*; pub use doc_defaults::*; pub use grid_span::*; pub use highlight::*; @@ -79,3 +82,4 @@ pub use table_row_property::*; pub use table_width::*; pub use text::*; pub use vertical_merge::*; +pub use zoom::*; diff --git a/docx-core/src/documents/elements/zoom.rs b/docx-core/src/documents/elements/zoom.rs new file mode 100644 index 0000000..820a28e --- /dev/null +++ b/docx-core/src/documents/elements/zoom.rs @@ -0,0 +1,36 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct Zoom { + val: usize, +} + +impl Zoom { + pub fn new(val: usize) -> Zoom { + Zoom { val } + } +} + +impl BuildXML for Zoom { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.zoom(&format!("{}", self.val)).build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_zoom() { + let c = Zoom::new(20); + 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 index 2dae6a3..2104df4 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -5,6 +5,7 @@ mod document; mod document_rels; mod elements; mod rels; +mod settings; mod styles; mod xml_docx; @@ -16,6 +17,7 @@ pub use document::*; pub use document_rels::*; pub use elements::*; pub use rels::*; +pub use settings::*; pub use styles::*; pub use xml_docx::*; @@ -27,6 +29,7 @@ pub struct Docx { doc_props: DocProps, styles: Styles, document: Document, + settings: Settings, } impl Default for Docx { @@ -37,6 +40,7 @@ impl Default for Docx { let styles = Styles::new(); let document = Document::new(); let document_rels = DocumentRels::new(); + let settings = Settings::new(); Docx { content_type, rels, @@ -44,6 +48,7 @@ impl Default for Docx { styles, document, document_rels, + settings, } } } @@ -71,6 +76,7 @@ impl Docx { styles: self.styles.build(), document: self.document.build(), document_rels: self.document_rels.build(), + settings: self.settings.build(), } } } diff --git a/docx-core/src/documents/settings.rs b/docx-core/src/documents/settings.rs new file mode 100644 index 0000000..a248eb2 --- /dev/null +++ b/docx-core/src/documents/settings.rs @@ -0,0 +1,56 @@ +use super::{DefaultTabStop, Style, Zoom}; +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug)] +pub struct Settings { + default_tab_stop: DefaultTabStop, + zoom: Zoom, +} + +impl Settings { + pub fn new() -> Settings { + Default::default() + } +} + +impl Default for Settings { + fn default() -> Self { + Self { + default_tab_stop: DefaultTabStop::new(709), + zoom: Zoom::new(100), + } + } +} + +impl BuildXML for Settings { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.declaration(Some(true)) + .open_settings() + .add_child(&self.default_tab_stop) + .add_child(&self.zoom) + .close() + .build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_settings() { + let c = Settings::new(); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#" +"# + ); + } +} diff --git a/docx-core/src/documents/xml_docx.rs b/docx-core/src/documents/xml_docx.rs index 28df056..613eefd 100644 --- a/docx-core/src/documents/xml_docx.rs +++ b/docx-core/src/documents/xml_docx.rs @@ -12,6 +12,7 @@ pub struct XMLDocx { pub styles: Vec, pub document: Vec, pub document_rels: Vec, + pub settings: Vec, } impl XMLDocx { diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index af3f82d..dd2a1c4 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -126,6 +126,8 @@ impl XMLBuilder { closed_el!(tab, "w:tab"); closed_el!(br, "w:br", "w:type"); + closed_el!(zoom, "w:zoom", "w:percent"); + only_usize_val_el!(default_tab_stop, "w:defaultTabStop"); } #[cfg(test)] diff --git a/docx-core/src/xml_builder/mod.rs b/docx-core/src/xml_builder/mod.rs index 32d74d9..d684a69 100644 --- a/docx-core/src/xml_builder/mod.rs +++ b/docx-core/src/xml_builder/mod.rs @@ -7,6 +7,7 @@ mod document; mod elements; mod properties; mod relationship; +mod settings; mod styles; use crate::BuildXML; diff --git a/docx-core/src/xml_builder/settings.rs b/docx-core/src/xml_builder/settings.rs new file mode 100644 index 0000000..7e4b4c1 --- /dev/null +++ b/docx-core/src/xml_builder/settings.rs @@ -0,0 +1,33 @@ +use super::XMLBuilder; +use super::XmlEvent; + +impl XMLBuilder { + pub(crate) fn open_settings(mut self) -> Self { + self.writer + .write(XmlEvent::start_element("w:settings").attr( + "xmlns:w", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + )) + .expect("should write to buf"); + self + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_declaration() { + let b = XMLBuilder::new(); + let r = b.open_settings().close().build(); + assert_eq!( + str::from_utf8(&r).unwrap(), + r#""# + ); + } +} diff --git a/docx-core/src/zipper/mod.rs b/docx-core/src/zipper/mod.rs index 7eccde5..ec51f30 100644 --- a/docx-core/src/zipper/mod.rs +++ b/docx-core/src/zipper/mod.rs @@ -33,6 +33,8 @@ where zip.write_all(&xml.document)?; zip.start_file("word/styles.xml", options)?; zip.write_all(&xml.styles)?; + zip.start_file("word/settings.xml", options)?; + zip.write_all(&xml.settings)?; zip.finish()?; Ok(()) } diff --git a/fixtures/hello_libre_office/[Content_Types].xml b/fixtures/hello_libre_office/[Content_Types].xml index f0bf1dd..dc111cb 100644 --- a/fixtures/hello_libre_office/[Content_Types].xml +++ b/fixtures/hello_libre_office/[Content_Types].xml @@ -1,11 +1,3 @@ - - - - - - - - - + \ No newline at end of file diff --git a/fixtures/hello_libre_office/_rels/.rels b/fixtures/hello_libre_office/_rels/.rels index e13b32e..f0b72e7 100644 --- a/fixtures/hello_libre_office/_rels/.rels +++ b/fixtures/hello_libre_office/_rels/.rels @@ -1,6 +1,3 @@ - - - - + \ No newline at end of file diff --git a/fixtures/hello_libre_office/docProps/app.xml b/fixtures/hello_libre_office/docProps/app.xml index 2c7f60f..f819b8e 100644 --- a/fixtures/hello_libre_office/docProps/app.xml +++ b/fixtures/hello_libre_office/docProps/app.xml @@ -1,12 +1,2 @@ - - - 0 - LibreOffice/6.0.7.3$Linux_X86_64 LibreOffice_project/00m0$Build-3 - 1 - 1 - 5 - 5 - 1 - \ No newline at end of file +0LibreOffice/6.0.7.3$Linux_X86_64 LibreOffice_project/00m0$Build-311551 \ No newline at end of file diff --git a/fixtures/hello_libre_office/docProps/core.xml b/fixtures/hello_libre_office/docProps/core.xml index 4e90bed..aa2d79a 100644 --- a/fixtures/hello_libre_office/docProps/core.xml +++ b/fixtures/hello_libre_office/docProps/core.xml @@ -1,16 +1,2 @@ - - 2019-09-13T19:32:17Z - - - ja-JP - - 2019-09-13T19:32:34Z - 1 - - - \ No newline at end of file +2019-09-13T19:32:17Zja-JP2019-09-13T19:32:34Z1 \ No newline at end of file diff --git a/fixtures/hello_libre_office/word/_rels/document.xml.rels b/fixtures/hello_libre_office/word/_rels/document.xml.rels index 6ac87fd..8a2db8a 100644 --- a/fixtures/hello_libre_office/word/_rels/document.xml.rels +++ b/fixtures/hello_libre_office/word/_rels/document.xml.rels @@ -1,6 +1,3 @@ - - - - + \ No newline at end of file diff --git a/fixtures/hello_libre_office/word/document.xml b/fixtures/hello_libre_office/word/document.xml index 775c58a..d53dbbe 100644 --- a/fixtures/hello_libre_office/word/document.xml +++ b/fixtures/hello_libre_office/word/document.xml @@ -1,33 +1,2 @@ - - - - - - - - - - Hello - - - - - - - - - - - - \ No newline at end of file +Hello \ No newline at end of file diff --git a/fixtures/hello_libre_office/word/settings.xml b/fixtures/hello_libre_office/word/settings.xml new file mode 100644 index 0000000..4e5ecd8 --- /dev/null +++ b/fixtures/hello_libre_office/word/settings.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/fixtures/hello_libre_office/word/styles.xml b/fixtures/hello_libre_office/word/styles.xml index d8d8049..72e6f53 100644 --- a/fixtures/hello_libre_office/word/styles.xml +++ b/fixtures/hello_libre_office/word/styles.xml @@ -1,94 +1,2 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + \ No newline at end of file