@@ -51,7 +51,7 @@ class TwingLoaderPatternLab {
51
51
* @throws TwingErrorLoader When name is not found
52
52
*/
53
53
getSourceContext ( name , from ) {
54
- var pattern = this . patterns . get ( name ) ;
54
+ const pattern = this . patterns . get ( name ) ;
55
55
return Promise . resolve (
56
56
new TwingSource ( pattern . extendedTemplate , name , pattern . relPath )
57
57
) ;
@@ -116,23 +116,23 @@ const fileSystemLoader = new TwingLoaderFilesystem();
116
116
const patternLabLoader = new TwingLoaderPatternLab ( ) ;
117
117
const chainLoader = new TwingLoaderChain ( [ fileSystemLoader , patternLabLoader ] ) ;
118
118
const twing = new TwingEnvironment ( chainLoader ) ;
119
- var metaPath ;
119
+ let metaPath ;
120
+ let patternLabConfig = { } ;
120
121
121
- var engine_twig = {
122
+ const engine_twig = {
122
123
engine : twing ,
123
124
engineName : 'twig' ,
124
125
engineFileExtension : '.twig' ,
125
126
126
127
// regexes, stored here so they're only compiled once
127
128
findPartialsRE :
128
- / { % [ - ] ? \s * (?: e x t e n d s | i n c l u d e | e m b e d | f r o m | i m p o r t | u s e ) \s + ( ' [ ^ ' ] + ' | " [ ^ " ] + " ) .* ?% } / g,
129
- findPartialKeyRE : / " ( (?: \\ .| [ ^ " \\ ] ) * ) " / ,
129
+ / { [ % { ] \s * .* ?(?: e x t e n d s | i n c l u d e | e m b e d | f r o m | i m p o r t | u s e ) \( ? \s * [ ' " ] ( .+ ?) [ ' " ] [ \s \S ] * ?\) ? \s * [ % } ] } / g,
130
130
findListItemsRE :
131
131
/ ( { { # ( ) ? ) ( l i s t ( I | i ) t e m s .) ( o n e | t w o | t h r e e | f o u r | f i v e | s i x | s e v e n | e i g h t | n i n e | t e n | e l e v e n | t w e l v e | t h i r t e e n | f o u r t e e n | f i f t e e n | s i x t e e n | s e v e n t e e n | e i g h t e e n | n i n e t e e n | t w e n t y ) ( ) ? } } / g, // TODO
132
132
133
133
// render it
134
134
renderPattern : function renderPattern ( pattern , data , partials ) {
135
- var patternPath = pattern . basePattern
135
+ let patternPath = pattern . basePattern
136
136
? pattern . basePattern . relPath
137
137
: pattern . relPath ;
138
138
if ( patternPath . lastIndexOf ( metaPath ) === 0 ) {
@@ -150,8 +150,15 @@ var engine_twig = {
150
150
151
151
// find and return any {% include 'template-name' %} within pattern
152
152
findPartials : function findPartials ( pattern ) {
153
- var matches = pattern . template . match ( this . findPartialsRE ) ;
154
- return matches ;
153
+ const matches = pattern . template . match ( this . findPartialsRE ) ;
154
+ const filteredMatches =
155
+ matches &&
156
+ matches . filter ( ( match ) => {
157
+ // Filter out programmatically created includes.
158
+ // i.e. {% include '@namespace/icons/assets/' ~ name ~ '.svg' % }
159
+ return match . indexOf ( '~' ) === - 1 ;
160
+ } ) ;
161
+ return filteredMatches ;
155
162
} ,
156
163
157
164
// returns any patterns that match {{> value(foo:"bar") }} or {{>
@@ -161,18 +168,90 @@ var engine_twig = {
161
168
// being implemented here.
162
169
return [ ] ;
163
170
} ,
171
+
164
172
findListItems : function ( pattern ) {
165
- var matches = pattern . template . match ( this . findListItemsRE ) ;
173
+ const matches = pattern . template . match ( this . findListItemsRE ) ;
166
174
return matches ;
167
175
} ,
168
176
169
177
// given a pattern, and a partial string, tease out the "pattern key" and
170
178
// return it.
171
179
findPartial : function ( partialString ) {
172
- var partial = partialString . match ( this . findPartialKeyRE ) [ 0 ] ;
173
- partial = partial . replace ( / " / g, '' ) ;
180
+ try {
181
+ const partial = partialString . replace ( this . findPartialsRE , '$1' ) ;
182
+
183
+ // Check if namespaces is not empty.
184
+ const [ selectedNamespace ] = fileSystemLoader
185
+ . getNamespaces ( )
186
+ . filter ( ( namespace ) => {
187
+ // Check to see if this partial contains within the namespace id.
188
+ return partial . indexOf ( `@${ namespace } ` ) !== - 1 ;
189
+ } ) ;
190
+
191
+ let namespaceResolvedPartial = '' ;
192
+
193
+ if ( selectedNamespace . length > 0 ) {
194
+ // Loop through all namespaces and try to resolve the namespace to a file path.
195
+ const namespacePaths = fileSystemLoader . getPaths ( selectedNamespace ) ;
196
+
197
+ for ( let index = 0 ; index < namespacePaths . length ; index ++ ) {
198
+ const patternPath = path . isAbsolute ( namespacePaths [ index ] )
199
+ ? path . relative (
200
+ patternLabConfig . paths . source . root ,
201
+ namespacePaths [ index ]
202
+ )
203
+ : namespacePaths [ index ] ;
204
+
205
+ // Replace the name space with the actual path.
206
+ // i.e. @atoms -> source/_patterns/atoms
207
+ const tempPartial = path . join (
208
+ process . cwd ( ) ,
209
+ partial . replace ( `@${ selectedNamespace } ` , patternPath )
210
+ ) ;
174
211
175
- return partial ;
212
+ try {
213
+ // Check to see if the file actually exists.
214
+ if ( fs . existsSync ( tempPartial ) ) {
215
+ // get the path to the top-level folder of this pattern
216
+ // ex. /Users/bradfrost/sites/pattern-lab/packages/edition-twig/source/_patterns/atoms
217
+ const fullFolderPath = `${
218
+ tempPartial . split ( namespacePaths [ index ] ) [ 0 ]
219
+ } ${ namespacePaths [ index ] } `;
220
+
221
+ // then tease out the folder name itself (including the # prefix)
222
+ // ex. atoms
223
+ const folderName = fullFolderPath . substring (
224
+ fullFolderPath . lastIndexOf ( '/' ) + 1 ,
225
+ fullFolderPath . length
226
+ ) ;
227
+
228
+ // finally, return the Twig path we created from the full file path
229
+ // ex. atoms/buttons/button.twig
230
+ const fullIncludePath = tempPartial . replace (
231
+ tempPartial . split (
232
+ `${ folderName } ${ tempPartial . split ( folderName ) [ 1 ] } `
233
+ ) [ 0 ] ,
234
+ ''
235
+ ) ;
236
+
237
+ namespaceResolvedPartial = fullIncludePath ;
238
+
239
+ // After it matches one time, set the resolved partial and exit the loop.
240
+ break ;
241
+ }
242
+ } catch ( err ) {
243
+ console . error ( err ) ;
244
+ }
245
+ }
246
+ }
247
+ // Return the path with the namespace resolved OR the regex'd partial.
248
+ return namespaceResolvedPartial || partial ;
249
+ } catch ( err ) {
250
+ console . error (
251
+ 'Error occurred when trying to find partial name in: ' + partialString
252
+ ) ;
253
+ return null ;
254
+ }
176
255
} ,
177
256
178
257
spawnFile : function ( config , fileName ) {
@@ -209,31 +288,32 @@ var engine_twig = {
209
288
* @param {object } config - the global config object from core
210
289
*/
211
290
usePatternLabConfig : function ( config ) {
291
+ patternLabConfig = config ;
212
292
metaPath = path . resolve ( config . paths . source . meta ) ;
213
293
// Global paths
214
294
fileSystemLoader . addPath ( config . paths . source . meta ) ;
215
295
fileSystemLoader . addPath ( config . paths . source . patterns ) ;
216
296
// Namespaced paths
217
297
if (
218
- config [ ' engines' ] &&
219
- config [ ' engines' ] [ ' twig' ] &&
220
- config [ ' engines' ] [ ' twig' ] [ ' namespaces' ]
298
+ config . engines &&
299
+ config . engines . twig &&
300
+ config . engines . twig . namespaces
221
301
) {
222
- var namespaces = config [ ' engines' ] [ ' twig' ] [ ' namespaces' ] ;
302
+ const namespaces = config . engines . twig . namespaces ;
223
303
Object . keys ( namespaces ) . forEach ( function ( key , index ) {
224
304
fileSystemLoader . addPath ( namespaces [ key ] , key ) ;
225
305
} ) ;
226
306
}
227
307
228
308
// add twing extensions
229
309
if (
230
- config [ ' engines' ] &&
231
- config [ ' engines' ] [ ' twig' ] &&
232
- config [ ' engines' ] [ ' twig' ] [ ' loadExtensionsFile' ]
310
+ config . engines &&
311
+ config . engines . twig &&
312
+ config . engines . twig . loadExtensionsFile
233
313
) {
234
314
const extensionsFile = path . resolve (
235
315
'./' ,
236
- config [ ' engines' ] [ ' twig' ] [ ' loadExtensionsFile' ]
316
+ config . engines . twig . loadExtensionsFile
237
317
) ;
238
318
if ( fs . pathExistsSync ( extensionsFile ) ) {
239
319
try {
0 commit comments