From d92d111aa6f19ee380a5ed30c1e87757dd709c88 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Mon, 11 Nov 2019 14:39:22 +0900 Subject: [PATCH] feat: Add p style --- docx-core/src/documents/document.rs | 4 +- docx-core/src/documents/elements/mod.rs | 2 + docx-core/src/documents/elements/paragraph.rs | 8 ++- .../documents/elements/paragraph_property.rs | 8 ++- .../src/documents/elements/paragraph_style.rs | 56 +++++++++++++++++++ docx-core/src/documents/elements/sz.rs | 2 - docx-core/src/documents/elements/text.rs | 1 - docx-core/src/documents/mod.rs | 1 - docx-core/src/documents/styles.rs | 2 +- docx-core/src/xml_builder/document.rs | 2 - docx-core/src/xml_builder/elements.rs | 2 +- docx-core/src/xml_builder/macros.rs | 2 + docx-core/src/xml_builder/styles.rs | 2 - 13 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 docx-core/src/documents/elements/paragraph_style.rs diff --git a/docx-core/src/documents/document.rs b/docx-core/src/documents/document.rs index 9939fd2..a1a11af 100644 --- a/docx-core/src/documents/document.rs +++ b/docx-core/src/documents/document.rs @@ -1,7 +1,6 @@ -use super::{DocDefaults, Paragraph, Run, Style}; +use super::Paragraph; use crate::documents::BuildXML; use crate::xml_builder::*; -use crate::StyleType; #[derive(Debug)] pub struct Document { @@ -43,6 +42,7 @@ impl BuildXML for Document { #[cfg(test)] mod tests { + use super::super::Run; use super::*; #[cfg(test)] use pretty_assertions::assert_eq; diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index 9682254..6e153e7 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -6,6 +6,7 @@ mod name; mod next; mod paragraph; mod paragraph_property; +mod paragraph_style; mod q_format; mod run; mod run_property; @@ -22,6 +23,7 @@ pub use name::*; pub use next::*; pub use paragraph::*; pub use paragraph_property::*; +pub use paragraph_style::*; pub use q_format::*; pub use run::*; pub use run_property::*; diff --git a/docx-core/src/documents/elements/paragraph.rs b/docx-core/src/documents/elements/paragraph.rs index 4f1d0ca..895c4cc 100644 --- a/docx-core/src/documents/elements/paragraph.rs +++ b/docx-core/src/documents/elements/paragraph.rs @@ -1,4 +1,4 @@ -use super::{ParagraphProperty, Run, RunProperty, Text}; +use super::{ParagraphProperty, ParagraphStyle, Run}; use crate::documents::BuildXML; use crate::types::*; use crate::xml_builder::*; @@ -7,13 +7,16 @@ use crate::xml_builder::*; pub struct Paragraph { runs: Vec, property: ParagraphProperty, + style: ParagraphStyle, } impl Default for Paragraph { fn default() -> Self { + let s: Option<&str> = None; Self { runs: Vec::new(), property: ParagraphProperty::new(), + style: ParagraphStyle::new(s), } } } @@ -39,6 +42,7 @@ impl BuildXML for Paragraph { XMLBuilder::new() .open_paragraph() .add_child(&self.property) + .add_child(&self.style) .add_children(&self.runs) .close() .build() @@ -58,7 +62,7 @@ mod tests { let b = Paragraph::new().add_run(Run::new("Hello")).build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#"Hello"# + r#"Hello"# ); } } diff --git a/docx-core/src/documents/elements/paragraph_property.rs b/docx-core/src/documents/elements/paragraph_property.rs index 7f9af3d..cc716c7 100644 --- a/docx-core/src/documents/elements/paragraph_property.rs +++ b/docx-core/src/documents/elements/paragraph_property.rs @@ -1,4 +1,4 @@ -use super::Justification; +use super::{Justification, RunProperty}; use crate::documents::BuildXML; use crate::types::AlignmentType; use crate::xml_builder::*; @@ -6,11 +6,15 @@ use crate::xml_builder::*; #[derive(Debug)] pub struct ParagraphProperty { alignment: Option, + run_property: RunProperty, } impl Default for ParagraphProperty { fn default() -> Self { - ParagraphProperty { alignment: None } + ParagraphProperty { + alignment: None, + run_property: RunProperty::new(), + } } } diff --git a/docx-core/src/documents/elements/paragraph_style.rs b/docx-core/src/documents/elements/paragraph_style.rs new file mode 100644 index 0000000..afe4e75 --- /dev/null +++ b/docx-core/src/documents/elements/paragraph_style.rs @@ -0,0 +1,56 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug)] +pub struct ParagraphStyle { + val: String, +} + +impl Default for ParagraphStyle { + fn default() -> Self { + ParagraphStyle { + val: "Normal".to_owned(), + } + } +} + +// 17.9.23 +// pStyle (Paragraph Style's Associated Numbering Level) +// This element specifies the name of a paragraph style which shall automatically this numbering level when +// applied to the contents of the document. When a paragraph style is defined to include a numbering definition, +// any numbering level defined by the numPr element (ยง17.3.1.19) shall be ignored, and instead this element shall +// specify the numbering level associated with that paragraph style. +impl ParagraphStyle { + pub fn new(val: Option>) -> ParagraphStyle { + if let Some(v) = val { + ParagraphStyle { val: v.into() } + } else { + Default::default() + } + } +} + +impl BuildXML for ParagraphStyle { + fn build(&self) -> Vec { + XMLBuilder::new().paragraph_style(&self.val).build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_p_style() { + let c = ParagraphStyle::new(Some("Heading")); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } +} diff --git a/docx-core/src/documents/elements/sz.rs b/docx-core/src/documents/elements/sz.rs index c3e5cdd..6f79c10 100644 --- a/docx-core/src/documents/elements/sz.rs +++ b/docx-core/src/documents/elements/sz.rs @@ -1,8 +1,6 @@ use crate::documents::BuildXML; use crate::xml_builder::*; -use super::Name; - #[derive(Debug)] pub struct Sz { val: usize, diff --git a/docx-core/src/documents/elements/text.rs b/docx-core/src/documents/elements/text.rs index 90f2f98..2521102 100644 --- a/docx-core/src/documents/elements/text.rs +++ b/docx-core/src/documents/elements/text.rs @@ -1,4 +1,3 @@ -use super::{RunProperty, Sz}; use crate::documents::BuildXML; use crate::xml_builder::*; diff --git a/docx-core/src/documents/mod.rs b/docx-core/src/documents/mod.rs index 54d14ea..97a39ea 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -7,7 +7,6 @@ mod rels; mod styles; mod xml_docx; -pub(crate) use crate::xml_builder::*; pub(crate) use build_xml::*; pub use content_types::*; diff --git a/docx-core/src/documents/styles.rs b/docx-core/src/documents/styles.rs index 50c27de..dd79f32 100644 --- a/docx-core/src/documents/styles.rs +++ b/docx-core/src/documents/styles.rs @@ -1,7 +1,6 @@ use super::{DocDefaults, Style}; use crate::documents::BuildXML; use crate::xml_builder::*; -use crate::StyleType; #[derive(Debug)] pub struct Styles { @@ -44,6 +43,7 @@ impl BuildXML for Styles { mod tests { use super::*; + use crate::types::StyleType; #[cfg(test)] use pretty_assertions::assert_eq; use std::str; diff --git a/docx-core/src/xml_builder/document.rs b/docx-core/src/xml_builder/document.rs index cc887e1..5a6a345 100644 --- a/docx-core/src/xml_builder/document.rs +++ b/docx-core/src/xml_builder/document.rs @@ -1,5 +1,3 @@ -use std::fmt; - use super::XMLBuilder; use super::XmlEvent; diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index 5f05b6a..90c7586 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -36,7 +36,7 @@ impl XMLBuilder { // i.e. only_str_val_el!(justification, "w:jc"); // i.e. - only_str_val_el!(p_style, "w:pStyle"); + only_str_val_el!(paragraph_style, "w:pStyle"); // i.e. pub(crate) fn sz(mut self, val: usize) -> Self { self.writer diff --git a/docx-core/src/xml_builder/macros.rs b/docx-core/src/xml_builder/macros.rs index ee15e77..61b91a3 100644 --- a/docx-core/src/xml_builder/macros.rs +++ b/docx-core/src/xml_builder/macros.rs @@ -142,6 +142,7 @@ macro_rules! only_str_val_el { }; } +/* macro_rules! only_usize_val_el { ($name: ident, $el_name: expr) => { pub(crate) fn $name(mut self, val: usize) -> Self { @@ -152,3 +153,4 @@ macro_rules! only_usize_val_el { } }; } +*/ diff --git a/docx-core/src/xml_builder/styles.rs b/docx-core/src/xml_builder/styles.rs index ecfa9cf..99d219a 100644 --- a/docx-core/src/xml_builder/styles.rs +++ b/docx-core/src/xml_builder/styles.rs @@ -1,5 +1,3 @@ -use std::fmt; - use super::XMLBuilder; use super::XmlEvent;