fix: recursive style (#475)

main
bokuweb 2022-05-19 16:35:29 +09:00 committed by GitHub
parent 19c5dbe913
commit 585c2b1770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
41 changed files with 332 additions and 79 deletions

View File

@ -1,7 +1,7 @@
use docx_rs::*;
pub fn main() -> Result<(), DocxError> {
let path = std::path::Path::new("./output/hello.docx");
let path = std::path::Path::new("./output/examples/hello.docx");
let file = std::fs::File::create(&path).unwrap();
Docx::new()
.add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello")))

View File

@ -1,6 +1,9 @@
use serde::{Serialize, Serializer};
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone, PartialEq)]
pub struct Next {
val: String,
}
@ -11,6 +14,15 @@ impl Next {
}
}
impl Serialize for Next {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.val)
}
}
impl BuildXML for Next {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();

View File

@ -18,6 +18,7 @@ pub struct Style {
pub table_property: TableProperty,
pub table_cell_property: TableCellProperty,
pub based_on: Option<BasedOn>,
pub next: Option<Next>,
}
impl Default for Style {
@ -34,6 +35,7 @@ impl Default for Style {
table_property: TableProperty::new(),
table_cell_property: TableCellProperty::new(),
based_on: None,
next: None,
}
}
}
@ -58,6 +60,11 @@ impl Style {
self
}
pub fn next(mut self, next: impl Into<String>) -> Self {
self.next = Some(Next::new(next));
self
}
pub fn size(mut self, size: usize) -> Self {
self.run_property = self.run_property.size(size);
self
@ -141,13 +148,21 @@ impl BuildXML for Style {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
// Set "Normal" as default if you need change these values please fix it
b.open_style(self.style_type, &self.style_id)
let mut b = b
.open_style(self.style_type, &self.style_id)
.add_child(&self.name)
.add_child(&self.run_property)
.add_child(&self.paragraph_property)
.add_child(&BasedOn::new("Normal"))
.add_child(&Next::new("Normal"))
.add_child(&QFormat::new())
.add_child(&self.paragraph_property);
if let Some(ref based_on) = self.based_on {
b = b.add_child(based_on)
}
if let Some(ref next) = self.next {
b = b.add_child(next)
}
b.add_child(&QFormat::new())
.add_optional_child(&self.based_on)
.close()
.build()
@ -168,7 +183,7 @@ mod tests {
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:style w:type="paragraph" w:styleId="Heading"><w:name w:val="Heading1" /><w:rPr /><w:pPr><w:rPr /></w:pPr><w:basedOn w:val="Normal" /><w:next w:val="Normal" /><w:qFormat /></w:style>"#
r#"<w:style w:type="paragraph" w:styleId="Heading"><w:name w:val="Heading1" /><w:rPr /><w:pPr><w:rPr /></w:pPr><w:qFormat /></w:style>"#
);
}
}

View File

@ -99,7 +99,7 @@ mod tests {
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:styles xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 w15"><w:docDefaults><w:rPrDefault><w:rPr /></w:rPrDefault></w:docDefaults><w:style w:type="paragraph" w:styleId="Normal"><w:name w:val="Normal" /><w:rPr /><w:pPr><w:rPr /></w:pPr><w:basedOn w:val="Normal" /><w:next w:val="Normal" /><w:qFormat /></w:style><w:style w:type="paragraph" w:styleId="Title"><w:name w:val="TitleName" /><w:rPr /><w:pPr><w:rPr /></w:pPr><w:basedOn w:val="Normal" /><w:next w:val="Normal" /><w:qFormat /></w:style></w:styles>"#
r#"<w:styles xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 w15"><w:docDefaults><w:rPrDefault><w:rPr /></w:rPrDefault></w:docDefaults><w:style w:type="paragraph" w:styleId="Normal"><w:name w:val="Normal" /><w:rPr /><w:pPr><w:rPr /></w:pPr><w:qFormat /></w:style><w:style w:type="paragraph" w:styleId="Title"><w:name w:val="TitleName" /><w:rPr /><w:pPr><w:rPr /></w:pPr><w:qFormat /></w:style></w:styles>"#
);
}

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

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

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

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

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

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

File diff suppressed because it is too large Load Diff