{"size": 3429, "ext": "js", "max_stars_repo_path": "app.js", "max_stars_repo_name": "javascriptmy/website", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app.js", "max_issues_repo_name": "javascriptmy/website", "max_issues_repo_issues_event_min_datetime": "2015-04-02T15:11:39.000Z", "max_issues_repo_issues_event_max_datetime": "2015-06-03T09:22:36.000Z", "max_forks_repo_path": "app.js", "max_forks_repo_name": "javascriptmy/website", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 4, "max_forks_count": null, "avg_line_length": 28.106557377, "max_line_length": 98, "alphanum_fraction": 0.6048410615}
var express = require('express');
var path = require('path');
var fs = require('fs');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var _s = require('underscore.string');
var moment = require('moment');
var marked = require('marked');
var validator = require('validator');
var extend = require('extend');
var raneto = require('raneto-core');
var config = require('./config');
var app = express();

// Setup views
app.set('views', path.join(__dirname, 'views'));
app.set('layout', 'layout');
app.set('view engine', 'html');
app.enable('view cache');
app.engine('html', require('hogan-express'));

// Setup Express
app.use(favicon(__dirname +'/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// Setup config
extend(raneto.config, config);

// Load Global Partials
if(config.globalPartials) {
  app.set('partials', config.globalPartials);
}

// Handle all requests
app.all('*', function(req, res, next) {
  if(req.query.search){
    var searchQuery = validator.toString(validator.escape(_s.stripTags(req.query.search))).trim(),
      searchResults = raneto.doSearch(searchQuery),
      pageListSearch = raneto.getPages('');

    return res.render('search', {
      config: config,
      pages: pageListSearch,
      search: searchQuery,
      searchResults: searchResults,
      body_class: 'page-search'
    });
  }
  else if(req.params[0]){
    var slug = req.params[0];
    if(slug == '/') slug = '/index';

    var pageList = raneto.getPages(slug),
      filePath = path.normalize(raneto.config.content_dir + slug);
    if(!fs.existsSync(filePath)) filePath += '.md';

    if(slug == '/index' && !fs.existsSync(filePath)){
      return res.render('home', {
        config: config,
        pages: pageList,
        body_class: 'page-home'
      });
    } else {
      fs.readFile(filePath, 'utf8', function(err, content) {
        if(err){
          err.status = '404';
          err.message = 'Whoops. Looks like this page doesn\'t exist.';
          return next(err);
        }

        // Process Markdown files
        if(path.extname(filePath) == '.md'){
          // File info
          var stat = fs.lstatSync(filePath);
          // Meta
          var meta = raneto.processMeta(content);
          content = raneto.stripMeta(content);
          if(!meta.title) meta.title = raneto.slugToTitle(filePath);
          // Content
          content = raneto.processVars(content);
          var html = marked(content);
          var template = meta.template || 'page';

          return res.render(template, {
            config: config,
            pages: pageList,
            meta: meta,
            content: html,
            body_class: template + '-'+ raneto.cleanString(slug),
            last_modified: moment(stat.mtime).format('Do MMM YYYY')
          });
        } else {
          // Serve static file
          res.sendfile(filePath);
        }
      });
    }
  } else {
    next();
  }
});

// Handle any errors
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    config: config,
    status: err.status,
    message: err.message,
    error: {},
    body_class: 'page-error'
  });
});

module.exports = app;
