docx-rs/docx-core/src/documents/mod.rs

160 lines
4.5 KiB
Rust
Raw Normal View History

2019-11-06 05:29:17 +02:00
mod build_xml;
2019-12-04 10:39:59 +02:00
mod comments;
2019-11-05 11:03:23 +02:00
mod content_types;
2019-11-05 12:20:40 +02:00
mod doc_props;
2019-11-07 09:08:59 +02:00
mod document;
2019-11-13 11:50:15 +02:00
mod document_rels;
2019-11-06 07:55:14 +02:00
mod elements;
2019-11-14 12:21:45 +02:00
mod font_table;
2019-11-15 11:15:43 +02:00
mod history_id;
2019-12-06 12:18:48 +02:00
mod numberings;
2019-11-05 11:03:23 +02:00
mod rels;
2019-11-14 08:54:39 +02:00
mod settings;
2019-11-07 06:57:58 +02:00
mod styles;
2019-11-07 11:45:03 +02:00
mod xml_docx;
2019-11-05 08:10:48 +02:00
2019-11-15 11:15:43 +02:00
pub(crate) use build_xml::BuildXML;
pub(crate) use history_id::HistoryId;
2019-11-06 07:55:14 +02:00
2019-12-04 10:39:59 +02:00
pub use comments::*;
2019-11-07 09:08:59 +02:00
pub use content_types::*;
pub use doc_props::*;
pub use document::*;
2019-11-13 11:50:15 +02:00
pub use document_rels::*;
2019-11-07 09:08:59 +02:00
pub use elements::*;
2019-11-14 12:21:45 +02:00
pub use font_table::*;
2019-12-06 12:18:48 +02:00
pub use numberings::*;
2019-11-07 09:08:59 +02:00
pub use rels::*;
2019-11-14 08:54:39 +02:00
pub use settings::*;
2019-11-07 09:08:59 +02:00
pub use styles::*;
2019-11-07 11:45:03 +02:00
pub use xml_docx::*;
2019-11-05 08:10:48 +02:00
2019-11-07 11:45:03 +02:00
#[derive(Debug)]
pub struct Docx {
2019-11-05 08:10:48 +02:00
content_type: ContentTypes,
2019-11-05 11:03:23 +02:00
rels: Rels,
2019-11-13 11:50:15 +02:00
document_rels: DocumentRels,
doc_props: DocProps,
pub styles: Styles,
pub document: Document,
pub comments: Comments,
pub numberings: Numberings,
pub settings: Settings,
pub font_table: FontTable,
2019-11-05 08:10:48 +02:00
}
impl Default for Docx {
2019-11-07 10:31:04 +02:00
fn default() -> Self {
2019-11-05 08:10:48 +02:00
let content_type = ContentTypes::new();
2019-11-05 11:03:23 +02:00
let rels = Rels::new();
2019-11-14 18:50:30 +02:00
let doc_props = DocProps::new(CorePropsConfig::new());
2019-11-07 10:31:04 +02:00
let styles = Styles::new();
let document = Document::new();
2019-11-13 11:50:15 +02:00
let document_rels = DocumentRels::new();
2019-11-14 08:54:39 +02:00
let settings = Settings::new();
2019-11-14 12:21:45 +02:00
let font_table = FontTable::new();
2019-12-04 10:39:59 +02:00
let comments = Comments::new();
2019-12-06 12:18:48 +02:00
let numberings = Numberings::new();
2019-11-07 09:08:59 +02:00
Docx {
2019-11-05 12:20:40 +02:00
content_type,
rels,
doc_props,
2019-11-07 10:31:04 +02:00
styles,
document,
2019-12-04 10:39:59 +02:00
comments,
2019-11-13 11:50:15 +02:00
document_rels,
2019-11-14 08:54:39 +02:00
settings,
2019-11-14 12:21:45 +02:00
font_table,
2019-12-06 12:18:48 +02:00
numberings,
2019-11-07 10:31:04 +02:00
}
}
}
impl Docx {
pub fn new() -> Docx {
2019-11-07 10:31:04 +02:00
Default::default()
}
pub fn add_paragraph(mut self, p: Paragraph) -> Docx {
2019-11-07 10:31:04 +02:00
self.document = self.document.add_paragraph(p);
self
}
pub fn add_table(mut self, t: Table) -> Docx {
2019-11-13 06:55:58 +02:00
self.document = self.document.add_table(t);
self
}
pub fn add_numbering(mut self, num: Numbering) -> Docx {
2019-12-06 12:18:48 +02:00
self.numberings = self.numberings.add_numbering(num);
self
}
2019-12-14 17:39:15 +02:00
pub fn created_at(mut self, date: &str) -> Self {
self.doc_props = self.doc_props.created_at(date);
self
}
pub fn updated_at(mut self, date: &str) -> Self {
self.doc_props = self.doc_props.updated_at(date);
self
}
2019-12-05 08:44:18 +02:00
pub fn build(&mut self) -> XMLDocx {
self.update_comments();
2019-11-07 10:31:04 +02:00
XMLDocx {
content_type: self.content_type.build(),
rels: self.rels.build(),
doc_props: self.doc_props.build(),
styles: self.styles.build(),
document: self.document.build(),
2019-12-05 08:44:18 +02:00
comments: self.comments.build(),
2019-11-13 11:50:15 +02:00
document_rels: self.document_rels.build(),
2019-11-14 08:54:39 +02:00
settings: self.settings.build(),
2019-11-14 12:21:45 +02:00
font_table: self.font_table.build(),
2019-12-06 12:18:48 +02:00
numberings: self.numberings.build(),
2019-11-05 12:20:40 +02:00
}
2019-11-05 08:10:48 +02:00
}
2019-12-05 08:44:18 +02:00
2019-12-05 09:00:32 +02:00
// Traverse and clone comments from document and add to comments node.
2019-12-05 08:44:18 +02:00
fn update_comments(&mut self) {
let mut comments: Vec<Comment> = vec![];
2019-12-05 08:44:18 +02:00
for child in &self.document.children {
match child {
2019-12-05 09:00:32 +02:00
DocumentChild::Paragraph(paragraph) => {
for child in &paragraph.children {
2019-12-05 08:44:18 +02:00
match child {
ParagraphChild::CommentStart(c) => {
comments.push(c.comment());
}
_ => {}
}
}
}
2019-12-05 09:00:32 +02:00
DocumentChild::Table(table) => {
for row in &table.rows {
for cell in &row.cells {
for content in &cell.contents {
match content {
TableCellContent::Paragraph(paragraph) => {
for child in &paragraph.children {
match child {
ParagraphChild::CommentStart(c) => {
comments.push(c.comment());
}
_ => {}
}
}
}
}
}
}
}
}
2019-12-05 08:44:18 +02:00
_ => {}
}
}
self.comments.add_comments(comments);
}
2019-11-05 08:10:48 +02:00
}