Add sdt in header footer (#683)
* fix: read structured data tag in header/footer * spec: add specmain
parent
366ff60b05
commit
aefba38e68
|
@ -38,6 +38,7 @@ impl Footer {
|
||||||
pub enum FooterChild {
|
pub enum FooterChild {
|
||||||
Paragraph(Box<Paragraph>),
|
Paragraph(Box<Paragraph>),
|
||||||
Table(Box<Table>),
|
Table(Box<Table>),
|
||||||
|
StructuredDataTag(Box<StructuredDataTag>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for FooterChild {
|
impl Serialize for FooterChild {
|
||||||
|
@ -58,6 +59,12 @@ impl Serialize for FooterChild {
|
||||||
t.serialize_field("data", c)?;
|
t.serialize_field("data", c)?;
|
||||||
t.end()
|
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 {
|
match c {
|
||||||
FooterChild::Paragraph(p) => b = b.add_child(p),
|
FooterChild::Paragraph(p) => b = b.add_child(p),
|
||||||
FooterChild::Table(t) => b = b.add_child(t),
|
FooterChild::Table(t) => b = b.add_child(t),
|
||||||
|
FooterChild::StructuredDataTag(t) => b = b.add_child(t),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b.close().build()
|
b.close().build()
|
||||||
|
|
|
@ -32,12 +32,22 @@ impl Header {
|
||||||
self.children.push(HeaderChild::Table(Box::new(t)));
|
self.children.push(HeaderChild::Table(Box::new(t)));
|
||||||
self
|
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)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum HeaderChild {
|
pub enum HeaderChild {
|
||||||
Paragraph(Box<Paragraph>),
|
Paragraph(Box<Paragraph>),
|
||||||
Table(Box<Table>),
|
Table(Box<Table>),
|
||||||
|
StructuredDataTag(Box<StructuredDataTag>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for HeaderChild {
|
impl Serialize for HeaderChild {
|
||||||
|
@ -58,6 +68,12 @@ impl Serialize for HeaderChild {
|
||||||
t.serialize_field("data", c)?;
|
t.serialize_field("data", c)?;
|
||||||
t.end()
|
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 {
|
match c {
|
||||||
HeaderChild::Paragraph(p) => b = b.add_child(p),
|
HeaderChild::Paragraph(p) => b = b.add_child(p),
|
||||||
HeaderChild::Table(t) => b = b.add_child(t),
|
HeaderChild::Table(t) => b = b.add_child(t),
|
||||||
|
HeaderChild::StructuredDataTag(t) => b = b.add_child(t),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b.close().build()
|
b.close().build()
|
||||||
|
|
|
@ -920,6 +920,26 @@ impl Docx {
|
||||||
Some("header"),
|
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;
|
header_images[0] = images;
|
||||||
|
@ -945,6 +965,26 @@ impl Docx {
|
||||||
Some("header"),
|
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;
|
header_images[1] = images;
|
||||||
|
@ -970,6 +1010,26 @@ impl Docx {
|
||||||
Some("header"),
|
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;
|
header_images[2] = images;
|
||||||
|
@ -1002,6 +1062,26 @@ impl Docx {
|
||||||
Some("footer"),
|
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;
|
footer_images[0] = images;
|
||||||
|
@ -1027,6 +1107,26 @@ impl Docx {
|
||||||
Some("footer"),
|
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;
|
footer_images[1] = images;
|
||||||
|
@ -1052,6 +1152,26 @@ impl Docx {
|
||||||
Some("footer"),
|
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;
|
footer_images[2] = images;
|
||||||
|
|
|
@ -19,13 +19,15 @@ impl FromXML for Footer {
|
||||||
let e = XMLElement::from_str(&name.local_name).unwrap();
|
let e = XMLElement::from_str(&name.local_name).unwrap();
|
||||||
match e {
|
match e {
|
||||||
XMLElement::Paragraph => {
|
XMLElement::Paragraph => {
|
||||||
let p = Paragraph::read(&mut parser, &attributes)?;
|
if let Ok(p) = Paragraph::read(&mut parser, &attributes) {
|
||||||
footer = footer.add_paragraph(p);
|
footer = footer.add_paragraph(p);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
XMLElement::Table => {
|
XMLElement::Table => {
|
||||||
let t = Table::read(&mut parser, &attributes)?;
|
if let Ok(t) = Table::read(&mut parser, &attributes) {
|
||||||
footer = footer.add_table(t);
|
footer = footer.add_table(t);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
|
@ -19,13 +19,21 @@ impl FromXML for Header {
|
||||||
let e = XMLElement::from_str(&name.local_name).unwrap();
|
let e = XMLElement::from_str(&name.local_name).unwrap();
|
||||||
match e {
|
match e {
|
||||||
XMLElement::Paragraph => {
|
XMLElement::Paragraph => {
|
||||||
let p = Paragraph::read(&mut parser, &attributes)?;
|
if let Ok(p) = Paragraph::read(&mut parser, &attributes) {
|
||||||
header = header.add_paragraph(p);
|
header = header.add_paragraph(p);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
XMLElement::Table => {
|
XMLElement::Table => {
|
||||||
let t = Table::read(&mut parser, &attributes)?;
|
if let Ok(t) = Table::read(&mut parser, &attributes) {
|
||||||
header = header.add_table(t);
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import { ParagraphJSON } from "./paragraph";
|
import { ParagraphJSON } from "./paragraph";
|
||||||
|
import { StructuredTagJSON } from "./structured-data-tag";
|
||||||
import { TableJSON } from "./table";
|
import { TableJSON } from "./table";
|
||||||
|
|
||||||
export type FooterJSON = {
|
export type FooterJSON = {
|
||||||
children: (ParagraphJSON | TableJSON)[];
|
children: (ParagraphJSON | TableJSON | StructuredTagJSON)[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type FooterReferenceJSON = {
|
export type FooterReferenceJSON = {
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import { ParagraphJSON } from "./paragraph";
|
import { ParagraphJSON } from "./paragraph";
|
||||||
|
import { StructuredTagJSON } from "./structured-data-tag";
|
||||||
import { TableJSON } from "./table";
|
import { TableJSON } from "./table";
|
||||||
|
|
||||||
export type HeaderJSON = {
|
export type HeaderJSON = {
|
||||||
children: (ParagraphJSON | TableJSON)[];
|
children: (ParagraphJSON | TableJSON | StructuredTagJSON)[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type HeaderReferenceJSON = {
|
export type HeaderReferenceJSON = {
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -206,6 +206,14 @@ describe("reader", () => {
|
||||||
const json = w.readDocx(buffer);
|
const json = w.readDocx(buffer);
|
||||||
expect(json).toMatchSnapshot();
|
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", () => {
|
describe("writer", () => {
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue