Add sdt in header footer (#683)

* fix: read structured data tag in header/footer

* spec: add spec
main
bokuweb 2024-03-05 11:14:49 +09:00 committed by GitHub
parent 366ff60b05
commit aefba38e68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 4434 additions and 10 deletions

View File

@ -38,6 +38,7 @@ impl Footer {
pub enum FooterChild {
Paragraph(Box<Paragraph>),
Table(Box<Table>),
StructuredDataTag(Box<StructuredDataTag>),
}
impl Serialize for FooterChild {
@ -58,6 +59,12 @@ impl Serialize for FooterChild {
t.serialize_field("data", c)?;
t.end()
}
FooterChild::StructuredDataTag(ref r) => {
let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
t.serialize_field("type", "structuredDataTag")?;
t.serialize_field("data", r)?;
t.end()
}
}
}
}
@ -71,6 +78,7 @@ impl BuildXML for Footer {
match c {
FooterChild::Paragraph(p) => b = b.add_child(p),
FooterChild::Table(t) => b = b.add_child(t),
FooterChild::StructuredDataTag(t) => b = b.add_child(t),
}
}
b.close().build()

View File

@ -32,12 +32,22 @@ impl Header {
self.children.push(HeaderChild::Table(Box::new(t)));
self
}
pub fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
if t.has_numbering {
self.has_numbering = true
}
self.children
.push(HeaderChild::StructuredDataTag(Box::new(t)));
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum HeaderChild {
Paragraph(Box<Paragraph>),
Table(Box<Table>),
StructuredDataTag(Box<StructuredDataTag>),
}
impl Serialize for HeaderChild {
@ -58,6 +68,12 @@ impl Serialize for HeaderChild {
t.serialize_field("data", c)?;
t.end()
}
HeaderChild::StructuredDataTag(ref r) => {
let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
t.serialize_field("type", "structuredDataTag")?;
t.serialize_field("data", r)?;
t.end()
}
}
}
}
@ -71,6 +87,7 @@ impl BuildXML for Header {
match c {
HeaderChild::Paragraph(p) => b = b.add_child(p),
HeaderChild::Table(t) => b = b.add_child(t),
HeaderChild::StructuredDataTag(t) => b = b.add_child(t),
}
}
b.close().build()

View File

@ -920,6 +920,26 @@ impl Docx {
Some("header"),
);
}
HeaderChild::StructuredDataTag(tag) => {
for child in tag.children.iter_mut() {
if let StructuredDataTagChild::Paragraph(paragraph) = child {
collect_images_from_paragraph(
paragraph,
&mut images,
&mut image_bufs,
Some("header"),
);
}
if let StructuredDataTagChild::Table(table) = child {
collect_images_from_table(
table,
&mut images,
&mut image_bufs,
Some("header"),
);
}
}
}
}
}
header_images[0] = images;
@ -945,6 +965,26 @@ impl Docx {
Some("header"),
);
}
HeaderChild::StructuredDataTag(tag) => {
for child in tag.children.iter_mut() {
if let StructuredDataTagChild::Paragraph(paragraph) = child {
collect_images_from_paragraph(
paragraph,
&mut images,
&mut image_bufs,
Some("header"),
);
}
if let StructuredDataTagChild::Table(table) = child {
collect_images_from_table(
table,
&mut images,
&mut image_bufs,
Some("header"),
);
}
}
}
}
}
header_images[1] = images;
@ -970,6 +1010,26 @@ impl Docx {
Some("header"),
);
}
HeaderChild::StructuredDataTag(tag) => {
for child in tag.children.iter_mut() {
if let StructuredDataTagChild::Paragraph(paragraph) = child {
collect_images_from_paragraph(
paragraph,
&mut images,
&mut image_bufs,
Some("header"),
);
}
if let StructuredDataTagChild::Table(table) = child {
collect_images_from_table(
table,
&mut images,
&mut image_bufs,
Some("header"),
);
}
}
}
}
}
header_images[2] = images;
@ -1002,6 +1062,26 @@ impl Docx {
Some("footer"),
);
}
FooterChild::StructuredDataTag(tag) => {
for child in tag.children.iter_mut() {
if let StructuredDataTagChild::Paragraph(paragraph) = child {
collect_images_from_paragraph(
paragraph,
&mut images,
&mut image_bufs,
Some("header"),
);
}
if let StructuredDataTagChild::Table(table) = child {
collect_images_from_table(
table,
&mut images,
&mut image_bufs,
Some("header"),
);
}
}
}
}
}
footer_images[0] = images;
@ -1027,6 +1107,26 @@ impl Docx {
Some("footer"),
);
}
FooterChild::StructuredDataTag(tag) => {
for child in tag.children.iter_mut() {
if let StructuredDataTagChild::Paragraph(paragraph) = child {
collect_images_from_paragraph(
paragraph,
&mut images,
&mut image_bufs,
Some("header"),
);
}
if let StructuredDataTagChild::Table(table) = child {
collect_images_from_table(
table,
&mut images,
&mut image_bufs,
Some("header"),
);
}
}
}
}
}
footer_images[1] = images;
@ -1052,6 +1152,26 @@ impl Docx {
Some("footer"),
);
}
FooterChild::StructuredDataTag(tag) => {
for child in tag.children.iter_mut() {
if let StructuredDataTagChild::Paragraph(paragraph) = child {
collect_images_from_paragraph(
paragraph,
&mut images,
&mut image_bufs,
Some("header"),
);
}
if let StructuredDataTagChild::Table(table) = child {
collect_images_from_table(
table,
&mut images,
&mut image_bufs,
Some("header"),
);
}
}
}
}
}
footer_images[2] = images;

View File

@ -19,13 +19,15 @@ impl FromXML for Footer {
let e = XMLElement::from_str(&name.local_name).unwrap();
match e {
XMLElement::Paragraph => {
let p = Paragraph::read(&mut parser, &attributes)?;
if let Ok(p) = Paragraph::read(&mut parser, &attributes) {
footer = footer.add_paragraph(p);
}
continue;
}
XMLElement::Table => {
let t = Table::read(&mut parser, &attributes)?;
if let Ok(t) = Table::read(&mut parser, &attributes) {
footer = footer.add_table(t);
}
continue;
}
_ => {}

View File

@ -19,13 +19,21 @@ impl FromXML for Header {
let e = XMLElement::from_str(&name.local_name).unwrap();
match e {
XMLElement::Paragraph => {
let p = Paragraph::read(&mut parser, &attributes)?;
if let Ok(p) = Paragraph::read(&mut parser, &attributes) {
header = header.add_paragraph(p);
}
continue;
}
XMLElement::Table => {
let t = Table::read(&mut parser, &attributes)?;
if let Ok(t) = Table::read(&mut parser, &attributes) {
header = header.add_table(t);
}
continue;
}
XMLElement::StructuredDataTag => {
if let Ok(tag) = StructuredDataTag::read(&mut parser, &attributes) {
header = header.add_structured_data_tag(tag);
}
continue;
}
_ => {}

View File

@ -1,8 +1,9 @@
import { ParagraphJSON } from "./paragraph";
import { StructuredTagJSON } from "./structured-data-tag";
import { TableJSON } from "./table";
export type FooterJSON = {
children: (ParagraphJSON | TableJSON)[];
children: (ParagraphJSON | TableJSON | StructuredTagJSON)[];
};
export type FooterReferenceJSON = {

View File

@ -1,8 +1,9 @@
import { ParagraphJSON } from "./paragraph";
import { StructuredTagJSON } from "./structured-data-tag";
import { TableJSON } from "./table";
export type HeaderJSON = {
children: (ParagraphJSON | TableJSON)[];
children: (ParagraphJSON | TableJSON | StructuredTagJSON)[];
};
export type HeaderReferenceJSON = {

File diff suppressed because it is too large Load Diff

View File

@ -206,6 +206,14 @@ describe("reader", () => {
const json = w.readDocx(buffer);
expect(json).toMatchSnapshot();
});
test("should read page num in header", () => {
const buffer = readFileSync(
"../fixtures/page_num_in_header/page_num_in_header.docx"
);
const json = w.readDocx(buffer);
expect(json).toMatchSnapshot();
});
});
describe("writer", () => {