hdoc-tools 0.6.9 → 0.7.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-build.js +131 -3
- package/hdoc-serve.js +5 -0
- 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-serve.js
CHANGED
|
@@ -28,6 +28,11 @@
|
|
|
28
28
|
console.log(' Document Path:', source_path, '\r\n');
|
|
29
29
|
console.log(' Server Port:', port);
|
|
30
30
|
|
|
31
|
+
if(fs.existsSync(path.join(source_path, 'hdocbook-project.json')) == false) {
|
|
32
|
+
console.log("No hdocbook-project.js file found in working folder. Unable to continue.");
|
|
33
|
+
return -1;
|
|
34
|
+
}
|
|
35
|
+
|
|
31
36
|
// Get an express server instance
|
|
32
37
|
var app = express();
|
|
33
38
|
|
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.1",
|
|
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",
|