docgen-tool 3.2.14 → 3.3.1

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,28 +1,66 @@
1
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
+ };
2
41
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var rsvp = require('rsvp');
4
- var fs = require('fs-extra');
5
- var path = require('path');
6
- var cheerio = require('cheerio');
7
- var markdown = require('markdown-it')('commonmark').enable('table');
8
- var moment = require('moment');
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"));
9
48
  var child_process_1 = require("child_process");
10
- var schemaValidator = require('z-schema');
11
- var chalk = require('chalk');
12
- var spawnArgs = require('spawn-args');
13
- var cliSpinner = require('cli-spinner').Spinner;
14
- var imageSizeOf = require('image-size');
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");
15
54
  var package_json_1 = require("../../package.json");
55
+ var markdown = new markdown_it_1.default('commonmark').enable('table');
16
56
  //Allow CommonMark links that use other protocols, such as file:///
17
57
  //The markdown-it implementation is more restrictive than the CommonMark spec
18
58
  //See https://github.com/markdown-it/markdown-it/issues/108
19
59
  markdown.validateLink = function () {
20
60
  return true;
21
61
  };
22
- /**
23
- * DocGen class
24
- */
25
62
  function DocGen(process) {
63
+ var _this = this;
26
64
  var mainProcess = process;
27
65
  var wkhtmltopdfVersion = 'wkhtmltopdf 0.12.6 (with patched qt)'; //output from wkhtmltopdf -V
28
66
  var options;
@@ -37,150 +75,102 @@ function DocGen(process) {
37
75
  options = userOptions;
38
76
  //all user-specified paths must be normalized
39
77
  if (options.input) {
40
- options.input = path.normalize(options.input + '/');
78
+ options.input = path_1.default.normalize(options.input + '/');
41
79
  }
42
80
  if (options.output) {
43
- options.output = path.normalize(options.output + '/');
81
+ options.output = path_1.default.normalize(options.output + '/');
44
82
  }
45
83
  //wkhtmltopdf path does not need a trailing slash
46
84
  if (options.wkhtmltopdfPath && options.wkhtmltopdfPath !== '') {
47
- options.wkhtmltopdfPath = path.normalize(options.wkhtmltopdfPath);
85
+ options.wkhtmltopdfPath = path_1.default.normalize(options.wkhtmltopdfPath);
48
86
  }
49
87
  };
50
88
  /*
51
89
  copy the example src files (template) to any directory, when scaffold command is invoked
52
90
  */
53
- this.scaffold = function () {
54
- console.log(chalk.green('Creating scaffold template directory'));
55
- copyDirSync(__dirname + '/../include/example', options.output);
56
- };
57
- this.run = function () {
58
- console.log(chalk.green.bold('DocGen version ' + package_json_1.version));
59
- //delete and recreate the output directory
60
- remakeDirSync(options.output);
61
- loadTemplates();
62
- };
63
- /*
64
- read any file (async)
65
- */
66
- var readFile = function (filePath) {
67
- var normalized = path.normalize(filePath);
68
- return new rsvp.Promise(function (resolve, reject) {
69
- fs.readFile(normalized, 'utf8', function (error, data) {
70
- if (error) {
71
- console.log(chalk.red('Error reading file: ' + normalized));
72
- reject(error);
73
- }
74
- else {
75
- data = data.replace(/^\uFEFF/, ''); //remove the BOM (byte-order-mark) from UTF-8 files, if present
76
- resolve(data);
77
- }
78
- });
79
- });
80
- };
81
- /*
82
- write any file (async)
83
- */
84
- var writeFile = function (filePath, data) {
85
- var normalized = path.normalize(filePath);
86
- return new rsvp.Promise(function (resolve, reject) {
87
- fs.writeFile(normalized, data, function (error) {
88
- if (error) {
89
- console.log(chalk.red('Error writing file: ' + normalized));
90
- reject(error);
91
- }
92
- else {
93
- resolve(true);
94
- }
95
- });
96
- });
97
- };
98
- /*
99
- copy any directory (sync)
100
- */
101
- var copyDirSync = function (source, destination) {
102
- var normalizedSource = path.normalize(source);
103
- var normalizedDestination = path.normalize(destination);
104
- try {
105
- fs.copySync(normalizedSource, normalizedDestination);
106
- }
107
- catch (error) {
108
- console.log(chalk.red('Error copying directory: ' +
109
- normalizedSource +
110
- ' to ' +
111
- normalizedDestination));
112
- if (options.verbose === true) {
113
- console.log(chalk.red(error));
114
- mainProcess.exit(1);
115
- }
116
- }
117
- };
118
- /*
119
- remake a directory (sync) ... remove and then mkdir in one operation
120
- */
121
- var remakeDirSync = function (directoryPath) {
122
- var normalized = path.normalize(directoryPath);
123
- try {
124
- fs.removeSync(normalized);
125
- fs.mkdirpSync(normalized);
126
- }
127
- catch (error) {
128
- console.log(chalk.red('Error recreating directory: ' + normalized));
129
- if (options.verbose === true) {
130
- console.log(chalk.red(error));
131
- mainProcess.exit(1);
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*/];
132
100
  }
133
- }
134
- };
135
- /*
136
- remove any directory (sync)
137
- */
138
- var removeDirSync = function (directoryPath) {
139
- var normalized = path.normalize(directoryPath);
140
- try {
141
- fs.removeSync(normalized);
142
- }
143
- catch (error) {
144
- console.log(chalk.red('Error removing directory: ' + normalized));
145
- if (options.verbose === true) {
146
- console.log(chalk.red(error));
147
- mainProcess.exit(1);
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*/];
148
115
  }
149
- }
150
- };
116
+ });
117
+ }); };
151
118
  /*
152
119
  load all HTML template files
153
120
  */
154
- var loadTemplates = function () {
155
- console.log(chalk.green('Loading templates'));
156
- var files = {
157
- main: readFile(__dirname + '/../include/templates/main.html'),
158
- redirect: readFile(__dirname + '/../include/templates/redirect.html'),
159
- webCover: readFile(__dirname + '/../include/templates/webCover.html'),
160
- pdfCover: readFile(__dirname + '/../include/templates/pdfCover.html'),
161
- pdfHeader: readFile(__dirname + '/../include/templates/pdfHeader.html'),
162
- pdfFooter: readFile(__dirname + '/../include/templates/pdfFooter.html'),
163
- };
164
- rsvp
165
- .hash(files)
166
- .then(function (files) {
167
- for (var key in files) {
168
- if (files.hasOwnProperty(key)) {
169
- var file = files[key];
170
- var dom = cheerio.load(file);
171
- templates[key] = dom;
172
- }
173
- }
174
- loadMeta();
175
- })
176
- .catch(function (error) {
177
- console.log(chalk.red('Error loading templates'));
178
- if (options.verbose === true) {
179
- console.log(chalk.red(error));
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*/];
180
171
  }
181
- mainProcess.exit(1);
182
172
  });
183
- };
173
+ }); };
184
174
  /*
185
175
  JSON schema validation
186
176
  */
@@ -315,14 +305,14 @@ function DocGen(process) {
315
305
  };
316
306
  var validateJSON = function (key, data) {
317
307
  var schema = schemas[key];
318
- var validator = new schemaValidator();
308
+ var validator = new z_schema_1.default();
319
309
  var valid = validator.validate(data, schema);
320
310
  if (!valid) {
321
- console.log(chalk.red('Error parsing required file: ' +
311
+ console.log(chalk_1.default.red('Error parsing required file: ' +
322
312
  key +
323
313
  '.json (failed schema validation)'));
324
314
  if (options.verbose === true) {
325
- console.log(chalk.red(validator.getLastError()));
315
+ console.log(chalk_1.default.red(validator.getLastError()));
326
316
  }
327
317
  }
328
318
  return valid;
@@ -330,106 +320,131 @@ function DocGen(process) {
330
320
  /*
331
321
  load all metadata files (JSON)
332
322
  */
333
- var loadMeta = function () {
334
- console.log(chalk.green('Loading required JSON metadata files'));
335
- var files = {
336
- parameters: readFile(options.input + '/parameters.json'),
337
- contents: readFile(options.input + '/contents.json'),
338
- };
339
- rsvp
340
- .hash(files)
341
- .then(function (files) {
342
- for (var key in files) {
343
- if (files.hasOwnProperty(key)) {
344
- //ignore prototype
345
- try {
346
- var file = JSON.parse(files[key]);
347
- if (validateJSON(key, file)) {
348
- meta[key] = file;
349
- }
350
- else {
351
- mainProcess.exit(1);
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
+ }
352
360
  }
353
361
  }
354
- catch (error) {
355
- console.log(chalk.red('Error parsing required file: ' +
356
- key +
357
- '.json (invalid JSON)'));
358
- if (options.verbose === true) {
359
- console.log(chalk.red(error));
360
- }
361
- mainProcess.exit(1);
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));
362
374
  }
363
- }
364
- }
365
- //add the release notes to the contents list
366
- var extra = {
367
- heading: 'Extra',
368
- column: 5,
369
- pages: [{ title: 'Release notes', source: 'release-notes.md' }],
370
- };
371
- meta.contents.push(extra);
372
- loadMarkdown();
373
- })
374
- .catch(function (error) {
375
- console.log(chalk.red('Error loading required JSON metadata files'));
376
- if (options.verbose === true) {
377
- console.log(chalk.red(error));
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*/];
378
381
  }
379
- mainProcess.exit(1);
380
382
  });
381
- };
383
+ }); };
382
384
  /*
383
385
  load all markdown files (src)
384
386
  */
385
- var loadMarkdown = function () {
386
- console.log(chalk.green('Loading src files'));
387
- var keys = [];
388
- var files = [];
389
- meta.contents.forEach(function (section) {
390
- section.pages.forEach(function (page) {
391
- keys.push(page);
392
- files.push(options.input + '/' + page.source);
393
- });
394
- });
395
- //add the release notes page
396
- keys.push('ownership');
397
- files.push(options.input + '/release-notes.md');
398
- rsvp
399
- .all(files.map(readFile))
400
- .then(function (files) {
401
- files.forEach(function (page, index) {
402
- try {
403
- var key = keys[index];
404
- if (key.html === true) {
405
- //allow raw HTML input pages
406
- pages[key.source] = page;
407
- }
408
- else {
409
- //otherwise parse input from Markdown into HTML
410
- var html = markdown.render(page);
411
- pages[key.source] = html;
412
- }
413
- }
414
- catch (error) {
415
- console.log(chalk.red('Error parsing Markdown file: ' + file.source));
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'));
416
436
  if (options.verbose === true) {
417
- console.log(chalk.red(error));
437
+ console.log(chalk_1.default.red(error_3));
418
438
  }
419
439
  mainProcess.exit(1);
420
- }
421
- });
422
- processContent();
423
- })
424
- .catch(function (error) {
425
- console.log(error);
426
- console.log(chalk.red('Error loading src files'));
427
- if (options.verbose === true) {
428
- console.log(chalk.red(error));
440
+ return [3 /*break*/, 4];
441
+ case 4: return [4 /*yield*/, processContent()];
442
+ case 5:
443
+ _a.sent();
444
+ return [2 /*return*/];
429
445
  }
430
- mainProcess.exit(1);
431
446
  });
432
- };
447
+ }); };
433
448
  var sortPages = function () {
434
449
  //sort the contents by heading
435
450
  var headings = { 1: [], 2: [], 3: [], 4: [], 5: [] };
@@ -502,7 +517,7 @@ function DocGen(process) {
502
517
  var logoPath;
503
518
  try {
504
519
  logoPath = 'files/images/logo.svg';
505
- var logo = imageSizeOf("".concat(options.input, "/").concat(logoPath));
520
+ var logo = (0, image_size_1.default)("".concat(options.input, "/").concat(logoPath));
506
521
  logoWidth = logo.width;
507
522
  logoHeight = logo.height;
508
523
  hasLogo = true;
@@ -514,7 +529,7 @@ function DocGen(process) {
514
529
  //PNG fallback
515
530
  try {
516
531
  logoPath = 'files/images/logo.png';
517
- var logo = imageSizeOf("".concat(options.input, "/").concat(logoPath));
532
+ var logo = (0, image_size_1.default)("".concat(options.input, "/").concat(logoPath));
518
533
  logoWidth = logo.width;
519
534
  logoHeight = logo.height;
520
535
  hasLogo = true;
@@ -528,9 +543,9 @@ function DocGen(process) {
528
543
  var homelink = meta.contents[0].pages[0];
529
544
  homelink =
530
545
  homelink.source.substr(0, homelink.source.lastIndexOf('.')) + '.html';
531
- var date = moment().format('DD/MM/YYYY');
532
- var time = moment().format('HH:mm:ss');
533
- var year = moment().format('YYYY');
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');
534
549
  var attribution = 'Created by DocGen ' + package_json_1.version + ' on ' + date + ' at ' + time + '.';
535
550
  var releaseVersion = meta.parameters.version;
536
551
  if (options.setVersion !== false) {
@@ -672,107 +687,133 @@ function DocGen(process) {
672
687
  /*
673
688
  process each input into an output
674
689
  */
675
- var processContent = function () {
676
- console.log(chalk.green('Generating the static web content'));
677
- webToc();
678
- insertParameters();
679
- meta.contents.forEach(function (section) {
680
- section.pages.forEach(function (page) {
681
- var $ = cheerio.load(templates.main.html()); //clone
682
- var key = page.source;
683
- var content = pages[key];
684
- //add relevant container
685
- if (page.html === true) {
686
- //raw HTML pages should not be confined to the fixed width
687
- $('#dg-content').html('<div id="dg-innerContent"></div>');
688
- }
689
- else {
690
- //Markdown pages should be confined to the fixed width
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());
691
747
  $('#dg-content').html('<div class="w-fixed-width"><div id="dg-innerContent"></div></div>');
692
- }
693
- $('#dg-innerContent').html(content);
694
- //------------------------------------------------------------------------------------------------------
695
- //insert permalinks for every page heading
696
- //when pageToc is enabled, also insert a page-level table of contents
697
- var html = [], i = -1;
698
- var headings = $('h1, h2, h3, h4, h5, h6');
699
- if (headings.length > 0) {
700
- html[++i] = '<ul class="dg-pageToc">';
701
- }
702
- headings.each(function () {
703
- var label = $(this).text();
704
- var anchor = label.toLowerCase().replace(/\s+/g, '-');
705
- $(this).attr('id', anchor);
706
- html[++i] = '<li><a href="#' + anchor + '">' + label + '</a></li>';
707
- });
708
- if (headings.length > 0) {
709
- html[++i] = '</ul>';
710
- }
711
- if (options.pageToc === true && page.html !== true) {
712
- $('#dg-innerContent').prepend(html.join(''));
713
- }
714
- //------------------------------------------------------------------------------------------------------
715
- //prepend the auto heading (which makes the PDF table of contents match the web TOC)
716
- $('#dg-innerContent').prepend('<h1 id="dg-autoTitle">' + page.title + '</h1>');
717
- if (page.html === true) {
718
- $('#dg-autoTitle').addClass('dg-hiddenTitle');
719
- }
720
- //------------------------------------------------------------------------------------------------------
721
- //apply the w-table class
722
- $('table:not(.unstyled)').addClass('w-table w-fixed w-stripe');
723
- //------------------------------------------------------------------------------------------------------
724
- pages[key] = $;
725
- });
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
+ }
726
755
  });
727
- //add web ownership page
728
- var $ = cheerio.load(templates.main.html()); //clone
729
- $('#dg-content').html('<div class="w-fixed-width"><div id="dg-innerContent"></div></div>');
730
- $('#dg-innerContent').html(templates.webCover.html());
731
- templates.webCover = $;
732
- writePages();
733
- };
756
+ }); };
734
757
  /*
735
758
  write each html page
736
759
  */
737
- var writePages = function () {
738
- console.log(chalk.green('Writing the web page files'));
739
- var promises = {};
740
- meta.contents.forEach(function (section) {
741
- section.pages.forEach(function (page) {
742
- var key = page.source;
743
- var name = key.substr(0, page.source.lastIndexOf('.'));
744
- var path = options.output + name + '.html';
745
- var html = pages[key].html();
746
- promises[key] = writeFile(path, html);
747
- });
748
- });
749
- //add extra files
750
- promises['ownership'] = writeFile(options.output + 'ownership.html', templates.webCover.html());
751
- if (options.pdf === true) {
752
- var pdfTempDir = options.output + 'temp/';
753
- fs.mkdirsSync(pdfTempDir);
754
- promises['docgenPdfCover'] = writeFile(pdfTempDir + 'pdfCover.html', templates.pdfCover.html());
755
- promises['docgenPdfHeader'] = writeFile(pdfTempDir + 'pdfHeader.html', templates.pdfHeader.html());
756
- promises['docgenPdfFooter'] = writeFile(pdfTempDir + 'pdfFooter.html', templates.pdfFooter.html());
757
- }
758
- rsvp
759
- .hash(promises)
760
- .then(function () {
761
- copyDirSync(__dirname + '/../include/require', options.output + 'require'); //CSS, JavaScript
762
- copyDirSync(options.input + '/files', options.output + 'files'); //user-attached files and images
763
- if (options.mathKatex === true) {
764
- copyDirSync(__dirname + '/../include/optional/katex', options.output + 'require/katex');
765
- }
766
- checkPdfVersion();
767
- })
768
- .catch(function (error) {
769
- console.log(chalk.red('Error writing the web page files'));
770
- if (options.verbose === true) {
771
- console.log(chalk.red(error));
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*/];
772
814
  }
773
- mainProcess.exit(1);
774
815
  });
775
- };
816
+ }); };
776
817
  /*
777
818
  wkthmltopdf options
778
819
  */
@@ -815,114 +856,154 @@ function DocGen(process) {
815
856
  var args = pdfOptions.join('');
816
857
  args += allPages;
817
858
  args += ' ' + options.output + pdfName;
818
- return spawnArgs(args);
819
- };
820
- var checkPdfVersion = function () {
821
- if (options.pdf === true) {
822
- //first check that wkhtmltopdf is installed
823
- (0, child_process_1.exec)(options.wkhtmltopdfPath + ' -V', function (error, stdout, stderr) {
824
- if (error) {
825
- console.log(chalk.red('Unable to call wkhtmltopdf. Is it installed and in path? See http://wkhtmltopdf.org'));
826
- if (options.verbose === true) {
827
- console.log(chalk.red(error));
828
- }
829
- mainProcess.exit(1);
830
- }
831
- else {
832
- //warn if the version of wkhtmltopdf is not an expected version
833
- var actualWkhtmltopdfVersion = stdout.trim();
834
- if (actualWkhtmltopdfVersion !== wkhtmltopdfVersion) {
835
- var warning = 'Warning: unexpected version of wkhtmltopdf, which may work but is not tested or supported';
836
- var expectedVersion = ' expected version: ' + wkhtmltopdfVersion;
837
- var detectedVersion = ' detected version: ' + actualWkhtmltopdfVersion;
838
- console.log(chalk.yellow(warning));
839
- console.log(chalk.yellow(expectedVersion));
840
- console.log(chalk.yellow(detectedVersion));
841
- }
842
- generatePdf();
843
- }
844
- });
845
- }
846
- else {
847
- cleanUp();
848
- }
859
+ return (0, spawn_args_1.default)(args);
849
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
+ }); };
850
898
  /*
851
899
  call wkhtmltopdf as an external executable
852
900
  */
853
- var generatePdf = function () {
854
- console.log(chalk.green('Creating the PDF copy (may take some time)'));
855
- var args = getPdfArguments();
856
- var wkhtmltopdf = (0, child_process_1.spawn)(options.wkhtmltopdfPath, args);
857
- var spinner = new cliSpinner(chalk.green(' Processing... %s'));
858
- spinner.setSpinnerString('|/-\\');
859
- wkhtmltopdf.on('error', function (error) {
860
- console.log(chalk.red('Error calling wkhtmltopdf to generate the PDF'));
861
- if (options.verbose === true) {
862
- console.log(chalk.red(error));
863
- }
864
- });
865
- if (options.verbose !== true) {
866
- spinner.start(); //only show spinner when verbose is off (otherwise show raw wkhtmltopdf output)
867
- }
868
- else {
869
- //pipe the output from wkhtmltopdf back to stdout
870
- //however, wkhtmltpdf outputs to stderr, not stdout. See:
871
- //https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1980
872
- wkhtmltopdf.stderr.pipe(mainProcess.stdout);
873
- }
874
- wkhtmltopdf.stdout.on('data', function (data) {
875
- //do nothing
876
- });
877
- wkhtmltopdf.stderr.on('data', function (data) {
878
- //do nothing
879
- });
880
- wkhtmltopdf.on('close', function (code) {
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
+ });
881
916
  if (options.verbose !== true) {
882
- spinner.stop();
883
- console.log(''); //newline after spinner stops
917
+ spinner.start(); //only show spinner when verbose is off (otherwise show raw wkhtmltopdf output)
884
918
  }
885
- if (code !== 0) {
886
- var warning = 'wkhtmltopdf exited with a warning or error: try the -v option for details';
887
- console.log(chalk.yellow(warning));
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);
888
924
  }
889
- cleanUp();
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*/];
890
952
  });
891
- };
892
- var createRedirect = function () {
893
- if (options.redirect) {
894
- var parent_1 = options.output.replace(/\/$/, ''); //trim any trailing slash
895
- parent_1 = parent_1.split(path.sep).slice(-1).pop(); //get name of final directory in the path
896
- var homepage = meta.contents[0].pages[0];
897
- homepage =
898
- homepage.source.substr(0, homepage.source.lastIndexOf('.')) + '.html';
899
- var redirectLink = parent_1 + '/' + homepage;
900
- var $ = templates.redirect;
901
- $('a').attr('href', redirectLink);
902
- $('meta[http-equiv=REFRESH]').attr('content', '0;url=' + redirectLink);
903
- var file = options.output + '../' + 'index.html';
904
- try {
905
- fs.outputFileSync(file, $.html(), 'utf-8');
906
- }
907
- catch (error) {
908
- console.log(chalk.red('Error writing redirect file: ' + file));
909
- if (options.verbose === true) {
910
- console.log(chalk.red(error));
911
- }
912
- //don't exit because redirect error is not a fatal error
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*/];
913
985
  }
914
- }
915
- };
986
+ });
987
+ }); };
916
988
  /*
917
989
  cleanup
918
990
  */
919
- var cleanUp = function () {
920
- createRedirect();
921
- //remove temp files
922
- if (options.pdf === true) {
923
- removeDirSync(options.output + 'temp');
924
- }
925
- console.log(chalk.green.bold('Done!'));
926
- };
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
+ }); };
927
1008
  }
928
- module.exports = DocGen;
1009
+ exports.DocGen = DocGen;