hdoc-tools 0.6.8 → 0.7.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/hdoc-build.js +131 -3
- package/hdoc-help.js +12 -8
- package/hdoc-serve.js +19 -5
- package/hdoc.js +1 -1
- package/package.json +2 -1
package/hdoc-build.js
CHANGED
|
@@ -1,8 +1,70 @@
|
|
|
1
1
|
(function () {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const fs = require('fs-extra'),
|
|
5
|
+
path = require('path'),
|
|
6
|
+
dree = require('dree'),
|
|
7
|
+
AdmZip = require("adm-zip");
|
|
8
|
+
|
|
9
|
+
let conversion_attempted = 0,
|
|
10
|
+
conversion_success = 0,
|
|
11
|
+
conversion_failed = 0,
|
|
12
|
+
docId = '',
|
|
13
|
+
md_files = [];
|
|
14
|
+
|
|
15
|
+
function expand_variables(text) {
|
|
16
|
+
// For debug mode our base path is our root??
|
|
17
|
+
text = text.replaceAll('{{BASE_PATH}}', '/' + docId);
|
|
18
|
+
text = text.replaceAll('{{BUILD_NUMBER}}', '0');
|
|
19
|
+
|
|
20
|
+
let build_date = new Date().toISOString();
|
|
21
|
+
build_date = build_date.replace('T', ' ');
|
|
22
|
+
build_date = build_date.substring(0, 19);
|
|
23
|
+
|
|
24
|
+
text = text.replaceAll('{{BUILD_DATE}}', build_date);
|
|
25
|
+
return text;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function transform_markdown_and_save_html(file_path, md) {
|
|
29
|
+
conversion_attempted++;
|
|
30
|
+
if (fs.existsSync(file_path)) {
|
|
31
|
+
// Load markdown file
|
|
32
|
+
let md_txt = expand_variables(fs.readFileSync(file_path, 'utf8'));
|
|
33
|
+
|
|
34
|
+
// Render markdown into HTML
|
|
35
|
+
var html_txt = md.render(md_txt.toString());
|
|
36
|
+
|
|
37
|
+
// Save HTML into HTML file
|
|
38
|
+
const target_file = file_path.replace(path.extname(file_path), '.html');
|
|
39
|
+
fs.writeFile(target_file, html_txt, function writeJSON(err) {
|
|
40
|
+
if (err) return console.log('Error writing:',target_file, '\r\n', err);
|
|
41
|
+
});
|
|
42
|
+
conversion_success++;
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
conversion_failed++;
|
|
46
|
+
console.error('MD file does not exist:', file_path);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// File callback for scan
|
|
51
|
+
const fileCallback = function (element) {
|
|
52
|
+
md_files.push(element.path);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const dreeOptions = {
|
|
56
|
+
descendants: true,
|
|
57
|
+
depth: 10,
|
|
58
|
+
extensions: ['md'],
|
|
59
|
+
hash: false,
|
|
60
|
+
normalize: true,
|
|
61
|
+
size: true,
|
|
62
|
+
sizeInBytes: true,
|
|
63
|
+
stat: false,
|
|
64
|
+
symbolicLinks: false
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
exports.run = function (source_path, md) {
|
|
6
68
|
// GERRY: The purpose of this function is to create a zip file containing the hdocbook content,
|
|
7
69
|
// * Create a _work folder
|
|
8
70
|
// * copy the hdocbook content to the work folder
|
|
@@ -12,7 +74,73 @@
|
|
|
12
74
|
// folder, conceptually we are making a little mini website crawler to index all of the content
|
|
13
75
|
// within the book.
|
|
14
76
|
// * Package everything up into a ZIP file, ready for the build controller to package and publish
|
|
77
|
+
|
|
78
|
+
console.log('Hornbill HDocBook Build', '\r\n');
|
|
79
|
+
|
|
80
|
+
// Load the hdocbook-project.json file to get the docId
|
|
81
|
+
// use the docId to get the book config
|
|
82
|
+
const hdocbook_project_config_path = path.join(source_path, 'hdocbook-project.json'),
|
|
83
|
+
hdocbook_project = require(hdocbook_project_config_path);
|
|
84
|
+
|
|
85
|
+
docId = hdocbook_project.docId;
|
|
86
|
+
|
|
87
|
+
const book_path = path.join(source_path, docId),
|
|
88
|
+
hdocbook_path = path.join(book_path, 'hdocbook.json'),
|
|
89
|
+
hdocbook_config = require(hdocbook_path);
|
|
90
|
+
|
|
91
|
+
console.log(`Building: ${docId} v${hdocbook_config.version}...\r\n`);
|
|
92
|
+
|
|
93
|
+
// Get source-path - 1 to work out where to store _work
|
|
94
|
+
const work_path_arr = source_path.split(path.sep);
|
|
95
|
+
let work_path = work_path_arr[0];
|
|
96
|
+
for (let i = 1; i < work_path_arr.length - 1; i++) {
|
|
97
|
+
work_path += path.sep + work_path_arr[i];
|
|
98
|
+
}
|
|
99
|
+
work_path = path.join(work_path, '_work_' + docId);
|
|
100
|
+
|
|
101
|
+
// Make _work folder to copy everything into
|
|
102
|
+
if (fs.existsSync(work_path)) {
|
|
103
|
+
fs.rmSync(work_path, {
|
|
104
|
+
recursive: true,
|
|
105
|
+
force: true
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Create target book folder
|
|
110
|
+
fs.mkdirSync(work_path);
|
|
111
|
+
|
|
112
|
+
// Copy files from book into _work-docId folder
|
|
113
|
+
try {
|
|
114
|
+
fs.copySync(source_path, work_path);
|
|
115
|
+
} catch (e) {
|
|
116
|
+
console.error('Error copying from source_path:\r\n', e);
|
|
117
|
+
process.exit(1);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Remove git stuff from work_path
|
|
121
|
+
fs.rmSync(path.join(work_path, '.git'), {
|
|
122
|
+
recursive: true,
|
|
123
|
+
force: true
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Get a list of MD files in work_path
|
|
127
|
+
dree.scan(work_path, dreeOptions, fileCallback);
|
|
128
|
+
|
|
129
|
+
// Work through MD files and convert to HTML
|
|
130
|
+
md_files.forEach(function(md_file){
|
|
131
|
+
transform_markdown_and_save_html(md_file, md);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
console.log(` MD files found: ${conversion_attempted}`);
|
|
135
|
+
console.log(`Successfully converted to HTML: ${conversion_success}`);
|
|
136
|
+
console.log(` Failed to convert: ${conversion_failed}`);
|
|
15
137
|
|
|
16
|
-
|
|
138
|
+
// ZIP up content of _work-docId, save as docId_vx_y_z.zip where x/y/z is semantic version
|
|
139
|
+
const zip_path = path.join(work_path, docId + '_v' + hdocbook_config.version.replace('.', '_') + '.zip');
|
|
140
|
+
const zip = new AdmZip();
|
|
141
|
+
zip.addLocalFolder(work_path);
|
|
142
|
+
zip.writeZip(zip_path);
|
|
143
|
+
console.log(` ZIP Creation Success: ${zip_path}\r\n`);
|
|
144
|
+
console.log('Build Complete\r\n');
|
|
17
145
|
};
|
|
18
146
|
})();
|
package/hdoc-help.js
CHANGED
|
@@ -7,20 +7,24 @@
|
|
|
7
7
|
const helpText = `
|
|
8
8
|
Command Line Usage
|
|
9
9
|
|
|
10
|
-
hdoc
|
|
10
|
+
hdoc <command> [switches]
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
Commands
|
|
13
13
|
|
|
14
|
-
- help
|
|
14
|
+
- help
|
|
15
15
|
Outputs available arguments and switches
|
|
16
|
-
|
|
16
|
+
|
|
17
|
+
- init
|
|
17
18
|
Initializes a new HDocBook project from a template, using runtime input variables
|
|
18
|
-
|
|
19
|
+
|
|
20
|
+
- stats
|
|
19
21
|
Returns statistics regarding the book you are working on. Supports a -v switch for verbose output
|
|
20
|
-
|
|
22
|
+
|
|
23
|
+
- build
|
|
21
24
|
Performs a local build of the book, and outputs as a ZIP file.
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
|
|
26
|
+
- serve
|
|
27
|
+
Starts a local web server on port 3000, serving the content. Supports a -port N to use a different port
|
|
24
28
|
|
|
25
29
|
Example
|
|
26
30
|
|
package/hdoc-serve.js
CHANGED
|
@@ -5,13 +5,28 @@
|
|
|
5
5
|
var path = require('path');
|
|
6
6
|
const stream = require('stream');
|
|
7
7
|
var express = require('express');
|
|
8
|
+
var port = 3000;
|
|
8
9
|
|
|
9
10
|
exports.run = function (ui_path, source_path, md) {
|
|
10
11
|
|
|
12
|
+
for (let x = 0; x < process.argv.length; x++) {
|
|
13
|
+
// First two arguments are command, and script
|
|
14
|
+
if (x == 0 || x == 1)
|
|
15
|
+
continue;
|
|
16
|
+
|
|
17
|
+
if (process.argv[x] == '-port') {
|
|
18
|
+
x++;
|
|
19
|
+
if (x < process.argv.length) {
|
|
20
|
+
port = process.argv[x];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
11
25
|
console.log('Hornbill HDocBook Preview/Dev Server', '\r\n');
|
|
12
|
-
console.log(' Server Path:', __dirname);
|
|
13
|
-
console.log(' UI Root Path:', ui_path);
|
|
26
|
+
//console.log(' Server Path:', __dirname);
|
|
27
|
+
//console.log(' UI Root Path:', ui_path);
|
|
14
28
|
console.log(' Document Path:', source_path, '\r\n');
|
|
29
|
+
console.log(' Server Port:', port);
|
|
15
30
|
|
|
16
31
|
// Get an express server instance
|
|
17
32
|
var app = express();
|
|
@@ -44,7 +59,6 @@
|
|
|
44
59
|
res.send(JSON.stringify(library, null, 3));
|
|
45
60
|
});
|
|
46
61
|
|
|
47
|
-
|
|
48
62
|
function content_type_for_ext(ext) {
|
|
49
63
|
switch (ext) {
|
|
50
64
|
case '.z':
|
|
@@ -351,11 +365,11 @@
|
|
|
351
365
|
send_content_resource_404(req, res);
|
|
352
366
|
});
|
|
353
367
|
|
|
354
|
-
var server = app.listen(
|
|
368
|
+
var server = app.listen(port, '0.0.0.0', function () {
|
|
355
369
|
var host = server.address().address;
|
|
356
370
|
var port = server.address().port;
|
|
357
371
|
|
|
358
|
-
console.log('Server listening at http
|
|
372
|
+
console.log('Server listening at http://127.0.0.1:%s', port);
|
|
359
373
|
console.log('Document source path is: ' + source_path);
|
|
360
374
|
});
|
|
361
375
|
|
package/hdoc.js
CHANGED
|
@@ -94,7 +94,7 @@ const { createCipheriv } = require('crypto');
|
|
|
94
94
|
server.run(ui_path, source_path, md);
|
|
95
95
|
} else if (command == 'build') {
|
|
96
96
|
const builder = require(path.join(__dirname, 'hdoc-build.js'));
|
|
97
|
-
builder.run(
|
|
97
|
+
builder.run(source_path, md);
|
|
98
98
|
} else if (command == 'stats') {
|
|
99
99
|
const stats = require(path.join(__dirname, 'hdoc-stats.js'));
|
|
100
100
|
stats.run(ui_path, source_path, md, verbose);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hdoc-tools",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Hornbill HDocBook Development Support Tool",
|
|
5
5
|
"main": "hdoc.js",
|
|
6
6
|
"bin": {
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"author": "Hornbill Technologies Ltd",
|
|
26
26
|
"license": "ISC",
|
|
27
27
|
"dependencies": {
|
|
28
|
+
"adm-zip": "^0.5.9",
|
|
28
29
|
"body-parser": "^1.20.1",
|
|
29
30
|
"cookie-parser": "^1.4.6",
|
|
30
31
|
"dree": "^3.4.2",
|