Example #1
0
File: ejs.js Project: oldcookie/ejs
var Parser = exports.Parser = function(options) {
    this.options = options || {};
    this.delimiter = {
        open    : XRegExp.escape(options.open || exports.open || '<%')
      , close   : XRegExp.escape(options.close || exports.close || '%>')
    };
    this.keyword = {
        block   : 'block'
      , end     : 'end'
      , extends  : 'extends'
      , include : 'include'
    }
    this.viewsDir = options.viewsDir? options.viewsDir : '';
}
Example #2
0
    rv = replaceRecursive(rv, type, 'foreach', function(startMatches, endMatches, include, recurse) {
      var variable = (startMatches[1] || '').trim();
      var forParams = variable.split(' ');
      if (forParams.length === 3) {
        var contextVar = forParams[2];
        var arrString = context[contextVar];
        var eachArr;
        if (arrString.match(/\{(.*)\}/)) {
          eachArr = JSON.parse(arrString);
        } else if (arrString.match(/\[(.*)\]/)) {
          eachArr = arrString.slice(1, -1);
          eachArr = eachArr.split(',');
          eachArr = eachArr.map(function(arrEntry){
            return arrEntry.replace(/\s*(['"])(.*)\1\s*/, '$2');
          });
        } else {
          eachArr = arrString.split(',');
        }

        var replaceToken = new RegExp(XRegExp.escape(forParams[0]), 'g');
        var recursedInclude = recurse(include);

        return Object.keys(eachArr).reduce(function(stringBuilder, arrKey){
          var arrEntry = eachArr[arrKey];
          return stringBuilder + recursedInclude.replace(replaceToken, arrEntry);
        }, '');
      } else {
        return '';
      }
    });
 packageUtil.readPackageJSON([], 'whatevs', function (err, data) {
   assert(err)
   refute(data)
   assert(err instanceof JSONParseError)
   assert(err.cause)
   assert.match(err.message, /Unexpected token/)
   // includes reference to filename:
   assert.match(
       err.message
     , new RegExp(xregexp.XRegExp.escape(path.join('node_modules', 'whatevs', 'package.json')))
   )
   done()
 })
Example #4
0
Template.prototype.parseChangelog = function(changelog) {
    /*jshint boss:true */
    var tokenized, regexp,
        names = {}, idx = 1,
        matches,
        versions = [], version;

    // This makes a RegExp out of the template, and uses that to match against
    // the changelog.  The generated RegExp is only approximately able to parse
    // the formatted string (changelog), but it's good enough to pull out the
    // things we care about ({{version}} and {{changeid}}).

    tokenized = this.content.replace(/\{\{#([^}]+)\}\}[\s\S]*?\{\{\/\1\}\}/, ':CRANK:$1:');
    tokenized = tokenized.replace(/\{\{^([^}]+)\}\}[\s\S]*?\{\{\/\1\}\}/, ':CRANK:no-$1:');
    tokenized = tokenized.replace(/\{?\{\{([^}]*)\}\}\}?/g, ':CRANK:$1:');

    regexp = libxregexp.escape(tokenized);
    while (matches = regexp.match(/:CRANK:([a-z]+):/)) {
        // FUTURE:  This doesn't capture all of {{changes}}.
        // That's hard to do since it needs to only go up to the end of the
        // version, and not match into the next version.  (It's doable, but
        // tricky since the template could be user-defined, and thus we can't
        // make any assumptions about its structure.)
        regexp = regexp.replace(matches[0], '([\\s\\S]*?)');
        names[idx] = matches[1];
        idx++;
    }
    regexp = new RegExp(regexp, 'g');

    while (matches = regexp.exec(changelog)) {
        version = {};
        for (idx in names) {
            if (names.hasOwnProperty(idx)) {
                version[names[idx]] = matches[idx];
            }
        }
        versions.push(version);
    }
    return versions;
};