hdoc-tools 0.61.0 → 0.62.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.
Files changed (45) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +89 -75
  3. package/editor/dist/assets/index-Blewr90z.css +1 -1
  4. package/editor/dist/assets/index-BtxvGZHW.js +111 -111
  5. package/editor/dist/assets/spell.worker-sryEKmOj.js +13 -13
  6. package/editor/dist/index.html +14 -14
  7. package/hdoc-build-db.js +275 -275
  8. package/hdoc-build-embeddings.js +202 -202
  9. package/hdoc-build-pdf.js +232 -232
  10. package/hdoc-bump.js +125 -125
  11. package/hdoc-create.js +110 -110
  12. package/hdoc-db.js +114 -114
  13. package/hdoc-help.js +60 -60
  14. package/hdoc-install-browser.js +145 -145
  15. package/hdoc-mermaid.js +204 -204
  16. package/hdoc-module.js +1102 -1102
  17. package/hdoc-validate-config.js +355 -355
  18. package/hdoc-validate-interbook.js +321 -321
  19. package/hdoc-validate.js +1231 -1231
  20. package/hdoc-ver.js +45 -45
  21. package/package.json +12 -1
  22. package/templates/doc-header-non-git.html +19 -19
  23. package/templates/doc-header.html +26 -26
  24. package/templates/init/.github/workflows/hdocbuild_onpull.yml +16 -16
  25. package/templates/init/.github/workflows/hdocbuild_onpush.yml +15 -15
  26. package/templates/init/LICENSE +21 -21
  27. package/templates/init/README.md +9 -9
  28. package/templates/init/_hdocbook/index.md +4 -4
  29. package/templates/init/gitignore +8 -8
  30. package/templates/init/resources/README.md +2 -2
  31. package/templates/pdf/css/custom-block.css +90 -90
  32. package/templates/pdf/css/fonts.css +221 -221
  33. package/templates/pdf/css/hdocs-pdf.css +495 -495
  34. package/templates/pdf/css/vars.css +404 -404
  35. package/templates/pdf/template-footer.html +19 -19
  36. package/templates/pdf/template-header.html +37 -37
  37. package/templates/pdf/template.html +20 -20
  38. package/templates/pdf-header-non-git.html +12 -12
  39. package/templates/pdf-header.html +16 -16
  40. package/ui/content/invalid-hdocbook-json.html +6 -6
  41. package/ui/content/invalid-hdocbook-json.md +7 -7
  42. package/ui/css/theme-default/styles/components/content.css +124 -124
  43. package/ui/css/theme-default/styles/components/sidebar.css +182 -182
  44. package/ui/css/theme-default/styles/htldoc.layouts.css +310 -310
  45. package/ui/index.html +419 -419
package/hdoc-build-pdf.js CHANGED
@@ -1,232 +1,232 @@
1
- (() => {
2
- const cheerio = require("cheerio");
3
- const fs = require("node:fs");
4
- const path = require("node:path");
5
- const hdoc = require(path.join(__dirname, "hdoc-module.js"));
6
-
7
- let hb_logo = "";
8
- let footer = "";
9
- let header = "";
10
-
11
- const get_footer = (template_path) => {
12
- let footer_content = null;
13
- try {
14
- footer_content = fs.readFileSync(
15
- path.join(template_path, "template-footer.html"),
16
- "utf8",
17
- );
18
- } catch (err) {
19
- console.error(`Error loading template: ${err}`);
20
- }
21
- return footer_content;
22
- };
23
-
24
- const get_header = (template_path) => {
25
- let header_content = null;
26
- try {
27
- header_content = fs.readFileSync(
28
- path.join(template_path, "template-header.html"),
29
- "utf8",
30
- );
31
- } catch (err) {
32
- console.error(`Error loading template: ${err}`);
33
- }
34
- return header_content;
35
- };
36
-
37
- exports.process_images = async (file_path, html_source, verbose) => {
38
- const book_work_root = file_path.path.replace(file_path.relativePath, "");
39
- if (verbose) console.log("Parsing img tags from HTML source");
40
-
41
- let processed_html_source = html_source;
42
- // Use cheerio to parse html
43
- const $ = cheerio.load(processed_html_source);
44
-
45
- // Get iFrames from HTML, to replace with a tags
46
- const iframes = [];
47
- const iframe_html = $("iframe")
48
- .map(function () {
49
- const response = {
50
- html: $.html(this),
51
- src: $(this).attr("src"),
52
- title: $(this).attr("title")
53
- ? $(this).attr("title")
54
- : "No Link Title Provided",
55
- };
56
- return response;
57
- })
58
- .get();
59
- iframes.push(...iframe_html);
60
- for (let i = 0; i < iframes.length; i++) {
61
- const link = `<p><a href="${iframes[i].src}">${iframes[i].title}</a></p>`;
62
- const regex = new RegExp(
63
- `<iframe.*src="${iframes[i].src.replace("/", "\\/")}".*</iframe>`,
64
- );
65
- processed_html_source = processed_html_source.replace(regex, link);
66
- }
67
-
68
- // Get image links from HTML, to embed into the pdf
69
- const imgs = [];
70
- const srcs = $("img")
71
- .map(function (i) {
72
- return $(this).attr("src");
73
- })
74
- .get();
75
- imgs.push(...srcs);
76
- for (let i = 0; i < imgs.length; i++) {
77
- if (!hdoc.valid_url(imgs[i])) {
78
- // Internal link
79
- const image_path = path.join(
80
- book_work_root,
81
- imgs[i].replace("_books/", ""),
82
- );
83
- try {
84
- const image_buffer = fs.readFileSync(image_path);
85
- // Inline MIME type lookup for image formats commonly found in documentation.
86
- // Falls back to application/octet-stream for any unrecognised extension so the
87
- // base64 data URI is still well-formed even if the browser can't render it.
88
- const mime_type = ({ '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml', '.webp': 'image/webp', '.bmp': 'image/bmp', '.ico': 'image/x-icon' })[path.extname(image_path).toLowerCase()] || 'application/octet-stream';
89
- let image_b64 = image_buffer.toString("base64");
90
- image_b64 = `data:${mime_type};base64,${image_b64}`;
91
- processed_html_source = processed_html_source.replace(
92
- imgs[i],
93
- image_b64,
94
- );
95
- } catch (err) {
96
- console.error(
97
- "Error reading image from HTML source [",
98
- image_path,
99
- "] -",
100
- err,
101
- );
102
- return null;
103
- }
104
- } else {
105
- // External Link
106
- try {
107
- const file_response = await fetch(imgs[i]);
108
- if (file_response.status === 200) {
109
- let image_b64 = imageEncode(await file_response.arrayBuffer(), file_response.headers.get('content-type'));
110
-
111
-
112
- const regexQ = `<img\\s+[^>]*src=["']${imgs[i].replaceAll('/', '\\/').replaceAll('.', '\\.')}["'][^>]*>`;
113
- const regex = new RegExp(regexQ);
114
-
115
- const found_img_tag = processed_html_source.match(regex);
116
- const new_img_tag = found_img_tag[0].replace(imgs[i], image_b64);
117
-
118
- processed_html_source = processed_html_source.replace(
119
- found_img_tag,
120
- new_img_tag,
121
- );
122
- } else {
123
- throw `Unexpected Status ${file_response.status}`;
124
- }
125
- } catch (err) {
126
- console.error(
127
- `Error downloading external source [${imgs[i]}] - ${err}`,
128
- );
129
- }
130
- }
131
- }
132
-
133
- return processed_html_source;
134
- };
135
-
136
- const imageEncode = (arrayBuffer, mimeType) => {
137
- let u8 = new Uint8Array(arrayBuffer)
138
- let b64encoded = btoa([].reduce.call(new Uint8Array(arrayBuffer),function(p,c){return p+String.fromCharCode(c)},''))
139
- let mimetype=`image/${mimeType}`
140
- return "data:"+mimetype+";base64,"+b64encoded
141
- }
142
-
143
- exports.generate_pdf = async (
144
- browser,
145
- pdf_template_path,
146
- pdf_template_content,
147
- book_config,
148
- html_source,
149
- target_file,
150
- css_templates,
151
- verbose = false,
152
- ) => {
153
- let pdf_size = 0;
154
- // Cache footer
155
- if (footer === "") footer = get_footer(pdf_template_path);
156
-
157
- // Read svg logo file into buffer, convert to B64 string
158
- if (hb_logo === "") {
159
- const hb_logo_path = path.join(
160
- pdf_template_path,
161
- "images",
162
- "hornbill-logo-full.svg",
163
- );
164
- try {
165
- const hb_logo_file_buffer = fs.readFileSync(hb_logo_path);
166
- hb_logo = hb_logo_file_buffer.toString("base64");
167
- hb_logo = `data:image/svg+xml;base64,${hb_logo}`;
168
- } catch (err) {
169
- console.error("Error reading logo from template:", err);
170
- return pdf_size;
171
- }
172
- }
173
-
174
- // Cache header
175
- if (header === "") {
176
- header = get_header(pdf_template_path)
177
- .replace("{{book_title}}", book_config.title)
178
- .replace("{{hb_logo}}", hb_logo);
179
- }
180
-
181
- const processed_html_source = pdf_template_content
182
- .replace("{{book_title}}", book_config.title)
183
- .replace("{{document_content}}", html_source);
184
-
185
- const page = await browser.newPage();
186
-
187
- // To reflect CSS used for screens instead of print
188
- await page.emulateMediaType("screen");
189
-
190
- // Set HTML content from HTML source
191
- await page.setContent(processed_html_source, {
192
- waitUntil: "domcontentloaded",
193
- });
194
- for (let i = 0; i < css_templates.length; i++) {
195
- try {
196
- await page.addStyleTag({
197
- content: css_templates[i],
198
- });
199
- } catch (e) {
200
- console.error(`Error applying template for [${target_file}]: ${e}`);
201
- }
202
- }
203
-
204
- try {
205
- const pdf_gen = await page.pdf({
206
- path: target_file,
207
- printBackground: true,
208
- format: "A4",
209
- displayHeaderFooter: true,
210
- headerTemplate: header,
211
- footerTemplate: footer,
212
- margin: {
213
- top: "90px",
214
- right: "30px",
215
- bottom: "60px",
216
- left: "30px",
217
- },
218
- timeout: 0,
219
- });
220
- const currdate = new Date();
221
- const datetime = currdate.toISOString();
222
- if (verbose)
223
- console.log(`[${datetime}] PDF generation success: ${target_file}`);
224
-
225
- pdf_size = pdf_gen.byteLength;
226
- } catch (err) {
227
- console.error(`Error generating PDF ${target_file} - ${err}`);
228
- }
229
- await page.close();
230
- return pdf_size;
231
- };
232
- })();
1
+ (() => {
2
+ const cheerio = require("cheerio");
3
+ const fs = require("node:fs");
4
+ const path = require("node:path");
5
+ const hdoc = require(path.join(__dirname, "hdoc-module.js"));
6
+
7
+ let hb_logo = "";
8
+ let footer = "";
9
+ let header = "";
10
+
11
+ const get_footer = (template_path) => {
12
+ let footer_content = null;
13
+ try {
14
+ footer_content = fs.readFileSync(
15
+ path.join(template_path, "template-footer.html"),
16
+ "utf8",
17
+ );
18
+ } catch (err) {
19
+ console.error(`Error loading template: ${err}`);
20
+ }
21
+ return footer_content;
22
+ };
23
+
24
+ const get_header = (template_path) => {
25
+ let header_content = null;
26
+ try {
27
+ header_content = fs.readFileSync(
28
+ path.join(template_path, "template-header.html"),
29
+ "utf8",
30
+ );
31
+ } catch (err) {
32
+ console.error(`Error loading template: ${err}`);
33
+ }
34
+ return header_content;
35
+ };
36
+
37
+ exports.process_images = async (file_path, html_source, verbose) => {
38
+ const book_work_root = file_path.path.replace(file_path.relativePath, "");
39
+ if (verbose) console.log("Parsing img tags from HTML source");
40
+
41
+ let processed_html_source = html_source;
42
+ // Use cheerio to parse html
43
+ const $ = cheerio.load(processed_html_source);
44
+
45
+ // Get iFrames from HTML, to replace with a tags
46
+ const iframes = [];
47
+ const iframe_html = $("iframe")
48
+ .map(function () {
49
+ const response = {
50
+ html: $.html(this),
51
+ src: $(this).attr("src"),
52
+ title: $(this).attr("title")
53
+ ? $(this).attr("title")
54
+ : "No Link Title Provided",
55
+ };
56
+ return response;
57
+ })
58
+ .get();
59
+ iframes.push(...iframe_html);
60
+ for (let i = 0; i < iframes.length; i++) {
61
+ const link = `<p><a href="${iframes[i].src}">${iframes[i].title}</a></p>`;
62
+ const regex = new RegExp(
63
+ `<iframe.*src="${iframes[i].src.replace("/", "\\/")}".*</iframe>`,
64
+ );
65
+ processed_html_source = processed_html_source.replace(regex, link);
66
+ }
67
+
68
+ // Get image links from HTML, to embed into the pdf
69
+ const imgs = [];
70
+ const srcs = $("img")
71
+ .map(function (i) {
72
+ return $(this).attr("src");
73
+ })
74
+ .get();
75
+ imgs.push(...srcs);
76
+ for (let i = 0; i < imgs.length; i++) {
77
+ if (!hdoc.valid_url(imgs[i])) {
78
+ // Internal link
79
+ const image_path = path.join(
80
+ book_work_root,
81
+ imgs[i].replace("_books/", ""),
82
+ );
83
+ try {
84
+ const image_buffer = fs.readFileSync(image_path);
85
+ // Inline MIME type lookup for image formats commonly found in documentation.
86
+ // Falls back to application/octet-stream for any unrecognised extension so the
87
+ // base64 data URI is still well-formed even if the browser can't render it.
88
+ const mime_type = ({ '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml', '.webp': 'image/webp', '.bmp': 'image/bmp', '.ico': 'image/x-icon' })[path.extname(image_path).toLowerCase()] || 'application/octet-stream';
89
+ let image_b64 = image_buffer.toString("base64");
90
+ image_b64 = `data:${mime_type};base64,${image_b64}`;
91
+ processed_html_source = processed_html_source.replace(
92
+ imgs[i],
93
+ image_b64,
94
+ );
95
+ } catch (err) {
96
+ console.error(
97
+ "Error reading image from HTML source [",
98
+ image_path,
99
+ "] -",
100
+ err,
101
+ );
102
+ return null;
103
+ }
104
+ } else {
105
+ // External Link
106
+ try {
107
+ const file_response = await fetch(imgs[i]);
108
+ if (file_response.status === 200) {
109
+ let image_b64 = imageEncode(await file_response.arrayBuffer(), file_response.headers.get('content-type'));
110
+
111
+
112
+ const regexQ = `<img\\s+[^>]*src=["']${imgs[i].replaceAll('/', '\\/').replaceAll('.', '\\.')}["'][^>]*>`;
113
+ const regex = new RegExp(regexQ);
114
+
115
+ const found_img_tag = processed_html_source.match(regex);
116
+ const new_img_tag = found_img_tag[0].replace(imgs[i], image_b64);
117
+
118
+ processed_html_source = processed_html_source.replace(
119
+ found_img_tag,
120
+ new_img_tag,
121
+ );
122
+ } else {
123
+ throw `Unexpected Status ${file_response.status}`;
124
+ }
125
+ } catch (err) {
126
+ console.error(
127
+ `Error downloading external source [${imgs[i]}] - ${err}`,
128
+ );
129
+ }
130
+ }
131
+ }
132
+
133
+ return processed_html_source;
134
+ };
135
+
136
+ const imageEncode = (arrayBuffer, mimeType) => {
137
+ let u8 = new Uint8Array(arrayBuffer)
138
+ let b64encoded = btoa([].reduce.call(new Uint8Array(arrayBuffer),function(p,c){return p+String.fromCharCode(c)},''))
139
+ let mimetype=`image/${mimeType}`
140
+ return "data:"+mimetype+";base64,"+b64encoded
141
+ }
142
+
143
+ exports.generate_pdf = async (
144
+ browser,
145
+ pdf_template_path,
146
+ pdf_template_content,
147
+ book_config,
148
+ html_source,
149
+ target_file,
150
+ css_templates,
151
+ verbose = false,
152
+ ) => {
153
+ let pdf_size = 0;
154
+ // Cache footer
155
+ if (footer === "") footer = get_footer(pdf_template_path);
156
+
157
+ // Read svg logo file into buffer, convert to B64 string
158
+ if (hb_logo === "") {
159
+ const hb_logo_path = path.join(
160
+ pdf_template_path,
161
+ "images",
162
+ "hornbill-logo-full.svg",
163
+ );
164
+ try {
165
+ const hb_logo_file_buffer = fs.readFileSync(hb_logo_path);
166
+ hb_logo = hb_logo_file_buffer.toString("base64");
167
+ hb_logo = `data:image/svg+xml;base64,${hb_logo}`;
168
+ } catch (err) {
169
+ console.error("Error reading logo from template:", err);
170
+ return pdf_size;
171
+ }
172
+ }
173
+
174
+ // Cache header
175
+ if (header === "") {
176
+ header = get_header(pdf_template_path)
177
+ .replace("{{book_title}}", book_config.title)
178
+ .replace("{{hb_logo}}", hb_logo);
179
+ }
180
+
181
+ const processed_html_source = pdf_template_content
182
+ .replace("{{book_title}}", book_config.title)
183
+ .replace("{{document_content}}", html_source);
184
+
185
+ const page = await browser.newPage();
186
+
187
+ // To reflect CSS used for screens instead of print
188
+ await page.emulateMediaType("screen");
189
+
190
+ // Set HTML content from HTML source
191
+ await page.setContent(processed_html_source, {
192
+ waitUntil: "domcontentloaded",
193
+ });
194
+ for (let i = 0; i < css_templates.length; i++) {
195
+ try {
196
+ await page.addStyleTag({
197
+ content: css_templates[i],
198
+ });
199
+ } catch (e) {
200
+ console.error(`Error applying template for [${target_file}]: ${e}`);
201
+ }
202
+ }
203
+
204
+ try {
205
+ const pdf_gen = await page.pdf({
206
+ path: target_file,
207
+ printBackground: true,
208
+ format: "A4",
209
+ displayHeaderFooter: true,
210
+ headerTemplate: header,
211
+ footerTemplate: footer,
212
+ margin: {
213
+ top: "90px",
214
+ right: "30px",
215
+ bottom: "60px",
216
+ left: "30px",
217
+ },
218
+ timeout: 0,
219
+ });
220
+ const currdate = new Date();
221
+ const datetime = currdate.toISOString();
222
+ if (verbose)
223
+ console.log(`[${datetime}] PDF generation success: ${target_file}`);
224
+
225
+ pdf_size = pdf_gen.byteLength;
226
+ } catch (err) {
227
+ console.error(`Error generating PDF ${target_file} - ${err}`);
228
+ }
229
+ await page.close();
230
+ return pdf_size;
231
+ };
232
+ })();