1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | 'use strict';
|
18 |
|
19 | function 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 |
|
21 | function _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 |
|
23 | const fs = require('fs');
|
24 |
|
25 | const PdfTransformer = require('./PdfTransformer');
|
26 |
|
27 | const CiceroMarkTransformer = require('@accordproject/markdown-cicero').CiceroMarkTransformer;
|
28 |
|
29 | let pdfTransformer = null;
|
30 |
|
31 | beforeAll(() => {
|
32 | pdfTransformer = new PdfTransformer();
|
33 | });
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function 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 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function 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 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function 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 |
|
85 | describe('pdf import', () => {
|
86 | getPdfFiles().forEach((_ref, i) => {
|
87 | let [file, pdfContent] = _ref;
|
88 | it("converts ".concat(file, " to cicero mark"), _asyncToGenerator(function* () {
|
89 | const ciceroMarkDom = yield pdfTransformer.toCiceroMark(pdfContent, 'json');
|
90 |
|
91 | expect(ciceroMarkDom).toMatchSnapshot();
|
92 | }));
|
93 | });
|
94 | });
|
95 | describe('pdf generation', () => {
|
96 | getMarkdownFiles().forEach((_ref3, i) => {
|
97 | let [file, markdownContent] = _ref3;
|
98 | it("converts ".concat(file, " to pdf"), _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"), _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 |