|
8 | 8 | *
|
9 | 9 | */
|
10 | 10 |
|
| 11 | + |
11 | 12 | (function () {
|
12 | 13 | 'use strict';
|
13 | 14 |
|
|
17 | 18 | 'handlebars'
|
18 | 19 | ];
|
19 | 20 |
|
| 21 | + // mapping of file extensions to engine names, for lookup use |
| 22 | + var engineNameForExtension = {}; |
| 23 | + |
20 | 24 | // Object/hash of all loaded pattern engines, empty at first.
|
21 | 25 | // My intention here is to make this return an object that can be used to
|
22 | 26 | // obtain any loaded PatternEngine by addressing them like this:
|
|
25 | 29 | // var Mustache = PatternEngines['mustache'];
|
26 | 30 | //
|
27 | 31 | var PatternEngines = Object.create({
|
| 32 | + supportedPatternEngineNames: supportedPatternEngineNames, |
| 33 | + |
28 | 34 | 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 |
30 | 43 | return 'mustache';
|
31 | 44 | },
|
32 | 45 | getEngineForPattern: function (pattern) {
|
|
36 | 49 | });
|
37 | 50 |
|
38 | 51 | // 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 | + |
46 | 75 |
|
47 | 76 | module.exports = PatternEngines;
|
48 | 77 | })();
|
0 commit comments