support: num style link (#43)

* support: num style link

* chore: update README

* chore: update snap
main
bokuweb 2020-02-26 20:26:32 +09:00 committed by GitHub
parent c7c6584d7e
commit ff44dfb7fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 107 additions and 13 deletions

View File

@ -106,3 +106,7 @@ writeFileSync("hello.docx", buf);
- [x] HIstory
- [ ] Table of contents
- [ ] Section
## Requirements
- wasm-pack 0.9.1+

View File

@ -7,18 +7,35 @@ use serde::Serialize;
#[serde(rename_all = "camelCase")]
pub struct AbstractNumbering {
id: usize,
style_link: Option<String>,
num_style_link: Option<String>,
levels: Vec<Level>,
}
impl AbstractNumbering {
pub fn new(id: usize) -> Self {
Self { id, levels: vec![] }
Self {
id,
style_link: None,
num_style_link: None,
levels: vec![],
}
}
pub fn add_level(mut self, level: Level) -> Self {
self.levels.push(level);
self
}
pub fn num_style_link(mut self, link: impl Into<String>) -> Self {
self.num_style_link = Some(link.into());
self
}
pub fn style_link(mut self, link: impl Into<String>) -> Self {
self.style_link = Some(link.into());
self
}
}
impl BuildXML for AbstractNumbering {
@ -58,4 +75,22 @@ mod tests {
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>"#
);
}
#[test]
fn test_numbering_json() {
let mut c = AbstractNumbering::new(0);
c = c
.add_level(Level::new(
1,
Start::new(1),
NumberFormat::new("decimal"),
LevelText::new("%4."),
LevelJc::new("left"),
))
.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":"Normal","numberingProperty":null,"alignment":null,"indent":null}}]}"#
);
}
}

View File

@ -34,9 +34,19 @@ impl FromXML for Numberings {
attributes, name, ..
}) => {
let e = XMLElement::from_str(&name.local_name).unwrap();
if let XMLElement::Level = e {
let l = Level::read(&mut parser, &attributes)?;
abs_num = abs_num.add_level(l);
match e {
XMLElement::Level => {
let l = Level::read(&mut parser, &attributes)?;
abs_num = abs_num.add_level(l);
}
XMLElement::StyleLink => {
abs_num = abs_num.style_link(&attributes[0].value)
}
XMLElement::NumStyleLink => {
abs_num =
abs_num.num_style_link(&attributes[0].value)
}
_ => {}
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
@ -112,8 +122,7 @@ mod tests {
#[test]
fn test_numberings_from_xml() {
let xml =
r#"<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
let xml = r#"<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" >
<w:abstractNum w:abstractNumId="0" w15:restartNumberingAfterBreak="0">
<w:multiLevelType w:val="hybridMultilevel"></w:multiLevelType>
@ -150,4 +159,44 @@ mod tests {
.add_numbering(Numbering::new(1, 0));
assert_eq!(n, nums)
}
#[test]
fn test_numberings_from_xml_with_num_style_link() {
let xml = r#"<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" >
<w:abstractNum w:abstractNumId="0">
<w:multiLevelType w:val="hybridMultilevel"/>
<w:numStyleLink w:val="style1"/>
</w:abstractNum>
<w:num w:numId="1">
<w:abstractNumId w:val="0"></w:abstractNumId>
</w:num>
</w:numbering>"#;
let n = Numberings::from_xml(xml.as_bytes()).unwrap();
let mut nums = Numberings::new();
nums = nums
.add_abstract_numbering(AbstractNumbering::new(0).num_style_link("style1"))
.add_numbering(Numbering::new(1, 0));
assert_eq!(n, nums)
}
#[test]
fn test_numberings_from_xml_with_style_link() {
let xml = r#"<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" >
<w:abstractNum w:abstractNumId="0">
<w:multiLevelType w:val="hybridMultilevel"/>
<w:styleLink w:val="style1"/>
</w:abstractNum>
<w:num w:numId="1">
<w:abstractNumId w:val="0"></w:abstractNumId>
</w:num>
</w:numbering>"#;
let n = Numberings::from_xml(xml.as_bytes()).unwrap();
let mut nums = Numberings::new();
nums = nums
.add_abstract_numbering(AbstractNumbering::new(0).style_link("style1"))
.add_numbering(Numbering::new(1, 0));
assert_eq!(n, nums)
}
}

View File

@ -76,6 +76,8 @@ pub enum XMLElement {
NumberFormat,
LevelText,
LevelJustification,
StyleLink,
NumStyleLink,
Unsupported,
}
@ -151,6 +153,8 @@ impl FromStr for XMLElement {
"numFmt" => Ok(XMLElement::NumberFormat),
"lvlText" => Ok(XMLElement::LevelText),
"lvlJc" => Ok(XMLElement::LevelJustification),
"numStyleLink" => Ok(XMLElement::NumStyleLink),
"styleLink" => Ok(XMLElement::StyleLink),
_ => Ok(XMLElement::Unsupported),
}
}

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

@ -13,6 +13,8 @@ export type LevelJSON = {
export type AbstractNumberingJSON = {
id: number;
levels: LevelJSON[];
numStyleLink: string | null;
styleLink: string | null;
};
export type NumberingJSON = {

View File

@ -1,6 +1,6 @@
{
"name": "docx-wasm",
"version": "0.0.27",
"version": "0.0.35",
"main": "dist/node/index.js",
"browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>",