UNPKG

6.22 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14// @ts-nocheck
15
16/* eslint-disable no-undef */
17'use strict';
18
19function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
20
21function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
22
23const fs = require('fs');
24
25const PdfTransformer = require('./PdfTransformer');
26
27const CiceroMarkTransformer = require('@accordproject/markdown-cicero').CiceroMarkTransformer;
28
29let pdfTransformer = null; // @ts-ignore
30
31beforeAll(() => {
32 pdfTransformer = new PdfTransformer();
33});
34/**
35 * Get the name and contents of all pdf test files
36 * @returns {*} an array of name/contents tuples
37 */
38
39function getPdfFiles() {
40 const result = [];
41 const files = fs.readdirSync(__dirname + '/../test/data');
42 files.forEach(function (file) {
43 if (file.endsWith('.pdf')) {
44 let contents = fs.readFileSync(__dirname + '/../test/data/' + file);
45 result.push([file, contents]);
46 }
47 });
48 return result;
49}
50/**
51 * Saves a cicero mark dom as a PDF to the output folder
52 * @param {object} ciceroMarkDom the dom
53 * @param {string} fileName the filename to use
54 * @return {Promise} a promise that resolves when the file has been written
55 */
56
57
58function saveCiceroMarkAsPdf(ciceroMarkDom, fileName) {
59 fs.mkdirSync('./output', {
60 recursive: true
61 });
62 const promise = new Promise(resolve => {
63 const outputStream = fs.createWriteStream("./output/".concat(fileName, ".pdf"));
64 outputStream.on('finish', () => {
65 resolve(true);
66 });
67 const options = {
68 info: {
69 title: 'Smart Legal Contract',
70 author: 'Dan',
71 subject: 'Test PDF rendering',
72 keywords: 'accord project, markdown transform, pdf'
73 },
74 defaultStyle: {
75 font: 'LiberationSerif'
76 },
77 tocHeading: 'Table of Contents',
78 headerText: 'Contract ABCDEF',
79 footerText: 'Copyright Acme Inc.',
80 footerPageNumber: true,
81 styles: {
82 Link: {
83 color: 'red',
84 fontSize: 16
85 }
86 }
87 };
88 pdfTransformer.toPdf(ciceroMarkDom, options, outputStream);
89 });
90 return promise;
91}
92/**
93 * Get the name and contents of all markdown test files
94 * @returns {*} an array of name/contents tuples
95 */
96
97
98function getMarkdownFiles() {
99 const result = [];
100 const files = fs.readdirSync(__dirname + '/../test/data');
101 files.forEach(function (file) {
102 if (file.endsWith('.md')) {
103 let contents = fs.readFileSync(__dirname + '/../test/data/' + file, 'utf8');
104 result.push([file, contents]);
105 }
106 });
107 return result;
108}
109/**
110 * Get the name and contents of all JSON test files
111 * @returns {*} an array of name/contents tuples
112 */
113
114
115function getJsonFiles() {
116 const result = [];
117 const files = fs.readdirSync(__dirname + '/../test/data');
118 files.forEach(function (file) {
119 if (file.endsWith('.json')) {
120 let contents = fs.readFileSync(__dirname + '/../test/data/' + file, 'utf8');
121 result.push([file, contents]);
122 }
123 });
124 return result;
125}
126
127describe('pdf import', () => {
128 jest.setTimeout(20000);
129 getPdfFiles().forEach((_ref, i) => {
130 let [file, pdfContent] = _ref;
131 it("converts ".concat(file, " to cicero mark"), /*#__PURE__*/_asyncToGenerator(function* () {
132 const ciceroMarkDom = yield pdfTransformer.toCiceroMark(pdfContent, 'json'); // if(file.startsWith('Land')) {
133 // console.log(JSON.stringify(ciceroMarkDom, null, 4));
134 // }
135
136 expect(ciceroMarkDom).toMatchSnapshot(); // (1)
137
138 return saveCiceroMarkAsPdf(ciceroMarkDom, file + '-import'); // roundtrip for debug
139 }));
140 });
141});
142describe('pdf generation', () => {
143 getMarkdownFiles().forEach((_ref3, i) => {
144 let [file, markdownContent] = _ref3;
145 it("converts ".concat(file, " to pdf"), /*#__PURE__*/_asyncToGenerator(function* () {
146 const ciceroMarkTransformer = new CiceroMarkTransformer();
147 const ciceroMarkDom = ciceroMarkTransformer.fromMarkdownCicero(markdownContent, 'json');
148 return saveCiceroMarkAsPdf(ciceroMarkDom, file);
149 }));
150 });
151 getJsonFiles().forEach((_ref5, i) => {
152 let [file, jsonContent] = _ref5;
153 it("converts ".concat(file, " to pdf"), /*#__PURE__*/_asyncToGenerator(function* () {
154 fs.mkdirSync('./output', {
155 recursive: true
156 });
157 const promise = new Promise(resolve => {
158 const outputStream = fs.createWriteStream("./output/".concat(file, ".pdf"));
159 outputStream.on('finish', () => {
160 resolve(true);
161 });
162 const options = {
163 info: {
164 title: 'Smart Legal Contract',
165 author: 'Dan',
166 subject: 'Test PDF rendering',
167 keywords: 'accord project, markdown transform, pdf'
168 },
169 defaultStyle: {
170 font: 'LiberationSerif'
171 },
172 tocHeading: 'Table of Contents',
173 headerText: 'Contract ABCDEF',
174 footerText: 'Copyright Acme Inc.',
175 footerPageNumber: true,
176 styles: {
177 Link: {
178 color: 'red',
179 fontSize: 16
180 }
181 }
182 };
183 pdfTransformer.toPdf(JSON.parse(jsonContent), options, outputStream);
184 });
185 return promise;
186 }));
187 });
188});
\No newline at end of file