-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
93 lines (75 loc) · 2.96 KB
/
parser.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
let fs = require('fs');
const jsdom = require("jsdom");
const {JSDOM} = jsdom;
const pagesPath = __dirname + '/pages';
const resultPath = __dirname + '/result'
const parsedPagesPath = resultPath + '/pages';
const pageLayout = '_base.html';
const pageLayoutTemplate = `{% layout = "layouts/${pageLayout}" %}\n\n`;
const contentHeadingTemplate = `{-contentHeading-}`;
const contentBodyTemplate = `{-contentBody-}`;
const requireByTemplate = `{-requireBy-}`;
// Router variables
const configPath = resultPath + '/config'
const contentPath = resultPath + '/content';
const files = fs.readdirSync(pagesPath);
let routerConfig = '';
files.forEach(function (fileName) {
parseHtml(fileName);
prepareRouterConfig(fileName);
generateContentFile(fileName);
});
storeRouterConfig();
function prepareRouterConfig(file) {
const entityName = file.replace('.doc', '').replace('.html', '');
routerConfig += `/${entityName}: content/${entityName}.txt\n`
}
function storeRouterConfig() {
fs.writeFileSync(`${configPath}/router.conf.yaml`, routerConfig);
}
function generateContentFile(file) {
const entityName = file.replace('.spec', '').replace('.doc', '').replace('.html', '');
let contentTemplate = `---
layout: pages/${entityName}.html
---`;
fs.writeFileSync(`${contentPath}/${entityName}.txt`, contentTemplate);
}
function parseHtml(file) {
const fileContent = fs.readFileSync(`${pagesPath}/${file}`, 'utf8',);
let window = new JSDOM(fileContent).window;
let document = window.document;
let elements = document.getElementsByClassName("slds-text-heading--large");
let $ = require('jquery')(window);
if (elements.length > 0) {
const contentHeading = elements[0].parentElement.innerHTML;
let jqueryContentBody = $('#graphql-schema-definition').siblings('code');
let jqueryRequireBy = $('.require-by');
$.each(jqueryContentBody.find('a'), function () {
$(this).attr('href', './'+$(this).attr('href').replace('.doc', '').replace('.spec', '').replace('.html', ''));
});
$.each(jqueryRequireBy.find('a'), function () {
$(this).attr('href', './'+$(this).attr('href').replace('.doc', '').replace('.spec', '').replace('.html', ''));
});
const parsedPageContent = pageLayoutTemplate
+ contentHeadingTemplate
+ "\n"
+ contentHeading.trim()
+ "\n"
+ contentHeadingTemplate
+ "\n"
+ contentBodyTemplate
+ "\n"
+ $('<div>').append(jqueryContentBody.clone()).html()
+ "\n"
+ contentBodyTemplate
+ "\n"
+ requireByTemplate
+ "\n"
+ $('<div>').append(jqueryRequireBy.clone()).html()
+ "\n"
+ requireByTemplate;
fs.writeFileSync(`${parsedPagesPath}/${file.replace('.doc', '').replace('.spec', '')}`, parsedPageContent);
} else {
console.log(file, 'has error');
}
}