-
Notifications
You must be signed in to change notification settings - Fork 406
/
Copy pathengine_twig.js
330 lines (295 loc) · 10.4 KB
/
engine_twig.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'use strict';
/*
* twig pattern engine for patternlab-node - v0.15.1 - 2015
*
* Geoffrey Pursell, Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
*
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
*
*/
/*
* ENGINE SUPPORT LEVEL:
*
* Full. Partial calls and lineage hunting are supported. Twig does not support
* the mustache-specific syntax extensions, style modifiers and pattern
* parameters, because their use cases are addressed by the core Twig feature
* set.
*
*/
const fs = require('fs-extra');
const path = require('path');
const {
TwingEnvironment,
TwingLoaderFilesystem,
TwingLoaderChain,
TwingSource,
} = require('twing');
class TwingLoaderPatternLab {
patterns = new Map();
constuctor() {}
registerPartial(pattern) {
if (pattern.patternPartial) {
this.patterns.set(pattern.patternPartial, pattern);
}
}
/**
* Returns the source context for a given template logical name.
*
* @param {string} name The template logical name
* @param {TwingSource} from The source that initiated the template loading
*
* @returns {Promise<TwingSource>}
*
* @throws TwingErrorLoader When name is not found
*/
getSourceContext(name, from) {
const pattern = this.patterns.get(name);
return Promise.resolve(
new TwingSource(pattern.extendedTemplate, name, pattern.relPath)
);
}
/**
* Gets the cache key to use for the cache for a given template name.
*
* @param {string} name The name of the template to load
* @param {TwingSource} from The source that initiated the template loading
*
* @returns {Promise<string>} The cache key
*
* @throws TwingErrorLoader When name is not found
*/
getCacheKey(name, from) {
return Promise.resolve(name);
}
/**
* Returns true if the template is still fresh.
*l
* @param {string} name The template name
* @param {number} time Timestamp of the last modification time of the cached template
* @param {TwingSource} from The source that initiated the template loading
*
* @returns {Promise<boolean>} true if the template is fresh, false otherwise
*
* @throws TwingErrorLoader When name is not found
*/
isFresh(name, time, from) {
return Promise.resolve(this.patterns.has(name) ? true : false);
}
/**
* Check if we have the source code of a template, given its name.
*
* @param {string} name The name of the template to check if we can load
* @param {TwingSource} from The source that initiated the template loading
*
* @returns {Promise<boolean>} If the template source code is handled by this loader or not
*/
exists(name, from) {
return Promise.resolve(this.patterns.has(name) ? true : false);
}
/**
* Resolve the path of a template, given its name, whatever it means in the context of the loader.
*
* @param {string} name The name of the template to resolve
* @param {TwingSource} from The source that initiated the template loading
*
* @returns {Promise<string>} The resolved path of the template
*/
resolve(name, from) {
pattern = this.patterns.get(name);
return null;
}
}
const fileSystemLoader = new TwingLoaderFilesystem();
const patternLabLoader = new TwingLoaderPatternLab();
const chainLoader = new TwingLoaderChain([fileSystemLoader, patternLabLoader]);
const twing = new TwingEnvironment(chainLoader);
let metaPath;
let patternLabConfig = {};
const engine_twig = {
engine: twing,
engineName: 'twig',
engineFileExtension: ['.twig', '.html.twig'],
// regexes, stored here so they're only compiled once
findPartialsRE:
/{[%{]\s*.*?(?:extends|include|embed|from|import|use)\(?\s*['"](.+?)['"][\s\S]*?\)?\s*[%}]}/g,
findListItemsRE:
/({{#( )?)(list(I|i)tems.)(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)( )?}}/g, // TODO
// render it
renderPattern: function renderPattern(pattern, data, partials) {
let patternPath = pattern.basePattern
? pattern.basePattern.relPath
: pattern.relPath;
if (patternPath.lastIndexOf(metaPath) === 0) {
patternPath = patternPath.substring(metaPath.length + 1);
}
return Promise.resolve(twing.render(patternPath, data));
},
registerPartial: function registerPartial(pattern) {
console.log(
`registerPartial(${pattern.name} - ${pattern.patternPartial} - ${pattern.patternPath} - ${pattern.relPath})`
);
patternLabLoader.registerPartial(pattern);
},
// find and return any {% include 'template-name' %} within pattern
findPartials: function findPartials(pattern) {
const matches = pattern.template.match(this.findPartialsRE);
const filteredMatches =
matches &&
matches.filter((match) => {
// Filter out programmatically created includes.
// i.e. {% include '@namespace/icons/assets/' ~ name ~ '.svg' %}
return match.indexOf('~') === -1;
});
return filteredMatches;
},
// returns any patterns that match {{> value(foo:"bar") }} or {{>
// value:mod(foo:"bar") }} within the pattern
findPartialsWithPatternParameters: function () {
// TODO: make the call to this from oPattern objects conditional on their
// being implemented here.
return [];
},
findListItems: function (pattern) {
const matches = pattern.template.match(this.findListItemsRE);
return matches;
},
// given a pattern, and a partial string, tease out the "pattern key" and
// return it.
findPartial: function (partialString) {
try {
const partial = partialString.replace(this.findPartialsRE, '$1');
// Check if namespaces is not empty.
const [selectedNamespace] = fileSystemLoader
.getNamespaces()
.filter((namespace) => {
// Check to see if this partial contains within the namespace id.
return partial.indexOf(`@${namespace}`) !== -1;
});
let namespaceResolvedPartial = '';
if (selectedNamespace.length > 0) {
// Loop through all namespaces and try to resolve the namespace to a file path.
const namespacePaths = fileSystemLoader.getPaths(selectedNamespace);
for (let index = 0; index < namespacePaths.length; index++) {
const patternPath = path.isAbsolute(namespacePaths[index])
? path.relative(
patternLabConfig.paths.source.root,
namespacePaths[index]
)
: namespacePaths[index];
// Replace the name space with the actual path.
// i.e. @atoms -> source/_patterns/atoms
const tempPartial = path.join(
process.cwd(),
partial.replace(`@${selectedNamespace}`, patternPath)
);
try {
// Check to see if the file actually exists.
if (fs.existsSync(tempPartial)) {
// get the path to the top-level folder of this pattern
// ex. /Users/bradfrost/sites/pattern-lab/packages/edition-twig/source/_patterns/atoms
const fullFolderPath = `${
tempPartial.split(namespacePaths[index])[0]
}${namespacePaths[index]}`;
// then tease out the folder name itself (including the # prefix)
// ex. atoms
const folderName = fullFolderPath.substring(
fullFolderPath.lastIndexOf('/') + 1,
fullFolderPath.length
);
// finally, return the Twig path we created from the full file path
// ex. atoms/buttons/button.twig
const fullIncludePath = tempPartial.replace(
tempPartial.split(
`${folderName}${tempPartial.split(folderName)[1]}`
)[0],
''
);
namespaceResolvedPartial = fullIncludePath;
// After it matches one time, set the resolved partial and exit the loop.
break;
}
} catch (err) {
console.error(err);
}
}
}
// Return the path with the namespace resolved OR the regex'd partial.
return namespaceResolvedPartial || partial;
} catch (err) {
console.error(
'Error occurred when trying to find partial name in: ' + partialString
);
return null;
}
},
spawnFile: function (config, fileName) {
const paths = config.paths;
const metaFilePath = path.resolve(paths.source.meta, fileName);
try {
fs.statSync(metaFilePath);
} catch (err) {
//not a file, so spawn it from the included file
const metaFileContent = fs.readFileSync(
path.resolve(__dirname, '..', '_meta/', fileName),
'utf8'
);
fs.outputFileSync(metaFilePath, metaFileContent);
}
},
/**
* Checks to see if the _meta directory has engine-specific head and foot files,
* spawning them if not found.
*
* @param {object} config - the global config object from core, since we won't
* assume it's already present
*/
spawnMeta: function (config) {
this.spawnFile(config, '_head.' + config.patternExtension);
this.spawnFile(config, '_foot.' + config.patternExtension);
},
/**
* Accept a Pattern Lab config object from the core and use the settings to
* load helpers.
*
* @param {object} config - the global config object from core
*/
usePatternLabConfig: function (config) {
patternLabConfig = config;
metaPath = path.resolve(config.paths.source.meta);
// Global paths
fileSystemLoader.addPath(config.paths.source.meta);
fileSystemLoader.addPath(config.paths.source.patterns);
// Namespaced paths
if (
config.engines &&
config.engines.twig &&
config.engines.twig.namespaces
) {
const namespaces = config.engines.twig.namespaces;
Object.keys(namespaces).forEach(function (key, index) {
fileSystemLoader.addPath(namespaces[key], key);
});
}
// add twing extensions
if (
config.engines &&
config.engines.twig &&
config.engines.twig.loadExtensionsFile
) {
const extensionsFile = path.resolve(
'./',
config.engines.twig.loadExtensionsFile
);
if (fs.pathExistsSync(extensionsFile)) {
try {
const extensionsMap = require(extensionsFile);
twing.addExtensions(extensionsMap);
} catch (e) {
console.error(e);
}
}
}
},
};
module.exports = engine_twig;