Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 29 additions & 38 deletions lib/util_mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,25 @@

// the term "alphanumeric" includes underscores.

// todo: document this exact regex long form.
var partialsRE = new RegExp(/{{>\s*?([\w\-\.\/~]+)(?:\:[A-Za-z0-9-_|]+)?(?:(?:| )\(.*)?(?:\s*)?}}/g);
// look for an opening mustache include tag, followed by >=0 whitespaces
var partialsStr = '{{>\\s*';

// one or more characters comprising any combination of alphanumerics,
// hyphens, periods, slashses, and tildes
partialsStr += '([\\w\\-\\.\\/~]+)';

// an optional group comprising a colon followed by one or more characters
// comprising any combination of alphanumerics, hyphens, and pipes
partialsStr += '(\\:[\\w\\-\\|]+)?';

// an optional group of characters starting with >=0 whitespaces, followed by an
// opening parenthesis, followed by a lazy match of non-whitespace or whitespace
// characters (to include newlines), followed by a closing parenthesis
partialsStr += '(\\s*\\([\\S\\s]*?\\))?';

// look for >=0 whitespaces, followed by closing mustache tag
partialsStr += '\\s*}}';
var partialsRE = new RegExp(partialsStr, 'g');

// look for an opening mustache include tag, followed by >=0 whitespaces
var partialsWithStyleModifiersStr = '{{>\\s*';
Expand All @@ -29,10 +46,10 @@ partialsWithStyleModifiersStr += '(?!\\()';
// of alphanumerics, hyphens, and pipes
partialsWithStyleModifiersStr += '(\\:[\\w\\-\\|]+)';

// an optional group of characters starting with >=0 whitespaces, followed by
// an opening parenthesis, followed by any number of characters that are not
// closing parentheses, followed by a closing parenthesis
partialsWithStyleModifiersStr += '(\\s*\\([^\\)]*\\))?';
// an optional group of characters starting with >=0 whitespaces, followed by an
// opening parenthesis, followed by a lazy match of non-whitespace or whitespace
// characters (to include newlines), followed by a closing parenthesis
partialsWithStyleModifiersStr += '(\\s*\\([\\S\\s]*?\\))?';

// look for >=0 whitespaces, followed by closing mustache tag
partialsWithStyleModifiersStr += '\\s*}}';
Expand All @@ -46,14 +63,13 @@ var partialsWithPatternParametersStr = '{{>\\s*';
partialsWithPatternParametersStr += '([\\w\\-\\.\\/~]+)';

// an optional group comprising a colon followed by one or more characters
// comprising any combination of alphanumerics,
// hyphens, and pipes
// comprising any combination of alphanumerics, hyphens, and pipes
partialsWithPatternParametersStr += '(\\:[\\w\\-\\|]+)?';

// a group of characters starting with >=0 whitespaces, followed by an opening
// parenthesis, followed by any number of characters that are not closing
// parentheses, followed by a closing parenthesis
partialsWithPatternParametersStr += '(\\s*\\([^\\)]*\\))';
// a group of characters starting with >=0 whitespaces, followed by an
// opening parenthesis, followed by a lazy match of non-whitespace or whitespace
// characters (to include newlines), followed by a closing parenthesis
partialsWithPatternParametersStr += '(\\s*\\([\\S\\s)]*?\\))';

// look for >=0 whitespaces, followed by closing mustache tag
partialsWithPatternParametersStr += '\\s*}}';
Expand All @@ -72,36 +88,11 @@ listItemsStr += '(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve
listItemsStr += '\\s*}}';
var listItemsRE = new RegExp(listItemsStr, 'g');

// look for an opening mustache loop tag, followed by >=0 whitespaces
var partialKeyStr = '{{>\\s*';

// one or more characters comprising any combination of alphanumerics,
// hyphens, periods, slashses, and tildes
partialKeyStr += '([\\w\\-\\.\\/~]+)';

// an optional group of characters starting with a colon, followed by >0
// alphanumerics, hyphens, or pipes
partialKeyStr += '(\\:[\\w\\-|]+)?';

// an optional group of characters starting with a colon, followed by >0
// alphanumerics or hyphens
partialKeyStr += '(\\:[\\w\\-]+)?';

// an optional group of characters starting with >=0 whitespaces, followed by
// an opening parenthesis, followed by any number of characters that are not
// closing parentheses, followed by a closing parenthesis
partialKeyStr += '(\\s*\\([^\\)]*\\))?';

// look for >=0 whitespaces, followed by closing mustache tag
partialKeyStr += '\\s*}}';
var partialKeyRE = new RegExp(partialKeyStr, 'g');

var utilMustache = {
partialsRE: partialsRE,
partialsWithStyleModifiersRE: partialsWithStyleModifiersRE,
partialsWithPatternParametersRE: partialsWithPatternParametersRE,
listItemsRE: listItemsRE,
partialKeyRE: partialKeyRE
listItemsRE: listItemsRE
};

module.exports = utilMustache;