added xmlKeys to xmlcomponent
This commit is contained in:
@ -1,8 +1,12 @@
|
|||||||
import {XmlComponent, Attributes} from "../../xml-components";
|
import {XmlComponent, Attributes} from "../../xml-components";
|
||||||
|
|
||||||
export class Columns {
|
export class Columns implements XmlComponent {
|
||||||
private cols: Array<XmlComponent>;
|
private cols: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
cols: 'w:cols'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.cols = new Array<XmlComponent>();
|
this.cols = new Array<XmlComponent>();
|
||||||
this.cols.push(new Attributes({
|
this.cols.push(new Attributes({
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import {XmlComponent, Attributes} from "../../xml-components";
|
import {XmlComponent, Attributes} from "../../xml-components";
|
||||||
|
|
||||||
export class DocumentGrid {
|
export class DocumentGrid implements XmlComponent {
|
||||||
private docGrid: Array<XmlComponent>;
|
private docGrid: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
docGrid: 'w:docGrid'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.docGrid = new Array<XmlComponent>();
|
this.docGrid = new Array<XmlComponent>();
|
||||||
this.docGrid.push(new Attributes({
|
this.docGrid.push(new Attributes({
|
||||||
|
@ -5,9 +5,13 @@ import {PageMargin} from "./page-margin";
|
|||||||
import {Columns} from "./columns";
|
import {Columns} from "./columns";
|
||||||
import {DocumentGrid} from "./doc-grid";
|
import {DocumentGrid} from "./doc-grid";
|
||||||
|
|
||||||
export class Body {
|
export class Body implements XmlComponent {
|
||||||
private body: Array<XmlComponent>;
|
private body: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
body: 'w:body'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.body = new Array<XmlComponent>();
|
this.body = new Array<XmlComponent>();
|
||||||
this.body.push(new SectionProperties());
|
this.body.push(new SectionProperties());
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import {XmlComponent, Attributes} from "../../xml-components";
|
import {XmlComponent, Attributes} from "../../xml-components";
|
||||||
|
|
||||||
export class PageMargin {
|
export class PageMargin implements XmlComponent {
|
||||||
private pgMar: Array<XmlComponent>;
|
private pgMar: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
pgMar: 'w:pgMar'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.pgMar = new Array<XmlComponent>();
|
this.pgMar = new Array<XmlComponent>();
|
||||||
this.pgMar.push(new Attributes({
|
this.pgMar.push(new Attributes({
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import {XmlComponent, Attributes} from "../../xml-components";
|
import {XmlComponent, Attributes} from "../../xml-components";
|
||||||
|
|
||||||
export class PageSize {
|
export class PageSize implements XmlComponent {
|
||||||
private pgSz: Array<XmlComponent>;
|
private pgSz: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
pgSz: 'w:pgSz'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.pgSz = new Array<XmlComponent>();
|
this.pgSz = new Array<XmlComponent>();
|
||||||
this.pgSz.push(new Attributes({
|
this.pgSz.push(new Attributes({
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import {XmlComponent, Attributes} from "../../xml-components";
|
import {XmlComponent, Attributes} from "../../xml-components";
|
||||||
|
|
||||||
export class SectionProperties {
|
export class SectionProperties implements XmlComponent {
|
||||||
private sectPr: Array<XmlComponent>;
|
private sectPr: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
sectPr: 'sectPr'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.sectPr = new Array<XmlComponent>();
|
this.sectPr = new Array<XmlComponent>();
|
||||||
this.sectPr.push(new Attributes({
|
this.sectPr.push(new Attributes({
|
||||||
|
@ -3,6 +3,10 @@ import {XmlComponent, Attributes} from "../xml-components";
|
|||||||
class Border implements XmlComponent {
|
class Border implements XmlComponent {
|
||||||
private bottom: Array<XmlComponent>;
|
private bottom: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
bottom: 'w:bottom'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.bottom = new Array<XmlComponent>();
|
this.bottom = new Array<XmlComponent>();
|
||||||
this.bottom.push(new Attributes({
|
this.bottom.push(new Attributes({
|
||||||
@ -14,9 +18,13 @@ class Border implements XmlComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ThematicBreak {
|
export class ThematicBreak implements XmlComponent {
|
||||||
private pBdr: Array<XmlComponent>;
|
private pBdr: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
pBdr: 'w:pBdr'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.pBdr = new Array<XmlComponent>();
|
this.pBdr = new Array<XmlComponent>();
|
||||||
this.pBdr.push(new Border());
|
this.pBdr.push(new Border());
|
||||||
|
@ -7,9 +7,13 @@ import {TabStop} from "../tab-stop";
|
|||||||
import {Style} from "./style";
|
import {Style} from "./style";
|
||||||
import {NumberProperties} from "./unordered-list";
|
import {NumberProperties} from "./unordered-list";
|
||||||
|
|
||||||
class Alignment {
|
class Alignment implements XmlComponent {
|
||||||
private jc: Array<XmlComponent>;
|
private jc: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
jc: 'w:jc'
|
||||||
|
}
|
||||||
|
|
||||||
constructor(type: string) {
|
constructor(type: string) {
|
||||||
this.jc = new Array<XmlComponent>();
|
this.jc = new Array<XmlComponent>();
|
||||||
this.jc.push(new Attributes({
|
this.jc.push(new Attributes({
|
||||||
@ -18,10 +22,14 @@ class Alignment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Paragraph {
|
export class Paragraph implements XmlComponent {
|
||||||
private p: Array<XmlComponent>;
|
private p: Array<XmlComponent>;
|
||||||
private properties: ParagraphProperties;
|
private properties: ParagraphProperties;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
p: 'w:p'
|
||||||
|
}
|
||||||
|
|
||||||
constructor(text?: string) {
|
constructor(text?: string) {
|
||||||
this.p = new Array<XmlComponent>();
|
this.p = new Array<XmlComponent>();
|
||||||
this.p.push(new Attributes());
|
this.p.push(new Attributes());
|
||||||
|
@ -3,6 +3,11 @@ import {Run} from "../run";
|
|||||||
|
|
||||||
class Break implements XmlComponent {
|
class Break implements XmlComponent {
|
||||||
private br: Array<XmlComponent>;
|
private br: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
br: 'w:br'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.br = new Array<XmlComponent>();
|
this.br = new Array<XmlComponent>();
|
||||||
this.br.push(new Attributes({
|
this.br.push(new Attributes({
|
||||||
|
@ -3,6 +3,10 @@ import {XmlComponent, Attributes} from "../xml-components";
|
|||||||
export class ParagraphProperties implements XmlComponent {
|
export class ParagraphProperties implements XmlComponent {
|
||||||
private pPr: Array<XmlComponent>;
|
private pPr: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
pPr: 'w:rPr'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.pPr = new Array<XmlComponent>();
|
this.pPr = new Array<XmlComponent>();
|
||||||
this.pPr.push(new Attributes());
|
this.pPr.push(new Attributes());
|
||||||
|
@ -3,6 +3,10 @@ import {XmlComponent, Attributes} from "../xml-components";
|
|||||||
export class Style implements XmlComponent {
|
export class Style implements XmlComponent {
|
||||||
private pStyle: Array<XmlComponent>;
|
private pStyle: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
pStyle: 'w:pStyle'
|
||||||
|
}
|
||||||
|
|
||||||
constructor(type: string) {
|
constructor(type: string) {
|
||||||
this.pStyle = new Array<XmlComponent>();
|
this.pStyle = new Array<XmlComponent>();
|
||||||
this.pStyle.push(new Attributes({
|
this.pStyle.push(new Attributes({
|
||||||
|
@ -4,6 +4,10 @@ import {Style} from "./style";
|
|||||||
export class NumberProperties implements XmlComponent {
|
export class NumberProperties implements XmlComponent {
|
||||||
private numPr: Array<XmlComponent>;
|
private numPr: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
numPr: 'w:numPr'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.numPr = new Array<XmlComponent>();
|
this.numPr = new Array<XmlComponent>();
|
||||||
this.numPr.push(new IndentLevel(0));
|
this.numPr.push(new IndentLevel(0));
|
||||||
@ -14,6 +18,10 @@ export class NumberProperties implements XmlComponent {
|
|||||||
export class IndentLevel implements XmlComponent {
|
export class IndentLevel implements XmlComponent {
|
||||||
private ilvl: Array<XmlComponent>;
|
private ilvl: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
ilvl: 'w:ilvl'
|
||||||
|
}
|
||||||
|
|
||||||
constructor(level: number) {
|
constructor(level: number) {
|
||||||
this.ilvl = new Array<XmlComponent>();
|
this.ilvl = new Array<XmlComponent>();
|
||||||
this.ilvl.push(new Attributes({
|
this.ilvl.push(new Attributes({
|
||||||
@ -25,6 +33,10 @@ export class IndentLevel implements XmlComponent {
|
|||||||
export class NumberId implements XmlComponent {
|
export class NumberId implements XmlComponent {
|
||||||
private ilvl: Array<XmlComponent>;
|
private ilvl: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
ilvl: 'w:ilvl'
|
||||||
|
}
|
||||||
|
|
||||||
constructor(id: number) {
|
constructor(id: number) {
|
||||||
this.ilvl = new Array<XmlComponent>();
|
this.ilvl = new Array<XmlComponent>();
|
||||||
this.ilvl.push(new Attributes({
|
this.ilvl.push(new Attributes({
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import {XmlComponent, Attributes} from "../xml-components";
|
import {XmlComponent, Attributes} from "../xml-components";
|
||||||
|
|
||||||
export class Bold {
|
export class Bold implements XmlComponent {
|
||||||
private b: Array<XmlComponent>;
|
private b: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
b: 'w:b'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.b = new Array<XmlComponent>();
|
this.b = new Array<XmlComponent>();
|
||||||
this.b.push(new Attributes({
|
this.b.push(new Attributes({
|
||||||
@ -14,6 +18,10 @@ export class Bold {
|
|||||||
export class Italics {
|
export class Italics {
|
||||||
private i: Array<XmlComponent>;
|
private i: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
i: 'w:i'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.i = new Array<XmlComponent>();
|
this.i = new Array<XmlComponent>();
|
||||||
this.i.push(new Attributes({
|
this.i.push(new Attributes({
|
||||||
@ -25,6 +33,10 @@ export class Italics {
|
|||||||
export class Underline {
|
export class Underline {
|
||||||
private u: Array<XmlComponent>;
|
private u: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
u: 'w:u'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.u = new Array<XmlComponent>();
|
this.u = new Array<XmlComponent>();
|
||||||
this.u.push(new Attributes({
|
this.u.push(new Attributes({
|
||||||
|
@ -6,6 +6,10 @@ export class Run implements XmlComponent {
|
|||||||
protected r: Array<XmlComponent>;
|
protected r: Array<XmlComponent>;
|
||||||
private properties: RunProperties;
|
private properties: RunProperties;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
r: 'w:r'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.r = new Array<XmlComponent>();
|
this.r = new Array<XmlComponent>();
|
||||||
this.properties = new RunProperties();
|
this.properties = new RunProperties();
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import {XmlComponent, Attributes} from "../xml-components";
|
import {XmlComponent, Attributes} from "../xml-components";
|
||||||
|
|
||||||
export class RunProperties {
|
export class RunProperties implements XmlComponent {
|
||||||
private rPr: Array<XmlComponent>;
|
private rPr: Array<XmlComponent>;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
rPr: 'w:rPr'
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.rPr = new Array<XmlComponent>();
|
this.rPr = new Array<XmlComponent>();
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
export class TabStop {
|
import {XmlComponent, Attributes} from "./xml-components";
|
||||||
|
|
||||||
|
export class TabStop implements XmlComponent{
|
||||||
|
xmlKeys = {}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
export interface XmlComponent {
|
export interface XmlComponent {
|
||||||
|
xmlKeys: Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AttributesProperties {
|
interface AttributesProperties {
|
||||||
@ -26,6 +26,8 @@ interface AttributesProperties {
|
|||||||
export class Attributes implements XmlComponent {
|
export class Attributes implements XmlComponent {
|
||||||
private _attrs: Object;
|
private _attrs: Object;
|
||||||
|
|
||||||
|
xmlKeys = {};
|
||||||
|
|
||||||
constructor(properties?: AttributesProperties) {
|
constructor(properties?: AttributesProperties) {
|
||||||
this._attrs = properties
|
this._attrs = properties
|
||||||
|
|
||||||
@ -38,6 +40,10 @@ export class Attributes implements XmlComponent {
|
|||||||
export class Text implements XmlComponent {
|
export class Text implements XmlComponent {
|
||||||
private t: string;
|
private t: string;
|
||||||
|
|
||||||
|
xmlKeys = {
|
||||||
|
t: 'w:t'
|
||||||
|
}
|
||||||
|
|
||||||
constructor(text: string) {
|
constructor(text: string) {
|
||||||
this.t = text;
|
this.t = text;
|
||||||
}
|
}
|
||||||
|
@ -2,55 +2,34 @@ import * as _ from "lodash";
|
|||||||
|
|
||||||
export class Formatter {
|
export class Formatter {
|
||||||
|
|
||||||
private xmlKeyDictionary = {
|
|
||||||
p: 'w:p',
|
|
||||||
t: 'w:t',
|
|
||||||
color: 'w:color',
|
|
||||||
space: 'w:space',
|
|
||||||
sz: 'w:sz',
|
|
||||||
val: 'w:val',
|
|
||||||
type: 'w:type',
|
|
||||||
ilvl: 'w:ilvl',
|
|
||||||
numId: 'w:numId',
|
|
||||||
pBdr: 'w:pBdr',
|
|
||||||
jc: 'w:jc',
|
|
||||||
r: 'w:r',
|
|
||||||
pPr: 'w:pPr',
|
|
||||||
pStyle: 'w:pStyle',
|
|
||||||
numPr: 'w:numPr',
|
|
||||||
b: 'w:b',
|
|
||||||
i: 'w:i',
|
|
||||||
u: 'w:u',
|
|
||||||
rPr: 'w:rPr'
|
|
||||||
};
|
|
||||||
|
|
||||||
format(input: any): Object {
|
format(input: any): Object {
|
||||||
var newJson = this.jsonify(input);
|
this.deepTraverseJson(input, (parent, value, key) => {
|
||||||
|
|
||||||
this.deepTraverseJson(newJson, (parent, value, key) => {
|
|
||||||
if (isNaN(key)) {
|
if (isNaN(key)) {
|
||||||
var newKey = this.getReplacementKey(key);
|
var newKey = this.getReplacementKey(parent, key);
|
||||||
parent[newKey] = parent[key];
|
|
||||||
if (newKey !== key) {
|
if (newKey !== key) {
|
||||||
|
parent[newKey] = parent[key];
|
||||||
delete parent[key];
|
delete parent[key];
|
||||||
} else {
|
} else {
|
||||||
console.error("Key is not in dictionary:" + key);
|
//console.error("Key is not in dictionary:" + key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
newJson = this.clenseProperties(newJson);
|
var newJson = this.clense(input);
|
||||||
|
|
||||||
return newJson;
|
return newJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
private clenseProperties(input: any): Object {
|
private clense(input: any): Object {
|
||||||
var newJson = this.jsonify(input);
|
var newJson = this.jsonify(input);
|
||||||
|
|
||||||
this.deepTraverseJson(newJson, (parent, value, key) => {
|
this.deepTraverseJson(newJson, (parent, value, key) => {
|
||||||
if (key === "properties") {
|
if (key === "properties") {
|
||||||
delete parent[key];
|
delete parent[key];
|
||||||
}
|
}
|
||||||
|
if (key === "xmlKeys") {
|
||||||
|
delete parent[key];
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return newJson
|
return newJson
|
||||||
@ -63,15 +42,15 @@ export class Formatter {
|
|||||||
|
|
||||||
private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void): void {
|
private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void): void {
|
||||||
_.forOwn(json, (value, key) => {
|
_.forOwn(json, (value, key) => {
|
||||||
if (_.isObject(value)) {
|
if (_.isObject(value) && key !== "xmlKeys") {
|
||||||
this.deepTraverseJson(value, lambda);
|
this.deepTraverseJson(value, lambda);
|
||||||
}
|
}
|
||||||
lambda(json, value, key);
|
lambda(json, value, key);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private getReplacementKey(key: string): string {
|
private getReplacementKey(input: any, key: string): string {
|
||||||
var newKey = this.xmlKeyDictionary[key];
|
var newKey = input.xmlKeys[key];
|
||||||
|
|
||||||
if (newKey !== undefined) {
|
if (newKey !== undefined) {
|
||||||
return newKey;
|
return newKey;
|
||||||
|
@ -7,7 +7,7 @@ export class ExpressPacker extends Packer {
|
|||||||
private res: express.Response;
|
private res: express.Response;
|
||||||
|
|
||||||
constructor(document: Document, res: express.Response) {
|
constructor(document: Document, res: express.Response) {
|
||||||
super(document);
|
super(document, null, null);
|
||||||
this.res = res;
|
this.res = res;
|
||||||
|
|
||||||
this.res.on('close', () => {
|
this.res.on('close', () => {
|
||||||
|
@ -6,7 +6,7 @@ export class LocalPacker extends Packer {
|
|||||||
private stream: fs.WriteStream
|
private stream: fs.WriteStream
|
||||||
|
|
||||||
constructor(document: Document, path: string) {
|
constructor(document: Document, path: string) {
|
||||||
super(document);
|
super(document, null, null);
|
||||||
this.stream = fs.createWriteStream(path);
|
this.stream = fs.createWriteStream(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
export class Properties {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
export class Style {
|
||||||
|
|
||||||
|
}
|
@ -8,7 +8,7 @@ function jsonify(obj: Object) {
|
|||||||
return JSON.parse(stringifiedJson);
|
return JSON.parse(stringifiedJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
describe.only('Body', () => {
|
describe('Body', () => {
|
||||||
var body: Body;
|
var body: Body;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
@ -19,7 +19,7 @@ describe('Formatter', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('#format()', () => {
|
describe('#format()', () => {
|
||||||
it("should format simple paragraph", () => {
|
it.only("should format simple paragraph", () => {
|
||||||
var paragraph = new docx.Paragraph();
|
var paragraph = new docx.Paragraph();
|
||||||
var newJson = formatter.format(paragraph);
|
var newJson = formatter.format(paragraph);
|
||||||
newJson = jsonify(newJson);
|
newJson = jsonify(newJson);
|
||||||
|
Reference in New Issue
Block a user