fixed attribute

This commit is contained in:
Dolan Miu
2016-04-05 01:49:12 +01:00
parent d197a2c717
commit d4c2b10285
9 changed files with 46 additions and 24 deletions

View File

@ -4,7 +4,7 @@ export class SectionProperties implements XmlComponent {
private sectPr: Array<XmlComponent>; private sectPr: Array<XmlComponent>;
xmlKeys = { xmlKeys = {
sectPr: 'sectPr' sectPr: 'w:sectPr'
} }
constructor() { constructor() {

View File

@ -3,9 +3,14 @@ import {DocumentAttributes} from "../xml-components/document-attributes"
import {Body} from "./body"; import {Body} from "./body";
import {Paragraph} from "../paragraph"; import {Paragraph} from "../paragraph";
export class Document { export class Document implements XmlComponent {
private document: Array<XmlComponent>; private document: Array<XmlComponent>;
private body: Body; private body: Body;
xmlKeys = {
document: "w:document",
body: "w:body"
};
constructor() { constructor() {
this.document = new Array<XmlComponent>(); this.document = new Array<XmlComponent>();

View File

@ -27,7 +27,7 @@ interface DocumentAttributesProperties {
} }
export class DocumentAttributes implements XmlComponent { export class DocumentAttributes implements XmlComponent {
private _attrs: Object; private _attr: Object;
xmlKeys = { xmlKeys = {
wpc: 'xmlns:wpc', wpc: 'xmlns:wpc',
@ -56,11 +56,11 @@ export class DocumentAttributes implements XmlComponent {
}; };
constructor(properties?: DocumentAttributesProperties) { constructor(properties?: DocumentAttributesProperties) {
this._attrs = properties this._attr = properties
if (!properties) { if (!properties) {
this._attrs = {}; this._attr = {};
} }
this._attrs["xmlKeys"] = this.xmlKeys; this._attr["xmlKeys"] = this.xmlKeys;
} }
} }

View File

@ -24,7 +24,7 @@ interface AttributesProperties {
} }
export class Attributes implements XmlComponent { export class Attributes implements XmlComponent {
private _attrs: Object; private _attr: Object;
xmlKeys = { xmlKeys = {
val: "w:val", val: "w:val",
@ -48,13 +48,13 @@ export class Attributes implements XmlComponent {
}; };
constructor(properties?: AttributesProperties) { constructor(properties?: AttributesProperties) {
this._attrs = properties this._attr = properties
if (!properties) { if (!properties) {
this._attrs = {}; this._attr = {};
} }
this._attrs["xmlKeys"] = this.xmlKeys; this._attr["xmlKeys"] = this.xmlKeys;
} }
} }

View File

@ -1,12 +1,13 @@
import {Packer} from "./packer"; import {Packer} from "./packer";
import * as fs from 'fs'; import * as fs from 'fs';
import {Document} from "../../docx/document"; import {Document} from "../../docx/document";
import {Properties} from "../../properties";
export class LocalPacker extends Packer { export class LocalPacker extends Packer {
private stream: fs.WriteStream private stream: fs.WriteStream
constructor(document: Document, path: string) { constructor(document: Document, style: any, properties: Properties, path: string) {
super(document, null, null); super(document, null, properties);
this.stream = fs.createWriteStream(path); this.stream = fs.createWriteStream(path);
} }

View File

@ -1,5 +1,6 @@
import * as archiver from "archiver"; import * as archiver from "archiver";
import * as fs from "fs"; import * as fs from "fs";
import * as xml from "xml";
import {Formatter} from "../formatter"; import {Formatter} from "../formatter";
import {Document} from "../../docx"; import {Document} from "../../docx";
import {Style} from "../../style"; import {Style} from "../../style";
@ -36,16 +37,20 @@ export abstract class Packer {
]); ]);
//this.archive.directory(__dirname + "/template", "/"); //this.archive.directory(__dirname + "/template", "/");
var xmlDocument = xml(this.formatter.format(this.document));
this.archive.append(this.document, { //var xmlStyle = xml(this.style);
var xmlProperties = xml(this.formatter.format(this.properties));
console.log(JSON.stringify(this.formatter.format(this.document), null, " "));
console.log(xmlDocument);
this.archive.append(xmlDocument, {
name: 'word/document.xml' name: 'word/document.xml'
}); });
this.archive.append(this.style, { //this.archive.append(xmlStyle, {
name: 'word/newStyle.xml' // name: 'word/newStyle.xml'
}); //});
this.archive.append(this.properties, { this.archive.append(xmlProperties, {
name: 'docProps/core.xml' name: 'docProps/core.xml'
}); });

View File

@ -28,6 +28,7 @@ describe("ThematicBreak", () => {
val: "single", val: "single",
sz: "6" sz: "6"
}; };
delete newJson.pBdr[0].bottom[0]._attrs.xmlKeys;
assert(JSON.stringify(newJson.pBdr[0].bottom[0]._attrs) === JSON.stringify(attributes)); assert(JSON.stringify(newJson.pBdr[0].bottom[0]._attrs) === JSON.stringify(attributes));
}); });
}) })

View File

@ -26,11 +26,16 @@ describe("Formatter", () => {
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);
console.log(JSON.stringify(newJson, null, " "));
assert.isDefined(newJson["w:p"]); assert.isDefined(newJson["w:p"]);
}); });
it("should remove xmlKeys", () => {
var paragraph = new docx.Paragraph();
var newJson = formatter.format(paragraph);
var stringifiedJson = JSON.stringify(newJson);
assert(stringifiedJson.indexOf("xmlKeys") < 0);
});
it("should format simple paragraph with bold text", () => { it("should format simple paragraph with bold text", () => {
var paragraph = new docx.Paragraph(); var paragraph = new docx.Paragraph();
paragraph.addText(new docx.TextRun("test").bold()); paragraph.addText(new docx.TextRun("test").bold());
@ -62,14 +67,14 @@ describe("Formatter", () => {
assert.isDefined(newJson["w:p"]); assert.isDefined(newJson["w:p"]);
}); });
it.only("should format Properties object correctly", () => { it("should format Properties object correctly", () => {
var properties = new Properties({ var properties = new Properties({
title: "test document", title: "test document",
creator: "Dolan" creator: "Dolan"
}); });
var newJson = formatter.format(properties); var newJson = formatter.format(properties);
newJson = jsonify(newJson); newJson = jsonify(newJson);
console.log(JSON.stringify(newJson, null, " ")); assert.isDefined(newJson["cp:coreProperties"]);
}); });
}); });
}); });

View File

@ -1,17 +1,22 @@
/// <reference path="../typings/mocha/mocha.d.ts" /> /// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" /> /// <reference path="../typings/chai/chai.d.ts" />
/// <reference path="../typings/archiver/archiver.d.ts" /> /// <reference path="../typings/archiver/archiver.d.ts" />
/// <reference path="../typings/xml/xml.d.ts" />
import {LocalPacker} from "../export/packer/local"; import {LocalPacker} from "../export/packer/local";
import {assert} from "chai"; import {assert} from "chai";
import {Document} from "../docx/document" import {Document} from "../docx/document"
import {Properties} from "../properties"
describe("Packer", () => { describe.only("Packer", () => {
var packer: LocalPacker; var packer: LocalPacker;
beforeEach(() => { beforeEach(() => {
var document = new Document(); var document = new Document();
packer = new LocalPacker(document, "build/tests/test.zip"); var properties = new Properties({
title: "test document"
});
packer = new LocalPacker(document, undefined, properties, "build/tests/test.zip");
}); });
describe('#pack()', () => { describe('#pack()', () => {