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

Commit a422daa

Browse files
author
e2tha-e
committed
a parameterized partial finder. comments on partial finding regex
1 parent dc43f4a commit a422daa

File tree

2 files changed

+159
-9
lines changed

2 files changed

+159
-9
lines changed

builder/pattern_engines/engine_mustache.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"use strict";
1313

1414
var Mustache = require('mustache');
15+
var utilMustache = require('./util_mustache');
1516

1617
var engine_mustache = {
1718
engine: Mustache,
@@ -23,11 +24,11 @@
2324
expandPartials: true,
2425

2526
// regexes, stored here so they're only compiled once
26-
findPartialsRE: /{{>\s*((?:\d+-[\w-]+\/)+(\d+-[\w-]+(\.\w+)?)|[A-Za-z0-9-]+)(\:[\w-]+(?:\|[\w-]+)*)?(\(\s*\w+\s*:\s*(?:'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")\))?\s*}}/g,
27-
findPartialsWithStyleModifiersRE: /{{>([ ])?([\w\-\.\/~]+)(?!\()(\:[A-Za-z0-9-_|]+)+(?:(| )\(.*)?([ ])?}}/g,
28-
findPartialsWithPatternParametersRE: /{{>([ ])?([\w\-\.\/~]+)(?:\:[A-Za-z0-9-_|]+)?(?:(| )\(.*)+([ ])?}}/g,
29-
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,
30-
findPartialKeyRE: /{{>([ ])?([\w\-\.\/~]+)(:[A-z-_|]+)?(?:\:[A-Za-z0-9-_]+)?(?:(| )\(.*)?([ ])?}}/g,
27+
findPartialsRE: utilMustache.partialsRE,
28+
findPartialsWithStyleModifiersRE: utilMustache.partialsWithStyleModifiersRE,
29+
findPartialsWithPatternParametersRE: utilMustache.partialsWithPatternParametersRE,
30+
findListItemsRE: utilMustache.listItemsRE,
31+
findPartialKeyRE: utilMustache.partialKeyRE,
3132

3233
// render it
3334
renderPattern: function renderPattern(template, data, partials) {
@@ -37,23 +38,38 @@
3738
return Mustache.render(template, data);
3839
},
3940

41+
// find partials based on regex.
42+
// @param {string|object} pattern - either a string or a pattern object.
43+
// @param {object} regex - a JavaScript RegExp object.
44+
// @returns {array}
45+
partialsFinder: function partialsFinder(pattern, regex){
46+
var matches = [];
47+
48+
if(typeof pattern === 'string'){
49+
matches = pattern.match(regex);
50+
} else if(typeof pattern === 'object' && typeof pattern.template === 'string'){
51+
matches = pattern.template.match(regex);
52+
}
53+
54+
return matches;
55+
},
4056
// find and return any {{> template-name }} within pattern
4157
findPartials: function findPartials(pattern) {
42-
var matches = pattern.template.match(this.findPartialsRE);
58+
var matches = this.partialsFinder(pattern, this.findPartialsRE);
4359
return matches;
4460
},
4561
findPartialsWithStyleModifiers: function(pattern) {
46-
var matches = pattern.template.match(this.findPartialsWithStyleModifiersRE);
62+
var matches = this.partialsFinder(pattern, this.findPartialsWithStyleModifiersRE);
4763
return matches;
4864
},
4965
// returns any patterns that match {{> value(foo:"bar") }} or {{>
5066
// value:mod(foo:"bar") }} within the pattern
5167
findPartialsWithPatternParameters: function(pattern) {
52-
var matches = pattern.template.match(this.findPartialsWithPatternParametersRE);
68+
var matches = this.partialsFinder(pattern, this.findPartialsWithPatternParametersRE);
5369
return matches;
5470
},
5571
findListItems: function(pattern) {
56-
var matches = pattern.template.match(this.findListItemsRE);
72+
var matches = this.partialsFinder(pattern, this.findListItemsRE);
5773
return matches;
5874
},
5975
// given a pattern, and a partial string, tease out the "pattern key" and
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* mustache utilities for patternlab-node - v0.10.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+
(function () {
12+
'use strict';
13+
14+
// the term "alphanumeric" includes underscores.
15+
16+
// look for an opening mustache include tag, followed by >=0 whitespaces
17+
var partialsStr = '{{>\\s*';
18+
// begin 1st exterior group, a mandatory group
19+
// look for >0 of an interior group comprising
20+
// >0 digits, followed by a hyphen, followed by >0 alphanumerics
21+
partialsStr += '((\\d+-[\\w-]+\\/)+';
22+
// then an interior group comprising
23+
// >0 digits, followed by a hyphen, followed by >0 alphanumerics,
24+
// followed by an optional group of a period followed by >0 alphanumerics
25+
partialsStr += '(\\d+-[\\w-]+(\\.\\w+)?)';
26+
// if the previous two interior groups are not found, look for any number of
27+
// alphanumerics or hyphens
28+
partialsStr += '|[\\w\\-]+)';
29+
// end 1st exterior group
30+
// begin 2nd exterior group, an optional group
31+
// look for a colon, followed by >0 alphanumerics or hyphens,
32+
// followed by >=0 interior groups
33+
// comprising a pipe, followed by >0 alphanumerics or hyphens
34+
partialsStr += '(\\:[\\w\\-]+(\\|[\\w\\-]+)*)?';
35+
// end 2nd exterior group
36+
// begin 3rd exterior group, an optional group
37+
// look for an opening parenthesis, followed by >=0 whitespaces,
38+
// followed by >=0 whitespaces, followed by a colon, followed by >=0 whitespaces
39+
partialsStr += '(\\(\\s*\\w+\\s*:\\s*';
40+
// followed by an interior group
41+
// comprising a single quote, followed by an interior group comprising
42+
// >0 characters that are not single quotes or backslashes
43+
// or >0 character pairs comprising a backlash, followed by any character
44+
// look for a single quote to termination this pattern
45+
partialsStr += '(\'([^\'\\\\]|\\\\.)*\'';
46+
// if the pattern wrapped in single quotes is not found, look for one wrapped
47+
// in double quotes
48+
// look for a double quote, followed by an interior group comprising
49+
// >0 characters that are not double quotes or backslashes
50+
// or >0 character pairs comprising a backlash, followed by any character
51+
// look for a double quote to termination this pattern
52+
partialsStr += '|"([^"\\\\]|\\\\.)*")';
53+
// look for a closing parenthesis
54+
partialsStr += '\\))?';
55+
// end 3rd exterior group
56+
// look for >=0 whitespaces, followed by closing mustache tag
57+
partialsStr += '\\s*}}';
58+
var partialsRE = new RegExp(partialsStr, 'g');
59+
60+
// look for an opening mustache include tag, followed by >=0 whitespaces
61+
var partialsWithStyleModifiersStr = '{{>\\s*';
62+
// one or more characters comprising any combination of alphanumerics,
63+
// hyphens, periods, slashses, and tildes
64+
partialsWithStyleModifiersStr += '([\\w\\-\\.\\/~]+)';
65+
// the previous group cannot be followed by an opening parenthesis
66+
partialsWithStyleModifiersStr += '(?!\\()';
67+
// a colon followed by one or more characters comprising any combination
68+
// of alphanumerics, hyphens, and pipes
69+
partialsWithStyleModifiersStr += '(\\:[\\w\\-\\|]+)';
70+
// an optional group of characters starting with >=0 whitespaces, followed by
71+
// an opening parenthesis, followed by any number of characters that are not
72+
// closing parentheses, followed by a closing parenthesis
73+
partialsWithStyleModifiersStr += '(\\s*\\([^\\)]*\\))?';
74+
// look for >=0 whitespaces, followed by closing mustache tag
75+
partialsWithStyleModifiersStr += '\\s*}}';
76+
var partialsWithStyleModifiersRE = new RegExp(partialsWithStyleModifiersStr, 'g');
77+
78+
// look for an opening mustache include tag, followed by >=0 whitespaces
79+
var partialsWithPatternParametersStr = '{{>\\s*';
80+
// one or more characters comprising any combination of alphanumerics,
81+
// hyphens, periods, slashses, and tildes
82+
partialsWithPatternParametersStr += '([\\w\\-\\.\\/~]+)';
83+
// an optional group comprising a colon followed by one or more characters
84+
// comprising any combination of alphanumerics,
85+
// hyphens, and pipes
86+
partialsWithPatternParametersStr += '(\\:[\\w\\-\\|]+)?';
87+
// a group of characters starting with >=0 whitespaces, followed by an opening
88+
// parenthesis, followed by any number of characters that are not closing
89+
// parentheses, followed by a closing parenthesis
90+
partialsWithPatternParametersStr += '(\\s*\\([^\\)]*\\))';
91+
// look for >=0 whitespaces, followed by closing mustache tag
92+
partialsWithPatternParametersStr += '\\s*}}';
93+
var partialsWithPatternParametersRE = new RegExp(partialsWithPatternParametersStr, 'g');
94+
95+
// look for an opening mustache loop tag, followed by >=0 whitespaces
96+
var listItemsStr = '{{#\\s*';
97+
// look for the string 'listItems.' or 'listitems.'
98+
listItemsStr += '(list(I|i)tems\\.)';
99+
// look for a number 1 - 20, spelled out
100+
listItemsStr += '(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)';
101+
// look for >=0 whitespaces, followed by closing mustache tag
102+
listItemsStr += '\\s*}}';
103+
var listItemsRE = new RegExp(listItemsStr, 'g');
104+
105+
// look for an opening mustache loop tag, followed by >=0 whitespaces
106+
var partialKeyStr = '{{>\\s*';
107+
// one or more characters comprising any combination of alphanumerics,
108+
// hyphens, periods, slashses, and tildes
109+
partialKeyStr += '([\\w\\-\\.\\/~]+)';
110+
// an optional group of characters starting with a colon, followed by >0
111+
// alphanumerics, hyphens, or pipes
112+
partialKeyStr += '(\\:[\\w\\-|]+)?';
113+
// an optional group of characters starting with a colon, followed by >0
114+
// alphanumerics or hyphens
115+
partialKeyStr += '(\\:[\\w\\-]+)?';
116+
// an optional group of characters starting with >=0 whitespaces, followed by
117+
// an opening parenthesis, followed by any number of characters that are not
118+
// closing parentheses, followed by a closing parenthesis
119+
partialKeyStr += '(\\s*\\([^\\)]*\\))?';
120+
// look for >=0 whitespaces, followed by closing mustache tag
121+
partialKeyStr += '\\s*}}';
122+
partialKeyStr += '\\s*}}';
123+
var partialKeyRE = new RegExp(partialKeyStr, 'g');
124+
125+
var utilMustache = {
126+
partialsRE: partialsRE,
127+
partialsWithStyleModifiersRE: partialsWithStyleModifiersRE,
128+
partialsWithPatternParametersRE: partialsWithPatternParametersRE,
129+
listItemsRE: listItemsRE,
130+
partialKeyRE: partialKeyRE
131+
};
132+
133+
module.exports = utilMustache;
134+
})();

0 commit comments

Comments
 (0)