This repository was archived by the owner on May 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathengine_liquid.js
More file actions
194 lines (169 loc) · 5.6 KB
/
engine_liquid.js
File metadata and controls
194 lines (169 loc) · 5.6 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
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
/*
* Liquid pattern engine for patternlab-node - v2.X.X - 2017
*
* Cameron Roe
* Licensed under the MIT license.
*
*
*/
'use strict';
const fs = require('fs-extra');
const path = require('path');
const isDirectory = (source) => fs.lstatSync(source).isDirectory();
const getDirectories = (source) =>
fs
.readdirSync(source)
.map((name) => path.join(source, name))
.filter(isDirectory);
const { lstatSync, readdirSync } = require('fs');
const { join } = require('path');
var utils = require('./util_liquid');
var Liquid = require('liquidjs');
let engine = Liquid({
dynamicPartials: false,
});
// This holds the config from from core. The core has to call
// usePatternLabConfig() at load time for this to be populated.
let patternLabConfig = {};
module.exports = {
engine: engine,
engineName: 'liquid',
engineFileExtension: ['.liquid', '.html'],
isAsync: true,
// // partial expansion is only necessary for Mustache templates that have
// // style modifiers or pattern parameters (I think)
// expandPartials: true,
// regexes, stored here so they're only compiled once
findPartialsRE: utils.partialsRE,
findPartialsWithStyleModifiersRE: utils.partialsWithStyleModifiersRE,
findPartialsWithPatternParametersRE: utils.partialsWithPatternParametersRE,
findListItemsRE: utils.listItemsRE,
findPartialRE: utils.partialRE,
// render it
renderPattern: function renderPattern(pattern, data, partials) {
return engine
.parseAndRender(pattern.template, data)
.then(function (html) {
return html;
})
.catch(function (ex) {
console.log(40, ex);
});
},
/**
* Find regex matches within both pattern strings and pattern objects.
*
* @param {string|object} pattern Either a string or a pattern object.
* @param {object} regex A JavaScript RegExp object.
* @returns {array|null} An array if a match is found, null if not.
*/
patternMatcher: function patternMatcher(pattern, regex) {
var matches;
if (typeof pattern === 'string') {
matches = pattern.match(regex);
} else if (
typeof pattern === 'object' &&
typeof pattern.template === 'string'
) {
matches = pattern.template.match(regex);
}
return matches;
},
// find and return any {{> template-name }} within pattern
findPartials: function findPartials(pattern) {
var matches = this.patternMatcher(pattern, this.findPartialsRE);
return matches;
},
findPartialsWithStyleModifiers: function (pattern) {
var matches = this.patternMatcher(
pattern,
this.findPartialsWithStyleModifiersRE
);
return matches;
},
// returns any patterns that match {{> value(foo:"bar") }} or {{>
// value:mod(foo:"bar") }} within the pattern
findPartialsWithPatternParameters: function (pattern) {
var matches = this.patternMatcher(
pattern,
this.findPartialsWithPatternParametersRE
);
return matches;
},
findListItems: function (pattern) {
var matches = this.patternMatcher(pattern, this.findListItemsRE);
return matches;
},
// given a pattern, and a partial string, tease out the "pattern key" and
// return it.
findPartial_new: function (partialString) {
var partial = partialString.replace(this.findPartialRE, '$1');
return partial;
},
// GTP: the old implementation works better. We might not need
// this.findPartialRE anymore if it works in all cases!
findPartial: function (partialString) {
//strip out the template cruft
var foundPatternPartial = partialString
.replace('{{> ', '')
.replace(' }}', '')
.replace('{{>', '')
.replace('}}', '');
// remove any potential pattern parameters. this and the above are rather brutish but I didn't want to do a regex at the time
if (foundPatternPartial.indexOf('(') > 0) {
foundPatternPartial = foundPatternPartial.substring(
0,
foundPatternPartial.indexOf('(')
);
}
//remove any potential stylemodifiers.
foundPatternPartial = foundPatternPartial.split(':')[0];
return foundPatternPartial;
},
/**
* Accept a Pattern Lab config object from the core and put it in
* this module's closure scope so we can configure engine behavior.
*
* @param {object} config - the global config object from core
*/
usePatternLabConfig: function (config) {
patternLabConfig = config;
let patternsPath = patternLabConfig.paths.source.patterns;
if (patternsPath.slice(-1) === '/') {
patternsPath = patternsPath.slice(0, -1);
}
const allPaths = getDirectories(patternsPath).reduce((allDirs, dir) => {
return allDirs.concat(getDirectories(dir));
}, []);
engine = Liquid({
dynamicPartials: false,
root: allPaths,
});
},
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 localMetaFilePath = path.resolve(__dirname, '_meta/', fileName);
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.liquid');
this.spawnFile(config, '_foot.liquid');
},
};