Skip to content

Commit 95d84d3

Browse files
committed
fix: num pages
1 parent 8beaf9e commit 95d84d3

File tree

13 files changed

+316
-21
lines changed

13 files changed

+316
-21
lines changed

docx-core/src/documents/elements/instr_text.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Serialize for InstrText {
6666
}
6767
InstrText::NUMPAGES(ref s) => {
6868
let mut t = serializer.serialize_struct("NUMPAGES", 2)?;
69-
t.serialize_field("type", "numpages")?;
69+
t.serialize_field("type", "numPages")?;
7070
t.serialize_field("data", s)?;
7171
t.end()
7272
}

docx-core/src/documents/elements/num_pages.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,11 @@ mod tests {
186186
use std::str;
187187

188188
#[test]
189-
fn test_page() {
189+
fn test_num_pages() {
190190
let b = NumPages::new().build();
191191
assert_eq!(
192192
str::from_utf8(&b).unwrap(),
193-
r#"<w:sdt><w:sdtPr><w:rPr /></w:sdtPr><w:sdtContent><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="begin" w:dirty="false" /><w:instrText>PAGE</w:instrText><w:fldChar w:fldCharType="separate" w:dirty="false" /><w:t xml:space="preserve">1</w:t><w:fldChar w:fldCharType="end" w:dirty="false" /></w:r></w:p></w:sdtContent>
194-
</w:sdt>"#
195-
);
196-
}
197-
198-
#[test]
199-
fn test_page_with_wrap() {
200-
let b = NumPages::new().wrap("none").x_align("left").build();
201-
assert_eq!(
202-
str::from_utf8(&b).unwrap(),
203-
r#"<w:sdt><w:sdtPr><w:rPr /></w:sdtPr><w:sdtContent><w:p w14:paraId="12345678"><w:pPr><w:rPr /><w:framePr w:wrap="none" w:xAlign="left" /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="begin" w:dirty="false" /><w:instrText>PAGE</w:instrText><w:fldChar w:fldCharType="separate" w:dirty="false" /><w:t xml:space="preserve">1</w:t><w:fldChar w:fldCharType="end" w:dirty="false" /></w:r></w:p></w:sdtContent>
193+
r#"<w:sdt><w:sdtPr><w:rPr /></w:sdtPr><w:sdtContent><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="begin" w:dirty="false" /><w:instrText>NUMPAGES</w:instrText><w:fldChar w:fldCharType="separate" w:dirty="false" /><w:t xml:space="preserve">1</w:t><w:fldChar w:fldCharType="end" w:dirty="false" /></w:r></w:p></w:sdtContent>
204194
</w:sdt>"#
205195
);
206196
}

docx-core/src/documents/footer.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ impl Footer {
3838
self
3939
}
4040

41+
pub fn add_num_pages(mut self, p: NumPages) -> Self {
42+
self.children.push(FooterChild::NumPages(Box::new(p)));
43+
self
44+
}
45+
4146
/// reader only
4247
pub(crate) fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
4348
if t.has_numbering {
@@ -54,6 +59,7 @@ pub enum FooterChild {
5459
Paragraph(Box<Paragraph>),
5560
Table(Box<Table>),
5661
PageNum(Box<PageNum>),
62+
NumPages(Box<NumPages>),
5763
StructuredDataTag(Box<StructuredDataTag>),
5864
}
5965

@@ -81,6 +87,12 @@ impl Serialize for FooterChild {
8187
t.serialize_field("data", r)?;
8288
t.end()
8389
}
90+
FooterChild::NumPages(ref r) => {
91+
let mut t = serializer.serialize_struct("NumPages", 2)?;
92+
t.serialize_field("type", "numPages")?;
93+
t.serialize_field("data", r)?;
94+
t.end()
95+
}
8496
FooterChild::StructuredDataTag(ref r) => {
8597
let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
8698
t.serialize_field("type", "structuredDataTag")?;
@@ -101,6 +113,7 @@ impl BuildXML for Footer {
101113
FooterChild::Paragraph(p) => b = b.add_child(p),
102114
FooterChild::Table(t) => b = b.add_child(t),
103115
FooterChild::PageNum(p) => b = b.add_child(p),
116+
FooterChild::NumPages(p) => b = b.add_child(p),
104117
FooterChild::StructuredDataTag(t) => b = b.add_child(t),
105118
}
106119
}

docx-core/src/documents/header.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ impl Header {
3838
self
3939
}
4040

41+
pub fn add_num_pages(mut self, p: NumPages) -> Self {
42+
self.children.push(HeaderChild::NumPages(Box::new(p)));
43+
self
44+
}
45+
4146
/// reader only
4247
pub(crate) fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
4348
if t.has_numbering {
@@ -54,6 +59,7 @@ pub enum HeaderChild {
5459
Paragraph(Box<Paragraph>),
5560
Table(Box<Table>),
5661
PageNum(Box<PageNum>),
62+
NumPages(Box<NumPages>),
5763
StructuredDataTag(Box<StructuredDataTag>),
5864
}
5965

@@ -81,6 +87,12 @@ impl Serialize for HeaderChild {
8187
t.serialize_field("data", r)?;
8288
t.end()
8389
}
90+
HeaderChild::NumPages(ref r) => {
91+
let mut t = serializer.serialize_struct("numPages", 2)?;
92+
t.serialize_field("type", "numPages")?;
93+
t.serialize_field("data", r)?;
94+
t.end()
95+
}
8496
HeaderChild::StructuredDataTag(ref r) => {
8597
let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
8698
t.serialize_field("type", "structuredDataTag")?;
@@ -101,6 +113,7 @@ impl BuildXML for Header {
101113
HeaderChild::Paragraph(p) => b = b.add_child(p),
102114
HeaderChild::Table(t) => b = b.add_child(t),
103115
HeaderChild::PageNum(p) => b = b.add_child(p),
116+
HeaderChild::NumPages(p) => b = b.add_child(p),
104117
HeaderChild::StructuredDataTag(t) => b = b.add_child(t),
105118
}
106119
}

docx-core/src/documents/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,7 @@ impl Docx {
926926
);
927927
}
928928
HeaderChild::PageNum(_) => {}
929+
HeaderChild::NumPages(_) => {}
929930
HeaderChild::StructuredDataTag(tag) => {
930931
for child in tag.children.iter_mut() {
931932
if let StructuredDataTagChild::Paragraph(paragraph) = child {
@@ -972,6 +973,7 @@ impl Docx {
972973
);
973974
}
974975
HeaderChild::PageNum(_) => {}
976+
HeaderChild::NumPages(_) => {}
975977
HeaderChild::StructuredDataTag(tag) => {
976978
for child in tag.children.iter_mut() {
977979
if let StructuredDataTagChild::Paragraph(paragraph) = child {
@@ -1018,6 +1020,7 @@ impl Docx {
10181020
);
10191021
}
10201022
HeaderChild::PageNum(_) => {}
1023+
HeaderChild::NumPages(_) => {}
10211024
HeaderChild::StructuredDataTag(tag) => {
10221025
for child in tag.children.iter_mut() {
10231026
if let StructuredDataTagChild::Paragraph(paragraph) = child {
@@ -1063,6 +1066,7 @@ impl Docx {
10631066
);
10641067
}
10651068
FooterChild::PageNum(_) => {}
1069+
FooterChild::NumPages(_) => {}
10661070
FooterChild::Table(table) => {
10671071
collect_images_from_table(
10681072
table,
@@ -1109,6 +1113,7 @@ impl Docx {
11091113
);
11101114
}
11111115
FooterChild::PageNum(_) => {}
1116+
FooterChild::NumPages(_) => {}
11121117
FooterChild::Table(table) => {
11131118
collect_images_from_table(
11141119
table,
@@ -1155,6 +1160,7 @@ impl Docx {
11551160
);
11561161
}
11571162
FooterChild::PageNum(_) => {}
1163+
FooterChild::NumPages(_) => {}
11581164
FooterChild::Table(table) => {
11591165
collect_images_from_table(
11601166
table,

docx-wasm/js/footer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { NumPages } from "./num-pages";
12
import { PageNum } from "./page-num";
23
import { Paragraph } from "./paragraph";
34
import { Table } from "./table";
45

56
export class Footer {
67
hasNumberings = false;
7-
children: (Paragraph | Table | PageNum)[] = [];
8+
children: (Paragraph | Table | PageNum | NumPages)[] = [];
89

910
addParagraph(p: Paragraph) {
1011
if (p.hasNumberings) {
@@ -26,4 +27,9 @@ export class Footer {
2627
this.children.push(p);
2728
return this;
2829
}
30+
31+
addNumPages(p: NumPages) {
32+
this.children.push(p);
33+
return this;
34+
}
2935
}

docx-wasm/js/header.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { NumPages } from "./num-pages";
12
import { PageNum } from "./page-num";
23
import { Paragraph } from "./paragraph";
34
import { Table } from "./table";
45

56
export class Header {
67
hasNumberings = false;
7-
children: (Paragraph | Table | PageNum)[] = [];
8+
children: (Paragraph | Table | PageNum | NumPages)[] = [];
89

910
addParagraph(p: Paragraph) {
1011
if (p.hasNumberings) {
@@ -26,4 +27,9 @@ export class Header {
2627
this.children.push(p);
2728
return this;
2829
}
30+
31+
addNumPages(p: NumPages) {
32+
this.children.push(p);
33+
return this;
34+
}
2935
}

docx-wasm/js/index.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { DocGridType, DocxJSON } from "./json";
2525

2626
import * as wasm from "./pkg";
2727
import { Level } from "./level";
28+
import { PageNum } from "./page-num";
29+
import { NumPages } from "./num-pages";
2830

2931
export class Docx {
3032
children: (
@@ -424,9 +426,12 @@ export class Docx {
424426
header = header.add_paragraph(build(c));
425427
} else if (c instanceof Table) {
426428
header = header.add_table(c.build());
427-
} else {
429+
} else if (c instanceof PageNum) {
428430
const p = c.build();
429431
header = header.add_page_num(p);
432+
} else if (c instanceof NumPages) {
433+
const p = c.build();
434+
header = header.add_num_pages(p);
430435
}
431436
});
432437
docx = docx.header(header);
@@ -439,9 +444,12 @@ export class Docx {
439444
header = header.add_paragraph(build(c));
440445
} else if (c instanceof Table) {
441446
header = header.add_table(c.build());
442-
} else {
447+
} else if (c instanceof PageNum) {
443448
const p = c.build();
444449
header = header.add_page_num(p);
450+
} else if (c instanceof NumPages) {
451+
const p = c.build();
452+
header = header.add_num_pages(p);
445453
}
446454
});
447455
docx = docx.first_header(header);
@@ -454,9 +462,12 @@ export class Docx {
454462
header = header.add_paragraph(build(c));
455463
} else if (c instanceof Table) {
456464
header = header.add_table(c.build());
457-
} else {
465+
} else if (c instanceof PageNum) {
458466
const p = c.build();
459467
header = header.add_page_num(p);
468+
} else if (c instanceof NumPages) {
469+
const p = c.build();
470+
header = header.add_num_pages(p);
460471
}
461472
});
462473
docx = docx.even_header(header);
@@ -469,9 +480,12 @@ export class Docx {
469480
footer = footer.add_paragraph(build(c));
470481
} else if (c instanceof Table) {
471482
footer = footer.add_table(c.build());
472-
} else {
483+
} else if (c instanceof PageNum) {
473484
const p = c.build();
474485
footer = footer.add_page_num(p);
486+
} else if (c instanceof NumPages) {
487+
const p = c.build();
488+
footer = footer.add_num_pages(p);
475489
}
476490
});
477491
docx = docx.footer(footer);
@@ -484,9 +498,12 @@ export class Docx {
484498
footer = footer.add_paragraph(build(c));
485499
} else if (c instanceof Table) {
486500
footer = footer.add_table(c.build());
487-
} else {
501+
} else if (c instanceof PageNum) {
488502
const p = c.build();
489503
footer = footer.add_page_num(p);
504+
} else if (c instanceof NumPages) {
505+
const p = c.build();
506+
footer = footer.add_num_pages(p);
490507
}
491508
});
492509
docx = docx.first_footer(footer);
@@ -499,9 +516,12 @@ export class Docx {
499516
footer = footer.add_paragraph(build(c));
500517
} else if (c instanceof Table) {
501518
footer = footer.add_table(c.build());
502-
} else {
519+
} else if (c instanceof PageNum) {
503520
const p = c.build();
504521
footer = footer.add_page_num(p);
522+
} else if (c instanceof NumPages) {
523+
const p = c.build();
524+
footer = footer.add_num_pages(p);
505525
}
506526
});
507527
docx = docx.even_footer(footer);

0 commit comments

Comments
 (0)