Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support suff #76

Merged
merged 2 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docx-core/src/documents/elements/abstract_numbering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mod tests {
.num_style_link("style1");
assert_eq!(
serde_json::to_string(&c).unwrap(),
r#"{"id":0,"styleLink":null,"numStyleLink":"style1","levels":[{"level":1,"start":1,"format":"decimal","text":"%4.","jc":"left","pstyle":null,"paragraphProperty":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null},"style":null,"numberingProperty":null,"alignment":null,"indent":null}}]}"#
r#"{"id":0,"styleLink":null,"numStyleLink":"style1","levels":[{"level":1,"start":1,"format":"decimal","text":"%4.","jc":"left","pstyle":null,"paragraphProperty":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null},"style":null,"numberingProperty":null,"alignment":null,"indent":null},"suffix":"tab"}]}"#
);
}
}
36 changes: 32 additions & 4 deletions docx-core/src/documents/elements/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Level {
pub jc: LevelJc,
pub pstyle: Option<String>,
pub paragraph_property: ParagraphProperty,
pub suffix: LevelSuffixType,
}

impl Level {
Expand All @@ -32,6 +33,7 @@ impl Level {
jc,
pstyle: None,
paragraph_property: ParagraphProperty::new(),
suffix: LevelSuffixType::Tab,
}
}

Expand All @@ -52,19 +54,28 @@ impl Level {
self.pstyle = Some(style_id.into());
self
}

pub fn suffix(mut self, s: LevelSuffixType) -> Self {
self.suffix = s;
self
}
}

impl BuildXML for Level {
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
let mut b = 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()
.add_child(&self.paragraph_property);

if self.suffix != LevelSuffixType::Tab {
b = b.suffix(&self.suffix.to_string());
}

b.close().build()
}
}

Expand Down Expand Up @@ -108,4 +119,21 @@ mod tests {
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:rPr /><w:ind w:left="320" w:right="0" w:hanging="200" /></w:pPr></w:lvl>"#
);
}
#[test]
fn test_level_with_suff() {
let b = Level::new(
1,
Start::new(1),
NumberFormat::new("decimal"),
LevelText::new("%4."),
LevelJc::new("left"),
)
.suffix(LevelSuffixType::Space)
.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:rPr /></w:pPr><w:suff w:val="space" />
</w:lvl>"#
);
}
}
8 changes: 7 additions & 1 deletion docx-core/src/reader/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use xml::attribute::OwnedAttribute;
use xml::reader::{EventReader, XmlEvent};

use super::*;
use crate::types::*;

impl ElementReader for Level {
fn read<R: Read>(
Expand All @@ -23,6 +24,7 @@ impl ElementReader for Level {
let mut indent_end = None;
let mut start_chars = None;
let mut has_indent = false;
let mut suffix = LevelSuffixType::Tab;

loop {
let e = r.next();
Expand All @@ -42,6 +44,9 @@ impl ElementReader for Level {
XMLElement::NumberFormat => {
num_fmt = NumberFormat::new(attributes[0].value.clone());
}
XMLElement::Suffix => {
suffix = LevelSuffixType::from_str(&attributes[0].value)?;
}
XMLElement::LevelText => {
level_text = LevelText::new(attributes[0].value.clone());
}
Expand All @@ -62,7 +67,8 @@ impl ElementReader for Level {
Ok(XmlEvent::EndElement { name, .. }) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
if let XMLElement::Level = e {
let mut l = Level::new(level, start, num_fmt, level_text, jc);
let mut l =
Level::new(level, start, num_fmt, level_text, jc).suffix(suffix);
if let Some(style_id) = style_id {
l = l.paragraph_style(style_id);
}
Expand Down
2 changes: 2 additions & 0 deletions docx-core/src/reader/xml_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub enum XMLElement {
Num,
Start,
NumberFormat,
Suffix,
LevelText,
LevelJustification,
StyleLink,
Expand Down Expand Up @@ -215,6 +216,7 @@ impl FromStr for XMLElement {
"num" => Ok(XMLElement::Num),
"start" => Ok(XMLElement::Start),
"numFmt" => Ok(XMLElement::NumberFormat),
"suff" => Ok(XMLElement::Suffix),
"lvlText" => Ok(XMLElement::LevelText),
"lvlJc" => Ok(XMLElement::LevelJustification),
"numStyleLink" => Ok(XMLElement::NumStyleLink),
Expand Down
38 changes: 38 additions & 0 deletions docx-core/src/types/level_suffix_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::fmt;
use wasm_bindgen::prelude::*;

use serde::Serialize;

use super::errors;
use std::str::FromStr;

#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum LevelSuffixType {
Nothing,
Space,
Tab,
}

impl fmt::Display for LevelSuffixType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LevelSuffixType::Nothing => write!(f, "nothing"),
LevelSuffixType::Space => write!(f, "space"),
LevelSuffixType::Tab => write!(f, "tab"),
}
}
}

impl FromStr for LevelSuffixType {
type Err = errors::TypeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"nothing" => Ok(LevelSuffixType::Nothing),
"space" => Ok(LevelSuffixType::Space),
"tab" => Ok(LevelSuffixType::Tab),
_ => Ok(LevelSuffixType::Tab),
}
}
}
2 changes: 2 additions & 0 deletions docx-core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod vertical_align_type;
pub mod vertical_merge_type;
pub mod width_type;
pub mod emu;
pub mod level_suffix_type;

pub use alignment_type::*;
pub use border_position::*;
Expand All @@ -25,3 +26,4 @@ pub use vertical_align_type::*;
pub use emu::*;
pub use vertical_merge_type::*;
pub use width_type::*;
pub use level_suffix_type::*;
2 changes: 2 additions & 0 deletions docx-core/src/xml_builder/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ impl XMLBuilder {
// i.e. <w:u ... >
closed_with_str!(underline, "w:u");

closed_with_str!(suffix, "w:suff");

// i.e. <w:ind ... >
pub(crate) fn indent(
mut self,
Expand Down
2 changes: 1 addition & 1 deletion docx-core/tests/snapshots/lib__reader__read_hello.snap

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docx-core/tests/snapshots/lib__reader__read_numbering.snap

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docx-core/tests/snapshots/reader__read_hello.snap

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docx-core/tests/snapshots/reader__read_numbering.snap

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docx-core/tests/snapshots/reader__read_table_docx.snap

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docx-wasm/js/json/numbering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type LevelJSON = {
text: string;
jc: string;
pstyle: string | null;
suffix: 'tab' | 'nothing' | 'space';
paragraphProperty: ParagraphPropertyJSON;
};

Expand Down