hdoc-tools 0.9.10 → 0.9.12

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/README.md CHANGED
@@ -18,6 +18,10 @@ Outputs available arguments and switches.
18
18
 
19
19
  Initializes a new HDocBook project from a template, using runtime input variables.
20
20
 
21
+ ### createDocs
22
+
23
+ Creates folder structure and markdown documents as defined in the HDocBook navigation item links
24
+
21
25
  ### stats
22
26
 
23
27
  Returns statistics regarding the book you are working on:
package/hdoc-build.js CHANGED
@@ -565,94 +565,6 @@
565
565
  sorted: true
566
566
  };
567
567
 
568
- const build_breadcrumbs = function (nav_items) {
569
- console.log('Processing navigation breadcrumbs...\n');
570
- // Steve - need to rework this to use recursion, as it's hard-coded for a maximum of 4 levels deep right now
571
- for (let a = 0; a < nav_items.length; a++) {
572
- const nav_a = nav_items[a];
573
- const parent_a = nav_a.text;
574
- if (nav_a.link) {
575
- bc[nav_a.link] = [{
576
- text: nav_a.text,
577
- link: nav_a.link
578
- }];
579
- }
580
- if (nav_a.items) {
581
- for (let b = 0; b < nav_a.items.length; b++) {
582
- const nav_b = nav_a.items[b];
583
- const parent_b = nav_b.text;
584
- if (nav_b.link) {
585
- if (bc[nav_a.link]) {
586
- bc[nav_b.link] = bc[nav_a.link];
587
- } else {
588
- bc[nav_b.link] = [{
589
- text: parent_a,
590
- link: nav_a.items[0].link
591
- }];
592
- }
593
- bc[nav_b.link].push({
594
- text: nav_b.text,
595
- link: nav_b.link
596
- });
597
- }
598
-
599
- if (nav_b.items) {
600
- for (let c = 0; c < nav_b.items.length; c++) {
601
- const nav_c = nav_b.items[c];
602
- const parent_c = nav_c.text;
603
- if (nav_c.link) {
604
- if (bc[nav_b.link]) {
605
- bc[nav_c.link] = bc[nav_b.link];
606
- } else {
607
- bc[nav_c.link] = [{
608
- text: parent_a,
609
- link: nav_a.items[0].link
610
- }];
611
- bc[nav_c.link].push({
612
- text: parent_b,
613
- link: nav_b.items[0].link
614
- });
615
- }
616
- bc[nav_c.link].push({
617
- text: nav_c.text,
618
- link: nav_c.link
619
- });
620
- }
621
-
622
- if (nav_c.items) {
623
- for (let d = 0; d < nav_c.items.length; d++) {
624
- const nav_d = nav_c.items[d];
625
- if (nav_d.link) {
626
- if (bc[nav_c.link]) {
627
- bc[nav_d.link] = bc[nav_c.link];
628
- } else {
629
- bc[nav_d.link] = [{
630
- text: parent_a,
631
- link: nav_a.items[0].link
632
- }];
633
- bc[nav_d.link].push({
634
- text: parent_b,
635
- link: nav_b.items[0].link
636
- });
637
- bc[nav_d.link].push({
638
- text: parent_c,
639
- link: nav_c.items[0].link
640
- });
641
- }
642
- bc[nav_d.link].push({
643
- text: nav_d.text,
644
- link: nav_d.link
645
- });
646
- }
647
- }
648
- }
649
- }
650
- }
651
- }
652
- }
653
- }
654
- };
655
-
656
568
  exports.run = async function (source_path, verbose_output, github_api_token, validate) {
657
569
  if (github_api_token !== '') {
658
570
  git_token = github_api_token;
@@ -759,8 +671,8 @@
759
671
  process.exit(1);
760
672
  }
761
673
  }
762
-
763
- build_breadcrumbs(hdocbook_config.navigation.items);
674
+ console.log('Processing navigation breadcrumbs...\n');
675
+ bc = hdoc.build_breadcrumbs(hdocbook_config.navigation.items);
764
676
 
765
677
  console.log('Processing content...');
766
678
  // Get a list of MD files in work_path
package/hdoc-create.js ADDED
@@ -0,0 +1,91 @@
1
+ (function () {
2
+ 'use strict';
3
+
4
+ const fs = require('fs-extra'),
5
+ path = require('path'),
6
+ hdoc = require(path.join(__dirname, 'hdoc-module.js'));
7
+
8
+ let doc_id,
9
+ processed_links = {},
10
+ file_count = 0,
11
+ folder_count = 0;
12
+
13
+ exports.run = async function (source_path) {
14
+
15
+ console.log('Hornbill HDocBook Create', '\n');
16
+ console.log('Path:', source_path, '\n');
17
+
18
+ // Load the hdocbook-project.json file to get the docId
19
+ // use the docId to get the book config
20
+ const hdocbook_project_config_path = path.join(source_path, 'hdocbook-project.json');
21
+ let hdocbook_project = {};
22
+ try {
23
+ hdocbook_project = require(hdocbook_project_config_path);
24
+ } catch (e) {
25
+ console.log(`File not found: ${hdocbook_project_config_path}\n`);
26
+ console.log('hdoc create needs to be run in the root of a HDoc Book.\n');
27
+ process.exit(1);
28
+ }
29
+ doc_id = hdocbook_project.docId;
30
+
31
+ const book_path = path.join(source_path, doc_id),
32
+ hdocbook_path = path.join(book_path, 'hdocbook.json');
33
+
34
+ let hdocbook = {};
35
+ try {
36
+ hdocbook = require(hdocbook_path);
37
+ } catch (e) {
38
+ console.log(`File not found: ${hdocbook_path}\n`);
39
+ console.log('hdoc create needs to be run in the root of a HDoc Book.\n');
40
+ process.exit(1);
41
+ }
42
+
43
+ // Get paths from breadcrumb builder
44
+ let nav_paths = hdoc.build_breadcrumbs(hdocbook.navigation.items);
45
+ for (const key in nav_paths) {
46
+ if (nav_paths.hasOwnProperty(key)) {
47
+ for (let i = 0; i < nav_paths[key].length; i++) {
48
+ if (!processed_links[nav_paths[key][i].link]) {
49
+ nav_paths[key][i].path = path.join(source_path, nav_paths[key][i].link);
50
+ await add_doc(nav_paths[key][i]);
51
+ }
52
+ }
53
+ }
54
+ }
55
+ console.log('\n-----------------------');
56
+ console.log(' Docs Creation Summary');
57
+ console.log('-----------------------\n');
58
+ console.log(` Files Created: ${file_count}`);
59
+ console.log(`Folders Created: ${folder_count}\n`);
60
+ };
61
+
62
+ const add_doc = async function(doc_info) {
63
+
64
+ // Does folder exist? Create if not
65
+ const folder = path.dirname(doc_info.path);
66
+ if (!fs.existsSync(folder)) {
67
+ try {
68
+ fs.mkdirSync(folder, true);
69
+ console.log('Folder created:', folder);
70
+ folder_count++;
71
+ } catch {
72
+ console.log('\nError creating folder', folder, ':', e);
73
+ return;
74
+ }
75
+ }
76
+
77
+ // Does file exist? Create if not
78
+ if (!fs.existsSync(doc_info.path + '.md') && !fs.existsSync(doc_info.path + '.htm') && !fs.existsSync(doc_info.path + '.html')) {
79
+ try {
80
+ const file_path = doc_info.path + '.md';
81
+ fs.writeFileSync(file_path, `# ${doc_info.text}\n`);
82
+ console.log(' File created:', file_path);
83
+ processed_links[doc_info.link] = true;
84
+ file_count++;
85
+ } catch (e) {
86
+ console.log('\nError creating file', doc_info.path, ':', e);
87
+ }
88
+ }
89
+ }
90
+
91
+ })();
package/hdoc-help.js CHANGED
@@ -14,6 +14,9 @@ Commands
14
14
  - build
15
15
  Performs a local build of the book, and outputs as a ZIP file
16
16
 
17
+ - createDocs
18
+ Creates folder structure and markdown documents as defined in the HDocBook navigation item links
19
+
17
20
  - help
18
21
  Outputs available arguments and switches
19
22
 
package/hdoc-module.js CHANGED
@@ -290,4 +290,94 @@
290
290
  return response;
291
291
  };
292
292
 
293
+
294
+ exports.build_breadcrumbs = function (nav_items) {
295
+ let bc = {};
296
+ // Steve - need to rework this to use recursion, as it's hard-coded for a maximum of 4 levels deep right now
297
+ for (let a = 0; a < nav_items.length; a++) {
298
+ const nav_a = nav_items[a];
299
+ const parent_a = nav_a.text;
300
+ if (nav_a.link) {
301
+ bc[nav_a.link] = [{
302
+ text: nav_a.text,
303
+ link: nav_a.link
304
+ }];
305
+ }
306
+ if (nav_a.items) {
307
+ for (let b = 0; b < nav_a.items.length; b++) {
308
+ const nav_b = nav_a.items[b];
309
+ const parent_b = nav_b.text;
310
+ if (nav_b.link) {
311
+ if (bc[nav_a.link]) {
312
+ bc[nav_b.link] = bc[nav_a.link];
313
+ } else {
314
+ bc[nav_b.link] = [{
315
+ text: parent_a,
316
+ link: nav_a.items[0].link
317
+ }];
318
+ }
319
+ bc[nav_b.link].push({
320
+ text: nav_b.text,
321
+ link: nav_b.link
322
+ });
323
+ }
324
+
325
+ if (nav_b.items) {
326
+ for (let c = 0; c < nav_b.items.length; c++) {
327
+ const nav_c = nav_b.items[c];
328
+ const parent_c = nav_c.text;
329
+ if (nav_c.link) {
330
+ if (bc[nav_b.link]) {
331
+ bc[nav_c.link] = bc[nav_b.link];
332
+ } else {
333
+ bc[nav_c.link] = [{
334
+ text: parent_a,
335
+ link: nav_a.items[0].link
336
+ }];
337
+ bc[nav_c.link].push({
338
+ text: parent_b,
339
+ link: nav_b.items[0].link
340
+ });
341
+ }
342
+ bc[nav_c.link].push({
343
+ text: nav_c.text,
344
+ link: nav_c.link
345
+ });
346
+ }
347
+
348
+ if (nav_c.items) {
349
+ for (let d = 0; d < nav_c.items.length; d++) {
350
+ const nav_d = nav_c.items[d];
351
+ if (nav_d.link) {
352
+ if (bc[nav_c.link]) {
353
+ bc[nav_d.link] = bc[nav_c.link];
354
+ } else {
355
+ bc[nav_d.link] = [{
356
+ text: parent_a,
357
+ link: nav_a.items[0].link
358
+ }];
359
+ bc[nav_d.link].push({
360
+ text: parent_b,
361
+ link: nav_b.items[0].link
362
+ });
363
+ bc[nav_d.link].push({
364
+ text: parent_c,
365
+ link: nav_c.items[0].link
366
+ });
367
+ }
368
+ bc[nav_d.link].push({
369
+ text: nav_d.text,
370
+ link: nav_d.link
371
+ });
372
+ }
373
+ }
374
+ }
375
+ }
376
+ }
377
+ }
378
+ }
379
+ }
380
+ return bc;
381
+ };
382
+
293
383
  })();
package/hdoc-validate.js CHANGED
@@ -50,12 +50,13 @@
50
50
  const source_path = sourceFile.relativePath.replace('.'+sourceFile.extension, '');
51
51
  const translate_output = translator.translate(text, spellcheck_options);
52
52
  if(Object.keys(translate_output).length){
53
+
53
54
  for (const key in translate_output) {
54
55
  if (translate_output.hasOwnProperty(key)) {
55
56
  let error_message = `Line ${key} - British spelling:`;
56
57
  for (let i = 0; i < translate_output[key].length; i++) {
57
58
  for (const spelling in translate_output[key][i]) {
58
- if (translate_output[key][i].hasOwnProperty(spelling)) {
59
+ if (translate_output[key][i].hasOwnProperty(spelling) && (typeof translate_output[key][i][spelling].details === 'string')) {
59
60
  if (!excludes[source_path]) {
60
61
  errors[sourceFile.relativePath].push(`${error_message} ${spelling} should be ${translate_output[key][i][spelling].details}`);
61
62
  } else if (!excludes[source_path].includes(spelling.toLowerCase())) {
@@ -189,6 +190,23 @@
189
190
  }
190
191
  };
191
192
 
193
+ const checkTags = async function (htmlFile) {
194
+ const htmlBody = fs.readFileSync(htmlFile.path, 'utf8');
195
+ const $ = cheerio.load(htmlBody);
196
+
197
+ const h1_tags = $('h1').map(function () {
198
+ return $(this);
199
+ }).get();
200
+ if (h1_tags.length && h1_tags.length > 1) {
201
+ let error_msg = `${h1_tags.length} <h1> tags found in content: `;
202
+ for (let i = 0; i < h1_tags.length; i++) {
203
+ error_msg += h1_tags[i].text();
204
+ if (i < h1_tags.length - 1) error_msg += '; ';
205
+ }
206
+ errors[htmlFile.relativePath].push(error_msg);
207
+ }
208
+ };
209
+
192
210
  const dreeOptions = {
193
211
  descendants: true,
194
212
  depth: 10,
@@ -375,6 +393,9 @@
375
393
  await checkImages(source_path, html_to_validate[i], links.img);
376
394
  }
377
395
 
396
+ // Check for multiple H1 tags
397
+ await checkTags(html_to_validate[i]);
398
+
378
399
  // Build list content for Google
379
400
  listContent += `/${html_to_validate[i].relativePath.replace(path.extname(html_to_validate[i].relativePath), '')}`;
380
401
  if (i < html_to_validate.length - 1) {
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
 
@@ -69,6 +69,9 @@
69
69
  } else if (command == 'build') {
70
70
  const builder = require(path.join(__dirname, 'hdoc-build.js'));
71
71
  await builder.run(source_path, verbose, git_token, false);
72
+ } else if (command == 'createDocs') {
73
+ const creator = require(path.join(__dirname, 'hdoc-create.js'));
74
+ await creator.run(source_path);
72
75
  } else if (command == 'validate') {
73
76
  const builder = require(path.join(__dirname, 'hdoc-build.js'));
74
77
  await builder.run(source_path, verbose, git_token, true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hdoc-tools",
3
- "version": "0.9.10",
3
+ "version": "0.9.12",
4
4
  "description": "Hornbill HDocBook Development Support Tool",
5
5
  "main": "hdoc.js",
6
6
  "bin": {
@@ -12,6 +12,7 @@
12
12
  "hdoc-build.js",
13
13
  "hdoc-build-db.js",
14
14
  "hdoc-build-pdf.js",
15
+ "hdoc-create.js",
15
16
  "hdoc-help.js",
16
17
  "hdoc-init.js",
17
18
  "hdoc-module.js",