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
use crate::{
DeleteChild, DrawingData, InsertChild, Paragraph, ParagraphChild, RunChild,
StructuredDataTagChild, Table, TableCellContent, TableChild, TableRowChild, TocContent,
};
pub(crate) fn collect_images_from_paragraph(
paragraph: &mut Paragraph,
images: &mut Vec<(String, String)>,
image_bufs: &mut Vec<(String, Vec<u8>)>,
id_prefix: Option<&str>,
) {
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 {
let b = std::mem::take(&mut pic.image);
let buf = image_bufs.iter().find(|x| x.0 == pic.id || x.1 == b);
let pic_id = if let Some(prefix) = id_prefix {
format!("{}{}", prefix, pic.id)
} else {
pic.id.clone()
};
if buf.as_ref().is_none() {
images.push((
pic_id.clone(),
// For now only png supported
format!("media/{}.png", pic_id),
));
image_bufs.push((pic_id.clone(), b));
pic.id = pic_id;
} else {
pic.id = buf.unwrap().0.clone();
}
}
}
}
} else if let ParagraphChild::Insert(ins) = child {
for child in &mut ins.children {
match child {
InsertChild::Run(run) => {
for child in &mut run.children {
if let RunChild::Drawing(d) = child {
if let Some(DrawingData::Pic(pic)) = &mut d.data {
images.push((
pic.id.clone(),
// For now only png supported
format!("media/{}.png", pic.id),
));
let b = std::mem::take(&mut pic.image);
image_bufs.push((pic.id.clone(), b));
}
}
}
}
InsertChild::Delete(del) => {
for d in &mut del.children {
if let DeleteChild::Run(run) = d {
for child in &mut run.children {
if let RunChild::Drawing(d) = child {
if let Some(DrawingData::Pic(pic)) = &mut d.data {
images.push((
pic.id.clone(),
// For now only png supported
format!("media/{}.png", pic.id),
));
let b = std::mem::take(&mut pic.image);
image_bufs.push((pic.id.clone(), b));
}
}
}
}
}
}
_ => {}
}
}
} else if let ParagraphChild::Delete(del) = child {
for d in &mut del.children {
if let DeleteChild::Run(run) = d {
for child in &mut run.children {
if let RunChild::Drawing(d) = child {
if let Some(DrawingData::Pic(pic)) = &mut d.data {
images.push((
pic.id.clone(),
// For now only png supported
format!("media/{}.png", pic.id),
));
let b = std::mem::take(&mut pic.image);
image_bufs.push((pic.id.clone(), b));
}
}
}
}
}
}
}
}
pub(crate) fn collect_images_from_table(
table: &mut Table,
images: &mut Vec<(String, String)>,
image_bufs: &mut Vec<(String, Vec<u8>)>,
id_prefix: Option<&str>,
) {
for TableChild::TableRow(row) in &mut table.rows {
for TableRowChild::TableCell(cell) in &mut row.cells {
for content in &mut cell.children {
match content {
TableCellContent::Paragraph(paragraph) => {
collect_images_from_paragraph(paragraph, images, image_bufs, id_prefix);
}
TableCellContent::Table(table) => {
collect_images_from_table(table, images, image_bufs, id_prefix)
}
TableCellContent::StructuredDataTag(tag) => {
for child in &mut tag.children {
if let StructuredDataTagChild::Paragraph(paragraph) = child {
collect_images_from_paragraph(
paragraph, images, image_bufs, id_prefix,
);
}
if let StructuredDataTagChild::Table(table) = child {
collect_images_from_table(table, images, image_bufs, id_prefix);
}
}
}
TableCellContent::TableOfContents(t) => {
for child in &mut t.before_contents {
if let TocContent::Paragraph(paragraph) = child {
collect_images_from_paragraph(
paragraph, images, image_bufs, id_prefix,
);
}
if let TocContent::Table(table) = child {
collect_images_from_table(table, images, image_bufs, id_prefix);
}
}
for child in &mut t.after_contents {
if let TocContent::Paragraph(paragraph) = child {
collect_images_from_paragraph(
paragraph, images, image_bufs, id_prefix,
);
}
if let TocContent::Table(table) = child {
collect_images_from_table(table, images, image_bufs, id_prefix);
}
}
}
}
}
}
}
}