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

Commit 1578b0b

Browse files
committed
Clean up code in engine_twig and move to the correct location
1 parent 875f26e commit 1578b0b

File tree

2 files changed

+82
-87
lines changed

2 files changed

+82
-87
lines changed

builder/pattern_engines/engine_twig.js

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* handlebars pattern engine for patternlab-node - v0.15.1 - 2015
3+
*
4+
* Geoffrey Pursell, Brian Muenzenmeyer, and the web community.
5+
* Licensed under the MIT license.
6+
*
7+
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
8+
*
9+
*/
10+
11+
/*
12+
* ENGINE SUPPORT LEVEL:
13+
*
14+
* Full. Partial calls and lineage hunting are supported. Handlebars does not
15+
* support the mustache-specific syntax extensions, style modifiers and pattern
16+
* parameters, because their use cases are addressed by the core Handlebars
17+
* feature set.
18+
*
19+
*/
20+
21+
"use strict";
22+
23+
var Twig = require('twig/twig.js');
24+
var twig = Twig.twig;
25+
26+
var engine_twig = {
27+
engine: Twig,
28+
engineName: 'twig',
29+
engineFileExtension: '.twig',
30+
31+
//Important! Needed for Twig compilation. Can't resolve paths otherwise.
32+
expandPartials: true,
33+
34+
// regexes, stored here so they're only compiled once
35+
findPartialsRE: /{%([ ]+)?(?:extends|include|embed)(\s\S)(.*)%}/g,
36+
findPartialKeyRE: /"((?:\\.|[^"\\])*)"/,
37+
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
38+
39+
// render it
40+
renderPattern: function renderPattern(template, data) {
41+
var result = twig({
42+
data: template
43+
}).render(data);
44+
45+
return result;
46+
},
47+
48+
// find and return any {{> template-name }} within pattern
49+
findPartials: function findPartials(pattern) {
50+
var matches = pattern.template.match(this.findPartialsRE);
51+
return matches;
52+
},
53+
findPartialsWithStyleModifiers: function () {
54+
// TODO: make the call to this from oPattern objects conditional on their
55+
// being implemented here.
56+
return [];
57+
},
58+
59+
// returns any patterns that match {{> value(foo:"bar") }} or {{>
60+
// value:mod(foo:"bar") }} within the pattern
61+
findPartialsWithPatternParameters: function () {
62+
// TODO: make the call to this from oPattern objects conditional on their
63+
// being implemented here.
64+
return [];
65+
},
66+
findListItems: function (pattern) {
67+
var matches = pattern.template.match(this.findListItemsRE);
68+
return matches;
69+
},
70+
71+
// given a pattern, and a partial string, tease out the "pattern key" and
72+
// return it.
73+
findPartialKey: function (partialString) {
74+
//var partialKey = partialString.replace(this.findPartialsRE, '$1');
75+
var partialKey = partialString.match(this.findPartialKeyRE)[0];
76+
partialKey = partialKey.replace(/"/g, '');
77+
78+
return partialKey;
79+
}
80+
};
81+
82+
module.exports = engine_twig;

0 commit comments

Comments
 (0)