Support suff (#76)

* feat: Add suff writer

* feat: Add suff reader
main
bokuweb 2020-06-03 16:35:12 +09:00 committed by GitHub
parent 69aedcb9d2
commit 880f9802bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 91 additions and 12 deletions

View File

@ -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"}]}"#
);
}
}

View File

@ -14,6 +14,7 @@ pub struct Level {
pub jc: LevelJc,
pub pstyle: Option<String>,
pub paragraph_property: ParagraphProperty,
pub suffix: LevelSuffixType,
}
impl Level {
@ -32,6 +33,7 @@ impl Level {
jc,
pstyle: None,
paragraph_property: ParagraphProperty::new(),
suffix: LevelSuffixType::Tab,
}
}
@ -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()
}
}
@ -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>"#
);
}
}

View File

@ -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>(
@ -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();
@ -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());
}
@ -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);
}

View File

@ -79,6 +79,7 @@ pub enum XMLElement {
Num,
Start,
NumberFormat,
Suffix,
LevelText,
LevelJustification,
StyleLink,
@ -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),

View File

@ -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),
}
}
}

View File

@ -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::*;
@ -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::*;

View File

@ -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,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -7,6 +7,7 @@ export type LevelJSON = {
text: string;
jc: string;
pstyle: string | null;
suffix: 'tab' | 'nothing' | 'space';
paragraphProperty: ParagraphPropertyJSON;
};