-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathohConfigDocGen.cli.js
314 lines (254 loc) · 9.6 KB
/
ohConfigDocGen.cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env node
'use strict';
var myArgs = process.argv.slice(2);
var debug = require('debug')('debug');
const Prism = require('prismjs');
var fs = require('fs');
// workaround to use an own language description for highligthing
var langOpenhab = require('./language/prism-openhab.js');
/* eslint-disable */
let openhab = langOpenhab.openhabLang();
/* eslint-enable */
var version = '1.1.0';
const srcOHfolder = myArgs[0];
const targetOutfolder = myArgs[1];
var menuContent = {};
var numFolders = 0;
var numFiles = 0;
var globalItemsRegistry = [];
// ##############################################################
// get folder structure
readSrcFolder(srcOHfolder);
var fileName = targetOutfolder + '/test.html';
var html = buildHtml();
fs.writeFile(fileName, html, function(err) {
if (err) {
return console.log(err);
}
debug('The file was saved!');
});
function buildHtml(req) {
var title = 'openhab Config Docu';
var footer = '<div><p>' + title + ' - version: ' + version +
'</p><p class="w3-small">build on ' + new Date() + '</p></div>';
var template = getTemplate();
template = template.replace('$$TITLE$$', title);
template = template.replace('$$TITLEPAGE$$', title);
template = template.replace('$$LOGO$$', title);
template = template.replace('$$FOOTER$$', footer);
template = template.replace(/\$%NUM_FOLDERS%\$/g, numFolders);
template = template.replace(/\$%NUM_FILES%\$/g, numFiles);
template = template.replace(/\$%VERSION%\$/g, version);
template = addMenuStructure(template);
template = addFiles(template);
template = exportItems(template);
template = template.replace(/\$%NUM_ITEMS%\$/g, Object.keys(globalItemsRegistry).length);
return template;
};
function readOHfile(pathToFile) {
var fs = require('fs');
var buffer = fs.readFileSync(pathToFile);
return buffer.toString();
}
function getTemplate() {
var pathToTemplate = 'templates/main.html';
return readOHfile(pathToTemplate);
}
function addMenuStructure(templateString) {
var strPart = '';
strPart = templateString.split('<!-- $%MENUITEMS_START%$ -->');
strPart = strPart[1].split(' <!-- $%MENUITEMS_END%$ -->');
// put the menu in extracted string
var newTemplatePart = strPart[0];
// prototype for menu item
var menuLine = newTemplatePart;
// empty the newPart for filling with reald data
newTemplatePart = '';
debug('create menu');
for (var name in menuContent) {
var newEntry = menuLine.replace(/\$%MENU_ITEM%\$/g, name);
newEntry = newEntry.replace('$$MENU_ITEM_LINK$$', '#' + name);
// substructure
var newSubBlock = '';
var strSubPart = newEntry.split('<!-- $%MENUITEMS_SUB_START%$ -->');
strSubPart = strSubPart[1].split(' <!-- $%MENUITEMS_SUB_END%$ -->');
var menuSubLine = strSubPart[0];
if (menuContent[name] instanceof Object) {
for (var nameSub in menuContent[name]) {
var newSubEntry = menuSubLine.replace(/\$\$MENU_SUB_ITEM\$\$/g, nameSub);
newSubEntry = newSubEntry.replace('$$MENU_ITEM_LINK$$', '#' + nameSub);
newSubBlock = newSubBlock + newSubEntry;
// debug(' new sub menu entry: '+nameSub + ' line: '+newSubBlock);
}
}
// fill with sub block, empty when no sub
newEntry = newEntry.replace(menuSubLine, newSubBlock);
newTemplatePart = newTemplatePart + newEntry;
debug(' new menu entry: ' + name);
};
// remove the template string from template
templateString = templateString.replace(strPart[0], newTemplatePart);
// $$MENU_ITEM$$
return templateString;
}
function addFiles(templateString) {
var newFileContent = '';
// iterate FileList
for (var name in menuContent) {
// substructure
if (menuContent[name] instanceof Object) {
for (var nameSub in menuContent[name]) {
// if(menuContent[name][nameSub] != "X:\\rules\\Strom.rules") continue;
debug('read file: ' + menuContent[name][nameSub]);
var filecontent = readOHfile(menuContent[name][nameSub]);
if (menuContent[name][nameSub].endsWith('.items')) {
//global ItemsRegistry = {...globalItemsRegistry, ...getItems(filecontent)};
globalItemsRegistry = globalItemsRegistry.concat(getItems(filecontent));
debug('items sum ' + Object.keys(globalItemsRegistry).length);
}
newFileContent +=
'<div class="w3-panel w3-card w3-light-grey w3-hide closeable" id="' + nameSub + '">' +
'<h3>' + nameSub + '</h3>' +
'<div class="w3-code w3-small notranslate">' +
'<pre><code>' + enocdeForHtml(filecontent) + '</code></pre>' +
'</div>' +
'</div>';
}
}
}
// sort items for name
globalItemsRegistry.sort(function(a, b) {
var nameA = a.name;
var nameB = b.name;
if (nameA < nameB) { return -1; }
if (nameA > nameB) { return 1; }
return 0;
});
templateString = templateString.replace('$%CONTENT_FILE%$', newFileContent);
return templateString;
}
function enocdeForHtml(str) {
var Normalizer = require('prismjs/plugins/normalize-whitespace/prism-normalize-whitespace');
// Create a new Normalizer object
var nw = new Normalizer({
'remove-trailing': true,
'remove-indent': true,
'left-trim': true,
'right-trim': true,
'break-lines': 100,
// 'indent': 2,
// 'remove-initial-line-feed': false,
// 'tabs-to-spaces': 4,
// 'spaces-to-tabs': 4
});
// Removes leading and trailing whitespace
// and then indents by 1 tab
str = nw.normalize(str, { });
str = Prism.highlight(str, Prism.languages.openhab, 'openhab');
return str;
}
function readSrcFolder(srcFolder) {
debug('get folder structure for: ' + srcFolder);
const dirTree = require('directory-tree');
const tree = dirTree(srcFolder, { extensions: /\.(rules|items|persist|script|cfg|json|sitemap|things|map)$/ });
Object.keys(tree.children).forEach(function(key) {
var val = tree.children[key];
if (val.type === 'directory' && !((val.name).startsWith('.')) && val.size !== 0) {
debug('folder (' + key + '): ' + val.name);
Object.keys(val.children).forEach(function(key2) {
var val2 = val.children[key2];
if (val2.type === 'file') {
if (!(val.name in menuContent)) {
menuContent[val.name] = {};
numFolders++;
};
menuContent[val.name][val2.name] = val2.path;
numFiles++;
}
});
}
});
debug('get folder structure for: ' + srcFolder + ' END');
}
function getItems(itemFileStr) {
const nearley = require('nearley');
var grammar = require('./language/items.js');
try {
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
parser.feed((itemFileStr.replace(/[\r\n]+/g, '\n')).trim());
if (!parser.results.length)
return 'Unable to parse or no items were found, check your input';
// var jsonItems = JSON.stringify(
// parser.results[0].filter(i => i !== null),
// null,
// 4,
// );
var jsonItems = parser.results[0];
debug('items ' + Object.keys(jsonItems).length);
return jsonItems;
} catch (e) {
debug('error at parsing items' + e);
}
}
function exportItems(templateString) {
var newFileContent = '<div>';
var groupContent = `<div id="itemsregistry_groups" class="w3-hide"><h3>groups</h3>
<table class="w3-table-all w3-hoverable">
<tr>
<th>name</th>
<th>label</th>
<th>category</th>
<th>tags</th>
<th>parent groups</th>
</tr>`;
var itemsContent = `<div id="itemsregistry_items" class="w3-hide"><h3>items</h3>
<table class="w3-table-all w3-hoverable">
<tr>
<th>name</th>
<th>type</th>
<th>label</th>
</tr>
`;
for (var key in globalItemsRegistry) {
if (globalItemsRegistry[key].type === 'Group') {
groupContent = groupContent +
'<tr>' +
'<td>' + globalItemsRegistry[key].name + '</td>' +
'<td>' + globalItemsRegistry[key].label + '</td>' +
'<td>' + globalItemsRegistry[key].category + '</td>' +
'<td>' + globalItemsRegistry[key].tags + '</td>' +
'<td>' + globalItemsRegistry[key].groupNames + '</td>' +
'</tr>';
} else {
itemsContent = itemsContent +
'<tr>' +
'<td class="w3-tooltip">' + globalItemsRegistry[key].name +
'<span style="position:absolute;left:100px;bottom:28px" class="w3-text w3-tag w3-animate-opacity">' +
'<div class="w3-container">' +
'<div class="w3-left-align">category: ' + globalItemsRegistry[key].category + '</div>' +
'<div class="w3-left-align">tags: ' + globalItemsRegistry[key].tags + '</div>' +
'<div class="w3-left-align">groups: ' + globalItemsRegistry[key].groupNames + '</div>' +
'</div></span>' +
'</td>' +
'<td>' + globalItemsRegistry[key].type + '</td>' +
'<td>' + globalItemsRegistry[key].label + '</td>' +
// "<td>"+globalItemsRegistry[key].category+"</td>" +
// "<td>"+globalItemsRegistry[key].tags+"</td>" +
// "<td>"+globalItemsRegistry[key].groupNames+"</td>" +
'</tr>';
}
// globalItemsRegistry[key].name
// globalItemsRegistry[key].type
// globalItemsRegistry[key].category
// globalItemsRegistry[key].function
// globalItemsRegistry[key].label
// globalItemsRegistry[key].tags []
// globalItemsRegistry[key].groupNames []
}
newFileContent = newFileContent +
groupContent + '</table></div>' +
itemsContent + '</table></div>' +
'</div>';
templateString = templateString.replace('$%CONTENT_ITEMS%$', newFileContent);
return templateString;
}