<commit_before>'use strict';

var utils = require('../utils');

module.exports = function(proto) {
  var define = require('define-property');

  /**
   * Add `Router` and `Route` to the prototype
   */

  proto.Router = utils.router.Router;
  proto.Route = utils.router.Route;

  /**
   * Lazily initalize `router`, to allow options to
   * be passed in after init.
   */

  proto.lazyRouter = function(methods) {
    if (typeof this.router === 'undefined') {
      this.define('router', new this.Router({
        methods: utils.methods
      }));
    }
    if (typeof methods !== 'undefined') {
      this.router.method(methods);
    }
  };

  /**
   * Handle a middleware `method` for `view`.
   *
   * ```js
   * app.handle('customMethod', view, callback);
   * ```
   * @name .handle
   * @param {String} `method` Name of the router method to handle. See [router methods](./docs/router.md)
   * @param {Object} `view` View object
   * @param {Function} `callback` Callback function
   * @return {Object}
   * @api public
   */

  proto.handle = function(method, view, locals, next) {
    this.debug.routes('handling method "%s", for view "%s"', method, view.key);

    if (typeof locals === 'function') {
      next = locals;
      locals = {};
    }

    this.lazyRouter();
    if (!view.options.handled) {
      view.options.handled = [];
    }

    var done = this.handleError(method, view, next);
    view.options.method = method;
    view.options.handled.push(method);
    this.emit(method, view);

    // if inside a collection or the collection is not specified on view.options
    // just handle the route and return
    if (!(this.isApp && view.options.collection)) {
      this.router.handle(view, done);
      return;
    }

    // handle the collection routes first, then handle the app routes
    var collection = this[view.options.collection];

    collection.handle(method, view, function(err) {
      if (err) return done(err);
      this.router.handle(view, done);
    }.bind(this));
  };

  /**
   * Run the given middleware handler only if the view has not
   * already been handled by the method.
   *
   * @name .handleOnce
   * @param  {Object} `method`
   * @param  {Object} `view`
   * @param  {Object} `locals`
   */

  proto.handleOnce = function(method, view, cb) {<commit_msg>handle app routes before handling collection routes this is mainly to ensure global handlers like parsing frontmatter are done before more specific collection middleware is handled on a collection<commit_after>'use strict';

var utils = require('../utils');

module.exports = function(proto) {
  var define = require('define-property');

  /**
   * Add `Router` and `Route` to the prototype
   */

  proto.Router = utils.router.Router;
  proto.Route = utils.router.Route;

  /**
   * Lazily initalize `router`, to allow options to
   * be passed in after init.
   */

  proto.lazyRouter = function(methods) {
    if (typeof this.router === 'undefined') {
      this.define('router', new this.Router({
        methods: utils.methods
      }));
    }
    if (typeof methods !== 'undefined') {
      this.router.method(methods);
    }
  };

  /**
   * Handle a middleware `method` for `view`.
   *
   * ```js
   * app.handle('customMethod', view, callback);
   * ```
   * @name .handle
   * @param {String} `method` Name of the router method to handle. See [router methods](./docs/router.md)
   * @param {Object} `view` View object
   * @param {Function} `callback` Callback function
   * @return {Object}
   * @api public
   */

  proto.handle = function(method, view, locals, next) {
    this.debug.routes('handling method "%s", for view "%s"', method, view.key);

    if (typeof locals === 'function') {
      next = locals;
      locals = {};
    }

    this.lazyRouter();
    if (!view.options.handled) {
      view.options.handled = [];
    }

    var done = this.handleError(method, view, next);
    view.options.method = method;
    view.options.handled.push(method);
    this.emit(method, view);

    // if inside a collection or the collection is not specified on view.options
    // just handle the route and return
    if (!(this.isApp && view.options.collection)) {
      this.router.handle(view, done);
      return;
    }

    // handle the app routes first, then handle the collection routes
    var collection = this[view.options.collection];

    this.router.handle(view, function(err) {;
      if (err) return done(err);
      collection.handle(method, view, done);
    });
  };

  /**
   * Run the given middleware handler only if the view has not
   * already been handled by the method.
   *
   * @name .handleOnce
   * @param  {Object} `method`
   * @param  {Object} `view`
   * @param  {Object} `locals`
   */

  proto.handleOnce = function(method, view, cb) {
{'fraction_non_alphanumeric': 0.1428273125913552, 'fraction_numerical': 0.0, 'mean_word_length': 2.7955625990491284, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5409609', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1449, 'n_words': 429}