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

Commit 5d4c18f

Browse files
committed
Merge pull request #256 from e2tha-e/regex-cleanup-mustache
Regex and partial finding update on pattern-engines branch
2 parents dc43f4a + c1d034a commit 5d4c18f

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, followed by
38+
// >0 alphanumerics, followed by >=0 whitespaces, followed by a colon,
39+
// followed by >=0 whitespaces
40+
partialsStr += '(\\(\\s*\\w+\\s*\\:\\s*';
41+
// followed by an interior group
42+
// comprising a single quote, followed by an interior group comprising
43+
// >=0 characters that are not single quotes or backslashes
44+
// or >=0 character pairs comprising a backlash, followed by any character.
45+
// look for a single quote to terminate this pattern
46+
partialsStr += '(\'([^\'\\\\]|\\\\.)*\'';
47+
// if the pattern wrapped in single quotes is not found, look for one wrapped
48+
// in double quotes
49+
// look for a double quote, followed by an interior group comprising
50+
// >=0 characters that are not double quotes or backslashes
51+
// or >=0 character pairs comprising a backlash, followed by any character.
52+
// look for a double quote to terminate this pattern
53+
partialsStr += '|"([^"\\\\]|\\\\.)*")';
54+
// look for a closing parenthesis
55+
partialsStr += '\\))?';
56+
// end 3rd exterior group
57+
// look for >=0 whitespaces, followed by closing mustache tag
58+
partialsStr += '\\s*}}';
59+
var partialsRE = new RegExp(partialsStr, 'g');
60+
61+
// look for an opening mustache include tag, followed by >=0 whitespaces
62+
var partialsWithStyleModifiersStr = '{{>\\s*';
63+
// one or more characters comprising any combination of alphanumerics,
64+
// hyphens, periods, slashses, and tildes
65+
partialsWithStyleModifiersStr += '([\\w\\-\\.\\/~]+)';
66+
// the previous group cannot be followed by an opening parenthesis
67+
partialsWithStyleModifiersStr += '(?!\\()';
68+
// a colon followed by one or more characters comprising any combination
69+
// of alphanumerics, hyphens, and pipes
70+
partialsWithStyleModifiersStr += '(\\:[\\w\\-\\|]+)';
71+
// an optional group of characters starting with >=0 whitespaces, followed by
72+
// an opening parenthesis, followed by any number of characters that are not
73+
// closing parentheses, followed by a closing parenthesis
74+
partialsWithStyleModifiersStr += '(\\s*\\([^\\)]*\\))?';
75+
// look for >=0 whitespaces, followed by closing mustache tag
76+
partialsWithStyleModifiersStr += '\\s*}}';
77+
var partialsWithStyleModifiersRE = new RegExp(partialsWithStyleModifiersStr, 'g');
78+
79+
// look for an opening mustache include tag, followed by >=0 whitespaces
80+
var partialsWithPatternParametersStr = '{{>\\s*';
81+
// one or more characters comprising any combination of alphanumerics,
82+
// hyphens, periods, slashses, and tildes
83+
partialsWithPatternParametersStr += '([\\w\\-\\.\\/~]+)';
84+
// an optional group comprising a colon followed by one or more characters
85+
// comprising any combination of alphanumerics,
86+
// hyphens, and pipes
87+
partialsWithPatternParametersStr += '(\\:[\\w\\-\\|]+)?';
88+
// a group of characters starting with >=0 whitespaces, followed by an opening
89+
// parenthesis, followed by any number of characters that are not closing
90+
// parentheses, followed by a closing parenthesis
91+
partialsWithPatternParametersStr += '(\\s*\\([^\\)]*\\))';
92+
// look for >=0 whitespaces, followed by closing mustache tag
93+
partialsWithPatternParametersStr += '\\s*}}';
94+
var partialsWithPatternParametersRE = new RegExp(partialsWithPatternParametersStr, 'g');
95+
96+
// look for an opening mustache loop tag, followed by >=0 whitespaces
97+
var listItemsStr = '{{#\\s*';
98+
// look for the string 'listItems.' or 'listitems.'
99+
listItemsStr += '(list(I|i)tems\\.)';
100+
// look for a number 1 - 20, spelled out
101+
listItemsStr += '(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)';
102+
// look for >=0 whitespaces, followed by closing mustache tag
103+
listItemsStr += '\\s*}}';
104+
var listItemsRE = new RegExp(listItemsStr, 'g');
105+
106+
// look for an opening mustache loop tag, followed by >=0 whitespaces
107+
var partialKeyStr = '{{>\\s*';
108+
// one or more characters comprising any combination of alphanumerics,
109+
// hyphens, periods, slashses, and tildes
110+
partialKeyStr += '([\\w\\-\\.\\/~]+)';
111+
// an optional group of characters starting with a colon, followed by >0
112+
// alphanumerics, hyphens, or pipes
113+
partialKeyStr += '(\\:[\\w\\-|]+)?';
114+
// an optional group of characters starting with a colon, followed by >0
115+
// alphanumerics or hyphens
116+
partialKeyStr += '(\\:[\\w\\-]+)?';
117+
// an optional group of characters starting with >=0 whitespaces, followed by
118+
// an opening parenthesis, followed by any number of characters that are not
119+
// closing parentheses, followed by a closing parenthesis
120+
partialKeyStr += '(\\s*\\([^\\)]*\\))?';
121+
// look for >=0 whitespaces, followed by closing mustache tag
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)