Skip to content

Commit

Permalink
fix: num pages
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed May 16, 2024
1 parent 8beaf9e commit 95d84d3
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docx-core/src/documents/elements/instr_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Serialize for InstrText {
}
InstrText::NUMPAGES(ref s) => {
let mut t = serializer.serialize_struct("NUMPAGES", 2)?;
t.serialize_field("type", "numpages")?;
t.serialize_field("type", "numPages")?;
t.serialize_field("data", s)?;
t.end()
}
Expand Down
14 changes: 2 additions & 12 deletions docx-core/src/documents/elements/num_pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,11 @@ mod tests {
use std::str;

#[test]
fn test_page() {
fn test_num_pages() {
let b = NumPages::new().build();
assert_eq!(
str::from_utf8(&b).unwrap(),
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>
</w:sdt>"#
);
}

#[test]
fn test_page_with_wrap() {
let b = NumPages::new().wrap("none").x_align("left").build();
assert_eq!(
str::from_utf8(&b).unwrap(),
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>
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>
</w:sdt>"#
);
}
Expand Down
13 changes: 13 additions & 0 deletions docx-core/src/documents/footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ impl Footer {
self
}

pub fn add_num_pages(mut self, p: NumPages) -> Self {
self.children.push(FooterChild::NumPages(Box::new(p)));
self
}

/// reader only
pub(crate) fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
if t.has_numbering {
Expand All @@ -54,6 +59,7 @@ pub enum FooterChild {
Paragraph(Box<Paragraph>),
Table(Box<Table>),
PageNum(Box<PageNum>),
NumPages(Box<NumPages>),
StructuredDataTag(Box<StructuredDataTag>),
}

Expand Down Expand Up @@ -81,6 +87,12 @@ impl Serialize for FooterChild {
t.serialize_field("data", r)?;
t.end()
}
FooterChild::NumPages(ref r) => {
let mut t = serializer.serialize_struct("NumPages", 2)?;
t.serialize_field("type", "numPages")?;
t.serialize_field("data", r)?;
t.end()
}
FooterChild::StructuredDataTag(ref r) => {
let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
t.serialize_field("type", "structuredDataTag")?;
Expand All @@ -101,6 +113,7 @@ impl BuildXML for Footer {
FooterChild::Paragraph(p) => b = b.add_child(p),
FooterChild::Table(t) => b = b.add_child(t),
FooterChild::PageNum(p) => b = b.add_child(p),
FooterChild::NumPages(p) => b = b.add_child(p),
FooterChild::StructuredDataTag(t) => b = b.add_child(t),
}
}
Expand Down
13 changes: 13 additions & 0 deletions docx-core/src/documents/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ impl Header {
self
}

pub fn add_num_pages(mut self, p: NumPages) -> Self {
self.children.push(HeaderChild::NumPages(Box::new(p)));
self
}

/// reader only
pub(crate) fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
if t.has_numbering {
Expand All @@ -54,6 +59,7 @@ pub enum HeaderChild {
Paragraph(Box<Paragraph>),
Table(Box<Table>),
PageNum(Box<PageNum>),
NumPages(Box<NumPages>),
StructuredDataTag(Box<StructuredDataTag>),
}

Expand Down Expand Up @@ -81,6 +87,12 @@ impl Serialize for HeaderChild {
t.serialize_field("data", r)?;
t.end()
}
HeaderChild::NumPages(ref r) => {
let mut t = serializer.serialize_struct("numPages", 2)?;
t.serialize_field("type", "numPages")?;
t.serialize_field("data", r)?;
t.end()
}
HeaderChild::StructuredDataTag(ref r) => {
let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
t.serialize_field("type", "structuredDataTag")?;
Expand All @@ -101,6 +113,7 @@ impl BuildXML for Header {
HeaderChild::Paragraph(p) => b = b.add_child(p),
HeaderChild::Table(t) => b = b.add_child(t),
HeaderChild::PageNum(p) => b = b.add_child(p),
HeaderChild::NumPages(p) => b = b.add_child(p),
HeaderChild::StructuredDataTag(t) => b = b.add_child(t),
}
}
Expand Down
6 changes: 6 additions & 0 deletions docx-core/src/documents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ impl Docx {
);
}
HeaderChild::PageNum(_) => {}
HeaderChild::NumPages(_) => {}
HeaderChild::StructuredDataTag(tag) => {
for child in tag.children.iter_mut() {
if let StructuredDataTagChild::Paragraph(paragraph) = child {
Expand Down Expand Up @@ -972,6 +973,7 @@ impl Docx {
);
}
HeaderChild::PageNum(_) => {}
HeaderChild::NumPages(_) => {}
HeaderChild::StructuredDataTag(tag) => {
for child in tag.children.iter_mut() {
if let StructuredDataTagChild::Paragraph(paragraph) = child {
Expand Down Expand Up @@ -1018,6 +1020,7 @@ impl Docx {
);
}
HeaderChild::PageNum(_) => {}
HeaderChild::NumPages(_) => {}
HeaderChild::StructuredDataTag(tag) => {
for child in tag.children.iter_mut() {
if let StructuredDataTagChild::Paragraph(paragraph) = child {
Expand Down Expand Up @@ -1063,6 +1066,7 @@ impl Docx {
);
}
FooterChild::PageNum(_) => {}
FooterChild::NumPages(_) => {}
FooterChild::Table(table) => {
collect_images_from_table(
table,
Expand Down Expand Up @@ -1109,6 +1113,7 @@ impl Docx {
);
}
FooterChild::PageNum(_) => {}
FooterChild::NumPages(_) => {}
FooterChild::Table(table) => {
collect_images_from_table(
table,
Expand Down Expand Up @@ -1155,6 +1160,7 @@ impl Docx {
);
}
FooterChild::PageNum(_) => {}
FooterChild::NumPages(_) => {}
FooterChild::Table(table) => {
collect_images_from_table(
table,
Expand Down
8 changes: 7 additions & 1 deletion docx-wasm/js/footer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { NumPages } from "./num-pages";
import { PageNum } from "./page-num";
import { Paragraph } from "./paragraph";
import { Table } from "./table";

export class Footer {
hasNumberings = false;
children: (Paragraph | Table | PageNum)[] = [];
children: (Paragraph | Table | PageNum | NumPages)[] = [];

addParagraph(p: Paragraph) {
if (p.hasNumberings) {
Expand All @@ -26,4 +27,9 @@ export class Footer {
this.children.push(p);
return this;
}

addNumPages(p: NumPages) {
this.children.push(p);
return this;
}
}
8 changes: 7 additions & 1 deletion docx-wasm/js/header.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { NumPages } from "./num-pages";
import { PageNum } from "./page-num";
import { Paragraph } from "./paragraph";
import { Table } from "./table";

export class Header {
hasNumberings = false;
children: (Paragraph | Table | PageNum)[] = [];
children: (Paragraph | Table | PageNum | NumPages)[] = [];

addParagraph(p: Paragraph) {
if (p.hasNumberings) {
Expand All @@ -26,4 +27,9 @@ export class Header {
this.children.push(p);
return this;
}

addNumPages(p: NumPages) {
this.children.push(p);
return this;
}
}
32 changes: 26 additions & 6 deletions docx-wasm/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { DocGridType, DocxJSON } from "./json";

import * as wasm from "./pkg";
import { Level } from "./level";
import { PageNum } from "./page-num";
import { NumPages } from "./num-pages";

export class Docx {
children: (
Expand Down Expand Up @@ -424,9 +426,12 @@ export class Docx {
header = header.add_paragraph(build(c));
} else if (c instanceof Table) {
header = header.add_table(c.build());
} else {
} else if (c instanceof PageNum) {
const p = c.build();
header = header.add_page_num(p);
} else if (c instanceof NumPages) {
const p = c.build();
header = header.add_num_pages(p);
}
});
docx = docx.header(header);
Expand All @@ -439,9 +444,12 @@ export class Docx {
header = header.add_paragraph(build(c));
} else if (c instanceof Table) {
header = header.add_table(c.build());
} else {
} else if (c instanceof PageNum) {
const p = c.build();
header = header.add_page_num(p);
} else if (c instanceof NumPages) {
const p = c.build();
header = header.add_num_pages(p);
}
});
docx = docx.first_header(header);
Expand All @@ -454,9 +462,12 @@ export class Docx {
header = header.add_paragraph(build(c));
} else if (c instanceof Table) {
header = header.add_table(c.build());
} else {
} else if (c instanceof PageNum) {
const p = c.build();
header = header.add_page_num(p);
} else if (c instanceof NumPages) {
const p = c.build();
header = header.add_num_pages(p);
}
});
docx = docx.even_header(header);
Expand All @@ -469,9 +480,12 @@ export class Docx {
footer = footer.add_paragraph(build(c));
} else if (c instanceof Table) {
footer = footer.add_table(c.build());
} else {
} else if (c instanceof PageNum) {
const p = c.build();
footer = footer.add_page_num(p);
} else if (c instanceof NumPages) {
const p = c.build();
footer = footer.add_num_pages(p);
}
});
docx = docx.footer(footer);
Expand All @@ -484,9 +498,12 @@ export class Docx {
footer = footer.add_paragraph(build(c));
} else if (c instanceof Table) {
footer = footer.add_table(c.build());
} else {
} else if (c instanceof PageNum) {
const p = c.build();
footer = footer.add_page_num(p);
} else if (c instanceof NumPages) {
const p = c.build();
footer = footer.add_num_pages(p);
}
});
docx = docx.first_footer(footer);
Expand All @@ -499,9 +516,12 @@ export class Docx {
footer = footer.add_paragraph(build(c));
} else if (c instanceof Table) {
footer = footer.add_table(c.build());
} else {
} else if (c instanceof PageNum) {
const p = c.build();
footer = footer.add_page_num(p);
} else if (c instanceof NumPages) {
const p = c.build();
footer = footer.add_num_pages(p);
}
});
docx = docx.even_footer(footer);
Expand Down

0 comments on commit 95d84d3

Please sign in to comment.