<commit_before>'use strict';


/*global nodeca, _*/


// nodeca
var HashTree = require('nlib').Support.HashTree;


// internals
var helpers = require('./renderer/helpers');


////////////////////////////////////////////////////////////////////////////////


// Filter middleware that renders view and required layout and sets
//
// - `response.headers` with approprite headers
// - `response.body` with rendered (and compressed if allowed) html
//
nodeca.filters.after('', { weight: 900 }, function renderer(params, callback) {
  var http    = this.origin.http,
      headers = this.response.headers,
      locale  = this.session.locale,
      theme   = this.session.theme,
      layout  = this.response.layout,
      viewsTree, locals;

  if (!http) {
    // skip non-http requests
    callback();
    return;
  }

  //
  // Prepare variables
  //

  if (!nodeca.runtime.views[locale]) {
    callback(new Error("No localized views for " + locale));
    return;
  }

  if (!nodeca.runtime.views[locale][theme]) {
    callback(new Error("Theme " + theme + " not found"));
    return;
  }

  viewsTree = nodeca.runtime.views[locale][theme];

  if (!nodeca.shared.common.getByPath(viewsTree, this.response.view)) {
    callback(new Error("View " + this.response.view + " not found"));
    return;
  }

  //
  // 304 Not Modified
  //

  if (headers['ETag'] && headers['ETag'] === http.req.headers['if-none-match']) {
    // The one who sets `ETag` header must set also (by it's own):
    //  - `Last-Modified`
    //  - `Cache-Control`
    this.response.statusCode = 304;
    callback();
    return;
  }

  //
  // Set Content-Type and charset
  //

  headers['Content-Type'] = 'text/html; charset=UTF-8';

  //
  // HEAD requested - no need for real rendering
  //

  if ('HEAD' === http.req.method) {
    callback();
    return;
  }

  this.extras.puncher.start('Rendering');

  try {
    locals = _.extend(this.response.data, helpers, this.helpers);
    this.response.body = nodeca.shared.common.render(viewsTree, this.response.view, layout, locals);
  } catch (err) {
    this.extras.puncher.stop();
    callback(err);
    return;
  }

  this.extras.puncher.stop();
  callback();
});<commit_msg>Sync with fontello<commit_after>'use strict';


/*global nodeca, _*/


// nodeca
var HashTree = require('nlib').Support.HashTree;


// internals
var helpers = require('./renderer/helpers');


////////////////////////////////////////////////////////////////////////////////


// Filter middleware that renders view and required layout and sets
//
// - `response.headers` with approprite headers
// - `response.body` with rendered (and compressed if allowed) html
//
nodeca.filters.after('', { weight: 900 }, function renderer(params, callback) {
  var http    = this.origin.http,
      headers = this.response.headers,
      locale  = this.session.locale,
      theme   = this.session.theme,
      layout  = this.response.layout,
      viewsTree, locals;

  if (!http) {
    // skip non-http requests
    callback();
    return;
  }

  //
  // Prepare variables
  //

  if (!nodeca.runtime.views[locale]) {
    callback(new Error("No localized views for " + locale));
    return;
  }

  if (!nodeca.runtime.views[locale][theme]) {
    callback(new Error("Theme " + theme + " not found"));
    return;
  }

  viewsTree = nodeca.runtime.views[locale][theme];

  if (!nodeca.shared.common.getByPath(viewsTree, this.response.view)) {
    callback(new Error("View " + this.response.view + " not found"));
    return;
  }

  //
  // Set Content-Type and charset
  //

  headers['Content-Type'] = 'text/html; charset=UTF-8';

  //
  // 304 Not Modified
  //

  if (headers['ETag'] && headers['ETag'] === http.req.headers['if-none-match']) {
    // The one who sets `ETag` header must set also (by it's own):
    //  - `Last-Modified`
    //  - `Cache-Control`
    this.response.statusCode = 304;
    callback();
    return;
  }

  //
  // HEAD requested - no need for real rendering
  //

  if ('HEAD' === http.req.method) {
    callback();
    return;
  }

  this.extras.puncher.start('Rendering');

  try {
    locals = _.extend(this.response.data, helpers, this.helpers);
    this.response.body = nodeca.shared.common.render(viewsTree, this.response.view, layout, locals);
  } catch (err) {
    this.extras.puncher.stop();
    callback(err);
    return;
  }

  this.extras.puncher.stop();
  callback();
});
{'fraction_non_alphanumeric': 0.19240391175801683, 'fraction_numerical': 0.004548555833522857, 'mean_word_length': 2.4066615027110765, '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': '5774169', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1357, 'n_words': 306}