1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
mod build_xml;
mod comments;
mod content_types;
mod doc_props;
mod document;
mod document_rels;
mod elements;
mod font_table;
mod history_id;
mod numberings;
mod pic_id;
mod rels;
mod settings;
mod styles;
mod xml_docx;
pub(crate) use build_xml::BuildXML;
pub(crate) use history_id::HistoryId;
pub(crate) use pic_id::*;
pub use comments::*;
pub use content_types::*;
pub use doc_props::*;
pub use document::*;
pub use document_rels::*;
pub use elements::*;
pub use font_table::*;
pub use numberings::*;
pub use rels::*;
pub use settings::*;
pub use styles::*;
pub use xml_docx::*;
use serde::Serialize;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Docx {
pub content_type: ContentTypes,
pub rels: Rels,
pub document_rels: DocumentRels,
pub doc_props: DocProps,
pub styles: Styles,
pub document: Document,
pub comments: Comments,
pub numberings: Numberings,
pub settings: Settings,
pub font_table: FontTable,
pub media: Vec<(usize, Vec<u8>)>,
}
impl Default for Docx {
fn default() -> Self {
let content_type = ContentTypes::new().set_default();
let rels = Rels::new().set_default();
let doc_props = DocProps::new(CorePropsConfig::new());
let styles = Styles::new();
let document = Document::new();
let document_rels = DocumentRels::new();
let settings = Settings::new();
let font_table = FontTable::new();
let comments = Comments::new();
let numberings = Numberings::new();
let media = vec![];
Docx {
content_type,
rels,
doc_props,
styles,
document,
comments,
document_rels,
settings,
font_table,
numberings,
media,
}
}
}
impl Docx {
pub fn new() -> Docx {
Default::default()
}
pub fn document(mut self, d: Document) -> Docx {
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;
}
}
}
}
self.document = d;
self
}
pub fn styles(mut self, s: Styles) -> Self {
self.styles = s;
self
}
pub fn numberings(mut self, n: Numberings) -> Self {
self.numberings = n;
self
}
pub fn add_paragraph(mut self, p: Paragraph) -> Docx {
if p.has_numbering {
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 {
self.document_rels.has_numberings = true;
}
self.document = self.document.add_table(t);
self
}
pub fn add_abstract_numbering(mut self, num: AbstractNumbering) -> Docx {
self.numberings = self.numberings.add_abstract_numbering(num);
self
}
pub fn add_numbering(mut self, num: Numbering) -> Docx {
self.numberings = self.numberings.add_numbering(num);
self
}
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
}
pub fn build(&mut self) -> XMLDocx {
self.update_comments();
let (image_ids, images) = self.create_images();
self.document_rels.image_ids = image_ids;
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(),
comments: self.comments.build(),
document_rels: self.document_rels.build(),
settings: self.settings.build(),
font_table: self.font_table.build(),
numberings: self.numberings.build(),
media: images,
}
}
pub fn json(&mut self) -> String {
self.update_comments();
serde_json::to_string_pretty(&self).unwrap()
}
fn update_comments(&mut self) {
let mut comments: Vec<Comment> = vec![];
for child in &self.document.children {
match child {
DocumentChild::Paragraph(paragraph) => {
for child in ¶graph.children {
if let ParagraphChild::CommentStart(c) = child {
comments.push(c.comment());
}
}
}
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 {
comments.push(c.comment());
}
}
}
}
}
}
}
}
}
}
if !comments.is_empty() {
self.document_rels.has_comments = true;
}
self.comments.add_comments(comments);
}
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)
}
}