hdoc-tools 0.19.8 → 0.20.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.
- package/custom_modules/tips.js +88 -86
- package/hdoc-build-db.js +260 -198
- package/hdoc-build-pdf.js +219 -173
- package/hdoc-build.js +1480 -1505
- package/hdoc-bump.js +123 -109
- package/hdoc-create.js +108 -95
- package/hdoc-db.js +97 -125
- package/hdoc-help.js +48 -51
- package/hdoc-init.js +181 -147
- package/hdoc-module.js +708 -723
- package/hdoc-serve.js +390 -361
- package/hdoc-stats.js +187 -184
- package/hdoc-validate.js +956 -717
- package/hdoc-ver.js +43 -43
- package/hdoc.js +142 -124
- package/package.json +60 -60
- package/templates/init/_hdocbook/hdocbook.json +23 -23
- package/templates/init/hdocbook-project.json +10 -10
- package/templates/init/package.json +8 -10
- package/ui/js/doc.hornbill.js +708 -659
- package/validateNodeVer.js +17 -14
package/hdoc-build-pdf.js
CHANGED
@@ -1,173 +1,219 @@
|
|
1
|
-
(
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
1
|
+
(() => {
|
2
|
+
const axios = require("axios");
|
3
|
+
const cheerio = require("cheerio");
|
4
|
+
const fs = require("fs-extra");
|
5
|
+
const mime = require("mime-types");
|
6
|
+
const path = require("node:path");
|
7
|
+
const hdoc = require(path.join(__dirname, "hdoc-module.js"));
|
8
|
+
|
9
|
+
let hb_logo = "";
|
10
|
+
let footer = "";
|
11
|
+
let header = "";
|
12
|
+
|
13
|
+
const get_footer = (template_path) => {
|
14
|
+
let footer_content = null;
|
15
|
+
try {
|
16
|
+
footer_content = fs.readFileSync(
|
17
|
+
path.join(template_path, "template-footer.html"),
|
18
|
+
"utf8",
|
19
|
+
);
|
20
|
+
} catch (err) {
|
21
|
+
console.error(`Error loading template: ${err}`);
|
22
|
+
}
|
23
|
+
return footer_content;
|
24
|
+
};
|
25
|
+
|
26
|
+
const get_header = (template_path) => {
|
27
|
+
let header_content = null;
|
28
|
+
try {
|
29
|
+
header_content = fs.readFileSync(
|
30
|
+
path.join(template_path, "template-header.html"),
|
31
|
+
"utf8",
|
32
|
+
);
|
33
|
+
} catch (err) {
|
34
|
+
console.error(`Error loading template: ${err}`);
|
35
|
+
}
|
36
|
+
return header_content;
|
37
|
+
};
|
38
|
+
|
39
|
+
exports.process_images = async (file_path, html_source, verbose) => {
|
40
|
+
const book_work_root = file_path.path.replace(file_path.relativePath, "");
|
41
|
+
if (verbose) console.log("Parsing img tags from HTML source");
|
42
|
+
|
43
|
+
let processed_html_source = html_source;
|
44
|
+
// Use cheerio to parse html
|
45
|
+
const $ = cheerio.load(processed_html_source);
|
46
|
+
|
47
|
+
// Get iFrames from HTML, to replace with a tags
|
48
|
+
const iframes = [];
|
49
|
+
const iframe_html = $("iframe")
|
50
|
+
.map(function () {
|
51
|
+
const response = {
|
52
|
+
html: $.html(this),
|
53
|
+
src: $(this).attr("src"),
|
54
|
+
title: $(this).attr("title")
|
55
|
+
? $(this).attr("title")
|
56
|
+
: "No Link Title Provided",
|
57
|
+
};
|
58
|
+
return response;
|
59
|
+
})
|
60
|
+
.get();
|
61
|
+
iframes.push(...iframe_html);
|
62
|
+
for (let i = 0; i < iframes.length; i++) {
|
63
|
+
const link = `<p><a href="${iframes[i].src}">${iframes[i].title}</a></p>`;
|
64
|
+
const regex = new RegExp(
|
65
|
+
`<iframe.*src="${iframes[i].src.replace("/", "\\/")}".*</iframe>`,
|
66
|
+
);
|
67
|
+
processed_html_source = processed_html_source.replace(regex, link);
|
68
|
+
}
|
69
|
+
|
70
|
+
// Get image links from HTML, to embed into the pdf
|
71
|
+
const imgs = [];
|
72
|
+
const srcs = $("img")
|
73
|
+
.map(function (i) {
|
74
|
+
return $(this).attr("src");
|
75
|
+
})
|
76
|
+
.get();
|
77
|
+
imgs.push(...srcs);
|
78
|
+
for (let i = 0; i < imgs.length; i++) {
|
79
|
+
if (!hdoc.valid_url(imgs[i])) {
|
80
|
+
// Internal link
|
81
|
+
const image_path = path.join(
|
82
|
+
book_work_root,
|
83
|
+
imgs[i].replace("_books/", ""),
|
84
|
+
);
|
85
|
+
try {
|
86
|
+
const image_buffer = fs.readFileSync(image_path);
|
87
|
+
const mime_type = mime.lookup(image_path);
|
88
|
+
let image_b64 = image_buffer.toString("base64");
|
89
|
+
image_b64 = `data:${mime_type};base64,${image_b64}`;
|
90
|
+
processed_html_source = processed_html_source.replace(
|
91
|
+
imgs[i],
|
92
|
+
image_b64,
|
93
|
+
);
|
94
|
+
} catch (err) {
|
95
|
+
console.error(
|
96
|
+
"Error reading image from HTML source [",
|
97
|
+
image_path,
|
98
|
+
"] -",
|
99
|
+
err,
|
100
|
+
);
|
101
|
+
return null;
|
102
|
+
}
|
103
|
+
} else {
|
104
|
+
// External Link
|
105
|
+
try {
|
106
|
+
const file_response = await axios.get(imgs[i]);
|
107
|
+
if (file_response.status === 200) {
|
108
|
+
const image_buffer = file_response.data;
|
109
|
+
const mime_type = mime.lookup(imgs[i]);
|
110
|
+
let image_b64 = image_buffer.toString("base64");
|
111
|
+
image_b64 = `data:${mime_type};base64,${image_b64}`;
|
112
|
+
processed_html_source = processed_html_source.replace(
|
113
|
+
imgs[i],
|
114
|
+
image_b64,
|
115
|
+
);
|
116
|
+
} else {
|
117
|
+
throw `Unexpected Status ${file_response.status}`;
|
118
|
+
}
|
119
|
+
} catch (err) {
|
120
|
+
console.error(
|
121
|
+
`Error downloading external source [${imgs[i]}] - ${err}`,
|
122
|
+
);
|
123
|
+
}
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
return processed_html_source;
|
128
|
+
};
|
129
|
+
|
130
|
+
exports.generate_pdf = async (
|
131
|
+
browser,
|
132
|
+
pdf_template_path,
|
133
|
+
pdf_template_content,
|
134
|
+
book_config,
|
135
|
+
html_source,
|
136
|
+
target_file,
|
137
|
+
css_templates,
|
138
|
+
verbose = false,
|
139
|
+
) => {
|
140
|
+
let pdf_size = 0;
|
141
|
+
// Cache footer
|
142
|
+
if (footer === "") footer = get_footer(pdf_template_path);
|
143
|
+
|
144
|
+
// Read svg logo file into buffer, convert to B64 string
|
145
|
+
if (hb_logo === "") {
|
146
|
+
const hb_logo_path = path.join(
|
147
|
+
pdf_template_path,
|
148
|
+
"images",
|
149
|
+
"hornbill-logo-full.svg",
|
150
|
+
);
|
151
|
+
try {
|
152
|
+
const hb_logo_file_buffer = fs.readFileSync(hb_logo_path);
|
153
|
+
hb_logo = hb_logo_file_buffer.toString("base64");
|
154
|
+
hb_logo = `data:image/svg+xml;base64,${hb_logo}`;
|
155
|
+
} catch (err) {
|
156
|
+
console.error("Error reading logo from template:", err);
|
157
|
+
return pdf_size;
|
158
|
+
}
|
159
|
+
}
|
160
|
+
|
161
|
+
// Cache header
|
162
|
+
if (header === "") {
|
163
|
+
header = get_header(pdf_template_path)
|
164
|
+
.replace("{{book_title}}", book_config.title)
|
165
|
+
.replace("{{hb_logo}}", hb_logo);
|
166
|
+
}
|
167
|
+
|
168
|
+
const processed_html_source = pdf_template_content
|
169
|
+
.replace("{{book_title}}", book_config.title)
|
170
|
+
.replace("{{document_content}}", html_source);
|
171
|
+
|
172
|
+
const page = await browser.newPage();
|
173
|
+
|
174
|
+
// To reflect CSS used for screens instead of print
|
175
|
+
await page.emulateMediaType("screen");
|
176
|
+
|
177
|
+
// Set HTML content from HTML source
|
178
|
+
await page.setContent(processed_html_source, {
|
179
|
+
waitUntil: "domcontentloaded",
|
180
|
+
});
|
181
|
+
for (let i = 0; i < css_templates.length; i++) {
|
182
|
+
try {
|
183
|
+
await page.addStyleTag({
|
184
|
+
content: css_templates[i],
|
185
|
+
});
|
186
|
+
} catch (e) {
|
187
|
+
console.error(`Error applying template for [${target_file}]: ${e}`);
|
188
|
+
}
|
189
|
+
}
|
190
|
+
|
191
|
+
try {
|
192
|
+
const pdf_gen = await page.pdf({
|
193
|
+
path: target_file,
|
194
|
+
printBackground: true,
|
195
|
+
format: "A4",
|
196
|
+
displayHeaderFooter: true,
|
197
|
+
headerTemplate: header,
|
198
|
+
footerTemplate: footer,
|
199
|
+
margin: {
|
200
|
+
top: "90px",
|
201
|
+
right: "30px",
|
202
|
+
bottom: "60px",
|
203
|
+
left: "30px",
|
204
|
+
},
|
205
|
+
timeout: 0,
|
206
|
+
});
|
207
|
+
const currdate = new Date();
|
208
|
+
const datetime = currdate.toISOString();
|
209
|
+
if (verbose)
|
210
|
+
console.log(`[${datetime}] PDF generation success: ${target_file}`);
|
211
|
+
|
212
|
+
pdf_size = pdf_gen.byteLength;
|
213
|
+
} catch (err) {
|
214
|
+
console.error(`Error generating PDF ${target_file} - ${err}`);
|
215
|
+
}
|
216
|
+
await page.close();
|
217
|
+
return pdf_size;
|
218
|
+
};
|
219
|
+
})();
|