hdoc-tools 0.11.7 → 0.11.8

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
@@ -1,173 +1,173 @@
1
- (function () {
2
- 'use strict';
3
-
4
- const axios = require('axios'),
5
- cheerio = require('cheerio'),
6
- fs = require('fs-extra'),
7
- mime = require('mime-types'),
8
- path = require('path'),
9
- hdoc = require(path.join(__dirname, 'hdoc-module.js'));
10
-
11
- let hb_logo = '',
12
- footer = '',
13
- header = '';
14
-
15
- const get_footer = function (template_path) {
16
- let footer_content = null;
17
- try {
18
- footer_content = fs.readFileSync(path.join(template_path, 'template-footer.html'), 'utf8');
19
- } catch (err) {
20
- console.log(`Error loading template: ${err}`);
21
- }
22
- return footer_content;
23
- };
24
-
25
- const get_header = function (template_path) {
26
- let header_content = null;
27
- try {
28
- header_content = fs.readFileSync(path.join(template_path, 'template-header.html'), 'utf8');
29
- } catch (err) {
30
- console.log(`Error loading template: ${err}`);
31
- }
32
- return header_content;
33
- };
34
-
35
- exports.process_images = async function (file_path, html_source, verbose) {
36
- const book_work_root = file_path.path.replace(file_path.relativePath, '');
37
- if (verbose) console.log('Parsing img tags from HTML source');
38
-
39
- // Use cheerio to parse html
40
- const $ = cheerio.load(html_source);
41
-
42
- // Get iFrames from HTML, to replace with a tags
43
- let iframes = [];
44
- const iframe_html = $('iframe').map(function () {
45
- const response = {
46
- html: $.html(this),
47
- src: $(this).attr('src'),
48
- title: $(this).attr('title') ? $(this).attr('title') : 'No Link Title Provided'
49
- };
50
- return response;
51
- }).get();
52
- iframes.push(...iframe_html);
53
- for (let i = 0; i < iframes.length; i++) {
54
- const link = `<p><a href="${iframes[i].src}">${iframes[i].title}</a></p>`;
55
- const regex = new RegExp(`<iframe.*src="${iframes[i].src.replace('/', '\\/')}".*</iframe>`);
56
- html_source = html_source.replace(regex, link);
57
- }
58
-
59
- // Get image links from HTML, to embed into the pdf
60
- let imgs = [];
61
- const srcs = $('img').map(function (i) {
62
- return $(this).attr('src');
63
- }).get();
64
- imgs.push(...srcs);
65
- for (let i = 0; i < imgs.length; i++) {
66
- if (!hdoc.valid_url(imgs[i])) {
67
- // Internal link
68
- const image_path = path.join(book_work_root, imgs[i].replace('_books/', ''));
69
- try {
70
- const image_buffer = fs.readFileSync(image_path);
71
- const mime_type = mime.lookup(image_path);
72
- let image_b64 = image_buffer.toString("base64");
73
- image_b64 = `data:${mime_type};base64,${image_b64}`;
74
- html_source = html_source.replace(imgs[i], image_b64);
75
- } catch (err) {
76
- console.log('Error reading image from HTML source [', image_path, '] -', err);
77
- return null;
78
- }
79
- } else {
80
- // External Link
81
- try {
82
- const file_response = await axios.get(imgs[i]);
83
- if (file_response.status === 200) {
84
- const image_buffer = file_response.data;
85
- const mime_type = mime.lookup(imgs[i]);
86
- let image_b64 = image_buffer.toString("base64");
87
- image_b64 = `data:${mime_type};base64,${image_b64}`;
88
- html_source = html_source.replace(imgs[i], image_b64);
89
- } else {
90
- throw `Unexpected Status ${file_response.status}`;
91
- }
92
- } catch (err) {
93
- console.log(`Error downloading external source [${imgs[i]}] - ${err}`);
94
- }
95
- }
96
- }
97
-
98
- return html_source;
99
- };
100
-
101
- exports.generate_pdf = async function (browser, pdf_template_path, pdf_template_content, book_config, html_source, target_file, css_templates, verbose = false) {
102
- let pdf_size = 0;
103
-
104
- // Cache footer
105
- if (footer === '') footer = get_footer(pdf_template_path);
106
-
107
- // Read svg logo file into buffer, convert to B64 string
108
- if (hb_logo === '') {
109
- const hb_logo_path = path.join(pdf_template_path, 'images', 'hornbill-logo-full.svg');
110
- try {
111
- const hb_logo_file_buffer = fs.readFileSync(hb_logo_path);
112
- hb_logo = hb_logo_file_buffer.toString("base64");
113
- hb_logo = `data:image/svg+xml;base64,${hb_logo}`;
114
- } catch (err) {
115
- console.log('Error reading logo from template:', err);
116
- return pdf_size;
117
- }
118
- }
119
-
120
- // Cache header
121
- if (header === '') {
122
- header = get_header(pdf_template_path).replace('{{book_title}}', book_config.title).replace('{{hb_logo}}', hb_logo);
123
- }
124
-
125
- html_source = pdf_template_content.replace('{{book_title}}', book_config.title).replace('{{document_content}}', html_source);
126
-
127
- const page = await browser.newPage();
128
-
129
- // To reflect CSS used for screens instead of print
130
- await page.emulateMediaType('screen');
131
-
132
- // Set HTML content from HTML source
133
- await page.setContent(html_source, {
134
- waitUntil: 'domcontentloaded'
135
- });
136
- for (let i = 0; i < css_templates.length; i++) {
137
- try {
138
- await page.addStyleTag({
139
- content: css_templates[i]
140
- });
141
- } catch (e) {
142
- console.log(`Error applying template for [${target_file}]: ${e}`);
143
- }
144
- }
145
-
146
- try {
147
- const pdf_gen = await page.pdf({
148
- path: target_file,
149
- printBackground: true,
150
- format: 'A4',
151
- displayHeaderFooter: true,
152
- headerTemplate: header,
153
- footerTemplate: footer,
154
- margin: {
155
- top: "90px",
156
- right: "30px",
157
- bottom: "60px",
158
- left: "30px"
159
- },
160
- timeout: 0
161
- });
162
- let currdate = new Date;
163
- let datetime = currdate.toISOString();
164
- if (verbose) console.log(`[${datetime}] PDF generation success: ${target_file}`);
165
-
166
- pdf_size = pdf_gen.byteLength;
167
- } catch (err) {
168
- console.log(`Error generating PDF ${target_file} - ${err}`);
169
- }
170
- await page.close();
171
- return pdf_size;
172
- };
1
+ (function () {
2
+ 'use strict';
3
+
4
+ const axios = require('axios'),
5
+ cheerio = require('cheerio'),
6
+ fs = require('fs-extra'),
7
+ mime = require('mime-types'),
8
+ path = require('path'),
9
+ hdoc = require(path.join(__dirname, 'hdoc-module.js'));
10
+
11
+ let hb_logo = '',
12
+ footer = '',
13
+ header = '';
14
+
15
+ const get_footer = function (template_path) {
16
+ let footer_content = null;
17
+ try {
18
+ footer_content = fs.readFileSync(path.join(template_path, 'template-footer.html'), 'utf8');
19
+ } catch (err) {
20
+ console.log(`Error loading template: ${err}`);
21
+ }
22
+ return footer_content;
23
+ };
24
+
25
+ const get_header = function (template_path) {
26
+ let header_content = null;
27
+ try {
28
+ header_content = fs.readFileSync(path.join(template_path, 'template-header.html'), 'utf8');
29
+ } catch (err) {
30
+ console.log(`Error loading template: ${err}`);
31
+ }
32
+ return header_content;
33
+ };
34
+
35
+ exports.process_images = async function (file_path, html_source, verbose) {
36
+ const book_work_root = file_path.path.replace(file_path.relativePath, '');
37
+ if (verbose) console.log('Parsing img tags from HTML source');
38
+
39
+ // Use cheerio to parse html
40
+ const $ = cheerio.load(html_source);
41
+
42
+ // Get iFrames from HTML, to replace with a tags
43
+ let iframes = [];
44
+ const iframe_html = $('iframe').map(function () {
45
+ const response = {
46
+ html: $.html(this),
47
+ src: $(this).attr('src'),
48
+ title: $(this).attr('title') ? $(this).attr('title') : 'No Link Title Provided'
49
+ };
50
+ return response;
51
+ }).get();
52
+ iframes.push(...iframe_html);
53
+ for (let i = 0; i < iframes.length; i++) {
54
+ const link = `<p><a href="${iframes[i].src}">${iframes[i].title}</a></p>`;
55
+ const regex = new RegExp(`<iframe.*src="${iframes[i].src.replace('/', '\\/')}".*</iframe>`);
56
+ html_source = html_source.replace(regex, link);
57
+ }
58
+
59
+ // Get image links from HTML, to embed into the pdf
60
+ let imgs = [];
61
+ const srcs = $('img').map(function (i) {
62
+ return $(this).attr('src');
63
+ }).get();
64
+ imgs.push(...srcs);
65
+ for (let i = 0; i < imgs.length; i++) {
66
+ if (!hdoc.valid_url(imgs[i])) {
67
+ // Internal link
68
+ const image_path = path.join(book_work_root, imgs[i].replace('_books/', ''));
69
+ try {
70
+ const image_buffer = fs.readFileSync(image_path);
71
+ const mime_type = mime.lookup(image_path);
72
+ let image_b64 = image_buffer.toString("base64");
73
+ image_b64 = `data:${mime_type};base64,${image_b64}`;
74
+ html_source = html_source.replace(imgs[i], image_b64);
75
+ } catch (err) {
76
+ console.log('Error reading image from HTML source [', image_path, '] -', err);
77
+ return null;
78
+ }
79
+ } else {
80
+ // External Link
81
+ try {
82
+ const file_response = await axios.get(imgs[i]);
83
+ if (file_response.status === 200) {
84
+ const image_buffer = file_response.data;
85
+ const mime_type = mime.lookup(imgs[i]);
86
+ let image_b64 = image_buffer.toString("base64");
87
+ image_b64 = `data:${mime_type};base64,${image_b64}`;
88
+ html_source = html_source.replace(imgs[i], image_b64);
89
+ } else {
90
+ throw `Unexpected Status ${file_response.status}`;
91
+ }
92
+ } catch (err) {
93
+ console.log(`Error downloading external source [${imgs[i]}] - ${err}`);
94
+ }
95
+ }
96
+ }
97
+
98
+ return html_source;
99
+ };
100
+
101
+ exports.generate_pdf = async function (browser, pdf_template_path, pdf_template_content, book_config, html_source, target_file, css_templates, verbose = false) {
102
+ let pdf_size = 0;
103
+
104
+ // Cache footer
105
+ if (footer === '') footer = get_footer(pdf_template_path);
106
+
107
+ // Read svg logo file into buffer, convert to B64 string
108
+ if (hb_logo === '') {
109
+ const hb_logo_path = path.join(pdf_template_path, 'images', 'hornbill-logo-full.svg');
110
+ try {
111
+ const hb_logo_file_buffer = fs.readFileSync(hb_logo_path);
112
+ hb_logo = hb_logo_file_buffer.toString("base64");
113
+ hb_logo = `data:image/svg+xml;base64,${hb_logo}`;
114
+ } catch (err) {
115
+ console.log('Error reading logo from template:', err);
116
+ return pdf_size;
117
+ }
118
+ }
119
+
120
+ // Cache header
121
+ if (header === '') {
122
+ header = get_header(pdf_template_path).replace('{{book_title}}', book_config.title).replace('{{hb_logo}}', hb_logo);
123
+ }
124
+
125
+ html_source = pdf_template_content.replace('{{book_title}}', book_config.title).replace('{{document_content}}', html_source);
126
+
127
+ const page = await browser.newPage();
128
+
129
+ // To reflect CSS used for screens instead of print
130
+ await page.emulateMediaType('screen');
131
+
132
+ // Set HTML content from HTML source
133
+ await page.setContent(html_source, {
134
+ waitUntil: 'domcontentloaded'
135
+ });
136
+ for (let i = 0; i < css_templates.length; i++) {
137
+ try {
138
+ await page.addStyleTag({
139
+ content: css_templates[i]
140
+ });
141
+ } catch (e) {
142
+ console.log(`Error applying template for [${target_file}]: ${e}`);
143
+ }
144
+ }
145
+
146
+ try {
147
+ const pdf_gen = await page.pdf({
148
+ path: target_file,
149
+ printBackground: true,
150
+ format: 'A4',
151
+ displayHeaderFooter: true,
152
+ headerTemplate: header,
153
+ footerTemplate: footer,
154
+ margin: {
155
+ top: "90px",
156
+ right: "30px",
157
+ bottom: "60px",
158
+ left: "30px"
159
+ },
160
+ timeout: 0
161
+ });
162
+ let currdate = new Date;
163
+ let datetime = currdate.toISOString();
164
+ if (verbose) console.log(`[${datetime}] PDF generation success: ${target_file}`);
165
+
166
+ pdf_size = pdf_gen.byteLength;
167
+ } catch (err) {
168
+ console.log(`Error generating PDF ${target_file} - ${err}`);
169
+ }
170
+ await page.close();
171
+ return pdf_size;
172
+ };
173
173
  })();
package/hdoc-build.js CHANGED
@@ -179,6 +179,7 @@
179
179
 
180
180
  // Get contributor data from Github, if exists
181
181
  let contribs = [];
182
+ let last_commit = null;
182
183
  if (hdocbook_config.publicSource && hdocbook_config.publicSource !== '' && hdocbook_config.publicSource.includes('github.com/Hornbill-Docs')) {
183
184
 
184
185
  const github_paths = hdoc.get_github_api_path(hdocbook_config.publicSource, file_path.relativePath);
@@ -187,6 +188,7 @@
187
188
  if (!contributors.success) {
188
189
  console.log(`Error retrieving contributors from Github: ${contributors.error}`);
189
190
  } else {
191
+ last_commit = contributors.last_commit_date;
190
192
  metadata.last_commit = contributors.last_commit_date;
191
193
  metadata.contributor_count = contributors.contributor_count;
192
194
  metadata.edit_url = github_paths.edit_path;
@@ -254,7 +256,8 @@
254
256
  metadata: metadata,
255
257
  contributors: contribs,
256
258
  pdf_size: pdf_size,
257
- md5: file_path.hash
259
+ md5: file_path.hash,
260
+ lastmod: last_commit !== null ? last_commit : file_path.hb_lastmod
258
261
  });
259
262
  // Save HTML into HTML file
260
263
  try {
@@ -387,6 +390,7 @@
387
390
 
388
391
  // Get contributor data from Github, if exists
389
392
  let contribs = [];
393
+ let last_commit = null;
390
394
  if (hdocbook_config.publicSource && hdocbook_config.publicSource !== '' && hdocbook_config.publicSource.includes('github.com/Hornbill-Docs')) {
391
395
 
392
396
  const github_paths = hdoc.get_github_api_path(hdocbook_config.publicSource, file_path.relativePath);
@@ -395,6 +399,7 @@
395
399
  if (!contributors.success) {
396
400
  console.log(`Error retrieving contributors from Github: ${contributors.error}`);
397
401
  } else {
402
+ last_commit = contributors.last_commit_date;
398
403
  metadata.last_commit = contributors.last_commit_date;
399
404
  metadata.contributor_count = contributors.contributor_count;
400
405
  metadata.edit_url = github_paths.edit_path;
@@ -475,7 +480,7 @@
475
480
  contributors: contribs,
476
481
  pdf_size: pdf_size,
477
482
  md5: file_path.hash,
478
- lastmod: file_path.hb_lastmod
483
+ lastmod: last_commit !== null ? last_commit : file_path.hb_lastmod
479
484
  });
480
485
 
481
486
  // Add MD file to delete queue
package/hdoc-bump.js CHANGED
@@ -1,109 +1,109 @@
1
- (function () {
2
- 'use strict';
3
-
4
- const fs = require('fs'),
5
- path = require('path');
6
-
7
- exports.run = function (source_path, bump_type) {
8
- if (bump_type !== 'patch' && bump_type !== 'minor' && bump_type !== 'major') {
9
- console.log(`Unsupported bump type: ${bump_type}`);
10
- process.exit(1);
11
- }
12
- console.log(`Bumping ${bump_type} book version...\n`);
13
-
14
- // Get document ID
15
- const hdocbook_project_config_path = path.join(source_path, 'hdocbook-project.json');
16
- let hdocbook_project;
17
- try {
18
- hdocbook_project = require(hdocbook_project_config_path);
19
- } catch (e) {
20
- console.log('File not found: hdocbook-project.json:');
21
- console.log(e, '\n');
22
- console.log('hdoc bump needs to be run in the root of a HDoc Book.\n');
23
- process.exit(1);
24
- }
25
- const doc_id = hdocbook_project.docId;
26
-
27
- const book_path = path.join(source_path, doc_id),
28
- hdocbook_path = path.join(book_path, 'hdocbook.json');
29
-
30
- let hdocbook_config;
31
- try {
32
- hdocbook_config = require(hdocbook_path);
33
- } catch (e) {
34
- console.log('File not found: hdocbook.json');
35
- console.log(e, '\n');
36
- console.log('hdoc bump needs to be run in the root of a HDoc Book.\n');
37
- process.exit(1);
38
- }
39
- const initial_version = hdocbook_config.version;
40
- let hdocbook_version = hdocbook_config.version.split('.');
41
- if (hdocbook_version.length !== 3) {
42
- console.log(`Book version does not appear to be in a semantic versioning format: ${initial_version}`);
43
- process.exit(1);
44
- }
45
-
46
- if (isNaN(hdocbook_version[0])) {
47
- console.log(`Existing major version is not a number: ${hdocbook_version[0]}`);
48
- process.exit(1);
49
- }
50
- if (isNaN(hdocbook_version[1])) {
51
- console.log(`Existing minor version is not a number: ${hdocbook_version[1]}`);
52
- process.exit(1);
53
- }
54
- if (isNaN(hdocbook_version[2])) {
55
- console.log(`Existing patch version is not a number: ${hdocbook_version[2]}`);
56
- process.exit(1);
57
- }
58
-
59
- switch (bump_type) {
60
- case 'major':
61
- try {
62
- hdocbook_version[0] = parseInt(hdocbook_version[0], 10) + 1;
63
- hdocbook_version[1] = 0;
64
- hdocbook_version[2] = 0;
65
- } catch (e) {
66
- console.log('Failed to update major version:');
67
- console.log(e);
68
- process.exit(1);
69
- }
70
- break;
71
- case 'minor':
72
- try {
73
- hdocbook_version[0] = parseInt(hdocbook_version[0], 10);
74
- hdocbook_version[1] = parseInt(hdocbook_version[1], 10) + 1;
75
- hdocbook_version[2] = 0;
76
- } catch (e) {
77
- console.log('Failed to update minor version:');
78
- console.log(e);
79
- process.exit(1);
80
- }
81
- break;
82
- case 'patch':
83
- default:
84
- try {
85
- hdocbook_version[0] = parseInt(hdocbook_version[0], 10);
86
- hdocbook_version[1] = parseInt(hdocbook_version[1], 10);
87
- hdocbook_version[2] = parseInt(hdocbook_version[2], 10) + 1;
88
- } catch (e) {
89
- console.log('Failed to update patch version:');
90
- console.log(e);
91
- process.exit(1);
92
- }
93
- break;
94
- }
95
-
96
- hdocbook_config.version = hdocbook_version.join('.');
97
-
98
- try {
99
- fs.writeFileSync(hdocbook_path, JSON.stringify(hdocbook_config, null, 2));
100
- } catch (e) {
101
- console.log('Error writing bumped version to book config:', e);
102
- process.exit(1);
103
- }
104
-
105
- console.log(`Book version updated from ${initial_version} to ${hdocbook_config.version}\n`);
106
- return true;
107
- };
108
-
1
+ (function () {
2
+ 'use strict';
3
+
4
+ const fs = require('fs'),
5
+ path = require('path');
6
+
7
+ exports.run = function (source_path, bump_type) {
8
+ if (bump_type !== 'patch' && bump_type !== 'minor' && bump_type !== 'major') {
9
+ console.log(`Unsupported bump type: ${bump_type}`);
10
+ process.exit(1);
11
+ }
12
+ console.log(`Bumping ${bump_type} book version...\n`);
13
+
14
+ // Get document ID
15
+ const hdocbook_project_config_path = path.join(source_path, 'hdocbook-project.json');
16
+ let hdocbook_project;
17
+ try {
18
+ hdocbook_project = require(hdocbook_project_config_path);
19
+ } catch (e) {
20
+ console.log('File not found: hdocbook-project.json:');
21
+ console.log(e, '\n');
22
+ console.log('hdoc bump needs to be run in the root of a HDoc Book.\n');
23
+ process.exit(1);
24
+ }
25
+ const doc_id = hdocbook_project.docId;
26
+
27
+ const book_path = path.join(source_path, doc_id),
28
+ hdocbook_path = path.join(book_path, 'hdocbook.json');
29
+
30
+ let hdocbook_config;
31
+ try {
32
+ hdocbook_config = require(hdocbook_path);
33
+ } catch (e) {
34
+ console.log('File not found: hdocbook.json');
35
+ console.log(e, '\n');
36
+ console.log('hdoc bump needs to be run in the root of a HDoc Book.\n');
37
+ process.exit(1);
38
+ }
39
+ const initial_version = hdocbook_config.version;
40
+ let hdocbook_version = hdocbook_config.version.split('.');
41
+ if (hdocbook_version.length !== 3) {
42
+ console.log(`Book version does not appear to be in a semantic versioning format: ${initial_version}`);
43
+ process.exit(1);
44
+ }
45
+
46
+ if (isNaN(hdocbook_version[0])) {
47
+ console.log(`Existing major version is not a number: ${hdocbook_version[0]}`);
48
+ process.exit(1);
49
+ }
50
+ if (isNaN(hdocbook_version[1])) {
51
+ console.log(`Existing minor version is not a number: ${hdocbook_version[1]}`);
52
+ process.exit(1);
53
+ }
54
+ if (isNaN(hdocbook_version[2])) {
55
+ console.log(`Existing patch version is not a number: ${hdocbook_version[2]}`);
56
+ process.exit(1);
57
+ }
58
+
59
+ switch (bump_type) {
60
+ case 'major':
61
+ try {
62
+ hdocbook_version[0] = parseInt(hdocbook_version[0], 10) + 1;
63
+ hdocbook_version[1] = 0;
64
+ hdocbook_version[2] = 0;
65
+ } catch (e) {
66
+ console.log('Failed to update major version:');
67
+ console.log(e);
68
+ process.exit(1);
69
+ }
70
+ break;
71
+ case 'minor':
72
+ try {
73
+ hdocbook_version[0] = parseInt(hdocbook_version[0], 10);
74
+ hdocbook_version[1] = parseInt(hdocbook_version[1], 10) + 1;
75
+ hdocbook_version[2] = 0;
76
+ } catch (e) {
77
+ console.log('Failed to update minor version:');
78
+ console.log(e);
79
+ process.exit(1);
80
+ }
81
+ break;
82
+ case 'patch':
83
+ default:
84
+ try {
85
+ hdocbook_version[0] = parseInt(hdocbook_version[0], 10);
86
+ hdocbook_version[1] = parseInt(hdocbook_version[1], 10);
87
+ hdocbook_version[2] = parseInt(hdocbook_version[2], 10) + 1;
88
+ } catch (e) {
89
+ console.log('Failed to update patch version:');
90
+ console.log(e);
91
+ process.exit(1);
92
+ }
93
+ break;
94
+ }
95
+
96
+ hdocbook_config.version = hdocbook_version.join('.');
97
+
98
+ try {
99
+ fs.writeFileSync(hdocbook_path, JSON.stringify(hdocbook_config, null, 2));
100
+ } catch (e) {
101
+ console.log('Error writing bumped version to book config:', e);
102
+ process.exit(1);
103
+ }
104
+
105
+ console.log(`Book version updated from ${initial_version} to ${hdocbook_config.version}\n`);
106
+ return true;
107
+ };
108
+
109
109
  })();