diff --git a/ts/export/formatter.ts b/ts/export/formatter.ts index 105ab7f4ca..d1fa335b74 100644 --- a/ts/export/formatter.ts +++ b/ts/export/formatter.ts @@ -3,27 +3,65 @@ import * as _ from "lodash"; export class Formatter { private xmlKeyDictionary = { - p: 'w:p' + 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) { - var stringified = JSON.stringify(input); - var newJson = JSON.parse(stringified); + format(input: any): Object { + var newJson = this.jsonify(input); + this.deepTraverseJson(newJson, (parent, value, key) => { if (isNaN(key)) { var newKey = this.getReplacementKey(key); parent[newKey] = parent[key]; if (newKey !== key) { delete parent[key]; + } else { + console.error("Key is not in dictionary:" + key); } } }); - console.log(newJson); + + newJson = this.clenseProperties(newJson); return newJson; } - private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void) { + private clenseProperties(input: any): Object { + var newJson = this.jsonify(input); + + this.deepTraverseJson(newJson, (parent, value, key) => { + if (key === "properties") { + delete parent[key]; + } + }); + + return newJson + } + + private jsonify(obj: Object): Object { + var stringifiedJson = JSON.stringify(obj); + return JSON.parse(stringifiedJson); + } + + private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void): void { _.forOwn(json, (value, key) => { if (_.isObject(value)) { this.deepTraverseJson(value, lambda); diff --git a/ts/tests/formatterTest.ts b/ts/tests/formatterTest.ts index cd0ac2c670..6f8e199c83 100644 --- a/ts/tests/formatterTest.ts +++ b/ts/tests/formatterTest.ts @@ -2,6 +2,12 @@ /// import {Formatter} from "../export/Formatter"; +import * as docx from "../docx"; + +function jsonify(obj: Object) { + var stringifiedJson = JSON.stringify(obj); + return JSON.parse(stringifiedJson); +} describe.only('Formatter', () => { var formatter: Formatter; @@ -11,8 +17,11 @@ describe.only('Formatter', () => { }); describe('#format()', () => { - it("should work", () => { - var newJson = formatter.format({ "p": [{ "t": "test" }] }); + it("should format simple paragraph", () => { + var paragraph = new docx.Paragraph(); + var newJson = formatter.format(paragraph); + newJson = jsonify(newJson); + console.log(newJson); }); it("should should change 'p' tag into 'w:p' tag", () => {