fix: numbering error on word online (#15)

main
bokuweb 2019-12-16 11:36:04 +09:00 committed by GitHub
parent 43ee93aa77
commit 0af89dbc79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 45 additions and 8 deletions

View File

@ -6,6 +6,7 @@ use crate::xml_builder::*;
pub struct Document {
pub(crate) children: Vec<DocumentChild>,
pub section_property: SectionProperty,
pub has_numbering: bool,
}
#[derive(Debug, Clone)]
@ -20,11 +21,17 @@ impl Document {
}
pub fn add_paragraph(mut self, p: Paragraph) -> Self {
if p.has_numbering {
self.has_numbering = true
}
self.children.push(DocumentChild::Paragraph(p));
self
}
pub fn add_table(mut self, t: Table) -> Self {
if t.has_numbering {
self.has_numbering = true
}
self.children.push(DocumentChild::Table(t));
self
}
@ -35,6 +42,7 @@ impl Default for Document {
Self {
children: Vec::new(),
section_property: SectionProperty::new(),
has_numbering: false,
}
}
}

View File

@ -7,6 +7,7 @@ use crate::xml_builder::*;
pub struct Paragraph {
pub children: Vec<ParagraphChild>,
pub property: ParagraphProperty,
pub has_numbering: bool,
attrs: Vec<(String, String)>,
}
@ -15,6 +16,7 @@ impl Default for Paragraph {
Self {
children: Vec::new(),
property: ParagraphProperty::new(),
has_numbering: false,
attrs: Vec::new(),
}
}
@ -121,6 +123,7 @@ impl Paragraph {
pub fn numbering(mut self, id: NumberingId, level: IndentLevel) -> Self {
self.property = self.property.numbering(id, level);
self.has_numbering = true;
self
}
}

View File

@ -7,17 +7,20 @@ use crate::xml_builder::*;
pub struct Table {
pub rows: Vec<TableRow>,
pub grid: Vec<usize>,
pub(crate) has_numbering: bool,
property: TableProperty,
}
impl Table {
pub fn new(rows: Vec<TableRow>) -> Table {
let property = TableProperty::new();
let has_numbering = rows.iter().any(|c| c.has_numbering);
let grid = vec![];
Self {
property,
rows,
grid,
has_numbering,
}
}

View File

@ -7,6 +7,7 @@ use crate::xml_builder::*;
pub struct TableCell {
pub contents: Vec<TableCellContent>,
pub property: TableCellProperty,
pub has_numbering: bool,
}
#[derive(Debug, Clone)]
@ -18,10 +19,17 @@ impl TableCell {
pub fn new() -> TableCell {
let property = TableCellProperty::new();
let contents = vec![];
Self { property, contents }
Self {
property,
contents,
has_numbering: false,
}
}
pub fn add_paragraph(mut self, p: Paragraph) -> TableCell {
if p.has_numbering {
self.has_numbering = true
}
self.contents.push(TableCellContent::Paragraph(p));
self
}

View File

@ -5,13 +5,19 @@ use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct TableRow {
pub cells: Vec<TableCell>,
pub(crate) has_numbering: bool,
property: TableRowProperty,
}
impl TableRow {
pub fn new(cells: Vec<TableCell>) -> TableRow {
let property = TableRowProperty::new();
Self { property, cells }
let has_numbering = cells.iter().any(|c| c.has_numbering);
Self {
property,
cells,
has_numbering,
}
}
}

View File

@ -76,11 +76,21 @@ impl Docx {
}
pub fn add_paragraph(mut self, p: Paragraph) -> Docx {
if p.has_numbering {
// If this document has numbering, set numberings.xml to document_rels.
// This is because numberings.xml without numbering cause an error on word online.
self.document_rels.has_numberings = true;
}
self.document = self.document.add_paragraph(p);
self
}
pub fn add_table(mut self, t: Table) -> Docx {
if t.has_numbering {
// If this document has numbering, set numberings.xml to document_rels.
// This is because numberings.xml without numbering cause an error on word online.
self.document_rels.has_numberings = true;
}
self.document = self.document.add_table(t);
self
}
@ -151,7 +161,6 @@ impl Docx {
}
}
}
_ => {}
}
}
// If this document has comments, set comments.xml to document_rels.

View File

@ -37,7 +37,7 @@ impl BuildXML for Numberings {
}
fn create_default_numbering() -> Numbering {
Numbering::new(0)
Numbering::new(1)
.add_level(
Level::new(
0,

View File

@ -323,22 +323,22 @@ pub fn default_numbering() -> Result<(), DocxError> {
.add_paragraph(
Paragraph::new()
.add_run(Run::new().add_text("Hello"))
.numbering(NumberingId::new(0), IndentLevel::new(0)),
.numbering(NumberingId::new(1), IndentLevel::new(0)),
)
.add_paragraph(
Paragraph::new()
.add_run(Run::new().add_text("World!"))
.numbering(NumberingId::new(0), IndentLevel::new(1)),
.numbering(NumberingId::new(1), IndentLevel::new(1)),
)
.add_paragraph(
Paragraph::new()
.add_run(Run::new().add_text("Foooo!"))
.numbering(NumberingId::new(0), IndentLevel::new(2)),
.numbering(NumberingId::new(1), IndentLevel::new(2)),
)
.add_paragraph(
Paragraph::new()
.add_run(Run::new().add_text("Bar!"))
.numbering(NumberingId::new(0), IndentLevel::new(3)),
.numbering(NumberingId::new(1), IndentLevel::new(3)),
)
.build()
.pack(file)?;