feat: Add p style

main
bokuweb 2019-11-11 14:39:22 +09:00
parent 879189da3c
commit d92d111aa6
13 changed files with 76 additions and 16 deletions

View File

@ -1,7 +1,6 @@
use super::{DocDefaults, Paragraph, Run, Style}; use super::Paragraph;
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::xml_builder::*; use crate::xml_builder::*;
use crate::StyleType;
#[derive(Debug)] #[derive(Debug)]
pub struct Document { pub struct Document {
@ -43,6 +42,7 @@ impl BuildXML for Document {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::super::Run;
use super::*; use super::*;
#[cfg(test)] #[cfg(test)]
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;

View File

@ -6,6 +6,7 @@ mod name;
mod next; mod next;
mod paragraph; mod paragraph;
mod paragraph_property; mod paragraph_property;
mod paragraph_style;
mod q_format; mod q_format;
mod run; mod run;
mod run_property; mod run_property;
@ -22,6 +23,7 @@ pub use name::*;
pub use next::*; pub use next::*;
pub use paragraph::*; pub use paragraph::*;
pub use paragraph_property::*; pub use paragraph_property::*;
pub use paragraph_style::*;
pub use q_format::*; pub use q_format::*;
pub use run::*; pub use run::*;
pub use run_property::*; pub use run_property::*;

View File

@ -1,4 +1,4 @@
use super::{ParagraphProperty, Run, RunProperty, Text}; use super::{ParagraphProperty, ParagraphStyle, Run};
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::types::*; use crate::types::*;
use crate::xml_builder::*; use crate::xml_builder::*;
@ -7,13 +7,16 @@ use crate::xml_builder::*;
pub struct Paragraph { pub struct Paragraph {
runs: Vec<Run>, runs: Vec<Run>,
property: ParagraphProperty, property: ParagraphProperty,
style: ParagraphStyle,
} }
impl Default for Paragraph { impl Default for Paragraph {
fn default() -> Self { fn default() -> Self {
let s: Option<&str> = None;
Self { Self {
runs: Vec::new(), runs: Vec::new(),
property: ParagraphProperty::new(), property: ParagraphProperty::new(),
style: ParagraphStyle::new(s),
} }
} }
} }
@ -39,6 +42,7 @@ impl BuildXML for Paragraph {
XMLBuilder::new() XMLBuilder::new()
.open_paragraph() .open_paragraph()
.add_child(&self.property) .add_child(&self.property)
.add_child(&self.style)
.add_children(&self.runs) .add_children(&self.runs)
.close() .close()
.build() .build()
@ -58,7 +62,7 @@ mod tests {
let b = Paragraph::new().add_run(Run::new("Hello")).build(); let b = Paragraph::new().add_run(Run::new("Hello")).build();
assert_eq!( assert_eq!(
str::from_utf8(&b).unwrap(), str::from_utf8(&b).unwrap(),
r#"<w:p><w:r><w:rPr /><w:t>Hello</w:t></w:r></w:p>"# r#"<w:p><w:pPr /><w:pStyle w:val="Normal" /><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"#
); );
} }
} }

View File

@ -1,4 +1,4 @@
use super::Justification; use super::{Justification, RunProperty};
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::types::AlignmentType; use crate::types::AlignmentType;
use crate::xml_builder::*; use crate::xml_builder::*;
@ -6,11 +6,15 @@ use crate::xml_builder::*;
#[derive(Debug)] #[derive(Debug)]
pub struct ParagraphProperty { pub struct ParagraphProperty {
alignment: Option<Justification>, alignment: Option<Justification>,
run_property: RunProperty,
} }
impl Default for ParagraphProperty { impl Default for ParagraphProperty {
fn default() -> Self { fn default() -> Self {
ParagraphProperty { alignment: None } ParagraphProperty {
alignment: None,
run_property: RunProperty::new(),
}
} }
} }

View File

@ -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<impl Into<String>>) -> ParagraphStyle {
if let Some(v) = val {
ParagraphStyle { val: v.into() }
} else {
Default::default()
}
}
}
impl BuildXML for ParagraphStyle {
fn build(&self) -> Vec<u8> {
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#"<w:pStyle w:val="Heading" />"#
);
}
}

View File

@ -1,8 +1,6 @@
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::xml_builder::*; use crate::xml_builder::*;
use super::Name;
#[derive(Debug)] #[derive(Debug)]
pub struct Sz { pub struct Sz {
val: usize, val: usize,

View File

@ -1,4 +1,3 @@
use super::{RunProperty, Sz};
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::xml_builder::*; use crate::xml_builder::*;

View File

@ -7,7 +7,6 @@ mod rels;
mod styles; mod styles;
mod xml_docx; mod xml_docx;
pub(crate) use crate::xml_builder::*;
pub(crate) use build_xml::*; pub(crate) use build_xml::*;
pub use content_types::*; pub use content_types::*;

View File

@ -1,7 +1,6 @@
use super::{DocDefaults, Style}; use super::{DocDefaults, Style};
use crate::documents::BuildXML; use crate::documents::BuildXML;
use crate::xml_builder::*; use crate::xml_builder::*;
use crate::StyleType;
#[derive(Debug)] #[derive(Debug)]
pub struct Styles { pub struct Styles {
@ -44,6 +43,7 @@ impl BuildXML for Styles {
mod tests { mod tests {
use super::*; use super::*;
use crate::types::StyleType;
#[cfg(test)] #[cfg(test)]
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use std::str; use std::str;

View File

@ -1,5 +1,3 @@
use std::fmt;
use super::XMLBuilder; use super::XMLBuilder;
use super::XmlEvent; use super::XmlEvent;

View File

@ -36,7 +36,7 @@ impl XMLBuilder {
// i.e. <w:jc ... > // i.e. <w:jc ... >
only_str_val_el!(justification, "w:jc"); only_str_val_el!(justification, "w:jc");
// i.e. <w:pStyle ... > // i.e. <w:pStyle ... >
only_str_val_el!(p_style, "w:pStyle"); only_str_val_el!(paragraph_style, "w:pStyle");
// i.e. <w:sz ... > // i.e. <w:sz ... >
pub(crate) fn sz(mut self, val: usize) -> Self { pub(crate) fn sz(mut self, val: usize) -> Self {
self.writer self.writer

View File

@ -142,6 +142,7 @@ macro_rules! only_str_val_el {
}; };
} }
/*
macro_rules! only_usize_val_el { macro_rules! only_usize_val_el {
($name: ident, $el_name: expr) => { ($name: ident, $el_name: expr) => {
pub(crate) fn $name(mut self, val: usize) -> Self { pub(crate) fn $name(mut self, val: usize) -> Self {
@ -152,3 +153,4 @@ macro_rules! only_usize_val_el {
} }
}; };
} }
*/

View File

@ -1,5 +1,3 @@
use std::fmt;
use super::XMLBuilder; use super::XMLBuilder;
use super::XmlEvent; use super::XmlEvent;