docgen-tool 3.3.1 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1009 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.DocGen = void 0;
43
- var chalk_1 = __importDefault(require("chalk"));
44
- var path_1 = __importDefault(require("path"));
45
- var cheerio_1 = __importDefault(require("cheerio"));
46
- var moment_1 = __importDefault(require("moment"));
47
- var z_schema_1 = __importDefault(require("z-schema"));
48
- var child_process_1 = require("child_process");
49
- var spawn_args_1 = __importDefault(require("spawn-args"));
50
- var markdown_it_1 = __importDefault(require("markdown-it"));
51
- var image_size_1 = __importDefault(require("image-size"));
52
- var cli_spinner_1 = require("cli-spinner");
53
- var fs_1 = require("./fs/fs");
54
- var package_json_1 = require("../../package.json");
55
- var markdown = new markdown_it_1.default('commonmark').enable('table');
56
- //Allow CommonMark links that use other protocols, such as file:///
57
- //The markdown-it implementation is more restrictive than the CommonMark spec
58
- //See https://github.com/markdown-it/markdown-it/issues/108
59
- markdown.validateLink = function () {
60
- return true;
61
- };
62
- function DocGen(process) {
63
- var _this = this;
64
- var mainProcess = process;
65
- var wkhtmltopdfVersion = 'wkhtmltopdf 0.12.6 (with patched qt)'; //output from wkhtmltopdf -V
66
- var options;
67
- var templates = {};
68
- var meta = {};
69
- var pages = {};
70
- var sortedPages = {};
71
- this.getVersion = function () {
72
- return package_json_1.version;
73
- };
74
- this.setOptions = function (userOptions) {
75
- options = userOptions;
76
- //all user-specified paths must be normalized
77
- if (options.input) {
78
- options.input = path_1.default.normalize(options.input + '/');
79
- }
80
- if (options.output) {
81
- options.output = path_1.default.normalize(options.output + '/');
82
- }
83
- //wkhtmltopdf path does not need a trailing slash
84
- if (options.wkhtmltopdfPath && options.wkhtmltopdfPath !== '') {
85
- options.wkhtmltopdfPath = path_1.default.normalize(options.wkhtmltopdfPath);
86
- }
87
- };
88
- /*
89
- copy the example src files (template) to any directory, when scaffold command is invoked
90
- */
91
- this.scaffold = function () { return __awaiter(_this, void 0, void 0, function () {
92
- return __generator(this, function (_a) {
93
- switch (_a.label) {
94
- case 0:
95
- console.log(chalk_1.default.green('Creating scaffold template directory'));
96
- return [4 /*yield*/, (0, fs_1.copyDirectory)(__dirname + '/../include/example', options.output, options.verbose === true)];
97
- case 1:
98
- _a.sent();
99
- return [2 /*return*/];
100
- }
101
- });
102
- }); };
103
- this.run = function () { return __awaiter(_this, void 0, void 0, function () {
104
- return __generator(this, function (_a) {
105
- switch (_a.label) {
106
- case 0:
107
- console.log(chalk_1.default.green.bold('DocGen version ' + package_json_1.version));
108
- //delete and recreate the output directory
109
- return [4 /*yield*/, (0, fs_1.cleanDirectory)(options.output)];
110
- case 1:
111
- //delete and recreate the output directory
112
- _a.sent();
113
- loadTemplates();
114
- return [2 /*return*/];
115
- }
116
- });
117
- }); };
118
- /*
119
- load all HTML template files
120
- */
121
- var loadTemplates = function () { return __awaiter(_this, void 0, void 0, function () {
122
- var files, key, file, dom, error_1;
123
- var _a;
124
- return __generator(this, function (_b) {
125
- switch (_b.label) {
126
- case 0:
127
- console.log(chalk_1.default.green('Loading templates'));
128
- _b.label = 1;
129
- case 1:
130
- _b.trys.push([1, 8, , 9]);
131
- _a = {};
132
- return [4 /*yield*/, (0, fs_1.readFile)(__dirname + '/../include/templates/main.html')];
133
- case 2:
134
- _a.main = _b.sent();
135
- return [4 /*yield*/, (0, fs_1.readFile)(__dirname + '/../include/templates/redirect.html')];
136
- case 3:
137
- _a.redirect = _b.sent();
138
- return [4 /*yield*/, (0, fs_1.readFile)(__dirname + '/../include/templates/webCover.html')];
139
- case 4:
140
- _a.webCover = _b.sent();
141
- return [4 /*yield*/, (0, fs_1.readFile)(__dirname + '/../include/templates/pdfCover.html')];
142
- case 5:
143
- _a.pdfCover = _b.sent();
144
- return [4 /*yield*/, (0, fs_1.readFile)(__dirname + '/../include/templates/pdfHeader.html')];
145
- case 6:
146
- _a.pdfHeader = _b.sent();
147
- return [4 /*yield*/, (0, fs_1.readFile)(__dirname + '/../include/templates/pdfFooter.html')];
148
- case 7:
149
- files = (_a.pdfFooter = _b.sent(),
150
- _a);
151
- for (key in files) {
152
- if (files.hasOwnProperty(key)) {
153
- file = files[key];
154
- dom = cheerio_1.default.load(file);
155
- templates[key] = dom;
156
- }
157
- }
158
- return [3 /*break*/, 9];
159
- case 8:
160
- error_1 = _b.sent();
161
- console.log(chalk_1.default.red('Error loading templates'));
162
- if (options.verbose === true) {
163
- console.log(chalk_1.default.red(error_1));
164
- }
165
- mainProcess.exit(1);
166
- return [3 /*break*/, 9];
167
- case 9: return [4 /*yield*/, loadMeta()];
168
- case 10:
169
- _b.sent();
170
- return [2 /*return*/];
171
- }
172
- });
173
- }); };
174
- /*
175
- JSON schema validation
176
- */
177
- var schemas = {
178
- parameters: {
179
- title: 'DocGen Parameters Schema',
180
- type: 'object',
181
- required: [
182
- 'title',
183
- 'name',
184
- 'version',
185
- 'date',
186
- 'organization',
187
- 'author',
188
- 'owner',
189
- 'contributors',
190
- 'website',
191
- 'module',
192
- 'id',
193
- 'summary',
194
- 'marking',
195
- 'legalese',
196
- ],
197
- properties: {
198
- title: { type: 'string' },
199
- name: { type: 'string' },
200
- version: { type: 'string' },
201
- date: { type: 'string' },
202
- organization: {
203
- type: 'object',
204
- required: ['name', 'url'],
205
- properties: {
206
- name: { type: 'string' },
207
- url: { type: 'string' },
208
- },
209
- },
210
- author: {
211
- type: 'object',
212
- required: ['name', 'url'],
213
- properties: {
214
- name: { type: 'string' },
215
- url: { type: 'string' },
216
- },
217
- },
218
- owner: {
219
- type: 'object',
220
- required: ['name', 'url'],
221
- properties: {
222
- name: { type: 'string' },
223
- url: { type: 'string' },
224
- },
225
- },
226
- contributors: {
227
- type: 'array',
228
- items: {
229
- oneOf: [
230
- {
231
- type: 'object',
232
- required: ['name', 'url'],
233
- properties: {
234
- name: { type: 'string' },
235
- url: { type: 'string' },
236
- },
237
- },
238
- ],
239
- },
240
- },
241
- website: {
242
- type: 'object',
243
- required: ['name', 'url'],
244
- properties: {
245
- name: { type: 'string' },
246
- url: { type: 'string' },
247
- },
248
- },
249
- sponsorLink: {
250
- type: 'object',
251
- required: ['name', 'url', 'logo'],
252
- properties: {
253
- name: { type: 'string' },
254
- url: { type: 'string' },
255
- logo: { type: 'string' },
256
- },
257
- },
258
- backlink: {
259
- type: 'object',
260
- required: ['name', 'url'],
261
- properties: {
262
- name: { type: 'string' },
263
- url: { type: 'string' },
264
- },
265
- },
266
- module: { type: 'string' },
267
- id: { type: 'string' },
268
- summary: { type: 'string' },
269
- marking: { type: 'string' },
270
- legalese: { type: 'string' },
271
- },
272
- },
273
- contents: {
274
- title: 'DocGen Table of Contents Schema',
275
- type: 'array',
276
- items: {
277
- oneOf: [
278
- {
279
- type: 'object',
280
- required: ['heading', 'column', 'pages'],
281
- properties: {
282
- name: { type: 'string' },
283
- column: { type: 'integer', minimum: 1, maximum: 4 },
284
- pages: {
285
- type: 'array',
286
- items: {
287
- oneOf: [
288
- {
289
- type: 'object',
290
- required: ['title', 'source'],
291
- properties: {
292
- title: { type: 'string' },
293
- source: { type: 'string' },
294
- html: { type: 'boolean' },
295
- },
296
- },
297
- ],
298
- },
299
- },
300
- },
301
- },
302
- ],
303
- },
304
- },
305
- };
306
- var validateJSON = function (key, data) {
307
- var schema = schemas[key];
308
- var validator = new z_schema_1.default();
309
- var valid = validator.validate(data, schema);
310
- if (!valid) {
311
- console.log(chalk_1.default.red('Error parsing required file: ' +
312
- key +
313
- '.json (failed schema validation)'));
314
- if (options.verbose === true) {
315
- console.log(chalk_1.default.red(validator.getLastError()));
316
- }
317
- }
318
- return valid;
319
- };
320
- /*
321
- load all metadata files (JSON)
322
- */
323
- var loadMeta = function () { return __awaiter(_this, void 0, void 0, function () {
324
- var files, key, file, extra, error_2;
325
- var _a;
326
- return __generator(this, function (_b) {
327
- switch (_b.label) {
328
- case 0:
329
- console.log(chalk_1.default.green('Loading required JSON metadata files'));
330
- _b.label = 1;
331
- case 1:
332
- _b.trys.push([1, 4, , 5]);
333
- _a = {};
334
- return [4 /*yield*/, (0, fs_1.readFile)(options.input + '/parameters.json')];
335
- case 2:
336
- _a.parameters = _b.sent();
337
- return [4 /*yield*/, (0, fs_1.readFile)(options.input + '/contents.json')];
338
- case 3:
339
- files = (_a.contents = _b.sent(),
340
- _a);
341
- for (key in files) {
342
- if (files.hasOwnProperty(key)) {
343
- //ignore prototype
344
- try {
345
- file = JSON.parse(files[key]);
346
- if (validateJSON(key, file)) {
347
- meta[key] = file;
348
- }
349
- else {
350
- mainProcess.exit(1);
351
- }
352
- }
353
- catch (error) {
354
- console.log(chalk_1.default.red('Error parsing required file: ' + key + '.json (invalid JSON)'));
355
- if (options.verbose === true) {
356
- console.log(chalk_1.default.red(error));
357
- }
358
- mainProcess.exit(1);
359
- }
360
- }
361
- }
362
- extra = {
363
- heading: 'Extra',
364
- column: 5,
365
- pages: [{ title: 'Release notes', source: 'release-notes.md' }],
366
- };
367
- meta.contents.push(extra);
368
- return [3 /*break*/, 5];
369
- case 4:
370
- error_2 = _b.sent();
371
- console.log(chalk_1.default.red('Error loading required JSON metadata files'));
372
- if (options.verbose === true) {
373
- console.log(chalk_1.default.red(error_2));
374
- }
375
- mainProcess.exit(1);
376
- return [3 /*break*/, 5];
377
- case 5: return [4 /*yield*/, loadMarkdown()];
378
- case 6:
379
- _b.sent();
380
- return [2 /*return*/];
381
- }
382
- });
383
- }); };
384
- /*
385
- load all markdown files (src)
386
- */
387
- var loadMarkdown = function () { return __awaiter(_this, void 0, void 0, function () {
388
- var keys_1, files_1, error_3;
389
- return __generator(this, function (_a) {
390
- switch (_a.label) {
391
- case 0:
392
- console.log(chalk_1.default.green('Loading src files'));
393
- _a.label = 1;
394
- case 1:
395
- _a.trys.push([1, 3, , 4]);
396
- keys_1 = [];
397
- files_1 = [];
398
- meta.contents.forEach(function (section) {
399
- section.pages.forEach(function (page) {
400
- keys_1.push(page);
401
- files_1.push(options.input + '/' + page.source);
402
- });
403
- });
404
- //add the release notes page
405
- keys_1.push('ownership');
406
- files_1.push(options.input + '/release-notes.md');
407
- return [4 /*yield*/, Promise.all(files_1.map(function (f) { return (0, fs_1.readFile)(f); }))];
408
- case 2:
409
- files_1 = _a.sent();
410
- files_1.forEach(function (page, index) {
411
- try {
412
- var key = keys_1[index];
413
- if (key.html === true) {
414
- //allow raw HTML input pages
415
- pages[key.source] = page;
416
- }
417
- else {
418
- //otherwise parse input from Markdown into HTML
419
- var html = markdown.render(page);
420
- pages[key.source] = html;
421
- }
422
- }
423
- catch (error) {
424
- console.log(chalk_1.default.red('Error parsing Markdown file: ' + file.source));
425
- if (options.verbose === true) {
426
- console.log(chalk_1.default.red(error));
427
- }
428
- mainProcess.exit(1);
429
- }
430
- });
431
- return [3 /*break*/, 4];
432
- case 3:
433
- error_3 = _a.sent();
434
- console.log(error_3);
435
- console.log(chalk_1.default.red('Error loading src files'));
436
- if (options.verbose === true) {
437
- console.log(chalk_1.default.red(error_3));
438
- }
439
- mainProcess.exit(1);
440
- return [3 /*break*/, 4];
441
- case 4: return [4 /*yield*/, processContent()];
442
- case 5:
443
- _a.sent();
444
- return [2 /*return*/];
445
- }
446
- });
447
- }); };
448
- var sortPages = function () {
449
- //sort the contents by heading
450
- var headings = { 1: [], 2: [], 3: [], 4: [], 5: [] };
451
- meta.contents.forEach(function (section) {
452
- if (headings.hasOwnProperty(section.column)) {
453
- headings[section.column].push(section);
454
- }
455
- });
456
- sortedPages = headings;
457
- };
458
- /*
459
- build the HTML for the table of contents
460
- */
461
- var webToc = function () {
462
- sortPages();
463
- var pdfName = meta.parameters.name.toLowerCase() + '.pdf';
464
- var $ = templates.main;
465
- var html = [], i = -1;
466
- html[++i] = '<div><table class="unstyled"><tr>';
467
- //build the contents HTML
468
- for (var key in sortedPages) {
469
- if (sortedPages.hasOwnProperty(key)) {
470
- if (key != 5) {
471
- //skip the extra column
472
- html[++i] = '<td class="dg-tocGroup">';
473
- sortedPages[key].forEach(function (section) {
474
- html[++i] =
475
- '<ul><li class="dg-tocHeading">' + section.heading + '</li>';
476
- section.pages.forEach(function (page) {
477
- var name = page.source.substr(0, page.source.lastIndexOf('.'));
478
- var path = name + '.html';
479
- html[++i] =
480
- '<li><a href="' + path + '">' + page.title + '</a></li>';
481
- });
482
- html[++i] = '</li></ul>';
483
- });
484
- html[++i] = '</td>';
485
- }
486
- }
487
- }
488
- //fixed-width column at end
489
- html[++i] = '<td class="dg-tocGroup" id="dg-tocFixedColumn"><ul>';
490
- html[++i] =
491
- '<li><span class="w-icon dg-tocIcon" data-name="person_group" title="archive"></span><a href="ownership.html">Ownership</a></li>';
492
- html[++i] =
493
- '<li><span class="w-icon dg-tocIcon" data-name="refresh" title="archive"></span><a href="release-notes.html">Release Notes</a></li>';
494
- html[++i] = '</ul><div>';
495
- if (options.pdf) {
496
- html[++i] =
497
- '<button class="whiteInverted" onclick="window.location=\'' +
498
- pdfName +
499
- '\';">';
500
- html[++i] = '<span>PDF</span>';
501
- html[++i] = '</button>';
502
- }
503
- html[++i] = '</div></td>';
504
- html[++i] = '</tr></table></div>';
505
- $('#dg-toc').html(html.join(''));
506
- templates.main = $;
507
- };
508
- /*
509
- insert the parameters into all templates
510
- */
511
- var insertParameters = function () {
512
- //------------------------------------------------------------------------------------------------------
513
- //logo dimensions
514
- var hasLogo = false;
515
- var logoWidth = 0;
516
- var logoHeight = 0;
517
- var logoPath;
518
- try {
519
- logoPath = 'files/images/logo.svg';
520
- var logo = (0, image_size_1.default)("".concat(options.input, "/").concat(logoPath));
521
- logoWidth = logo.width;
522
- logoHeight = logo.height;
523
- hasLogo = true;
524
- }
525
- catch (error) {
526
- //do nothing. If logo file cannot be read, logo is simply not shown
527
- }
528
- if (!hasLogo) {
529
- //PNG fallback
530
- try {
531
- logoPath = 'files/images/logo.png';
532
- var logo = (0, image_size_1.default)("".concat(options.input, "/").concat(logoPath));
533
- logoWidth = logo.width;
534
- logoHeight = logo.height;
535
- hasLogo = true;
536
- }
537
- catch (error) {
538
- //do nothing. If logo file cannot be read, logo is simply not shown
539
- }
540
- }
541
- //------------------------------------------------------------------------------------------------------
542
- //the homepage is the first link in the first heading
543
- var homelink = meta.contents[0].pages[0];
544
- homelink =
545
- homelink.source.substr(0, homelink.source.lastIndexOf('.')) + '.html';
546
- var date = (0, moment_1.default)().format('DD/MM/YYYY');
547
- var time = (0, moment_1.default)().format('HH:mm:ss');
548
- var year = (0, moment_1.default)().format('YYYY');
549
- var attribution = 'Created by DocGen ' + package_json_1.version + ' on ' + date + ' at ' + time + '.';
550
- var releaseVersion = meta.parameters.version;
551
- if (options.setVersion !== false) {
552
- releaseVersion = options.setVersion;
553
- }
554
- var releaseDate = meta.parameters.date;
555
- if (options.setReleaseDate !== false) {
556
- releaseDate = options.setReleaseDate;
557
- }
558
- var author = '';
559
- if (meta.parameters.author.url !== '') {
560
- author +=
561
- '<a href="' +
562
- meta.parameters.author.url +
563
- '">' +
564
- meta.parameters.author.name +
565
- '</a>';
566
- }
567
- else {
568
- author += meta.parameters.author.name;
569
- }
570
- var owner = '';
571
- if (meta.parameters.owner.url !== '') {
572
- owner +=
573
- '<a href="' +
574
- meta.parameters.owner.url +
575
- '">' +
576
- meta.parameters.owner.name +
577
- '</a>';
578
- }
579
- else {
580
- owner += meta.parameters.owner.name;
581
- }
582
- var organization = '';
583
- if (meta.parameters.organization.url !== '') {
584
- organization +=
585
- '<a href="' +
586
- meta.parameters.organization.url +
587
- '">' +
588
- meta.parameters.organization.name +
589
- '</a>';
590
- }
591
- else {
592
- organization += meta.parameters.organization.name;
593
- }
594
- var website = '';
595
- if (meta.parameters.website.url !== '') {
596
- website +=
597
- '<a href="' +
598
- meta.parameters.website.url +
599
- '">' +
600
- meta.parameters.website.name +
601
- '</a>';
602
- }
603
- else {
604
- website += meta.parameters.website.name;
605
- }
606
- var backlink = '';
607
- if (meta.parameters.backlink.url !== '') {
608
- backlink +=
609
- '<a class="button inverted" href="' +
610
- meta.parameters.backlink.url +
611
- '">' +
612
- meta.parameters.backlink.name +
613
- '</a>';
614
- }
615
- else {
616
- backlink += meta.parameters.backlink.name;
617
- }
618
- var sponsorLink = '';
619
- if (meta.parameters.sponsorLink) {
620
- sponsorLink = "\n <div id=\"headerSponsor\">\n <span>".concat(meta.parameters.sponsorLink.name, "</span>\n <a href=\"").concat(meta.parameters.sponsorLink.url, "\">\n <img id=\"sponsorLogo\" src=\"").concat(meta.parameters.sponsorLink.logo, "\" alt=\"sponsor logo\">\n </a>\n </div>\n ");
621
- }
622
- var contributors = '';
623
- meta.parameters.contributors.forEach(function (contributor) {
624
- if (contributor.url !== '') {
625
- contributors +=
626
- '<a href="' + contributor.url + '">' + contributor.name + '</a>, ';
627
- }
628
- else {
629
- contributors += contributor.name + ', ';
630
- }
631
- });
632
- contributors = contributors.replace(/,\s*$/, ''); //remove trailing commas
633
- var copyright = '&copy; ' + year + ' ' + organization;
634
- var webTitle = meta.parameters.title;
635
- var webFooter = 'Version ' + releaseVersion + ' released on ' + releaseDate + '.';
636
- for (var key in templates) {
637
- if (templates.hasOwnProperty(key)) {
638
- var $ = templates[key];
639
- //logo
640
- if (hasLogo === true) {
641
- var logoUrl = logoPath;
642
- $('#dg-logo').css('background-image', 'url(' + logoUrl + ')');
643
- $('#dg-logo').css('height', logoHeight + 'px');
644
- $('#dg-logo').css('padding-left', logoWidth + 25 + 'px');
645
- }
646
- else {
647
- $('#dg-logo').css('padding-left', '0');
648
- }
649
- //parameters
650
- $('title').text(meta.parameters.title);
651
- $('.dg-homelink').attr('href', homelink);
652
- $('#dg-title').text(meta.parameters.title);
653
- $('#dg-owner').html(owner);
654
- $('#dg-version').text(releaseVersion);
655
- $('#dg-web-title-version').text('(' + releaseVersion + ')');
656
- $('#dg-release-date').text(releaseDate);
657
- $('#dg-web-footer').text(webFooter);
658
- $('#dg-author').html(author);
659
- $('#dg-contributors').html(contributors);
660
- $('#dg-module').text(meta.parameters.module);
661
- $('#dg-id').html(meta.parameters.id);
662
- $('#dg-website').html(website);
663
- $('#dg-backlink').html(backlink);
664
- $('#headerLeftText').append(sponsorLink);
665
- $('#dg-summary').text(meta.parameters.summary);
666
- $('#dg-copyright').html(copyright);
667
- $('#dg-marking').text(meta.parameters.marking);
668
- $('#dg-legalese').text(meta.parameters.legalese);
669
- $('#dg-attribution').text(attribution);
670
- }
671
- }
672
- if (options.mathKatex === true) {
673
- var $ = templates.main;
674
- //support for KaTeX (bundled with DocGen)
675
- $('head').append('<link rel="stylesheet" href="require/katex/katex.min.css" type="text/css">');
676
- $('head').append('<script type="text/javascript" src="require/katex/katex.min.js"></script>');
677
- $('head').append('<script type="text/javascript" src="require/katexInjector.js"></script>');
678
- }
679
- if (options.mathMathjax === true) {
680
- //support for MathJax (only supported via CDN due to very large size)
681
- //MathJax configuration is the same as used by math.stackexchange.com
682
- //Note - wkhtmlpdf //cdn urls - see https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1634
683
- //Note - later than version 2 doesn't work with wkhtmltodpf
684
- $('head').append("<script type=\"text/javascript\" id=\"MathJax-script\" async\n src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML\">\n </script>");
685
- }
686
- };
687
- /*
688
- process each input into an output
689
- */
690
- var processContent = function () { return __awaiter(_this, void 0, void 0, function () {
691
- var $;
692
- return __generator(this, function (_a) {
693
- switch (_a.label) {
694
- case 0:
695
- console.log(chalk_1.default.green('Generating the static web content'));
696
- webToc();
697
- insertParameters();
698
- meta.contents.forEach(function (section) {
699
- section.pages.forEach(function (page) {
700
- var $ = cheerio_1.default.load(templates.main.html()); //clone
701
- var key = page.source;
702
- var content = pages[key];
703
- //add relevant container
704
- if (page.html === true) {
705
- //raw HTML pages should not be confined to the fixed width
706
- $('#dg-content').html('<div id="dg-innerContent"></div>');
707
- }
708
- else {
709
- //Markdown pages should be confined to the fixed width
710
- $('#dg-content').html('<div class="w-fixed-width"><div id="dg-innerContent"></div></div>');
711
- }
712
- $('#dg-innerContent').html(content);
713
- //------------------------------------------------------------------------------------------------------
714
- //insert permalinks for every page heading
715
- //when pageToc is enabled, also insert a page-level table of contents
716
- var html = [], i = -1;
717
- var headings = $('h1, h2, h3, h4, h5, h6');
718
- if (headings.length > 0) {
719
- html[++i] = '<ul class="dg-pageToc">';
720
- }
721
- headings.each(function () {
722
- var label = $(this).text();
723
- var anchor = label.toLowerCase().replace(/\s+/g, '-');
724
- $(this).attr('id', anchor);
725
- html[++i] = '<li><a href="#' + anchor + '">' + label + '</a></li>';
726
- });
727
- if (headings.length > 0) {
728
- html[++i] = '</ul>';
729
- }
730
- if (options.pageToc === true && page.html !== true) {
731
- $('#dg-innerContent').prepend(html.join(''));
732
- }
733
- //------------------------------------------------------------------------------------------------------
734
- //prepend the auto heading (which makes the PDF table of contents match the web TOC)
735
- $('#dg-innerContent').prepend('<h1 id="dg-autoTitle">' + page.title + '</h1>');
736
- if (page.html === true) {
737
- $('#dg-autoTitle').addClass('dg-hiddenTitle');
738
- }
739
- //------------------------------------------------------------------------------------------------------
740
- //apply the w-table class
741
- $('table:not(.unstyled)').addClass('w-table w-fixed w-stripe');
742
- //------------------------------------------------------------------------------------------------------
743
- pages[key] = $;
744
- });
745
- });
746
- $ = cheerio_1.default.load(templates.main.html());
747
- $('#dg-content').html('<div class="w-fixed-width"><div id="dg-innerContent"></div></div>');
748
- $('#dg-innerContent').html(templates.webCover.html());
749
- templates.webCover = $;
750
- return [4 /*yield*/, writePages()];
751
- case 1:
752
- _a.sent();
753
- return [2 /*return*/];
754
- }
755
- });
756
- }); };
757
- /*
758
- write each html page
759
- */
760
- var writePages = function () { return __awaiter(_this, void 0, void 0, function () {
761
- var promises_1, pdfTempDir, error_4;
762
- return __generator(this, function (_a) {
763
- switch (_a.label) {
764
- case 0:
765
- console.log(chalk_1.default.green('Writing the web page files'));
766
- _a.label = 1;
767
- case 1:
768
- _a.trys.push([1, 8, , 9]);
769
- promises_1 = {};
770
- meta.contents.forEach(function (section) {
771
- section.pages.forEach(function (page) {
772
- var key = page.source;
773
- var name = key.substr(0, page.source.lastIndexOf('.'));
774
- var path = options.output + name + '.html';
775
- var html = pages[key].html();
776
- promises_1[key] = (0, fs_1.writeFile)(path, html);
777
- });
778
- });
779
- //add extra files
780
- promises_1['ownership'] = (0, fs_1.writeFile)(options.output + 'ownership.html', templates.webCover.html());
781
- if (!(options.pdf === true)) return [3 /*break*/, 3];
782
- pdfTempDir = options.output + 'temp/';
783
- return [4 /*yield*/, (0, fs_1.makeDirectory)(pdfTempDir)];
784
- case 2:
785
- _a.sent();
786
- promises_1['docgenPdfCover'] = (0, fs_1.writeFile)(pdfTempDir + 'pdfCover.html', templates.pdfCover.html());
787
- promises_1['docgenPdfHeader'] = (0, fs_1.writeFile)(pdfTempDir + 'pdfHeader.html', templates.pdfHeader.html());
788
- promises_1['docgenPdfFooter'] = (0, fs_1.writeFile)(pdfTempDir + 'pdfFooter.html', templates.pdfFooter.html());
789
- _a.label = 3;
790
- case 3: return [4 /*yield*/, (0, fs_1.copyDirectory)(__dirname + '/../include/require', options.output + 'require', options.verbose === true)];
791
- case 4:
792
- _a.sent(); //CSS, JavaScript
793
- return [4 /*yield*/, (0, fs_1.copyDirectory)(options.input + '/files', options.output + 'files', options.verbose === true)];
794
- case 5:
795
- _a.sent(); //user-attached files and images
796
- if (!(options.mathKatex === true)) return [3 /*break*/, 7];
797
- return [4 /*yield*/, (0, fs_1.copyDirectory)(__dirname + '/../include/optional/katex', options.output + 'require/katex', options.verbose === true)];
798
- case 6:
799
- _a.sent();
800
- _a.label = 7;
801
- case 7: return [3 /*break*/, 9];
802
- case 8:
803
- error_4 = _a.sent();
804
- console.log(chalk_1.default.red('Error writing the web page files'));
805
- if (options.verbose === true) {
806
- console.log(chalk_1.default.red(error_4));
807
- }
808
- mainProcess.exit(1);
809
- return [3 /*break*/, 9];
810
- case 9: return [4 /*yield*/, checkPdfVersion()];
811
- case 10:
812
- _a.sent();
813
- return [2 /*return*/];
814
- }
815
- });
816
- }); };
817
- /*
818
- wkthmltopdf options
819
- */
820
- var pdfOptions = [
821
- ' --zoom 1.0',
822
- ' --image-quality 100',
823
- ' --print-media-type',
824
- ' --orientation portrait',
825
- ' --page-size A4',
826
- ' --margin-top 25',
827
- ' --margin-right 15',
828
- ' --margin-bottom 16',
829
- ' --margin-left 15',
830
- ' --header-spacing 5',
831
- ' --footer-spacing 5',
832
- ' --no-stop-slow-scripts',
833
- ];
834
- var getPdfArguments = function () {
835
- var pdfName = meta.parameters.name.toLowerCase() + '.pdf';
836
- pdfOptions.push(' --enable-local-file-access');
837
- pdfOptions.push(' --javascript-delay ' + options.pdfDelay); //code syntax highlight in wkhtmltopdf 0.12.2.1 fails without a delay (but why doesn't --no-stop-slow-scripts work?)
838
- pdfOptions.push(' --user-style-sheet ' + __dirname + '/../include/pdf-stylesheet.css');
839
- pdfOptions.push(' --header-html ' + options.output + 'temp/pdfHeader.html');
840
- pdfOptions.push(' --footer-html ' + options.output + 'temp/pdfFooter.html');
841
- pdfOptions.push(' cover ' + options.output + 'temp/pdfCover.html');
842
- pdfOptions.push(' toc --xsl-style-sheet ' + __dirname + '/../include/pdf-contents.xsl');
843
- var allPages = '';
844
- for (var key in sortedPages) {
845
- if (sortedPages.hasOwnProperty(key)) {
846
- sortedPages[key].forEach(function (section) {
847
- section.pages.forEach(function (page) {
848
- var key = page.source;
849
- var name = key.substr(0, page.source.lastIndexOf('.'));
850
- var path = options.output + name + '.html';
851
- allPages += ' ' + path;
852
- });
853
- });
854
- }
855
- }
856
- var args = pdfOptions.join('');
857
- args += allPages;
858
- args += ' ' + options.output + pdfName;
859
- return (0, spawn_args_1.default)(args);
860
- };
861
- var checkPdfVersion = function () { return __awaiter(_this, void 0, void 0, function () {
862
- return __generator(this, function (_a) {
863
- switch (_a.label) {
864
- case 0:
865
- if (!(options.pdf === true)) return [3 /*break*/, 1];
866
- //first check that wkhtmltopdf is installed
867
- (0, child_process_1.exec)(options.wkhtmltopdfPath + ' -V', function (error, stdout, stderr) {
868
- if (error) {
869
- console.log(chalk_1.default.red('Unable to call wkhtmltopdf. Is it installed and in path? See http://wkhtmltopdf.org'));
870
- if (options.verbose === true) {
871
- console.log(chalk_1.default.red(error));
872
- }
873
- mainProcess.exit(1);
874
- }
875
- else {
876
- //warn if the version of wkhtmltopdf is not an expected version
877
- var actualWkhtmltopdfVersion = stdout.trim();
878
- if (actualWkhtmltopdfVersion !== wkhtmltopdfVersion) {
879
- var warning = 'Warning: unexpected version of wkhtmltopdf, which may work but is not tested or supported';
880
- var expectedVersion = ' expected version: ' + wkhtmltopdfVersion;
881
- var detectedVersion = ' detected version: ' + actualWkhtmltopdfVersion;
882
- console.log(chalk_1.default.yellow(warning));
883
- console.log(chalk_1.default.yellow(expectedVersion));
884
- console.log(chalk_1.default.yellow(detectedVersion));
885
- }
886
- generatePdf();
887
- }
888
- });
889
- return [3 /*break*/, 3];
890
- case 1: return [4 /*yield*/, cleanUp()];
891
- case 2:
892
- _a.sent();
893
- _a.label = 3;
894
- case 3: return [2 /*return*/];
895
- }
896
- });
897
- }); };
898
- /*
899
- call wkhtmltopdf as an external executable
900
- */
901
- var generatePdf = function () { return __awaiter(_this, void 0, void 0, function () {
902
- var args, wkhtmltopdf, spinner;
903
- var _this = this;
904
- return __generator(this, function (_a) {
905
- console.log(chalk_1.default.green('Creating the PDF copy (may take some time)'));
906
- args = getPdfArguments();
907
- wkhtmltopdf = (0, child_process_1.spawn)(options.wkhtmltopdfPath, args);
908
- spinner = new cli_spinner_1.Spinner(chalk_1.default.green(' Processing... %s'));
909
- spinner.setSpinnerString('|/-\\');
910
- wkhtmltopdf.on('error', function (error) {
911
- console.log(chalk_1.default.red('Error calling wkhtmltopdf to generate the PDF'));
912
- if (options.verbose === true) {
913
- console.log(chalk_1.default.red(error));
914
- }
915
- });
916
- if (options.verbose !== true) {
917
- spinner.start(); //only show spinner when verbose is off (otherwise show raw wkhtmltopdf output)
918
- }
919
- else {
920
- //pipe the output from wkhtmltopdf back to stdout
921
- //however, wkhtmltpdf outputs to stderr, not stdout. See:
922
- //https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1980
923
- wkhtmltopdf.stderr.pipe(mainProcess.stdout);
924
- }
925
- wkhtmltopdf.stdout.on('data', function (data) {
926
- //do nothing
927
- });
928
- wkhtmltopdf.stderr.on('data', function (data) {
929
- //do nothing
930
- });
931
- wkhtmltopdf.on('close', function (code) { return __awaiter(_this, void 0, void 0, function () {
932
- var warning;
933
- return __generator(this, function (_a) {
934
- switch (_a.label) {
935
- case 0:
936
- if (options.verbose !== true) {
937
- spinner.stop();
938
- console.log(''); //newline after spinner stops
939
- }
940
- if (code !== 0) {
941
- warning = 'wkhtmltopdf exited with a warning or error: try the -v option for details';
942
- console.log(chalk_1.default.yellow(warning));
943
- }
944
- return [4 /*yield*/, cleanUp()];
945
- case 1:
946
- _a.sent();
947
- return [2 /*return*/];
948
- }
949
- });
950
- }); });
951
- return [2 /*return*/];
952
- });
953
- }); };
954
- var createRedirect = function () { return __awaiter(_this, void 0, void 0, function () {
955
- var parent_1, homepage, redirectLink, $, file, error_5;
956
- return __generator(this, function (_a) {
957
- switch (_a.label) {
958
- case 0:
959
- if (!options.redirect) return [3 /*break*/, 4];
960
- parent_1 = options.output.replace(/\/$/, '');
961
- parent_1 = parent_1.split(path_1.default.sep).slice(-1).pop(); //get name of final directory in the path
962
- homepage = meta.contents[0].pages[0];
963
- homepage =
964
- homepage.source.substr(0, homepage.source.lastIndexOf('.')) + '.html';
965
- redirectLink = parent_1 + '/' + homepage;
966
- $ = templates.redirect;
967
- $('a').attr('href', redirectLink);
968
- $('meta[http-equiv=REFRESH]').attr('content', '0;url=' + redirectLink);
969
- file = options.output + '../' + 'index.html';
970
- _a.label = 1;
971
- case 1:
972
- _a.trys.push([1, 3, , 4]);
973
- return [4 /*yield*/, (0, fs_1.writeFile)(file, $.html())];
974
- case 2:
975
- _a.sent();
976
- return [3 /*break*/, 4];
977
- case 3:
978
- error_5 = _a.sent();
979
- console.log(chalk_1.default.red('Error writing redirect file: ' + file));
980
- if (options.verbose === true) {
981
- console.log(chalk_1.default.red(error_5));
982
- }
983
- return [3 /*break*/, 4];
984
- case 4: return [2 /*return*/];
985
- }
986
- });
987
- }); };
988
- /*
989
- cleanup
990
- */
991
- var cleanUp = function () { return __awaiter(_this, void 0, void 0, function () {
992
- return __generator(this, function (_a) {
993
- switch (_a.label) {
994
- case 0: return [4 /*yield*/, createRedirect()];
995
- case 1:
996
- _a.sent();
997
- if (!(options.pdf === true)) return [3 /*break*/, 3];
998
- return [4 /*yield*/, (0, fs_1.removeDirectory)(options.output + 'temp')];
999
- case 2:
1000
- _a.sent();
1001
- _a.label = 3;
1002
- case 3:
1003
- console.log(chalk_1.default.green.bold('Done!'));
1004
- return [2 /*return*/];
1005
- }
1006
- });
1007
- }); };
1008
- }
1009
- exports.DocGen = DocGen;