-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.js
More file actions
161 lines (115 loc) · 4.57 KB
/
template.js
File metadata and controls
161 lines (115 loc) · 4.57 KB
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
const fs = require('fs');
const path = require('path')
const crypto = require('crypto');
class Template {
DELIMITERS = {
OPEN: '<%',
CLOSE: '%>',
OUTPUT: '<%='
}
DEFAULT_EXTENSION = '.tem'
fileName = ''
dirName = ''
regexDelimiter = new RegExp(/(<%%|%%>|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)/)
compile(template, data) {
const parsedTemplate = this.generateSource(this.parse(template))
const src = `let { ${Object.keys(data).join(', ')} } = data;\n ${parsedTemplate}`
const fn = new Function('data', src)
return fn(data)
}
renderFile(fileName, data) {
this.fileName = path.basename(fileName)
this.dirName = path.dirname(fileName) + '/'
const cachedTemplate = this.getCached()
if (cachedTemplate !== false)
return cachedTemplate
const template = fs.readFileSync(`${fileName}${this.DEFAULT_EXTENSION}`).toString()
const compiled = this.compile(template, data)
this.cacheResult(compiled)
return compiled
}
parse(template) {
const tokens = []
let includePos
const includeRegex = new RegExp(/<%.*include\((.*)\).*%>/gm) // better to identify line breaks
while (includePos = includeRegex.exec(template)) {
let partialTemplate = ''
const pathToPartial = `${this.dirName}${includePos[1]}${this.DEFAULT_EXTENSION}`
if (fs.existsSync(pathToPartial))
partialTemplate = fs.readFileSync(pathToPartial).toString()
template = template.replace(includePos[0], partialTemplate)
}
let result = this.regexDelimiter.exec(template)
while (result) {
let delimiterPos = result.index
let delimiterChar = result[0]
if (delimiterPos !== 0) {
tokens.push(template.substring(0, delimiterPos))
template = template.substring(delimiterPos)
}
tokens.push(delimiterChar)
template = template.slice(delimiterChar.length)
result = this.regexDelimiter.exec(template)
}
if (template)
tokens.push(template)
return tokens
}
generateSource(tokens) {
const prepend = "var __output = '';\n function __append(str) { if (str !== undefined && str !== null) __output += str }\n\n try {\n"
let src = ''
const append = "return __output;\n} catch (e) {\nthrow new Error(e)\n}"
for (let index = 0; index < tokens.length; index++) {
const isOpen = tokens[index] === this.DELIMITERS.OPEN
const isOutput = tokens[index] === this.DELIMITERS.OUTPUT
const isHTML = !isOpen && !isOutput
if (isOpen || isOutput) {
if (tokens[index + 2] !== this.DELIMITERS.CLOSE) {
throw new Error("Não foi encontrado fechamento de delimitador")
}
}
if (isOpen) {
src += tokens[index + 1] + '\n '
index += 2
}
if (isOutput) {
src += '__append(' + tokens[index + 1] + ');\n '
index += 2
}
if (isHTML) {
src += '__append(`' + tokens[index] + '`);\n '
}
}
return prepend + src + append
}
cacheResult(html) {
if (!fs.existsSync('cache/')) {
fs.mkdirSync('cache')
}
const newFileName = crypto.createHash('md5').update(this.fileName).digest('hex')
fs.writeFile(`cache/${newFileName}.html`, html, (err) => {
if (err)
throw new Error('Template não foi cacheado')
})
}
getCached() {
if (!fs.existsSync('cache/')) {
return false
}
const newFileMd5 = crypto.createHash('md5').update(this.fileName).digest('hex')
const isCached = fs.readdirSync('cache/').filter(file => {
return file.split('.')[0] === newFileMd5
})
if (isCached.length > 0) {
const pathToCachedTemplate = `cache/${newFileMd5}.html`
const cachedStatus = fs.statSync(pathToCachedTemplate)
const currentFileStatus = fs.statSync(`${this.dirName}${this.fileName}${this.DEFAULT_EXTENSION}`)
if (currentFileStatus.mtimeMs > cachedStatus.mtimeMs) { // original template has been modified
return false
}
return fs.readFileSync(pathToCachedTemplate).toString()
}
return false
}
}
exports.Template = Template