Add default sect pr (#6)

* feat: Add section pr

* feat: Add sectPr to doc
main
bokuweb 2019-12-13 12:58:01 +09:00 committed by GitHub
parent f4f47c06c7
commit 7f65d18877
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 227 additions and 8 deletions

View File

@ -1,10 +1,11 @@
use super::{Paragraph, Table}; use super::{Paragraph, SectionProperty, Table};
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::xml_builder::*; use crate::xml_builder::*;
#[derive(Debug)] #[derive(Debug)]
pub struct Document { pub struct Document {
pub(crate) children: Vec<DocumentChild>, pub(crate) children: Vec<DocumentChild>,
pub section_property: SectionProperty,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -33,6 +34,7 @@ impl Default for Document {
fn default() -> Self { fn default() -> Self {
Self { Self {
children: Vec::new(), children: Vec::new(),
section_property: SectionProperty::new(),
} }
} }
} }
@ -49,7 +51,7 @@ impl BuildXML for Document {
DocumentChild::Table(t) => b = b.add_child(t), DocumentChild::Table(t) => b = b.add_child(t),
} }
} }
b.close().close().build() b.add_child(&self.section_property).close().close().build()
} }
} }
@ -71,7 +73,9 @@ mod tests {
str::from_utf8(&b).unwrap(), str::from_utf8(&b).unwrap(),
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?> r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14"> <w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14">
<w:body><w:p><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></w:body> <w:body><w:p><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><w:sectPr><w:pgSz w:w="11906" w:h="16838" /><w:pgMar w:top="1985" w:right="1701" w:bottom="1701" w:left="1701" w:header="851" w:footer="992" w:gutter="0" /><w:cols w:space="425" />
<w:docGrid w:type="lines" w:linePitch="360" />
</w:sectPr></w:body>
</w:document>"# </w:document>"#
); );
} }

View File

@ -30,6 +30,8 @@ mod number_format;
mod numbering; mod numbering;
mod numbering_id; mod numbering_id;
mod numbering_property; mod numbering_property;
mod page_margin;
mod page_size;
mod paragraph; mod paragraph;
mod paragraph_property; mod paragraph_property;
mod paragraph_style; mod paragraph_style;
@ -37,6 +39,7 @@ mod q_format;
mod run; mod run;
mod run_property; mod run_property;
mod run_property_default; mod run_property_default;
mod section_property;
mod start; mod start;
mod style; mod style;
mod sz; mod sz;
@ -92,6 +95,8 @@ pub use number_format::*;
pub use numbering::*; pub use numbering::*;
pub use numbering_id::*; pub use numbering_id::*;
pub use numbering_property::*; pub use numbering_property::*;
pub use page_margin::*;
pub use page_size::*;
pub use paragraph::*; pub use paragraph::*;
pub use paragraph_property::*; pub use paragraph_property::*;
pub use paragraph_style::*; pub use paragraph_style::*;
@ -99,6 +104,7 @@ pub use q_format::*;
pub use run::*; pub use run::*;
pub use run_property::*; pub use run_property::*;
pub use run_property_default::*; pub use run_property_default::*;
pub use section_property::*;
pub use start::*; pub use start::*;
pub use style::*; pub use style::*;
pub use sz::*; pub use sz::*;

View File

@ -0,0 +1,93 @@
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct PageMargin {
top: usize,
left: usize,
bottom: usize,
right: usize,
header: usize,
footer: usize,
gutter: usize,
}
// These values were based on microsoft office word2019 windows edition.
// <w:pgMar w:top="1985" w:right="1701" w:bottom="1701" w:left="1701" w:header="851" w:footer="992" w:gutter="0"/>
impl Default for PageMargin {
fn default() -> PageMargin {
PageMargin {
top: 1985,
left: 1701,
bottom: 1701,
right: 1701,
header: 851,
footer: 992,
gutter: 0,
}
}
}
impl PageMargin {
pub fn new() -> PageMargin {
Default::default()
}
pub fn top(self, v: usize) -> PageMargin {
PageMargin { top: v, ..self }
}
pub fn left(self, v: usize) -> PageMargin {
PageMargin { left: v, ..self }
}
pub fn bottom(self, v: usize) -> PageMargin {
PageMargin { bottom: v, ..self }
}
pub fn right(self, v: usize) -> PageMargin {
PageMargin { right: v, ..self }
}
pub fn header(self, v: usize) -> PageMargin {
PageMargin { header: v, ..self }
}
pub fn gutter(self, v: usize) -> PageMargin {
PageMargin { gutter: v, ..self }
}
}
impl BuildXML for PageMargin {
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
.page_margin(
&format!("{}", self.top),
&format!("{}", self.right),
&format!("{}", self.bottom),
&format!("{}", self.left),
&format!("{}", self.header),
&format!("{}", self.footer),
&format!("{}", self.gutter),
)
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_page_margin_default() {
let b = PageMargin::new().build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:pgMar w:top="1985" w:right="1701" w:bottom="1701" w:left="1701" w:header="851" w:footer="992" w:gutter="0" />"#
);
}
}

View File

@ -0,0 +1,52 @@
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct PageSize {
w: usize,
h: usize,
}
// These values were based on microsoft office word2019 windows edition.
// <w:pgSz w:w="11906" w:h="16838"/>
impl Default for PageSize {
fn default() -> PageSize {
PageSize { w: 11906, h: 16838 }
}
}
impl PageSize {
pub fn new() -> PageSize {
Default::default()
}
pub fn size(self, w: usize, h: usize) -> PageSize {
PageSize { w, h }
}
}
impl BuildXML for PageSize {
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
.page_size(&format!("{}", self.w), &format!("{}", self.h))
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_page_size_default() {
let b = PageSize::new().build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:pgSz w:w="11906" w:h="16838" />"#
);
}
}

View File

@ -0,0 +1,62 @@
use super::*;
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct SectionProperty {
page_size: PageSize,
page_margin: PageMargin,
columns: usize,
document_grid: usize,
}
impl SectionProperty {
pub fn new() -> SectionProperty {
Default::default()
}
}
impl Default for SectionProperty {
fn default() -> Self {
Self {
page_size: PageSize::new(),
page_margin: PageMargin::new(),
columns: 425,
document_grid: 360,
}
}
}
impl BuildXML for SectionProperty {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.open_section_property()
.add_child(&self.page_size)
.add_child(&self.page_margin)
.columns(&format!("{}", &self.columns))
.document_grid("lines", &format!("{}", &self.document_grid))
.close()
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_section_property_default() {
let c = SectionProperty::new();
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:sectPr><w:pgSz w:w="11906" w:h="16838" /><w:pgMar w:top="1985" w:right="1701" w:bottom="1701" w:left="1701" w:header="851" w:footer="992" w:gutter="0" /><w:cols w:space="425" />
<w:docGrid w:type="lines" w:linePitch="360" />
</w:sectPr>"#
);
}
}

View File

@ -152,20 +152,22 @@ impl XMLBuilder {
only_str_val_el!(family, "w:family"); only_str_val_el!(family, "w:family");
only_str_val_el!(charset, "w:charset"); only_str_val_el!(charset, "w:charset");
only_usize_val_el!(section_property, "w:sectPr"); opened_el!(open_section_property, "w:sectPr");
only_str_val_el!(type_tag, "w:type"); only_str_val_el!(type_tag, "w:type");
closed_el!(page_size, "w:pgSz", "w:w", "w:h"); closed_el!(page_size, "w:pgSz", "w:w", "w:h");
closed_el!( closed_el!(
page_margin, page_margin,
"w:pgMar", "w:pgMar",
"w:left",
"w:right",
"w:header",
"w:top", "w:top",
"w:footer", "w:right",
"w:bottom", "w:bottom",
"w:left",
"w:header",
"w:footer",
"w:gutter" "w:gutter"
); );
closed_el!(columns, "w:cols", "w:space");
closed_el!(document_grid, "w:docGrid", "w:type", "w:linePitch");
opened_el!(open_insert, "w:ins", "w:id", "w:author", "w:date"); opened_el!(open_insert, "w:ins", "w:id", "w:author", "w:date");
opened_el!(open_delete, "w:del", "w:id", "w:author", "w:date"); opened_el!(open_delete, "w:del", "w:id", "w:author", "w:date");

Binary file not shown.