added xmlKeys to xmlcomponent

This commit is contained in:
Dolan Miu
2016-04-03 01:44:18 +01:00
parent a70e82a7d0
commit 69edfcc0c9
24 changed files with 137 additions and 59 deletions

View File

@ -1,7 +1,11 @@
import {XmlComponent, Attributes} from "../../xml-components";
export class Columns {
export class Columns implements XmlComponent {
private cols: Array<XmlComponent>;
xmlKeys = {
cols: 'w:cols'
}
constructor() {
this.cols = new Array<XmlComponent>();

View File

@ -1,7 +1,11 @@
import {XmlComponent, Attributes} from "../../xml-components";
export class DocumentGrid {
export class DocumentGrid implements XmlComponent {
private docGrid: Array<XmlComponent>;
xmlKeys = {
docGrid: 'w:docGrid'
}
constructor() {
this.docGrid = new Array<XmlComponent>();

View File

@ -5,9 +5,13 @@ import {PageMargin} from "./page-margin";
import {Columns} from "./columns";
import {DocumentGrid} from "./doc-grid";
export class Body {
export class Body implements XmlComponent {
private body: Array<XmlComponent>;
xmlKeys = {
body: 'w:body'
}
constructor() {
this.body = new Array<XmlComponent>();
this.body.push(new SectionProperties());
@ -16,7 +20,7 @@ export class Body {
this.body.push(new Columns());
this.body.push(new DocumentGrid());
}
push(component: XmlComponent) {
this.body.push(component);
}

View File

@ -1,7 +1,11 @@
import {XmlComponent, Attributes} from "../../xml-components";
export class PageMargin {
export class PageMargin implements XmlComponent {
private pgMar: Array<XmlComponent>;
xmlKeys = {
pgMar: 'w:pgMar'
}
constructor() {
this.pgMar = new Array<XmlComponent>();

View File

@ -1,7 +1,11 @@
import {XmlComponent, Attributes} from "../../xml-components";
export class PageSize {
export class PageSize implements XmlComponent {
private pgSz: Array<XmlComponent>;
xmlKeys = {
pgSz: 'w:pgSz'
}
constructor() {
this.pgSz = new Array<XmlComponent>();

View File

@ -1,8 +1,12 @@
import {XmlComponent, Attributes} from "../../xml-components";
export class SectionProperties {
export class SectionProperties implements XmlComponent {
private sectPr: Array<XmlComponent>;
xmlKeys = {
sectPr: 'sectPr'
}
constructor() {
this.sectPr = new Array<XmlComponent>();
this.sectPr.push(new Attributes({

View File

@ -3,6 +3,10 @@ import {XmlComponent, Attributes} from "../xml-components";
class Border implements XmlComponent {
private bottom: Array<XmlComponent>;
xmlKeys = {
bottom: 'w:bottom'
}
constructor() {
this.bottom = new Array<XmlComponent>();
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>;
xmlKeys = {
pBdr: 'w:pBdr'
}
constructor() {
this.pBdr = new Array<XmlComponent>();
this.pBdr.push(new Border());

View File

@ -7,8 +7,12 @@ import {TabStop} from "../tab-stop";
import {Style} from "./style";
import {NumberProperties} from "./unordered-list";
class Alignment {
class Alignment implements XmlComponent {
private jc: Array<XmlComponent>;
xmlKeys = {
jc: 'w:jc'
}
constructor(type: string) {
this.jc = new Array<XmlComponent>();
@ -18,9 +22,13 @@ class Alignment {
}
}
export class Paragraph {
export class Paragraph implements XmlComponent {
private p: Array<XmlComponent>;
private properties: ParagraphProperties;
xmlKeys = {
p: 'w:p'
}
constructor(text?: string) {
this.p = new Array<XmlComponent>();

View File

@ -3,6 +3,11 @@ import {Run} from "../run";
class Break implements XmlComponent {
private br: Array<XmlComponent>;
xmlKeys = {
br: 'w:br'
}
constructor() {
this.br = new Array<XmlComponent>();
this.br.push(new Attributes({

View File

@ -2,6 +2,10 @@ import {XmlComponent, Attributes} from "../xml-components";
export class ParagraphProperties implements XmlComponent {
private pPr: Array<XmlComponent>;
xmlKeys = {
pPr: 'w:rPr'
}
constructor() {
this.pPr = new Array<XmlComponent>();

View File

@ -2,6 +2,10 @@ import {XmlComponent, Attributes} from "../xml-components";
export class Style implements XmlComponent {
private pStyle: Array<XmlComponent>;
xmlKeys = {
pStyle: 'w:pStyle'
}
constructor(type: string) {
this.pStyle = new Array<XmlComponent>();

View File

@ -4,6 +4,10 @@ import {Style} from "./style";
export class NumberProperties implements XmlComponent {
private numPr: Array<XmlComponent>;
xmlKeys = {
numPr: 'w:numPr'
}
constructor() {
this.numPr = new Array<XmlComponent>();
this.numPr.push(new IndentLevel(0));
@ -13,7 +17,11 @@ export class NumberProperties implements XmlComponent {
export class IndentLevel implements XmlComponent {
private ilvl: Array<XmlComponent>;
xmlKeys = {
ilvl: 'w:ilvl'
}
constructor(level: number) {
this.ilvl = new Array<XmlComponent>();
this.ilvl.push(new Attributes({
@ -24,7 +32,11 @@ export class IndentLevel implements XmlComponent {
export class NumberId implements XmlComponent {
private ilvl: Array<XmlComponent>;
xmlKeys = {
ilvl: 'w:ilvl'
}
constructor(id: number) {
this.ilvl = new Array<XmlComponent>();
this.ilvl.push(new Attributes({

View File

@ -1,8 +1,12 @@
import {XmlComponent, Attributes} from "../xml-components";
export class Bold {
export class Bold implements XmlComponent {
private b: Array<XmlComponent>;
xmlKeys = {
b: 'w:b'
}
constructor() {
this.b = new Array<XmlComponent>();
this.b.push(new Attributes({
@ -13,7 +17,11 @@ export class Bold {
export class Italics {
private i: Array<XmlComponent>;
xmlKeys = {
i: 'w:i'
}
constructor() {
this.i = new Array<XmlComponent>();
this.i.push(new Attributes({
@ -24,7 +32,11 @@ export class Italics {
export class Underline {
private u: Array<XmlComponent>;
xmlKeys = {
u: 'w:u'
}
constructor() {
this.u = new Array<XmlComponent>();
this.u.push(new Attributes({

View File

@ -5,6 +5,10 @@ import {Bold, Italics, Underline} from "./emphasis";
export class Run implements XmlComponent {
protected r: Array<XmlComponent>;
private properties: RunProperties;
xmlKeys = {
r: 'w:r'
}
constructor() {
this.r = new Array<XmlComponent>();

View File

@ -1,7 +1,11 @@
import {XmlComponent, Attributes} from "../xml-components";
export class RunProperties {
export class RunProperties implements XmlComponent {
private rPr: Array<XmlComponent>;
xmlKeys = {
rPr: 'w:rPr'
}
constructor() {
this.rPr = new Array<XmlComponent>();

View File

@ -1,3 +1,5 @@
export class TabStop {
import {XmlComponent, Attributes} from "./xml-components";
export class TabStop implements XmlComponent{
xmlKeys = {}
}

View File

@ -1,5 +1,5 @@
export interface XmlComponent {
xmlKeys: Object;
}
interface AttributesProperties {
@ -26,6 +26,8 @@ interface AttributesProperties {
export class Attributes implements XmlComponent {
private _attrs: Object;
xmlKeys = {};
constructor(properties?: AttributesProperties) {
this._attrs = properties
@ -38,6 +40,10 @@ export class Attributes implements XmlComponent {
export class Text implements XmlComponent {
private t: string;
xmlKeys = {
t: 'w:t'
}
constructor(text: string) {
this.t = text;
}

View File

@ -2,57 +2,36 @@ import * as _ from "lodash";
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 {
var newJson = this.jsonify(input);
this.deepTraverseJson(newJson, (parent, value, key) => {
this.deepTraverseJson(input, (parent, value, key) => {
if (isNaN(key)) {
var newKey = this.getReplacementKey(key);
parent[newKey] = parent[key];
var newKey = this.getReplacementKey(parent, key);
if (newKey !== key) {
parent[newKey] = parent[key];
delete parent[key];
} 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;
}
private clenseProperties(input: any): Object {
private clense(input: any): Object {
var newJson = this.jsonify(input);
this.deepTraverseJson(newJson, (parent, value, key) => {
if (key === "properties") {
delete parent[key];
}
if (key === "xmlKeys") {
delete parent[key];
}
});
return newJson
}
@ -63,15 +42,15 @@ export class Formatter {
private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void): void {
_.forOwn(json, (value, key) => {
if (_.isObject(value)) {
if (_.isObject(value) && key !== "xmlKeys") {
this.deepTraverseJson(value, lambda);
}
lambda(json, value, key);
});
}
private getReplacementKey(key: string): string {
var newKey = this.xmlKeyDictionary[key];
private getReplacementKey(input: any, key: string): string {
var newKey = input.xmlKeys[key];
if (newKey !== undefined) {
return newKey;

View File

@ -7,7 +7,7 @@ export class ExpressPacker extends Packer {
private res: express.Response;
constructor(document: Document, res: express.Response) {
super(document);
super(document, null, null);
this.res = res;
this.res.on('close', () => {

View File

@ -6,7 +6,7 @@ export class LocalPacker extends Packer {
private stream: fs.WriteStream
constructor(document: Document, path: string) {
super(document);
super(document, null, null);
this.stream = fs.createWriteStream(path);
}

View File

@ -0,0 +1,3 @@
export class Properties {
}

View File

@ -0,0 +1,3 @@
export class Style {
}

View File

@ -8,7 +8,7 @@ function jsonify(obj: Object) {
return JSON.parse(stringifiedJson);
}
describe.only('Body', () => {
describe('Body', () => {
var body: Body;
beforeEach(() => {

View File

@ -19,7 +19,7 @@ describe('Formatter', () => {
});
describe('#format()', () => {
it("should format simple paragraph", () => {
it.only("should format simple paragraph", () => {
var paragraph = new docx.Paragraph();
var newJson = formatter.format(paragraph);
newJson = jsonify(newJson);