Skip to content

Commit

Permalink
feat: Add numberings
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Dec 6, 2019
1 parent 2caff21 commit 9a1b92f
Show file tree
Hide file tree
Showing 27 changed files with 1,607 additions and 2 deletions.
93 changes: 93 additions & 0 deletions docx-core/src/documents/elements/level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use crate::documents::{BuildXML, LevelJc, LevelText, NumberFormat, ParagraphProperty, Start};
use crate::types::*;
use crate::xml_builder::*;

#[derive(Debug, Clone)]
pub struct Level<'a> {
level: usize,
start: Start,
format: NumberFormat<'a>,
text: LevelText<'a>,
jc: LevelJc<'a>,
paragraph_property: ParagraphProperty,
}

impl<'a> Level<'a> {
pub fn new(
level: usize,
start: Start,
format: NumberFormat<'a>,
text: LevelText<'a>,
jc: LevelJc<'a>,
) -> Level<'a> {
Self {
level,
start,
format,
text,
jc,
paragraph_property: ParagraphProperty::new(),
}
}

pub fn indent(mut self, left: usize, special_indent: Option<SpecialIndentType>) -> Self {
self.paragraph_property = self.paragraph_property.indent(left, special_indent);
self
}
}

impl<'a> BuildXML for Level<'a> {
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
.open_level(&format!("{}", self.level))
.add_child(&self.start)
.add_child(&self.format)
.add_child(&self.text)
.add_child(&self.jc)
.add_child(&self.paragraph_property)
.close()
.build()
}
}

#[cfg(test)]
mod tests {

use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;

#[test]
fn test_level() {
let b = Level::new(
1,
Start::new(1),
NumberFormat::new("decimal"),
LevelText::new("%4."),
LevelJc::new("left"),
)
.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:lvl w:ilvl="1"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%4." /><w:lvlJc w:val="left" /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr></w:lvl>"#
);
}

#[test]
fn test_level_indent() {
let b = Level::new(
1,
Start::new(1),
NumberFormat::new("decimal"),
LevelText::new("%4."),
LevelJc::new("left"),
)
.indent(320, Some(SpecialIndentType::Hanging(200)))
.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:lvl w:ilvl="1"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%4." /><w:lvlJc w:val="left" /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /><w:ind w:left="320" w:hanging="200" /></w:pPr></w:lvl>"#
);
}
}
36 changes: 36 additions & 0 deletions docx-core/src/documents/elements/level_jc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::documents::BuildXML;
use crate::xml_builder::*;

#[derive(Debug, Clone)]
pub struct LevelJc<'a> {
val: &'a str,
}

impl<'a> LevelJc<'a> {
pub fn new(val: &'a str) -> Self {
Self { val }
}
}

impl<'a> BuildXML for LevelJc<'a> {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.level_justification(self.val).build()
}
}

#[cfg(test)]
mod tests {

use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;

#[test]
fn test_level_jc() {
let c = LevelJc::new("left");
let b = c.build();
assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:lvlJc w:val="left" />"#);
}
}
36 changes: 36 additions & 0 deletions docx-core/src/documents/elements/level_text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::documents::BuildXML;
use crate::xml_builder::*;

#[derive(Debug, Clone)]
pub struct LevelText<'a> {
val: &'a str,
}

impl<'a> LevelText<'a> {
pub fn new(val: &'a str) -> Self {
Self { val }
}
}

impl<'a> BuildXML for LevelText<'a> {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.level_text(self.val).build()
}
}

#[cfg(test)]
mod tests {

use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;

#[test]
fn test_level_text() {
let c = LevelText::new("%4.");
let b = c.build();
assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:lvlText w:val="%4." />"#);
}
}
12 changes: 12 additions & 0 deletions docx-core/src/documents/elements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@ mod insert;
mod italic;
mod italic_cs;
mod justification;
mod level;
mod level_jc;
mod level_text;
mod name;
mod next;
mod number_format;
mod numbering;
mod paragraph;
mod paragraph_property;
mod paragraph_style;
mod q_format;
mod run;
mod run_property;
mod run_property_default;
mod start;
mod style;
mod sz;
mod sz_cs;
Expand Down Expand Up @@ -73,15 +79,21 @@ pub use insert::*;
pub use italic::*;
pub use italic_cs::*;
pub use justification::*;
pub use level::*;
pub use level_jc::*;
pub use level_text::*;
pub use name::*;
pub use next::*;
pub use number_format::*;
pub use numbering::*;
pub use paragraph::*;
pub use paragraph_property::*;
pub use paragraph_style::*;
pub use q_format::*;
pub use run::*;
pub use run_property::*;
pub use run_property_default::*;
pub use start::*;
pub use style::*;
pub use sz::*;
pub use sz_cs::*;
Expand Down
39 changes: 39 additions & 0 deletions docx-core/src/documents/elements/number_format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::documents::BuildXML;
use crate::xml_builder::*;

#[derive(Debug, Clone)]
pub struct NumberFormat<'a> {
val: &'a str,
}

impl<'a> NumberFormat<'a> {
pub fn new(val: &'a str) -> Self {
Self { val }
}
}

impl<'a> BuildXML for NumberFormat<'a> {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.number_format(self.val).build()
}
}

#[cfg(test)]
mod tests {

use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;

#[test]
fn test_start() {
let c = NumberFormat::new("decimal");
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:numFmt w:val="decimal" />"#
);
}
}
64 changes: 64 additions & 0 deletions docx-core/src/documents/elements/numbering.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::documents::{BuildXML, Level};
use crate::xml_builder::*;

#[derive(Debug, Clone)]
pub struct Numbering<'a> {
id: &'a str,
levels: Vec<Level<'a>>,
}

impl<'a> Numbering<'a> {
pub fn new(id: &'a str) -> Self {
Self { id, levels: vec![] }
}

pub fn add_level(mut self, level: Level<'a>) -> Self {
self.levels.push(level);
self
}
}

impl<'a> BuildXML for Numbering<'a> {
fn build(&self) -> Vec<u8> {
let mut b = XMLBuilder::new();
b = b.open_abstract_num(self.id);
for l in &self.levels {
b = b.add_child(l);
}
b.close()
.open_num(self.id)
.abstract_num_id(self.id)
.close()
.build()
}
}

#[cfg(test)]
mod tests {

use super::*;
#[cfg(test)]
use crate::documents::{Level, LevelJc, LevelText, NumberFormat, Start};
use pretty_assertions::assert_eq;
use std::str;

#[test]
fn test_numbering() {
let mut c = Numbering::new("0");
c = c.add_level(Level::new(
1,
Start::new(1),
NumberFormat::new("decimal"),
LevelText::new("%4."),
LevelJc::new("left"),
));
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:abstractNum w:abstractNumId="0"><w:lvl w:ilvl="1"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%4." /><w:lvlJc w:val="left" /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr></w:lvl></w:abstractNum>
<w:num w:numId="0">
<w:abstractNumId w:val="0" />
</w:num>"#
);
}
}
36 changes: 36 additions & 0 deletions docx-core/src/documents/elements/start.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::documents::BuildXML;
use crate::xml_builder::*;

#[derive(Debug, Clone)]
pub struct Start {
val: usize,
}

impl Start {
pub fn new(val: usize) -> Start {
Start { val }
}
}

impl BuildXML for Start {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.start(self.val).build()
}
}

#[cfg(test)]
mod tests {

use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;

#[test]
fn test_start() {
let c = Start::new(1);
let b = c.build();
assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:start w:val="1" />"#);
}
}
2 changes: 2 additions & 0 deletions docx-core/src/documents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod document_rels;
mod elements;
mod font_table;
mod history_id;
mod numberings;
mod rels;
mod settings;
mod styles;
Expand All @@ -22,6 +23,7 @@ pub use document::*;
pub use document_rels::*;
pub use elements::*;
pub use font_table::*;
pub use numberings::*;
pub use rels::*;
pub use settings::*;
pub use styles::*;
Expand Down
Loading

0 comments on commit 9a1b92f

Please sign in to comment.