hdoc-tools 0.6.0 → 0.6.1
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-init.js +36 -0
- package/hdoc-stats.js +146 -0
- package/package.json +3 -1
package/hdoc-init.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const prompt = require('prompt');
|
|
5
|
+
|
|
6
|
+
const properties = [{
|
|
7
|
+
name: 'documentId',
|
|
8
|
+
validator: /^[a-z-]+$/,
|
|
9
|
+
warning: 'Document ID must only contain lower case letters and dashes'
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
name: 'title'
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: 'initialVersion',
|
|
16
|
+
validator: /^[a-z-]+$/,
|
|
17
|
+
warning: 'Document ID must only contain lower case letters and dashes'
|
|
18
|
+
}
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
exports.run = function (ui_path, source_path, md) {
|
|
22
|
+
|
|
23
|
+
// GERRY: The init function should create a new starting point HDocBook folder structure
|
|
24
|
+
// ready to run the preview server and start editing.
|
|
25
|
+
//
|
|
26
|
+
// The init function should prompt for the ID of the doc book, title and initial version, then
|
|
27
|
+
// create the required files and folders. Its possible to just create a template and copy
|
|
28
|
+
// those files into place, with a few variable replacements.
|
|
29
|
+
//
|
|
30
|
+
//
|
|
31
|
+
|
|
32
|
+
console.log("Init is not yet implemented");
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
};
|
|
36
|
+
})();
|
package/hdoc-stats.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
|
|
2
|
+
(function () {
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
// Required modules
|
|
6
|
+
const { STATUS_CODES } = require('http');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const dree = require('dree');
|
|
9
|
+
const html2text = require('html-to-text');
|
|
10
|
+
const { markdownToTxt } = require('markdown-to-txt');
|
|
11
|
+
const wordsCount = require('words-count').default;
|
|
12
|
+
|
|
13
|
+
// Regex to remove Hornbill-specific tags
|
|
14
|
+
const hbMDTagRegex = /(:{3}[ ]note)|(:{3}[ ]tip)|(:{3}[ ]important)|(:{3}[ ]caution)|(:{3}[ ]warning)|(:{3})/g;
|
|
15
|
+
|
|
16
|
+
let stats = {
|
|
17
|
+
totalMDFiles: 0,
|
|
18
|
+
totalStaticHTMLFiles: 0,
|
|
19
|
+
totalWordCount: 0,
|
|
20
|
+
mdFiles: {},
|
|
21
|
+
staticHTMLFiles: {}
|
|
22
|
+
};
|
|
23
|
+
// File callback for scan
|
|
24
|
+
const fileCallback = function(element) {
|
|
25
|
+
if (element.extension === 'md' ) {
|
|
26
|
+
|
|
27
|
+
// Read markdown file as string
|
|
28
|
+
const md = fs.readFileSync(element.path, 'utf8');
|
|
29
|
+
|
|
30
|
+
// Strip out standard MD tags
|
|
31
|
+
let text = markdownToTxt(md);
|
|
32
|
+
|
|
33
|
+
// Strip out any Hornbill Docs specific markdown tags
|
|
34
|
+
text = text.replaceAll(hbMDTagRegex, '');
|
|
35
|
+
|
|
36
|
+
// Do the wordcount and add to status
|
|
37
|
+
const wordCount = wordsCount(text);
|
|
38
|
+
stats.totalWordCount += wordCount;
|
|
39
|
+
stats.mdFiles[element.relativePath] = {
|
|
40
|
+
wordCount: wordCount,
|
|
41
|
+
sizeInBytes: element.sizeInBytes
|
|
42
|
+
};
|
|
43
|
+
stats.totalMDFiles++;
|
|
44
|
+
} else {
|
|
45
|
+
// file must be html
|
|
46
|
+
const mdFilePath = element.path.slice(0, element.path.lastIndexOf('.')) + '.md';
|
|
47
|
+
if (!fs.existsSync(mdFilePath)) {
|
|
48
|
+
//Do we have a matching MD file - if not, then word count the HTML
|
|
49
|
+
stats.totalStaticHTMLFiles++;
|
|
50
|
+
const html = fs.readFileSync(element.path, 'utf8');
|
|
51
|
+
const text = html2text.convert(html, {
|
|
52
|
+
wordwrap: null
|
|
53
|
+
});
|
|
54
|
+
const wordCount = wordsCount(text);
|
|
55
|
+
stats.totalWordCount += wordCount;
|
|
56
|
+
stats.staticHTMLFiles[element.relativePath] = {
|
|
57
|
+
wordCount: wordCount,
|
|
58
|
+
sizeInBytes: element.sizeInBytes
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const dreeOptions = {
|
|
65
|
+
descendants: true,
|
|
66
|
+
depth: 10,
|
|
67
|
+
extensions: ['md','html','htm'],
|
|
68
|
+
hash: false,
|
|
69
|
+
normalize: true,
|
|
70
|
+
size: true,
|
|
71
|
+
sizeInBytes: true,
|
|
72
|
+
stat: false,
|
|
73
|
+
symbolicLinks: false
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
exports.run = function(ui_path, source_path, md, verbose = false) {
|
|
77
|
+
|
|
78
|
+
// GERRY: The stats here are needed to support content development. The idea is to count all of the ]
|
|
79
|
+
// words in a HDocBook so we know the size of the book, this helps with 3rd party involvement where
|
|
80
|
+
// we generally need to know the word count of the content in order to get a quote for things like
|
|
81
|
+
// copy editing. reviewing and translations.
|
|
82
|
+
//
|
|
83
|
+
// For each .md file, and for each static .HTML file (that is html files that we have not generated) we
|
|
84
|
+
// should do a word count, excluding MD or HTML tags
|
|
85
|
+
console.log('Hornbill HDocBook Stats', '\r\n');
|
|
86
|
+
|
|
87
|
+
// STEVE: Get the docId (book root) from the hdocbook-project.json
|
|
88
|
+
// From there, loop through looking for:
|
|
89
|
+
// * HTML files without a matching MD, and word count those
|
|
90
|
+
// * MD files, and word count those
|
|
91
|
+
|
|
92
|
+
const project_json_path = source_path + '/hdocbook-project.json';
|
|
93
|
+
|
|
94
|
+
if (!fs.existsSync(project_json_path)) {
|
|
95
|
+
// Book config does not exist
|
|
96
|
+
console.error('Required project file does not exist: ', project_json_path);
|
|
97
|
+
return;
|
|
98
|
+
} else {
|
|
99
|
+
// Book config exists - load book details
|
|
100
|
+
let book_details;
|
|
101
|
+
try {
|
|
102
|
+
book_details = JSON.parse(fs.readFileSync(project_json_path, 'utf8'));
|
|
103
|
+
} catch (e) {
|
|
104
|
+
console.error('Error reading book configuration: ', e);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Scan content path directory, send file info to callback for processing
|
|
109
|
+
dree.scan(source_path + '/' + book_details.docId, dreeOptions, fileCallback);
|
|
110
|
+
|
|
111
|
+
if (verbose) {
|
|
112
|
+
// Output verbose stats
|
|
113
|
+
|
|
114
|
+
// Output information about all markdown files in the book
|
|
115
|
+
console.log('--------------');
|
|
116
|
+
console.log(' Markdown ');
|
|
117
|
+
console.log('--------------');
|
|
118
|
+
for (const key in stats.mdFiles) {
|
|
119
|
+
if (stats.mdFiles.hasOwnProperty(key)) {
|
|
120
|
+
console.log('Relative Path: ', key);
|
|
121
|
+
console.log(' Word Count: ', stats.mdFiles[key].wordCount);
|
|
122
|
+
console.log('File Size (B): ', stats.mdFiles[key].sizeInBytes, '\r\n');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Output information about all static HTML in the book
|
|
127
|
+
console.log('---------------');
|
|
128
|
+
console.log(' Static HTML ');
|
|
129
|
+
console.log('---------------');
|
|
130
|
+
for (const key in stats.staticHTMLFiles) {
|
|
131
|
+
if (stats.staticHTMLFiles.hasOwnProperty(key)) {
|
|
132
|
+
console.log('Relative Path: ', key);
|
|
133
|
+
console.log(' Word Count: ', stats.staticHTMLFiles[key].wordCount);
|
|
134
|
+
console.log('File Size (B): ', stats.staticHTMLFiles[key].sizeInBytes, '\r\n');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Output stats
|
|
140
|
+
console.log(' Total Book Word Count: ' + stats.totalWordCount);
|
|
141
|
+
console.log(' Markdown Files: ' + stats.totalMDFiles);
|
|
142
|
+
console.log(' Static HTML Files: ' + stats.totalStaticHTMLFiles + '\r\n');
|
|
143
|
+
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hdoc-tools",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Hornbill HDocBook Development Support Tool",
|
|
5
5
|
"main": "hdoc.js",
|
|
6
6
|
"bin": {
|
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
"files": [
|
|
10
10
|
"hdoc.js",
|
|
11
11
|
"hdoc-serve.js",
|
|
12
|
+
"hdoc-stats.js",
|
|
13
|
+
"hdoc-init.js",
|
|
12
14
|
"hdoc-build.js",
|
|
13
15
|
"ui",
|
|
14
16
|
"custom_modules"
|