Skip to content

Commit aefba38

Browse files
authored
Add sdt in header footer (#683)
* fix: read structured data tag in header/footer * spec: add spec
1 parent 366ff60 commit aefba38

File tree

10 files changed

+4434
-10
lines changed

10 files changed

+4434
-10
lines changed

docx-core/src/documents/footer.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ impl Footer {
3838
pub enum FooterChild {
3939
Paragraph(Box<Paragraph>),
4040
Table(Box<Table>),
41+
StructuredDataTag(Box<StructuredDataTag>),
4142
}
4243

4344
impl Serialize for FooterChild {
@@ -58,6 +59,12 @@ impl Serialize for FooterChild {
5859
t.serialize_field("data", c)?;
5960
t.end()
6061
}
62+
FooterChild::StructuredDataTag(ref r) => {
63+
let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
64+
t.serialize_field("type", "structuredDataTag")?;
65+
t.serialize_field("data", r)?;
66+
t.end()
67+
}
6168
}
6269
}
6370
}
@@ -71,6 +78,7 @@ impl BuildXML for Footer {
7178
match c {
7279
FooterChild::Paragraph(p) => b = b.add_child(p),
7380
FooterChild::Table(t) => b = b.add_child(t),
81+
FooterChild::StructuredDataTag(t) => b = b.add_child(t),
7482
}
7583
}
7684
b.close().build()

docx-core/src/documents/header.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,22 @@ impl Header {
3232
self.children.push(HeaderChild::Table(Box::new(t)));
3333
self
3434
}
35+
36+
pub fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
37+
if t.has_numbering {
38+
self.has_numbering = true
39+
}
40+
self.children
41+
.push(HeaderChild::StructuredDataTag(Box::new(t)));
42+
self
43+
}
3544
}
3645

3746
#[derive(Debug, Clone, PartialEq)]
3847
pub enum HeaderChild {
3948
Paragraph(Box<Paragraph>),
4049
Table(Box<Table>),
50+
StructuredDataTag(Box<StructuredDataTag>),
4151
}
4252

4353
impl Serialize for HeaderChild {
@@ -58,6 +68,12 @@ impl Serialize for HeaderChild {
5868
t.serialize_field("data", c)?;
5969
t.end()
6070
}
71+
HeaderChild::StructuredDataTag(ref r) => {
72+
let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
73+
t.serialize_field("type", "structuredDataTag")?;
74+
t.serialize_field("data", r)?;
75+
t.end()
76+
}
6177
}
6278
}
6379
}
@@ -71,6 +87,7 @@ impl BuildXML for Header {
7187
match c {
7288
HeaderChild::Paragraph(p) => b = b.add_child(p),
7389
HeaderChild::Table(t) => b = b.add_child(t),
90+
HeaderChild::StructuredDataTag(t) => b = b.add_child(t),
7491
}
7592
}
7693
b.close().build()

docx-core/src/documents/mod.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,26 @@ impl Docx {
920920
Some("header"),
921921
);
922922
}
923+
HeaderChild::StructuredDataTag(tag) => {
924+
for child in tag.children.iter_mut() {
925+
if let StructuredDataTagChild::Paragraph(paragraph) = child {
926+
collect_images_from_paragraph(
927+
paragraph,
928+
&mut images,
929+
&mut image_bufs,
930+
Some("header"),
931+
);
932+
}
933+
if let StructuredDataTagChild::Table(table) = child {
934+
collect_images_from_table(
935+
table,
936+
&mut images,
937+
&mut image_bufs,
938+
Some("header"),
939+
);
940+
}
941+
}
942+
}
923943
}
924944
}
925945
header_images[0] = images;
@@ -945,6 +965,26 @@ impl Docx {
945965
Some("header"),
946966
);
947967
}
968+
HeaderChild::StructuredDataTag(tag) => {
969+
for child in tag.children.iter_mut() {
970+
if let StructuredDataTagChild::Paragraph(paragraph) = child {
971+
collect_images_from_paragraph(
972+
paragraph,
973+
&mut images,
974+
&mut image_bufs,
975+
Some("header"),
976+
);
977+
}
978+
if let StructuredDataTagChild::Table(table) = child {
979+
collect_images_from_table(
980+
table,
981+
&mut images,
982+
&mut image_bufs,
983+
Some("header"),
984+
);
985+
}
986+
}
987+
}
948988
}
949989
}
950990
header_images[1] = images;
@@ -970,6 +1010,26 @@ impl Docx {
9701010
Some("header"),
9711011
);
9721012
}
1013+
HeaderChild::StructuredDataTag(tag) => {
1014+
for child in tag.children.iter_mut() {
1015+
if let StructuredDataTagChild::Paragraph(paragraph) = child {
1016+
collect_images_from_paragraph(
1017+
paragraph,
1018+
&mut images,
1019+
&mut image_bufs,
1020+
Some("header"),
1021+
);
1022+
}
1023+
if let StructuredDataTagChild::Table(table) = child {
1024+
collect_images_from_table(
1025+
table,
1026+
&mut images,
1027+
&mut image_bufs,
1028+
Some("header"),
1029+
);
1030+
}
1031+
}
1032+
}
9731033
}
9741034
}
9751035
header_images[2] = images;
@@ -1002,6 +1062,26 @@ impl Docx {
10021062
Some("footer"),
10031063
);
10041064
}
1065+
FooterChild::StructuredDataTag(tag) => {
1066+
for child in tag.children.iter_mut() {
1067+
if let StructuredDataTagChild::Paragraph(paragraph) = child {
1068+
collect_images_from_paragraph(
1069+
paragraph,
1070+
&mut images,
1071+
&mut image_bufs,
1072+
Some("header"),
1073+
);
1074+
}
1075+
if let StructuredDataTagChild::Table(table) = child {
1076+
collect_images_from_table(
1077+
table,
1078+
&mut images,
1079+
&mut image_bufs,
1080+
Some("header"),
1081+
);
1082+
}
1083+
}
1084+
}
10051085
}
10061086
}
10071087
footer_images[0] = images;
@@ -1027,6 +1107,26 @@ impl Docx {
10271107
Some("footer"),
10281108
);
10291109
}
1110+
FooterChild::StructuredDataTag(tag) => {
1111+
for child in tag.children.iter_mut() {
1112+
if let StructuredDataTagChild::Paragraph(paragraph) = child {
1113+
collect_images_from_paragraph(
1114+
paragraph,
1115+
&mut images,
1116+
&mut image_bufs,
1117+
Some("header"),
1118+
);
1119+
}
1120+
if let StructuredDataTagChild::Table(table) = child {
1121+
collect_images_from_table(
1122+
table,
1123+
&mut images,
1124+
&mut image_bufs,
1125+
Some("header"),
1126+
);
1127+
}
1128+
}
1129+
}
10301130
}
10311131
}
10321132
footer_images[1] = images;
@@ -1052,6 +1152,26 @@ impl Docx {
10521152
Some("footer"),
10531153
);
10541154
}
1155+
FooterChild::StructuredDataTag(tag) => {
1156+
for child in tag.children.iter_mut() {
1157+
if let StructuredDataTagChild::Paragraph(paragraph) = child {
1158+
collect_images_from_paragraph(
1159+
paragraph,
1160+
&mut images,
1161+
&mut image_bufs,
1162+
Some("header"),
1163+
);
1164+
}
1165+
if let StructuredDataTagChild::Table(table) = child {
1166+
collect_images_from_table(
1167+
table,
1168+
&mut images,
1169+
&mut image_bufs,
1170+
Some("header"),
1171+
);
1172+
}
1173+
}
1174+
}
10551175
}
10561176
}
10571177
footer_images[2] = images;

docx-core/src/reader/footer.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ impl FromXML for Footer {
1919
let e = XMLElement::from_str(&name.local_name).unwrap();
2020
match e {
2121
XMLElement::Paragraph => {
22-
let p = Paragraph::read(&mut parser, &attributes)?;
23-
footer = footer.add_paragraph(p);
22+
if let Ok(p) = Paragraph::read(&mut parser, &attributes) {
23+
footer = footer.add_paragraph(p);
24+
}
2425
continue;
2526
}
2627
XMLElement::Table => {
27-
let t = Table::read(&mut parser, &attributes)?;
28-
footer = footer.add_table(t);
28+
if let Ok(t) = Table::read(&mut parser, &attributes) {
29+
footer = footer.add_table(t);
30+
}
2931
continue;
3032
}
3133
_ => {}

docx-core/src/reader/header.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ impl FromXML for Header {
1919
let e = XMLElement::from_str(&name.local_name).unwrap();
2020
match e {
2121
XMLElement::Paragraph => {
22-
let p = Paragraph::read(&mut parser, &attributes)?;
23-
header = header.add_paragraph(p);
22+
if let Ok(p) = Paragraph::read(&mut parser, &attributes) {
23+
header = header.add_paragraph(p);
24+
}
2425
continue;
2526
}
2627
XMLElement::Table => {
27-
let t = Table::read(&mut parser, &attributes)?;
28-
header = header.add_table(t);
28+
if let Ok(t) = Table::read(&mut parser, &attributes) {
29+
header = header.add_table(t);
30+
}
31+
continue;
32+
}
33+
XMLElement::StructuredDataTag => {
34+
if let Ok(tag) = StructuredDataTag::read(&mut parser, &attributes) {
35+
header = header.add_structured_data_tag(tag);
36+
}
2937
continue;
3038
}
3139
_ => {}

docx-wasm/js/json/footer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { ParagraphJSON } from "./paragraph";
2+
import { StructuredTagJSON } from "./structured-data-tag";
23
import { TableJSON } from "./table";
34

45
export type FooterJSON = {
5-
children: (ParagraphJSON | TableJSON)[];
6+
children: (ParagraphJSON | TableJSON | StructuredTagJSON)[];
67
};
78

89
export type FooterReferenceJSON = {

docx-wasm/js/json/header.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { ParagraphJSON } from "./paragraph";
2+
import { StructuredTagJSON } from "./structured-data-tag";
23
import { TableJSON } from "./table";
34

45
export type HeaderJSON = {
5-
children: (ParagraphJSON | TableJSON)[];
6+
children: (ParagraphJSON | TableJSON | StructuredTagJSON)[];
67
};
78

89
export type HeaderReferenceJSON = {

0 commit comments

Comments
 (0)