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

View File

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

View File

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

View File

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

View File

@ -5,13 +5,19 @@ use crate::xml_builder::*;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TableRow { pub struct TableRow {
pub cells: Vec<TableCell>, pub cells: Vec<TableCell>,
pub(crate) has_numbering: bool,
property: TableRowProperty, property: TableRowProperty,
} }
impl TableRow { impl TableRow {
pub fn new(cells: Vec<TableCell>) -> TableRow { pub fn new(cells: Vec<TableCell>) -> TableRow {
let property = TableRowProperty::new(); 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 { 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.document = self.document.add_paragraph(p);
self self
} }
pub fn add_table(mut self, t: Table) -> Docx { 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.document = self.document.add_table(t);
self self
} }
@ -151,7 +161,6 @@ impl Docx {
} }
} }
} }
_ => {}
} }
} }
// If this document has comments, set comments.xml to document_rels. // 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 { fn create_default_numbering() -> Numbering {
Numbering::new(0) Numbering::new(1)
.add_level( .add_level(
Level::new( Level::new(
0, 0,

View File

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