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