hdoc-tools 0.9.16 → 0.9.18

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.
package/hdoc-build-pdf.js CHANGED
@@ -7,7 +7,6 @@
7
7
  fs = require('fs-extra'),
8
8
  mime = require('mime-types'),
9
9
  path = require('path'),
10
- puppeteer = require('puppeteer'),
11
10
  hdoc = require(path.join(__dirname, 'hdoc-module.js'));
12
11
 
13
12
  const dree_options = {
@@ -99,7 +98,7 @@
99
98
  return html_source;
100
99
  };
101
100
 
102
- exports.generate_pdf = async function (pdf_template_path, pdf_template_content, book_config, html_source, target_file, verbose = false) {
101
+ exports.generate_pdf = async function (browser, pdf_template_path, pdf_template_content, book_config, html_source, target_file, verbose = false) {
103
102
  if (verbose) console.log(`Generating PDF: ${target_file}`);
104
103
  let pdf_size = 0;
105
104
  // Cache footer
@@ -120,9 +119,6 @@
120
119
 
121
120
  html_source = pdf_template_content.replace('{{book_title}}', book_config.title).replace('{{document_content}}', html_source).replace('{{hb_logo}}', hb_logo);
122
121
 
123
- // Create a browser instance
124
- const browser = await puppeteer.launch();
125
-
126
122
  // Create a new page
127
123
  const page = await browser.newPage();
128
124
 
@@ -166,9 +162,7 @@
166
162
  } catch (err) {
167
163
  console.log(`Error generating PDF ${target_file} - ${err}`);
168
164
  }
169
-
170
- // Close the browser instance
171
- await browser.close();
165
+ await page.close();
172
166
  return pdf_size;
173
167
  };
174
168
  })();
package/hdoc-build.js CHANGED
@@ -6,6 +6,7 @@
6
6
  fs = require('fs-extra'),
7
7
  mdfm = require('markdown-it-front-matter'),
8
8
  path = require('path'),
9
+ puppeteer = require('puppeteer'),
9
10
  URL = require("url").URL,
10
11
  hdoc_validate = require(path.join(__dirname, 'hdoc-validate.js')),
11
12
  hdoc = require(path.join(__dirname, 'hdoc-module.js')),
@@ -23,11 +24,12 @@
23
24
  non_git_pdf_header_template_path = path.join(__dirname, 'templates', 'pdf-header-non-git.html'),
24
25
  pdf_template_path = path.join(__dirname, 'templates', 'pdf'),
25
26
  pdf_template_file_path = path.join(pdf_template_path, 'template.html'),
26
- regex_version = /^[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}$/;
27
+ regex_version = /^[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,6}$/;
27
28
 
28
29
  let bc = {}, // Breadcrumbs map
29
- book_read_time = 0,
30
30
  built_file_hashes = [],
31
+ book_read_time = 0,
32
+ browser = {},
31
33
  conversion_attempted = 0,
32
34
  conversion_success = 0,
33
35
  conversion_failed = 0,
@@ -238,7 +240,7 @@
238
240
 
239
241
  // Generate PDF file from HTML
240
242
  const pdf_file_path = file_path.path.replace(path.extname(file_path.path), '.pdf');
241
- pdf_size = await hdoc_build_pdf.generate_pdf(pdf_template_path, pdf_template, hdocbook_config, pdf_txt, pdf_file_path, verbose);
243
+ pdf_size = await hdoc_build_pdf.generate_pdf(browser, pdf_template_path, pdf_template, hdocbook_config, pdf_txt, pdf_file_path, verbose);
242
244
  }
243
245
 
244
246
  html_txt = `${fm_header_content}\n${doc_header}\n${html_txt}`;
@@ -448,7 +450,7 @@
448
450
 
449
451
  // Generate PDF file from HTML
450
452
  const pdf_file_path = file_path.path.replace(path.extname(file_path.path), '.pdf');
451
- pdf_size = await hdoc_build_pdf.generate_pdf(pdf_template_path, pdf_template, hdocbook_config, pdf_txt, pdf_file_path, verbose);
453
+ pdf_size = await hdoc_build_pdf.generate_pdf(browser, pdf_template_path, pdf_template, hdocbook_config, pdf_txt, pdf_file_path, verbose);
452
454
  }
453
455
 
454
456
  html_txt = `${fm_header}\n${doc_header}\n${html_txt}`;
@@ -572,6 +574,7 @@
572
574
  }
573
575
  verbose = verbose_output;
574
576
 
577
+ const start_time = Date.now();
575
578
  // GERRY: The purpose of this function is to create a zip file containing the hdocbook content,
576
579
  // * Create a _work folder
577
580
  // * copy the hdocbook content to the work folder
@@ -688,6 +691,11 @@
688
691
  // Get a list of MD files in work_path
689
692
  dree.scan(work_path, dreeOptions, build_file_callback);
690
693
 
694
+ if (pdf_enable) {
695
+ // Create a Chromium browser instance to generate PDFs with
696
+ browser = await puppeteer.launch({headless: 'new'});
697
+ }
698
+
691
699
  // Work through MD files and convert to HTML
692
700
  for (let i = 0; i < md_files.length; i++) {
693
701
  await transform_markdown_and_save_html(md_files[i]);
@@ -698,6 +706,11 @@
698
706
  await transform_static_html(static_html_files[i]);
699
707
  }
700
708
 
709
+ if (pdf_enable) {
710
+ // Close the Chromium browser instance
711
+ await browser.close();
712
+ }
713
+
701
714
  // Output to console
702
715
  console.log(` MD files found: ${conversion_attempted}`);
703
716
  console.log(`Successfully converted to HTML: ${conversion_success}`);
@@ -710,6 +723,8 @@
710
723
  // Validate content
711
724
  const validation_success = await hdoc_validate.run(work_path, doc_id, verbose, hdocbook_config, hdocbook_project, bc);
712
725
  if (!validation_success) {
726
+ const end_time = Date.now();
727
+ console.log(`Time Taken: ${get_duration(start_time, end_time)}\n`);
713
728
  process.exit(1);
714
729
  }
715
730
 
@@ -761,5 +776,21 @@
761
776
  } else {
762
777
  console.log('\nValidation Complete\n');
763
778
  }
779
+ const end_time = Date.now();
780
+ console.log(`Time Taken: ${get_duration(start_time, end_time)}\n`);
764
781
  };
782
+
783
+ const get_duration = function(start, end) {
784
+ const total_time = new Date(end - start).toISOString().slice(11,19);
785
+ const duration_arr = total_time.split(':');
786
+ let duration = '';
787
+ if (parseInt(duration_arr[0], 10) > 0) {
788
+ duration += parseInt(duration_arr[0], 10) + 'h ';
789
+ }
790
+ if (duration !== '' || parseInt(duration_arr[1], 10)) {
791
+ duration += parseInt(duration_arr[1], 10) + 'm ';
792
+ }
793
+ duration += parseInt(duration_arr[2], 10) + 's';
794
+ return duration;
795
+ }
765
796
  })();
package/hdoc-module.js CHANGED
@@ -379,5 +379,4 @@
379
379
  }
380
380
  return bc;
381
381
  };
382
-
383
382
  })();
package/hdoc.js CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
  (async function () {
3
3
  'use strict';
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hdoc-tools",
3
- "version": "0.9.16",
3
+ "version": "0.9.18",
4
4
  "description": "Hornbill HDocBook Development Support Tool",
5
5
  "main": "hdoc.js",
6
6
  "bin": {
@@ -50,7 +50,7 @@
50
50
  "mime-types": "^2.1.35",
51
51
  "multer": "^1.4.5-lts.1",
52
52
  "prompt": "^1.3.0",
53
- "puppeteer": "^19.6.0",
53
+ "puppeteer": "^19.8.0",
54
54
  "stream": "0.0.2",
55
55
  "words-count": "^2.0.2",
56
56
  "zip-a-folder": "^1.1.5"