<commit_before>/**
 * Handlers
 */

var utils = require('./utils')
  , Post = require('./data').Post
  , path = require('path');

var config = module.parent.config;

/**
 * List
 */

var list = function(req, res, list) {
  if (!res.local('back')) {
    res.local('back', path.resolve(req.pathname, '..'));
  }
  res.local('list', list.map(function(item) {
    return {
      href: '/' + item.id,
      text: item.title,
      datetime: new Date(item.timestamp).toISOString(),
      time: utils.prettyTime(item.timestamp)
    };
  }));
  res.render('browse.html');
};

/**
 * Search Posts
 */

list.search = function(req, res, next) {
  var search = req.query.search;

  if (!search) {
    return next();
  }

  if (res.cached(Post.updated)) return;

  if (!search.trim()) {
    return res.error(404,
      'Please enter a search term.');
  }

  if (search === '/') {
    return res.redirect('/admin');
  }

  res.local('title', 'Search for ' + search);

  Post.search(search, function(err, posts) {
    if (err) {
      return res.error(404,
        'No articles found.');
    }
    list(req, res, posts);
  });
};

exports.search = list.search;

/**
 * Browse Posts by Year
 */

list.year = function(req, res, next) {
  if (res.cached(Post.updated)) return;

  res.local('rel', 'browse');

  var year = req.path[1];

  if (!year) {
    return Post.getLast(function(err, post) {
      year = (new Date(post.timestamp)).getFullYear();
      done();
    });
  } else {
    if (!/^\d{4}$/.test(year)) {
      return res.error(404);
    }

    year = +year;

    return done();
  }<commit_msg>handle error<commit_after>/**
 * Handlers
 */

var utils = require('./utils')
  , Post = require('./data').Post
  , path = require('path');

var config = module.parent.config;

/**
 * List
 */

var list = function(req, res, list) {
  if (!res.local('back')) {
    res.local('back', path.resolve(req.pathname, '..'));
  }
  res.local('list', list.map(function(item) {
    return {
      href: '/' + item.id,
      text: item.title,
      datetime: new Date(item.timestamp).toISOString(),
      time: utils.prettyTime(item.timestamp)
    };
  }));
  res.render('browse.html');
};

/**
 * Search Posts
 */

list.search = function(req, res, next) {
  var search = req.query.search;

  if (!search) {
    return next();
  }

  if (res.cached(Post.updated)) return;

  if (!search.trim()) {
    return res.error(404,
      'Please enter a search term.');
  }

  if (search === '/') {
    return res.redirect('/admin');
  }

  res.local('title', 'Search for ' + search);

  Post.search(search, function(err, posts) {
    if (err) {
      return res.error(404,
        'No articles found.');
    }
    list(req, res, posts);
  });
};

exports.search = list.search;

/**
 * Browse Posts by Year
 */

list.year = function(req, res, next) {
  if (res.cached(Post.updated)) return;

  res.local('rel', 'browse');

  var year = req.path[1];

  if (!year) {
    return Post.getLast(function(err, post) {
      if (err) return next(err);
      year = (new Date(post.timestamp)).getFullYear();
      done();
    });
  } else {
    if (!/^\d{4}$/.test(year)) {
      return res.error(404);
    }

    year = +year;

    return done();
  }
{'fraction_non_alphanumeric': 0.20031152647975078, 'fraction_numerical': 0.006853582554517134, 'mean_word_length': 2.624153498871332, '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': '3251313', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1121, 'n_words': 239}