Make compiler take in a file
This commit is contained in:
@ -52,5 +52,4 @@ export class Document extends XmlComponent {
|
|||||||
this.addTable(table);
|
this.addTable(table);
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import { Media } from "../media";
|
import { Media } from "../media";
|
||||||
import { Numbering } from "../numbering";
|
import { Numbering } from "../numbering";
|
||||||
import { Properties } from "../properties";
|
import { IPropertiesOptions, Properties } from "../properties";
|
||||||
import { Styles } from "../styles";
|
import { Styles } from "../styles";
|
||||||
|
import { DefaultStylesFactory } from "../styles/factory";
|
||||||
import { Document } from "./document";
|
import { Document } from "./document";
|
||||||
|
import { Paragraph, Table } from "./index";
|
||||||
|
|
||||||
export class File {
|
export class File {
|
||||||
|
|
||||||
@ -12,15 +14,57 @@ export class File {
|
|||||||
private numbering: Numbering;
|
private numbering: Numbering;
|
||||||
private media: Media;
|
private media: Media;
|
||||||
|
|
||||||
constructor() {
|
constructor(options?: IPropertiesOptions) {
|
||||||
this.document = new Document();
|
this.document = new Document();
|
||||||
this.styles = new Styles();
|
const stylesFactory = new DefaultStylesFactory();
|
||||||
this.properties = new Properties({
|
this.styles = stylesFactory.newInstance();
|
||||||
creator: "Un-named",
|
|
||||||
revision: "1",
|
if (!options) {
|
||||||
lastModifiedBy: "Un-named",
|
options = {
|
||||||
});
|
creator: "Un-named",
|
||||||
|
revision: "1",
|
||||||
|
lastModifiedBy: "Un-named",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
this.properties = new Properties(options);
|
||||||
this.numbering = new Numbering();
|
this.numbering = new Numbering();
|
||||||
this.media = new Media();
|
this.media = new Media();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public addParagraph(paragraph: Paragraph): void {
|
||||||
|
this.document.addParagraph(paragraph);
|
||||||
|
}
|
||||||
|
|
||||||
|
public createParagraph(text?: string): Paragraph {
|
||||||
|
return this.document.createParagraph();
|
||||||
|
}
|
||||||
|
|
||||||
|
public addTable(table: Table): void {
|
||||||
|
return this.document.addTable(table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public createTable(rows: number, cols: number): Table {
|
||||||
|
return this.document.createTable(rows, cols);
|
||||||
|
}
|
||||||
|
|
||||||
|
public get Document(): Document {
|
||||||
|
return this.document;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get Styles(): Styles {
|
||||||
|
return this.styles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get Properties(): Properties {
|
||||||
|
return this.properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get Numbering(): Numbering {
|
||||||
|
return this.numbering;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get Media(): Media {
|
||||||
|
return this.media;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,7 @@ import * as fs from "fs";
|
|||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import * as xml from "xml";
|
import * as xml from "xml";
|
||||||
|
|
||||||
import { Document } from "../../docx";
|
import { File } from "../../docx";
|
||||||
import { Media } from "../../media";
|
|
||||||
import { Numbering } from "../../numbering";
|
|
||||||
import { Properties } from "../../properties";
|
|
||||||
import { Styles } from "../../styles";
|
|
||||||
import { DefaultStylesFactory } from "../../styles/factory";
|
|
||||||
import { Formatter } from "../formatter";
|
import { Formatter } from "../formatter";
|
||||||
|
|
||||||
const TEMPLATE_PATH = path.resolve(__dirname, "../../../template");
|
const TEMPLATE_PATH = path.resolve(__dirname, "../../../template");
|
||||||
@ -17,29 +12,11 @@ const TEMPLATE_PATH = path.resolve(__dirname, "../../../template");
|
|||||||
export class Compiler {
|
export class Compiler {
|
||||||
protected archive: archiver.Archiver;
|
protected archive: archiver.Archiver;
|
||||||
private formatter: Formatter;
|
private formatter: Formatter;
|
||||||
private style: Styles;
|
|
||||||
|
|
||||||
constructor(
|
constructor(private file: File) {
|
||||||
protected document: Document,
|
|
||||||
style?: Styles,
|
|
||||||
private properties: Properties = new Properties({
|
|
||||||
creator: "Un-named",
|
|
||||||
revision: "1",
|
|
||||||
lastModifiedBy: "Un-named",
|
|
||||||
}),
|
|
||||||
private numbering: Numbering = new Numbering(),
|
|
||||||
private media: Media = new Media(),
|
|
||||||
) {
|
|
||||||
this.formatter = new Formatter();
|
this.formatter = new Formatter();
|
||||||
this.archive = archiver.create("zip", {});
|
this.archive = archiver.create("zip", {});
|
||||||
|
|
||||||
if (style) {
|
|
||||||
this.style = style;
|
|
||||||
} else {
|
|
||||||
const stylesFactory = new DefaultStylesFactory();
|
|
||||||
this.style = stylesFactory.newInstance();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.archive.on("error", (err) => {
|
this.archive.on("error", (err) => {
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
@ -55,15 +32,15 @@ export class Compiler {
|
|||||||
cwd: TEMPLATE_PATH,
|
cwd: TEMPLATE_PATH,
|
||||||
});
|
});
|
||||||
|
|
||||||
const xmlDocument = xml(this.formatter.format(this.document));
|
const xmlDocument = xml(this.formatter.format(this.file.Document));
|
||||||
const xmlStyles = xml(this.formatter.format(this.style));
|
const xmlStyles = xml(this.formatter.format(this.file.Styles));
|
||||||
const xmlProperties = xml(this.formatter.format(this.properties), {
|
const xmlProperties = xml(this.formatter.format(this.file.Properties), {
|
||||||
declaration: {
|
declaration: {
|
||||||
standalone: "yes",
|
standalone: "yes",
|
||||||
encoding: "UTF-8",
|
encoding: "UTF-8",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const xmlNumbering = xml(this.formatter.format(this.numbering));
|
const xmlNumbering = xml(this.formatter.format(this.file.Numbering));
|
||||||
|
|
||||||
this.archive.append(xmlDocument, {
|
this.archive.append(xmlDocument, {
|
||||||
name: "word/document.xml",
|
name: "word/document.xml",
|
||||||
@ -81,7 +58,7 @@ export class Compiler {
|
|||||||
name: "word/numbering.xml",
|
name: "word/numbering.xml",
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const data of this.media.array) {
|
for (const data of this.file.Media.array) {
|
||||||
this.archive.append(data.stream, {
|
this.archive.append(data.stream, {
|
||||||
name: `media/${data.fileName}`,
|
name: `media/${data.fileName}`,
|
||||||
});
|
});
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
import * as express from "express";
|
import * as express from "express";
|
||||||
|
|
||||||
import { Document } from "../../docx/document";
|
import { File } from "../../docx";
|
||||||
import { Media } from "../../media";
|
|
||||||
import { Numbering } from "../../numbering";
|
|
||||||
import { Properties } from "../../properties";
|
|
||||||
import { Styles } from "../../styles";
|
|
||||||
import { Compiler } from "./compiler";
|
import { Compiler } from "./compiler";
|
||||||
import { IPacker } from "./packer";
|
import { IPacker } from "./packer";
|
||||||
|
|
||||||
@ -12,8 +8,8 @@ export class ExpressPacker implements IPacker {
|
|||||||
private res: express.Response;
|
private res: express.Response;
|
||||||
private packer: Compiler;
|
private packer: Compiler;
|
||||||
|
|
||||||
constructor(document: Document, res: express.Response, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) {
|
constructor(file: File, res: express.Response) {
|
||||||
this.packer = new Compiler(document, styles, properties, numbering, media);
|
this.packer = new Compiler(file);
|
||||||
|
|
||||||
this.res = res;
|
this.res = res;
|
||||||
|
|
||||||
|
@ -1,31 +1,27 @@
|
|||||||
/* tslint:disable:typedef space-before-function-paren */
|
/* tslint:disable:typedef space-before-function-paren */
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
|
|
||||||
import { Document } from "../../docx/document";
|
import { File } from "../../docx";
|
||||||
import { Paragraph } from "../../docx/paragraph";
|
import { Paragraph } from "../../docx/paragraph";
|
||||||
import { LocalPacker } from "../../export/packer/local";
|
import { LocalPacker } from "../../export/packer/local";
|
||||||
import { Properties } from "../../properties";
|
|
||||||
import { DefaultStylesFactory } from "../../styles/factory";
|
|
||||||
|
|
||||||
describe("LocalPacker", () => {
|
describe.only("LocalPacker", () => {
|
||||||
let packer: LocalPacker;
|
let packer: LocalPacker;
|
||||||
let stylesFactory: DefaultStylesFactory;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const document = new Document();
|
const file = new File({
|
||||||
const paragraph = new Paragraph("test text");
|
|
||||||
const heading = new Paragraph("Hello world").heading1();
|
|
||||||
document.addParagraph(new Paragraph("title").title());
|
|
||||||
document.addParagraph(heading);
|
|
||||||
document.addParagraph(new Paragraph("heading 2").heading2());
|
|
||||||
document.addParagraph(paragraph);
|
|
||||||
const properties = new Properties({
|
|
||||||
creator: "Dolan Miu",
|
creator: "Dolan Miu",
|
||||||
revision: "1",
|
revision: "1",
|
||||||
lastModifiedBy: "Dolan Miu",
|
lastModifiedBy: "Dolan Miu",
|
||||||
});
|
});
|
||||||
stylesFactory = new DefaultStylesFactory();
|
const paragraph = new Paragraph("test text");
|
||||||
packer = new LocalPacker(document, stylesFactory.newInstance(), properties);
|
const heading = new Paragraph("Hello world").heading1();
|
||||||
|
file.addParagraph(new Paragraph("title").title());
|
||||||
|
file.addParagraph(heading);
|
||||||
|
file.addParagraph(new Paragraph("heading 2").heading2());
|
||||||
|
file.addParagraph(paragraph);
|
||||||
|
|
||||||
|
packer = new LocalPacker(file);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("#pack()", () => {
|
describe("#pack()", () => {
|
||||||
|
@ -2,11 +2,7 @@ import * as fs from "fs";
|
|||||||
import * as os from "os";
|
import * as os from "os";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
|
|
||||||
import { Document } from "../../docx/document";
|
import { File } from "../../docx";
|
||||||
import { Media } from "../../media";
|
|
||||||
import { Numbering } from "../../numbering";
|
|
||||||
import { Properties } from "../../properties";
|
|
||||||
import { Styles } from "../../styles";
|
|
||||||
import { Compiler } from "./compiler";
|
import { Compiler } from "./compiler";
|
||||||
import { IPacker } from "./packer";
|
import { IPacker } from "./packer";
|
||||||
import { PdfConvertWrapper } from "./pdf-convert-wrapper";
|
import { PdfConvertWrapper } from "./pdf-convert-wrapper";
|
||||||
@ -16,9 +12,9 @@ export class LocalPacker implements IPacker {
|
|||||||
private pdfConverter: PdfConvertWrapper;
|
private pdfConverter: PdfConvertWrapper;
|
||||||
private packer: Compiler;
|
private packer: Compiler;
|
||||||
|
|
||||||
constructor(document: Document, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) {
|
constructor(file: File) {
|
||||||
this.pdfConverter = new PdfConvertWrapper();
|
this.pdfConverter = new PdfConvertWrapper();
|
||||||
this.packer = new Compiler(document, styles, properties, numbering, media);
|
this.packer = new Compiler(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async pack(filePath: string): Promise<void> {
|
public async pack(filePath: string): Promise<void> {
|
||||||
|
Reference in New Issue
Block a user