simplify core-properties; add xsd comment

This commit is contained in:
Tom Hunkapiller
2021-05-26 09:27:05 +03:00
parent 60b7ce4785
commit e4709a7f8d
5 changed files with 62 additions and 105 deletions

View File

@ -24,8 +24,11 @@ import { eighthPointMeasureValue, hexColorValue, pointMeasureValue } from "../va
export interface IBorderOptions {
readonly style: BorderStyle;
/** Border color, in hex (eg 'FF00AA') */
readonly color?: string;
/** Size of the border in 1/8 pt */
readonly size?: number;
/** Spacing offset. Values are specified in pt */
readonly space?: number;
}

View File

@ -1,89 +0,0 @@
import { XmlComponent } from "file/xml-components";
import { DocumentAttributes } from "../document/document-attributes";
export class Title extends XmlComponent {
constructor(value: string) {
super("dc:title");
this.root.push(value);
}
}
export class Subject extends XmlComponent {
constructor(value: string) {
super("dc:subject");
this.root.push(value);
}
}
export class Creator extends XmlComponent {
constructor(value: string) {
super("dc:creator");
this.root.push(value);
}
}
export class Keywords extends XmlComponent {
constructor(value: string) {
super("cp:keywords");
this.root.push(value);
}
}
export class Description extends XmlComponent {
constructor(value: string) {
super("dc:description");
this.root.push(value);
}
}
export class LastModifiedBy extends XmlComponent {
constructor(value: string) {
super("cp:lastModifiedBy");
this.root.push(value);
}
}
export class Revision extends XmlComponent {
constructor(value: string) {
super("cp:revision");
this.root.push(value);
}
}
export abstract class DateComponent extends XmlComponent {
protected getCurrentDate(): string {
const date = new Date();
const year = date.getFullYear();
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const day = ("0" + date.getDate()).slice(-2);
const hours = ("0" + date.getHours()).slice(-2);
const minutes = ("0" + date.getMinutes()).slice(-2);
const seconds = ("0" + date.getSeconds()).slice(-2);
return year + "-" + month + "-" + day + "T" + hours + ":" + minutes + ":" + seconds + "Z";
}
}
export class Created extends DateComponent {
constructor() {
super("dcterms:created");
this.root.push(
new DocumentAttributes({
type: "dcterms:W3CDTF",
}),
);
this.root.push(this.getCurrentDate());
}
}
export class Modified extends DateComponent {
constructor() {
super("dcterms:modified");
this.root.push(
new DocumentAttributes({
type: "dcterms:W3CDTF",
}),
);
this.root.push(this.getCurrentDate());
}
}

View File

@ -27,8 +27,7 @@ describe("Properties", () => {
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["cp:coreProperties"]);
expect(tree["cp:coreProperties"]).to.be.an.instanceof(Array);
expect(Object.keys(tree["cp:coreProperties"][0])).to.deep.equal(["_attr"]);
expect(tree["cp:coreProperties"][1]).to.deep.equal({ "dc:title": ["test document"] });
expect(tree["cp:coreProperties"]).to.deep.include({ "dc:title": ["test document"] });
});
it("should create properties with all the attributes given", () => {
@ -44,9 +43,9 @@ describe("Properties", () => {
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["cp:coreProperties"]);
expect(tree["cp:coreProperties"]).to.be.an.instanceof(Array);
const key = (obj) => Object.keys(obj)[0];
const props = tree["cp:coreProperties"].map(key).sort();
expect(props).to.deep.equal([
expect(tree["cp:coreProperties"].map(key)).to.include.members([
"_attr",
"cp:keywords",
"cp:lastModifiedBy",
@ -58,7 +57,7 @@ describe("Properties", () => {
"dcterms:created",
"dcterms:modified",
]);
expect(tree["cp:coreProperties"].slice(1, -2).sort((a, b) => (key(a) < key(b) ? -1 : 1))).to.deep.equal([
expect(tree["cp:coreProperties"]).to.deep.include.members([
{ "cp:keywords": ["test docx"] },
{ "cp:lastModifiedBy": ["the author"] },
{ "cp:revision": ["123"] },

View File

@ -1,4 +1,4 @@
import { XmlComponent } from "file/xml-components";
import { StringContainer, XmlComponent } from "file/xml-components";
import { ICustomPropertyOptions } from "../custom-properties";
import { IDocumentBackgroundOptions } from "../document";
@ -7,7 +7,6 @@ import { ISectionOptions } from "../file";
import { INumberingOptions } from "../numbering";
import { Paragraph } from "../paragraph";
import { IStylesOptions } from "../styles";
import { Created, Creator, Description, Keywords, LastModifiedBy, Modified, Revision, Subject, Title } from "./components";
export interface IPropertiesOptions {
readonly sections: ISectionOptions[];
@ -36,6 +35,28 @@ export interface IPropertiesOptions {
readonly evenAndOddHeaderAndFooters?: boolean;
}
// <xs:element name="coreProperties" type="CT_CoreProperties"/>
// <xs:complexType name="CT_CoreProperties">
// <xs:all>
// <xs:element name="category" minOccurs="0" maxOccurs="1" type="xs:string"/>
// <xs:element name="contentStatus" minOccurs="0" maxOccurs="1" type="xs:string"/>
// <xs:element ref="dcterms:created" minOccurs="0" maxOccurs="1"/>
// <xs:element ref="dc:creator" minOccurs="0" maxOccurs="1"/>
// <xs:element ref="dc:description" minOccurs="0" maxOccurs="1"/>
// <xs:element ref="dc:identifier" minOccurs="0" maxOccurs="1"/>
// <xs:element name="keywords" minOccurs="0" maxOccurs="1" type="CT_Keywords"/>
// <xs:element ref="dc:language" minOccurs="0" maxOccurs="1"/>
// <xs:element name="lastModifiedBy" minOccurs="0" maxOccurs="1" type="xs:string"/>
// <xs:element name="lastPrinted" minOccurs="0" maxOccurs="1" type="xs:dateTime"/>
// <xs:element ref="dcterms:modified" minOccurs="0" maxOccurs="1"/>
// <xs:element name="revision" minOccurs="0" maxOccurs="1" type="xs:string"/>
// <xs:element ref="dc:subject" minOccurs="0" maxOccurs="1"/>
// <xs:element ref="dc:title" minOccurs="0" maxOccurs="1"/>
// <xs:element name="version" minOccurs="0" maxOccurs="1" type="xs:string"/>
// </xs:all>
// </xs:complexType>
export class CoreProperties extends XmlComponent {
constructor(options: Omit<IPropertiesOptions, "sections">) {
super("cp:coreProperties");
@ -49,27 +70,39 @@ export class CoreProperties extends XmlComponent {
}),
);
if (options.title) {
this.root.push(new Title(options.title));
this.root.push(new StringContainer("dc:title", options.title));
}
if (options.subject) {
this.root.push(new Subject(options.subject));
this.root.push(new StringContainer("dc:subject", options.subject));
}
if (options.creator) {
this.root.push(new Creator(options.creator));
this.root.push(new StringContainer("dc:creator", options.creator));
}
if (options.keywords) {
this.root.push(new Keywords(options.keywords));
this.root.push(new StringContainer("cp:keywords", options.keywords));
}
if (options.description) {
this.root.push(new Description(options.description));
this.root.push(new StringContainer("dc:description", options.description));
}
if (options.lastModifiedBy) {
this.root.push(new LastModifiedBy(options.lastModifiedBy));
this.root.push(new StringContainer("cp:lastModifiedBy", options.lastModifiedBy));
}
if (options.revision) {
this.root.push(new Revision(options.revision));
this.root.push(new StringContainer("cp:revision", options.revision));
}
this.root.push(new Created());
this.root.push(new Modified());
this.root.push(new TimestampElement("dcterms:created"));
this.root.push(new TimestampElement("dcterms:modified"));
}
}
class TimestampElement extends XmlComponent {
constructor(name: string) {
super(name);
this.root.push(
new DocumentAttributes({
type: "dcterms:W3CDTF",
}),
);
this.root.push(new Date().toISOString());
}
}

View File

@ -52,3 +52,14 @@ export class NumberValueElement extends XmlComponent {
this.root.push(new Attributes({ val }));
}
}
// Simple nodes containing text.
//
// new StringContainer("hello", "world")
// <hello>world</hello>
export class StringContainer extends XmlComponent {
constructor(name: string, val: string) {
super(name);
this.root.push(val);
}
}