-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtml-preprocessor.js
162 lines (128 loc) · 4.7 KB
/
html-preprocessor.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
var http = require('http');
var fs = require('fs');
var cheerio = require('cheerio');
class HtmlPreprocessor {
/*
@html: html page as a string
@replaceTagWith: Associative array
key: tag
value: replacement text
@return string containing the changed html
In html replaces the tag <var>tag</var> with the replacement text
*/
static variableInjection(html, inputVariables) {
var reg = /{{+([\w\d]+(\.?))+}}/g;
var matches = html.match(reg);
if(matches != null)
for(let i = 0; i < matches.length; i++){
var key = matches[i];
html = html.replace(key, HtmlPreprocessor.getNestedValue(inputVariables, key.replace('{{', '').replace('}}', '')));
}
var $ = cheerio.load(html);
$('var').each((index,element) => {
var temp = HtmlPreprocessor.getNestedValue(inputVariables, $(element).html());
if(temp != undefined) $(element).replaceWith(temp.toString());
});
return $.html();
}
/*
@html: html page as a string
@return string containing the changed html
Replaces <import file="filename"></import> with the text files content
*/
static fileInjection(html) {
var $ = cheerio.load(html);
while($('import').length !== 0)
{
$('import').each((index,element) =>{
var fileName = element.attribs.file;
if(fs.existsSync(fileName))
{
var file = fs.readFileSync(fileName);
$(element).replaceWith(file.toString());
}
else
throw("File: '" + fileName + "' not found!")
})
}
return $.html();
}
/*
@html: html page as a string
@replaceTagWith: Associative array
key: tag
value: replacement text
@return string containing the changed html
In html replaces the tag <var>tag</var> with the replacement text
*/
static conditionalInjection(html, inputVariables) {
var $ = cheerio.load(html);
$('if').each((index,element) => {
var content = $(element).html();
var temp = $(element).attr('condition');
var condition = true;
if(temp.charAt(0) == "!")
{
condition = false;
condition = !HtmlPreprocessor.getNestedValue(inputVariables, temp.substring(1));
}
else
{
condition = HtmlPreprocessor.getNestedValue(inputVariables, temp);
}
if(!condition){
$(element).replaceWith('');
}else{
$(element).replaceWith(content);
}
});
return $.html();
}
/*
@html: html page as a string
@inputVariables: Associative array
key: tag
value: replacement text
@return string containing the changed html
Parses html and repeats everything within <foreach></foreach> tags
*/
static foreachInjection(html, inputVariables){
var $ = cheerio.load(html);
// console.log($('foreach').attr('key'));
$('foreach').filter(
function(i, el) {
return $(this).parents().filter('foreach').length == 0;
}
).each((index, element) => {
var key = element.attribs.key;
var list = HtmlPreprocessor.getNestedValue(inputVariables, element.attribs.in);
var tag = element.attribs.tag;
var newElement = $('<'+tag+'><'+tag+'/>');
if(list != undefined){
var children = $(element).html();
list.forEach((item) => {
var loopList = {};
loopList[key] = item;
newElement.append(HtmlPreprocessor.variableInjection(HtmlPreprocessor.foreachInjection(children, loopList), loopList));
});
}
$(element).replaceWith(newElement);
});
return $.html();
}
/*
*/
static getNestedValue(obj, key) {
return key.split(".").reduce(function(result, key) {
return result[key]
}, obj);
}
static process(html, inputVariables) {
html = HtmlPreprocessor.fileInjection(html);
html = HtmlPreprocessor.conditionalInjection(html, inputVariables);
html = HtmlPreprocessor.foreachInjection(html, inputVariables);
html = HtmlPreprocessor.variableInjection(html, inputVariables);
return html;
}
}
module.exports = HtmlPreprocessor;