2020-08-13 19:57:59 +03:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2019-11-06 05:29:17 +02:00
|
|
|
mod build_xml;
|
2019-12-04 10:39:59 +02:00
|
|
|
mod comments;
|
2020-08-13 19:57:59 +03:00
|
|
|
mod comments_extended;
|
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;
|
2020-08-13 11:27:45 +03:00
|
|
|
mod header;
|
|
|
|
mod header_id;
|
2019-11-15 11:15:43 +02:00
|
|
|
mod history_id;
|
2019-12-06 12:18:48 +02:00
|
|
|
mod numberings;
|
2020-08-13 19:57:59 +03:00
|
|
|
mod paragraph_id;
|
2020-05-14 06:01:20 +03:00
|
|
|
mod pic_id;
|
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;
|
2020-08-13 19:57:59 +03:00
|
|
|
pub(crate) use paragraph_id::*;
|
2020-05-14 06:01:20 +03:00
|
|
|
pub(crate) use pic_id::*;
|
2019-11-06 07:55:14 +02:00
|
|
|
|
2019-12-04 10:39:59 +02:00
|
|
|
pub use comments::*;
|
2020-08-13 19:57:59 +03:00
|
|
|
pub use comments_extended::*;
|
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::*;
|
2020-08-13 11:27:45 +03:00
|
|
|
pub use header::*;
|
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
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-12-08 21:14:27 +02:00
|
|
|
pub struct Docx {
|
2020-02-11 10:01:39 +02:00
|
|
|
pub content_type: ContentTypes,
|
|
|
|
pub rels: Rels,
|
|
|
|
pub document_rels: DocumentRels,
|
|
|
|
pub doc_props: DocProps,
|
2019-12-11 07:12:22 +02:00
|
|
|
pub styles: Styles,
|
|
|
|
pub document: Document,
|
|
|
|
pub comments: Comments,
|
|
|
|
pub numberings: Numberings,
|
|
|
|
pub settings: Settings,
|
|
|
|
pub font_table: FontTable,
|
2020-05-14 06:01:20 +03:00
|
|
|
pub media: Vec<(usize, Vec<u8>)>,
|
2020-08-13 11:27:45 +03:00
|
|
|
pub header: Header,
|
2020-08-13 19:57:59 +03:00
|
|
|
pub comments_extended: CommentsExtended,
|
2019-11-05 08:10:48 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Default for Docx {
|
2019-11-07 10:31:04 +02:00
|
|
|
fn default() -> Self {
|
2020-02-11 10:01:39 +02:00
|
|
|
let content_type = ContentTypes::new().set_default();
|
2020-02-11 15:06:54 +02:00
|
|
|
let rels = Rels::new().set_default();
|
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();
|
2020-05-14 06:01:20 +03:00
|
|
|
let media = vec![];
|
2020-08-13 11:27:45 +03:00
|
|
|
let header = Header::new();
|
2020-08-13 19:57:59 +03:00
|
|
|
let comments_extended = CommentsExtended::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,
|
2020-05-14 06:01:20 +03:00
|
|
|
media,
|
2020-08-13 11:27:45 +03:00
|
|
|
header,
|
2020-08-13 19:57:59 +03:00
|
|
|
comments_extended,
|
2019-11-07 10:31:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Docx {
|
|
|
|
pub fn new() -> Docx {
|
2019-11-07 10:31:04 +02:00
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
pub fn document(mut self, d: Document) -> Docx {
|
2020-02-12 09:03:11 +02:00
|
|
|
for child in &self.document.children {
|
|
|
|
match child {
|
|
|
|
DocumentChild::Paragraph(paragraph) => {
|
|
|
|
if paragraph.has_numbering {
|
|
|
|
self.document_rels.has_numberings = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DocumentChild::Table(table) => {
|
|
|
|
if table.has_numbering {
|
|
|
|
self.document_rels.has_numberings = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-11 10:01:39 +02:00
|
|
|
self.document = d;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-02-12 09:03:11 +02:00
|
|
|
pub fn styles(mut self, s: Styles) -> Self {
|
|
|
|
self.styles = s;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-02-12 12:19:38 +02:00
|
|
|
pub fn numberings(mut self, n: Numberings) -> Self {
|
|
|
|
self.numberings = n;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn add_paragraph(mut self, p: Paragraph) -> Docx {
|
2019-12-16 04:36:04 +02:00
|
|
|
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;
|
|
|
|
}
|
2019-11-07 10:31:04 +02:00
|
|
|
self.document = self.document.add_paragraph(p);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn add_table(mut self, t: Table) -> Docx {
|
2019-12-16 04:36:04 +02:00
|
|
|
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;
|
|
|
|
}
|
2019-11-13 06:55:58 +02:00
|
|
|
self.document = self.document.add_table(t);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:27:45 +03:00
|
|
|
pub fn add_header_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.header = self.header.add_paragraph(p);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-02-11 17:21:26 +02:00
|
|
|
pub fn add_abstract_numbering(mut self, num: AbstractNumbering) -> Docx {
|
|
|
|
self.numberings = self.numberings.add_abstract_numbering(num);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-19 18:53:35 +03:00
|
|
|
pub fn doc_id(mut self, id: &str) -> Self {
|
|
|
|
self.settings = self.settings.doc_id(id);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:44:18 +02:00
|
|
|
pub fn build(&mut self) -> XMLDocx {
|
|
|
|
self.update_comments();
|
2020-05-14 06:01:20 +03:00
|
|
|
let (image_ids, images) = self.create_images();
|
|
|
|
|
|
|
|
self.document_rels.image_ids = image_ids;
|
|
|
|
|
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(),
|
2020-05-14 06:01:20 +03:00
|
|
|
media: images,
|
2020-08-13 11:27:45 +03:00
|
|
|
header: self.header.build(),
|
2020-08-13 19:57:59 +03:00
|
|
|
comments_extended: self.comments_extended.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
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
pub fn json(&mut self) -> String {
|
|
|
|
self.update_comments();
|
2020-02-12 12:19:38 +02:00
|
|
|
serde_json::to_string_pretty(&self).unwrap()
|
2020-02-11 10:01:39 +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) {
|
2019-12-08 21:14:27 +02:00
|
|
|
let mut comments: Vec<Comment> = vec![];
|
2020-08-13 19:57:59 +03:00
|
|
|
let mut comments_extended: Vec<CommentExtended> = vec![];
|
|
|
|
|
|
|
|
let mut comment_map: HashMap<usize, String> = HashMap::new();
|
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 ¶graph.children {
|
2020-01-24 10:57:14 +02:00
|
|
|
if let ParagraphChild::CommentStart(c) = child {
|
2020-08-13 19:57:59 +03:00
|
|
|
let comment = c.comment();
|
|
|
|
let para_id = comment.paragraph.id.clone();
|
|
|
|
comment_map.insert(comment.id(), para_id.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DocumentChild::Table(table) => {
|
|
|
|
for row in &table.rows {
|
|
|
|
for cell in &row.cells {
|
|
|
|
for content in &cell.children {
|
|
|
|
match content {
|
|
|
|
TableCellContent::Paragraph(paragraph) => {
|
|
|
|
for child in ¶graph.children {
|
|
|
|
if let ParagraphChild::CommentStart(c) = child {
|
|
|
|
let comment = c.comment();
|
|
|
|
let para_id = comment.paragraph.id.clone();
|
|
|
|
comment_map.insert(comment.id(), para_id.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for child in &self.document.children {
|
|
|
|
match child {
|
|
|
|
DocumentChild::Paragraph(paragraph) => {
|
|
|
|
for child in ¶graph.children {
|
|
|
|
if let ParagraphChild::CommentStart(c) = child {
|
|
|
|
let comment = c.comment();
|
|
|
|
let para_id = comment.paragraph.id.clone();
|
2020-01-24 10:57:14 +02:00
|
|
|
comments.push(c.comment());
|
2020-08-13 19:57:59 +03:00
|
|
|
let comment_extended = CommentExtended::new(para_id);
|
|
|
|
if let Some(parent_comment_id) = comment.parent_comment_id {
|
|
|
|
let parent_para_id =
|
|
|
|
comment_map.get(&parent_comment_id).unwrap().clone();
|
|
|
|
comments_extended
|
|
|
|
.push(comment_extended.parent_paragraph_id(parent_para_id));
|
|
|
|
} else {
|
|
|
|
comments_extended.push(comment_extended);
|
|
|
|
}
|
2019-12-05 08:44:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 09:00:32 +02:00
|
|
|
DocumentChild::Table(table) => {
|
|
|
|
for row in &table.rows {
|
|
|
|
for cell in &row.cells {
|
2020-02-11 10:01:39 +02:00
|
|
|
for content in &cell.children {
|
2019-12-05 09:00:32 +02:00
|
|
|
match content {
|
|
|
|
TableCellContent::Paragraph(paragraph) => {
|
|
|
|
for child in ¶graph.children {
|
2020-01-24 10:57:14 +02:00
|
|
|
if let ParagraphChild::CommentStart(c) = child {
|
2020-08-13 20:11:20 +03:00
|
|
|
let comment = c.comment();
|
|
|
|
let para_id = comment.paragraph.id.clone();
|
2020-01-24 10:57:14 +02:00
|
|
|
comments.push(c.comment());
|
2020-08-13 20:11:20 +03:00
|
|
|
let comment_extended =
|
|
|
|
CommentExtended::new(para_id);
|
|
|
|
if let Some(parent_comment_id) =
|
|
|
|
comment.parent_comment_id
|
|
|
|
{
|
|
|
|
let parent_para_id = comment_map
|
|
|
|
.get(&parent_comment_id)
|
|
|
|
.unwrap()
|
|
|
|
.clone();
|
|
|
|
comments_extended.push(
|
|
|
|
comment_extended
|
|
|
|
.parent_paragraph_id(parent_para_id),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
comments_extended.push(comment_extended);
|
|
|
|
}
|
2019-12-05 09:00:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 08:44:18 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-15 19:19:30 +02:00
|
|
|
// If this document has comments, set comments.xml to document_rels.
|
|
|
|
// This is because comments.xml without comment cause an error on word online.
|
2020-01-24 10:57:14 +02:00
|
|
|
if !comments.is_empty() {
|
2019-12-15 19:19:30 +02:00
|
|
|
self.document_rels.has_comments = true;
|
|
|
|
}
|
2020-08-13 19:57:59 +03:00
|
|
|
|
|
|
|
self.comments_extended
|
|
|
|
.add_comments_extended(comments_extended);
|
|
|
|
|
2019-12-05 08:44:18 +02:00
|
|
|
self.comments.add_comments(comments);
|
|
|
|
}
|
2020-05-14 06:01:20 +03:00
|
|
|
|
|
|
|
// Traverse and collect images from document.
|
|
|
|
fn create_images(&mut self) -> (Vec<usize>, Vec<(usize, Vec<u8>)>) {
|
|
|
|
let mut image_ids: Vec<usize> = vec![];
|
|
|
|
let mut images: Vec<(usize, Vec<u8>)> = vec![];
|
|
|
|
|
|
|
|
for child in &mut self.document.children {
|
|
|
|
match child {
|
|
|
|
DocumentChild::Paragraph(paragraph) => {
|
|
|
|
for child in &mut paragraph.children {
|
|
|
|
if let ParagraphChild::Run(run) = child {
|
|
|
|
for child in &mut run.children {
|
|
|
|
if let RunChild::Drawing(d) = child {
|
|
|
|
if let Some(DrawingData::Pic(pic)) = &mut d.data {
|
|
|
|
image_ids.push(pic.id);
|
|
|
|
let b = std::mem::replace(&mut pic.image, vec![]);
|
|
|
|
images.push((pic.id, b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DocumentChild::Table(table) => {
|
|
|
|
for row in &mut table.rows {
|
|
|
|
for cell in &mut row.cells {
|
|
|
|
for content in &mut cell.children {
|
|
|
|
match content {
|
|
|
|
TableCellContent::Paragraph(paragraph) => {
|
|
|
|
for child in &mut paragraph.children {
|
|
|
|
if let ParagraphChild::Run(run) = child {
|
|
|
|
for child in &mut run.children {
|
|
|
|
if let RunChild::Drawing(d) = child {
|
|
|
|
if let Some(DrawingData::Pic(pic)) =
|
|
|
|
&mut d.data
|
|
|
|
{
|
|
|
|
image_ids.push(pic.id);
|
|
|
|
let b = std::mem::replace(
|
|
|
|
&mut pic.image,
|
|
|
|
vec![],
|
|
|
|
);
|
|
|
|
images.push((pic.id, b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(image_ids, images)
|
|
|
|
}
|
2019-11-05 08:10:48 +02:00
|
|
|
}
|