docxtemplater
Version:
.docx generator working with templates and data (like Mustache)
157 lines (140 loc) • 4.39 kB
JavaScript
;
var FileTypeConfig = require("../file-type-config.js");
var XmlTemplater = require("../xml-templater");
var path = require("path");
var Docxtemplater = require("../docxtemplater.js");
var chai = require("chai");
var expect = chai.expect;
var JSZip = require("jszip");
var xmlPrettify = require("./xml-prettify");
var fs = require("fs");
var countFiles = 1;
var allStarted = false;
var examplesDirectory = void 0;
/* eslint-disable no-console */
function createXmlTemplaterDocx(content, options) {
options = options || {};
options.fileTypeConfig = FileTypeConfig.docx;
options.modules = options.fileTypeConfig.baseModules.map(function (moduleFunction) {
var module = moduleFunction();
module.optionsTransformer({}, { fileTypeConfig: options.fileTypeConfig });
return module;
});
return new XmlTemplater(content, options).parse().setTags(options.tags);
}
function shouldBeSame(options) {
var zip = options.doc.getZip();
var expectedName = options.expectedName;
var expectedZip = void 0;
var writeFile = path.resolve(examplesDirectory, "..", expectedName);
if (fs.writeFileSync) {
fs.writeFileSync(writeFile, zip.generate({ type: "nodebuffer", compression: "DEFLATE" }));
}
try {
expectedZip = docX[expectedName].zip;
} catch (e) {
console.log(JSON.stringify({ msg: "Expected name does not match", expectedName: expectedName }));
throw e;
}
var result = [];
try {
Object.keys(zip.files).map(function (filePath) {
var suffix = "for \"" + filePath + "\"";
expect(zip.files[filePath].name).to.be.equal(expectedZip.files[filePath].name, "Name differs " + suffix);
expect(zip.files[filePath].options.dir).to.be.equal(expectedZip.files[filePath].options.dir, "IsDir differs " + suffix);
var text1 = zip.files[filePath].asText().replace(/\n|\t/g, "");
var text2 = expectedZip.files[filePath].asText().replace(/\n|\t/g, "");
if (text1 !== text2 && filePath.indexOf(".png") === -1) {
var pText1 = xmlPrettify(text1, options);
var pText2 = xmlPrettify(text2, options);
expect(pText1).to.be.equal(pText2, "Content differs " + suffix + " lengths: \"" + text1.length + "\", \"" + text2.length + "\"");
} else {
expect(text1.length).to.be.equal(text2.length, "Content differs " + suffix);
}
});
} catch (e) {
console.log(JSON.stringify({ msg: "Expected name does not match", expectedName: expectedName }));
throw e;
}
return result;
}
var docX = {};
var imageData = {};
function load(name, content, fileType, obj) {
var zip = new JSZip(content);
obj[name] = new Docxtemplater();
obj[name].loadZip(zip);
obj[name].loadedName = name;
obj[name].loadedContent = content;
return obj[name];
}
function loadDocument(name, content) {
return load(name, content, "docx", docX);
}
function loadImage(name, content) {
imageData[name] = content;
}
function loadFile(name, callback) {
countFiles += 1;
if (fs.readFileSync) {
var _path = require("path");
var buffer = fs.readFileSync(_path.join(examplesDirectory, name), "binary");
callback(name, buffer);
return endLoadFile(-1);
}
return JSZipUtils.getBinaryContent("../examples/" + name, function (err, data) {
if (err) {
throw err;
}
callback(name, data);
return endLoadFile(-1);
});
}
function endLoadFile(change) {
change = change || 0;
countFiles += change;
if (countFiles === 0 && allStarted === true) {
return startFunction();
}
}
function start() {
allStarted = true;
endLoadFile(-1);
}
var startFunction = void 0;
function setStartFunction(sf) {
allStarted = false;
countFiles = 1;
startFunction = sf;
}
function setExamplesDirectory(ed) {
examplesDirectory = ed;
}
function removeSpaces(text) {
return text.replace(/\n|\t/g, "");
}
function makeDocx(name, content) {
var zip = new JSZip();
zip.file("word/document.xml", content);
var base64 = zip.generate({ type: "string" });
return load(name, base64, "docx", docX);
}
function createDoc(name) {
return loadDocument(name, docX[name].loadedContent);
}
module.exports = {
createXmlTemplaterDocx: createXmlTemplaterDocx,
createDoc: createDoc,
loadDocument: loadDocument,
loadImage: loadImage,
shouldBeSame: shouldBeSame,
imageData: imageData,
loadFile: loadFile,
start: start,
chai: chai,
expect: expect,
setStartFunction: setStartFunction,
setExamplesDirectory: setExamplesDirectory,
removeSpaces: removeSpaces,
makeDocx: makeDocx
};