Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit 37bacdc

Browse files
committed
Create a new mapping between file extensions and engine names at load
time that powers a real implementation of getEngineNameForPattern!
1 parent c4d5080 commit 37bacdc

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

builder/pattern_engines/pattern_engines.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
*/
1010

11+
1112
(function () {
1213
'use strict';
1314

@@ -17,6 +18,9 @@
1718
'handlebars'
1819
];
1920

21+
// mapping of file extensions to engine names, for lookup use
22+
var engineNameForExtension = {};
23+
2024
// Object/hash of all loaded pattern engines, empty at first.
2125
// My intention here is to make this return an object that can be used to
2226
// obtain any loaded PatternEngine by addressing them like this:
@@ -25,8 +29,17 @@
2529
// var Mustache = PatternEngines['mustache'];
2630
//
2731
var PatternEngines = Object.create({
32+
supportedPatternEngineNames: supportedPatternEngineNames,
33+
2834
getEngineNameForPattern: function (pattern) {
29-
console.log('pattern file name: ', pattern.fileName);
35+
// avoid circular dependency by putting this in here. TODO: is this slow?
36+
var of = require('../object_factory');
37+
38+
if (pattern instanceof of.oPattern) {
39+
return engineNameForExtension[pattern.fileExtension];
40+
}
41+
// otherwise, assume it's a plain mustache template string and act
42+
// accordingly
3043
return 'mustache';
3144
},
3245
getEngineForPattern: function (pattern) {
@@ -36,13 +49,29 @@
3649
});
3750

3851
// try to load all supported engines
39-
supportedPatternEngineNames.forEach(function (engineName) {
40-
try {
41-
PatternEngines[engineName] = require('./engine_' + engineName);
42-
} catch (err) {
43-
console.log(err, 'pattern engine "' + engineName + '" not loaded. Did you install its dependency with npm?');
44-
}
45-
});
52+
(function loadAllEngines() {
53+
supportedPatternEngineNames.forEach(function (engineName) {
54+
try {
55+
PatternEngines[engineName] = require('./engine_' + engineName);
56+
} catch (err) {
57+
console.log(err, 'pattern engine "' + engineName + '" not loaded. Did you install its dependency with npm?');
58+
}
59+
});
60+
})();
61+
62+
// produce a mapping between file extension and engine name for each of the
63+
// loaded engines
64+
engineNameForExtension = (function () {
65+
var mapping = {};
66+
67+
Object.keys(PatternEngines).forEach(function (engineName) {
68+
var extensionForEngine = PatternEngines[engineName].fileExtension;
69+
mapping[extensionForEngine] = engineName;
70+
});
71+
72+
return mapping;
73+
})();
74+
4675

4776
module.exports = PatternEngines;
4877
})();

0 commit comments

Comments
 (0)