@ololoepepe/template-renderer 0.1.40 → 0.1.41
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/package.json +1 -1
- package/src/template-renderer.js +50 -22
package/package.json
CHANGED
package/src/template-renderer.js
CHANGED
|
@@ -35,6 +35,25 @@ class TemplateRenderer {
|
|
|
35
35
|
this._templateSettings = templateSettings;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
async _getTemplateStringRecursiveInternal(templateString) {
|
|
39
|
+
const lines = templateString.split('\n');
|
|
40
|
+
|
|
41
|
+
for (let i = 0; i < lines.length; ++i) {
|
|
42
|
+
const line = lines[i];
|
|
43
|
+
|
|
44
|
+
const m = line.match(RX_INCLUDE);
|
|
45
|
+
|
|
46
|
+
if (m) {
|
|
47
|
+
// eslint-disable-next-line no-await-in-loop
|
|
48
|
+
const part = await this._getTemplateStringRecursive(m[2]);
|
|
49
|
+
|
|
50
|
+
lines[i] = `${m[1]}${part}${m[3]}`;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return lines.join('\n');
|
|
55
|
+
}
|
|
56
|
+
|
|
38
57
|
async _getTemplateStringRecursive(name) {
|
|
39
58
|
const templatePath = `${this.templatesDir}/${name}`;
|
|
40
59
|
|
|
@@ -50,6 +69,14 @@ class TemplateRenderer {
|
|
|
50
69
|
|
|
51
70
|
const templateString = await readFile(templatePath, 'utf8');
|
|
52
71
|
|
|
72
|
+
const result = await this._getTemplateStringRecursiveInternal(templateString);
|
|
73
|
+
|
|
74
|
+
this._templateStringCache.set(templatePath, result);
|
|
75
|
+
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
_getTemplateStringRecursiveInternalSync(templateString) {
|
|
53
80
|
const lines = templateString.split('\n');
|
|
54
81
|
|
|
55
82
|
for (let i = 0; i < lines.length; ++i) {
|
|
@@ -58,18 +85,13 @@ class TemplateRenderer {
|
|
|
58
85
|
const m = line.match(RX_INCLUDE);
|
|
59
86
|
|
|
60
87
|
if (m) {
|
|
61
|
-
|
|
62
|
-
const part = await this._getTemplateStringRecursive(m[2]);
|
|
88
|
+
const part = this._getTemplateStringRecursiveSync(m[2]);
|
|
63
89
|
|
|
64
90
|
lines[i] = `${m[1]}${part}${m[3]}`;
|
|
65
91
|
}
|
|
66
92
|
}
|
|
67
93
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
this._templateStringCache.set(templatePath, result);
|
|
71
|
-
|
|
72
|
-
return result;
|
|
94
|
+
return lines.join('\n');
|
|
73
95
|
}
|
|
74
96
|
|
|
75
97
|
_getTemplateStringRecursiveSync(name) {
|
|
@@ -87,21 +109,7 @@ class TemplateRenderer {
|
|
|
87
109
|
|
|
88
110
|
const templateString = fs.readFileSync(templatePath, 'utf8');
|
|
89
111
|
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
for (let i = 0; i < lines.length; ++i) {
|
|
93
|
-
const line = lines[i];
|
|
94
|
-
|
|
95
|
-
const m = line.match(RX_INCLUDE);
|
|
96
|
-
|
|
97
|
-
if (m) {
|
|
98
|
-
const part = this._getTemplateStringRecursiveSync(m[2]);
|
|
99
|
-
|
|
100
|
-
lines[i] = `${m[1]}${part}${m[3]}`;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const result = lines.join('\n');
|
|
112
|
+
const result = this._getTemplateStringRecursiveInternalSync(templateString);
|
|
105
113
|
|
|
106
114
|
this._templateStringCache.set(templatePath, result);
|
|
107
115
|
|
|
@@ -152,6 +160,26 @@ class TemplateRenderer {
|
|
|
152
160
|
return this._getTemplate(name, templateString);
|
|
153
161
|
}
|
|
154
162
|
|
|
163
|
+
async loadTemplate(name, text) {
|
|
164
|
+
if (this._templateCache.has(name)) {
|
|
165
|
+
console.warn(`Template renderer: template "${name}" already exists, overriding it`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const templateString = await this._getTemplateStringRecursiveInternal(text);
|
|
169
|
+
|
|
170
|
+
this._getTemplate(name, templateString);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
loadTemplateSync(name, text) {
|
|
174
|
+
if (this._templateCache.has(name)) {
|
|
175
|
+
console.warn(`Template renderer: template "${name}" already exists, overriding it`);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const templateString = this._getTemplateStringRecursiveInternalSync(text);
|
|
179
|
+
|
|
180
|
+
this._getTemplate(name, templateString);
|
|
181
|
+
}
|
|
182
|
+
|
|
155
183
|
preloadSync(extension) {
|
|
156
184
|
this._templateStringCache.clear();
|
|
157
185
|
this._templateCache.clear();
|