UNPKG

5.82 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 * Get the name and contents of all markdown test files
52 * @returns {*} an array of name/contents tuples
53 */
54
55
56function getMarkdownFiles() {
57 const result = [];
58 const files = fs.readdirSync(__dirname + '/../test/data');
59 files.forEach(function (file) {
60 if (file.endsWith('.md')) {
61 let contents = fs.readFileSync(__dirname + '/../test/data/' + file, 'utf8');
62 result.push([file, contents]);
63 }
64 });
65 return result;
66}
67/**
68 * Get the name and contents of all JSON test files
69 * @returns {*} an array of name/contents tuples
70 */
71
72
73function getJsonFiles() {
74 const result = [];
75 const files = fs.readdirSync(__dirname + '/../test/data');
76 files.forEach(function (file) {
77 if (file.endsWith('.json')) {
78 let contents = fs.readFileSync(__dirname + '/../test/data/' + file, 'utf8');
79 result.push([file, contents]);
80 }
81 });
82 return result;
83}
84
85describe('pdf import', () => {
86 getPdfFiles().forEach((_ref, i) => {
87 let [file, pdfContent] = _ref;
88 it("converts ".concat(file, " to cicero mark"), /*#__PURE__*/_asyncToGenerator(function* () {
89 const ciceroMarkDom = yield pdfTransformer.toCiceroMark(pdfContent, 'json'); //console.log(JSON.stringify(ciceroMarkDom, null, 4));
90
91 expect(ciceroMarkDom).toMatchSnapshot(); // (1)
92 }));
93 });
94});
95describe('pdf generation', () => {
96 getMarkdownFiles().forEach((_ref3, i) => {
97 let [file, markdownContent] = _ref3;
98 it("converts ".concat(file, " to pdf"), /*#__PURE__*/_asyncToGenerator(function* () {
99 const ciceroMarkTransformer = new CiceroMarkTransformer();
100 const ciceroMarkDom = ciceroMarkTransformer.fromMarkdown(markdownContent, 'json');
101 fs.mkdirSync('./output', {
102 recursive: true
103 });
104 const promise = new Promise(resolve => {
105 const outputStream = fs.createWriteStream("./output/".concat(file, ".pdf"));
106 outputStream.on('finish', () => {
107 resolve(true);
108 });
109 const options = {
110 info: {
111 title: 'Smart Legal Contract',
112 author: 'Dan',
113 subject: 'Test PDF rendering',
114 keywords: 'accord project, markdown transform, pdf'
115 },
116 defaultStyle: {
117 font: 'LiberationSerif'
118 },
119 tocHeading: 'Table of Contents',
120 headerText: 'Contract ABCDEF',
121 footerText: 'Copyright Acme Inc.',
122 footerPageNumber: true,
123 styles: {
124 Link: {
125 color: 'red',
126 fontSize: 16
127 }
128 }
129 };
130 pdfTransformer.toPdf(ciceroMarkDom, options, outputStream);
131 });
132 return promise;
133 }));
134 });
135 getJsonFiles().forEach((_ref5, i) => {
136 let [file, jsonContent] = _ref5;
137 it("converts ".concat(file, " to pdf"), /*#__PURE__*/_asyncToGenerator(function* () {
138 fs.mkdirSync('./output', {
139 recursive: true
140 });
141 const promise = new Promise(resolve => {
142 const outputStream = fs.createWriteStream("./output/".concat(file, ".pdf"));
143 outputStream.on('finish', () => {
144 resolve(true);
145 });
146 const options = {
147 info: {
148 title: 'Smart Legal Contract',
149 author: 'Dan',
150 subject: 'Test PDF rendering',
151 keywords: 'accord project, markdown transform, pdf'
152 },
153 defaultStyle: {
154 font: 'LiberationSerif'
155 },
156 tocHeading: 'Table of Contents',
157 headerText: 'Contract ABCDEF',
158 footerText: 'Copyright Acme Inc.',
159 footerPageNumber: true,
160 styles: {
161 Link: {
162 color: 'red',
163 fontSize: 16
164 }
165 }
166 };
167 pdfTransformer.toPdf(JSON.parse(jsonContent), options, outputStream);
168 });
169 return promise;
170 }));
171 });
172});
\No newline at end of file