Пример #1
0
module.exports = function(content, file, conf) {
  var assign = require('mixin-deep');
  var defConf = require('./package.json').defconf;
  var globals = defConf.globals;


  if (conf.globals) {
  	 globals = globals.concat(conf.globals);
  	 conf.globals = arrUniq(globals);
  	 delete defConf.globals;
  }

  if (conf.rules) {
  	assign(defConf.rules, conf.rules);
  	delete conf.rules;
  }
  var lastConf = assign(defConf, conf);

  if (eslintIgnore(file, lastConf)) {
  	return;
  }

  var CLIEngine = require("eslint").CLIEngine;
  var cli = new CLIEngine(lastConf);
  var report = cli.executeOnText(content);

  if (report.errorCount || report.warningCount) {
  	var msg = formatter(report.results);
  	fis.log.info('%s  %s \n%s', file.id, 'fail!'.red, msg);
  	return;
  }

  fis.log.info(file.id, ' pass!'.green);
};
Пример #2
0
  buildContext(file, locals) {
    const opts = Object.assign({}, this.options);
    const fileData = merge({}, file.data, file.data.permalink);
    const context = merge({}, this.parse(file), this.data, locals, fileData);
    const ctx = merge({}, {app: this}, {options: opts});
    const data = {};

    for (const key of Object.keys(context)) {
      const val = context[key];
      if (typeof val === 'function') {
        helpers[key] = val;
      } else {
        data[key] = val;
      }
    }

    // add special properties to context
    ctx.app.format = this.format;
    ctx.app.parse = this.parse;
    ctx.context = data;
    ctx.file = file;

    for (const name of Object.keys(this.presets)) {
      if (!this.helpers[name]) {
        this.helper(name, () => this.format(this.presets[name], file));
      }
    }

    // bind the context to helpers
    let helpers = deepBind(Object.assign({}, this.helpers), ctx);

    // call user-defined context functions
    for (const fn of this.fns) {
      fn.call(this, ctx.file, ctx.context);
    }

    // call "file" helper
    if (typeof helpers.file === 'function') {
      helpers.file(file, data, locals);
      delete helpers.file;
    }

    data.file = file;

    return {
      options: opts,
      helpers: helpers,
      data: data
    };
  }
Пример #3
0
function askWhen(app, name, options, cb) {
  debug('initializing from <%s>', __filename);

  if (typeof options === 'function') {
    cb = options;
    options = {};
  }

  if (typeof cb !== 'function') {
    throw new TypeError('expected a callback function');
  }

  var opts = merge({save: false}, app.base.options, app.options, options);
  var skip = get(opts, ['question.skip', name].join('.'));
  var data = merge({}, app.base.cache.data, opts.data, app.base.cache.answers);
  var val = get(data, name) || get(opts, name);

  var answers = {};
  answers[name] = val;

  var isAnswered = isAnswer(val);
  opts.force = isAnswered !== true;

  if (isSkipped(app, names, skip, isAnswered)) {
    opts.askWhen = 'not-answered';
    opts.force = false;
  }

  // conditionally prompt the user
  switch (opts.askWhen) {
    case 'never':
      cb(null, answers);
      return;

    case 'not-answered':
      if (isAnswered) {
        cb(null, answers);
        return;
      }
      break;

    case 'always':
    default: {
      break;
    }
  }
  app.ask(name, opts, cb);
};
Пример #4
0
  function format(file) {
    if (typeOf(file) !== 'object') {
      throw new TypeError('expected file to be a string or object');
    }

    if (File.isVinyl(file)) {
      return file.clone({contents: false});
    }

    let newFile = {};
    if (file.path) {
      newFile = exports.toFile(file.path, options);

    } else {
      const filepath = path.format(file) || file.basename;
      if (filepath) {
        newFile = exports.toFile(filepath, options);
      }
    }

    for (const key in file) {
      if (file.hasOwnProperty(key) && typeof file[key] === 'string') {
        newFile[key] = file[key];
      }
    }

    newFile.data = merge({}, newFile.data, file.data);
    return newFile;
  }
Пример #5
0
  return function (name, context) {
    if (typeof name !== 'string') return '';
    var partial = partials[name];

    var fn = engine.compile(partial);
    return fn(merge(this, context));
  };
Пример #6
0
function render(input, options = {}, locals = {}) {
  let context = merge({ bugs: { url: '' } }, locals);

  helpers.year = require('year');    // Get the current year
  helpers.month = require('month');  // Get the current month
  helpers.day = require('month-day');// Get the current month day
  // Get the current date: e.g. `January 1, 2017`
  helpers.today = function() {
    return helpers.month('MMMM') + ' ' + helpers.day('DD') + ', ' + helpers.year();
  };

  helpers.include = function(filename, options) {
    let cwd = context.cwd;
    if (filename.slice(0, 2) !== './' && !fs.existsSync(path.join(cwd, filename))) {
      cwd = path.join(__dirname, 'templates/includes');
      if (!/\.md$/.test(filename)) {
        filename += '.md';
      }
    }

    let fp = path.resolve(cwd, filename);
    return render(read(fp).contents, options, context);
  };

  function format(str, options) {
    return new Remarkable().use(prettify).render(str, options);
  }

  const str = input.split('<%%').join('__OPEN__');
  const settings = Object.assign({}, options.settings);
  settings.imports = merge({}, settings.imports, options.helpers, helpers);
  const engine = new Engine(settings);
  context = merge({ username: '' }, context, options);

  if (context.author && typeof context.author === 'string') {
    context.author = parseAuthor(context.author);
    const match = /github\.com\/(.*)/.exec(context.author.url);
    if (!context.username && match) {
      context.username = match[1];
    }
  }

  return format(engine.render(str, context))
    .split('__OPEN__')
    .join('<%');
}
Пример #7
0
 .preRender(/\.md/, function(file, next) {
   var category = file.data.category;
   if (!category) return next();
   file.data = merge({
     layout: category === 'recipes' ? 'recipe' : file.data.layout
   }, file.data);
   next();
 })
Пример #8
0
engine.renderSync = function(str, locals) {
  try {
    var ctx = merge({}, engine.options, locals);
    engine.lazyConfigure(ctx);
    return engine.env.renderString(str, ctx);
  } catch (err) {
    throw err;
  }
};
Пример #9
0
function Parser(options) {
  this.options = merge({}, {
    headers: {
      included: false,
      downcase: true,
      upcase: true
    },
    delimiter: 'tab',
    decimalSign: 'comma'
  }, options);
}
Пример #10
0
engine.compileSync = function(str, locals) {
  try {
    var ctx = merge({path: 'string'}, engine.options, locals);
    engine.lazyConfigure(ctx);
    var compiled = engine.nunjucks.compile(str, engine.env, ctx.base);
    return function(data) {
      return compiled.render(merge({}, ctx, data));
    };
  } catch (err) {
    throw err;
  }
};
Пример #11
0
      targetSeries: function(target, options, next) {
        if (typeof options === 'function') {
          next = options;
          options = {};
        }
        if (typeof next !== 'function') {
          return this.targetStream(target, options);
        }

        var opts = merge({}, this.options, options);
        return this.each(target, opts, next);
      },
Пример #12
0
  function helper(context, options) {
    if (typeof context === 'string') {
      var opts = merge({}, config, options);
      var md = new Remarkable(opts);
      return md.render(context);
    }

    if (isObject(context) && typeof context.fn === 'function') {
      options = context;
      context = {};
    }

    options = merge({ html: true, breaks: true }, config, options);
    options = merge({}, options, options.markdown, options.hash);
    if (options.hasOwnProperty('lang')) {
      options.langPrefix = options.lang;
    }

    var md = new Remarkable(options);
    var ctx = merge({}, options, this.context, context);
    return md.render(options.fn(ctx));
  }
Пример #13
0
engine.renderFile = function(filepath, locals, cb) {
  if (typeof locals === 'function') {
    cb = locals;
    locals = {};
  }
  try {
    var ctx = merge({}, engine.options, locals);
    engine.lazyConfigure(ctx);
    engine.env.render(filepath, ctx, cb);
  } catch (err) {
    cb(err);
  }
};
Пример #14
0
  engine.renderSync = function wrappedRenderSync(str, locals) {
    if (typeof str === 'function') {
      return str(locals);
    }

    if (typeof str !== 'string') {
      throw new TypeError('expected a string or compiled function');
    }

    var context = extend({}, locals);
    context.helpers = merge({}, this.helpers, context.helpers);
    return this.compile(str, context)(context);
  };
Пример #15
0
module.exports = function(options) {
  var opts = merge({}, defaults, options);
  if (opts.keepEmpty) {
    var keep = typeof opts.keepEmpty === 'string'
      ? opts.keepEmpty
      : [opts.keepEmpty];

    for (var i = 0; i < keep.length; i++) {
      opts.omitEmpty.splice(i, 1);
    }
  }

  return opts;
};
Пример #16
0
module.exports = function changelog(data, opts) {
  var filepath;
  if (typeof data === 'string') {
    filepath = data;
    data = readChangelog(data);
  }

  if (filepath && !data) {
    throw new Error(`stringify-changelog cannot read <${filepath}>`);
  }

  if (!data || typeof data !== 'object') {
    throw new TypeError('stringify-changelog expects an object or array');
  }

  var changes = [];

  if (Array.isArray(data)) {
    var len = data.length;
    while (len--) {
      var ele = data[len];
      var res = {};
      res.date = formatDate(ele.date, opts),
      res.version = ele.version;
      res.changes = ele.changes;
      changes.push(res);
    }
  } else {
    // Convert changelog object to an array. We want the
    // version key to convert to `version: "v0.1.0"`
    for (var version in data) {
      changes.push({
        date: formatDate(data[version].date, opts),
        version: version,
        changes: data[version].changes
      });
    }
  }

  opts = merge({}, opts, {
    columnSplitter: '   ',
    headingTransform: function (heading) {
      return formatHeading(heading, opts);
    },
    config: {changes: {maxWidth: 60, }}
  });

  // Prettify the changelog with columnify
  return columnify(changes, opts);
};
Пример #17
0
function mergeHelpers(engine, options) {
  if (!options || typeof options !== 'object') {
    throw new TypeError('expected an object');
  }

  var opts = extend({}, options);
  var helpers = merge({}, engine.helpers.cache, opts.helpers);
  var keys = Object.keys(helpers);
  for (var i = 0; i < keys.length; i++) {
    var key = keys[i];
    engine.asyncHelpers.set(key, helpers[key]);
  }

  opts.helpers = engine.asyncHelpers.get({wrap: opts.async});
  return opts;
}
Пример #18
0
  return function() {
    if (!thisArg.hasOwnProperty('options')) {
      thisArg.options = {};
    }

    if (typeof options.bindFn === 'function') {
      thisArg = options.bindFn(thisArg, key, this, options);
    }

    if (options.hasOwnProperty(key)) {
      var val = options[key];
      thisArg.options[key] = val;
      if (isObject(val)) {
        thisArg.options = merge({}, thisArg.options, val);
      }
    }
    return fn.apply(thisArg, arguments);
  }
Пример #19
0
exports.buildRegexGroup = function (re, options) {
  var opts = merge({
    escape: false,    // Escape delimiter regex
    noncapture: false // Build a non-capture group
  }, options);

  re = arrayify(re);
  var len = re.length;

  re = (len > 0) ? re.join('|') : re;
  re = (opts.escape === true)
    ? exports.escapeDelim(re)
    : re;

  if(opts.noncapture === true || len > 1) {
    re = '(?:' + re + ')';
  }
  return re;
};
Пример #20
0
engine.configure = function(options) {
  configured = true;
  engine.options = merge({}, defaults, options);
  var Loader = engine.options.loader;

  var base = engine.options.blocks || engine.options.base;
  if (typeof Loader === 'function') {
    engine.loader = new Loader(engine.options);
  } else if (typeof Loader === 'undefined') {
    engine.loader = new engine.nunjucks.FileSystemLoader(base);
  } else {
    engine.loader = Loader;
  }

  engine.env = engine.options.env || new engine.nunjucks.Environment(engine.loader);
  filters(engine);
  engine.__proto__ = engine.env;
  return engine;
};
Пример #21
0
function Renderer(options) {
  var defaults = {
    headers: {
      included: true,
      downcase: true,
      upcase: true
    },

    delimiter: 'tab',
    decimalSign: 'comma',
    outputDataType: 'json',
    columnDelimiter: "\t",
    rowDelimiter: '\n',

    inputHeader: {},
    outputHeader: {},
    dataSelect: {},

    outputText: '',

    newline: '\n',
    indent: '  ',

    commentLine: '//',
    commentLineEnd: '',
    tableName: 'converter',

    useUnderscores: true,
    includeWhiteSpace: true,
    useTabsForIndent: false
  };

  this.options = merge({}, defaults, options);
  if (this.options.includeWhiteSpace) {
    this.options.newline = '\n';
  } else {
    this.options.indent = '';
    this.options.newline = '';
  }
};
Пример #22
0
engine.renderString = function(str, locals, cb) {
  if (typeof locals === 'function') {
    cb = locals;
    locals = {};
  }

  if (typeof cb !== 'function') {
    return engine.renderSync.apply(engine, arguments);
  }

  try {
    if (typeof str === 'function') {
      var ctx = merge({}, engine.options, locals);
      engine.lazyConfigure(ctx);
      cb(null, str(ctx));
      return;
    }
    cb(null, engine.renderSync(str, locals));
  } catch (err) {
    cb(err);
  }
};
Пример #23
0
engine.render = function(str, locals, cb) {
  if (typeof locals === 'function') {
    cb = locals;
    locals = {};
  }

  if (typeof cb !== 'function') {
    return engine.renderSync.apply(engine, arguments);
  }

  try {
    var fn = str;
    var ctx = merge({}, engine.options, locals);
    engine.lazyConfigure(ctx);
    if (typeof str !== 'function') {
      fn = engine.compileSync(str, ctx);
    }
    cb(null, fn(ctx));
  } catch (err) {
    cb(err);
  }
};
Пример #24
0
  return function(app) {
    if (!isValid(app, 'assemble-nunjucks', ['app', 'collection'])) return;

    var opts = merge({}, this.options, config);
    var engine = opts.nunjucks || require('engine-nunjucks');
    engine.lazyConfigure(opts);
    app.engine('njk', engine);

    var asyncHelpers = app.asyncHelpers;
    var asyncHelper = app.asyncHelper;
    var helpers = app.helpers;
    var helper = app.helper;

    function redefine(name, fn) {
      return function() {
        if (typeof fn === 'undefined') {
          return engine[name].apply(engine, arguments);
        }
        this[name].apply(this, arguments);
        return fn.apply(this, arguments);
      };
    }

    app.define({
      helpers: redefine('addFilters', helpers),
      helper: redefine('addFilter', helper),

      asyncHelpers: redefine('asyncFilters', asyncHelpers),
      asyncHelper: redefine('asyncFilter', asyncHelper),

      addFilters: redefine('addFilters'),
      addFilter: redefine('addFilter'),

      asyncFilters: redefine('asyncFilters'),
      asyncFilter: redefine('asyncFilter')
    });
  };
Пример #25
0
    return function(locals, cb) {
      if (typeof locals === 'function') {
        cb = locals;
        locals = {};
      }

      // if compiled already, we can delete helpers and partials from
      // the `helpers` object, since were bound to the context and
      // passed to the engine at compile time
      if (typeof compiled === 'function') {
        str = compiled;
        helpers = {};
      }

      var data = {};
      if (typeof locals === 'string' || Array.isArray(locals)) {
        data = locals;

      } else if (options && typeof options.mergeFn === 'function') {
        data = options.mergeFn(helpers, locals);

      } else {
        data = merge({}, locals, helpers);
      }

      if (typeof cb !== 'function') {
        return renderSync.call(engine, str, data);
      }

      render.call(engine, str, data, function(err, str) {
        if (err) {
          cb(err);
          return;
        }
        engine.asyncHelpers.resolveIds(str, cb);
      });
    };
Пример #26
0
 return function(data) {
   return compiled.render(merge({}, ctx, data));
 };
Пример #27
0
app.onPermalink(/./, function(file, next) {
  file.data = merge({}, app.cache.data, file.data);
  next();
});
Пример #28
0
Engine.prototype.normalize = function () {
  var opts = extend({}, this.options);
  return normalize(opts).apply(null, arguments);
};
Пример #29
0
 Engine.prototype[plural] = function (key, value, locals, options) {
   var files = this.normalize(key, value, locals, options);
   extend(this.cache[plural], files);
   return this;
 };
Пример #30
0
app.onPermalink(/./, function(file, next) {
  debug('onPermalink for %s', file.path);
  permalinkCache[file.path] = true;
  file.data = merge({category: 'docs'}, app.cache.data, file.data);
  next();
});