<commit_before>/* jshint node: true */
'use strict';

/*
This file was generated by generator-ffwd,
you probably should not modify it
*/

var fs = require('fs');
var utils = require('ffwd-utils/server');
var _ = utils._;
var debug = utils.debug('ffwd-documents');
var marked = require('marked');
var yfm = require('yfm');
var toYaml = require('toYaml').toYaml;
var hljs = require('highlight.js');

// Synchronous highlighting with highlight.js
marked.setOptions({
  highlight: function (code) {
    return hljs.highlightAuto(code).value;
  }
});

/**
 * An example of feature implementation
 * @exports example-feature
 * @memberOf FFWD.server
 * @param  {Object} config ...
 * @return {Object}        ...
 */
module.exports = function(config) {
  config = config || {};
  var app = config.app;

  config.pages = config.pages || app.get('pages') || 'pages';
  config.navigation = config.navigation || {};
  var _navCache;


  // stoopid
  config.navigation = config.pages +'/navigation.yml';

  config.cache = typeof config.cache !== 'undefined' ?
                  config.cache :
                  (app.get('env') !== 'development');

  debug('cache documents menu %s %s', config.cache, app.get('env'));


  config.pagesBasePath = typeof config.pagesBasePath === 'undefined' ?
                          'pages' :
                          config.pagesBasePath
                          ;


  /**
   * Builds the navigation for a response.
   * @param  {connect.Request}  req  ...
   * @param  {connect.Response} res  ...
   * @param  {Function} next         ...
   */
  function extendMenu(req, res, next) {
    if (_navCache && config.cache !== false) {
      res.locals.navigation = _navCache;
      return next();
    }


    _navCache = {};
    if (!res.menu) {
      res.locals.navigation = _navCache;
      return next();
    }


    utils.glob(config.pages +'/**/*.{html,md}', function(err, files) {
      var pages = {};
      debug('extendMenu with %s entries', files.length);

      _.each(files, function(file) {
        // converts something like:
        // 'local-pages-folder/yada/doc.md'
        // into
        // 'url-pages-path/yada/doc'
        var urlPath = //config.pagesBasePath +'/'+
                      file
                      .slice((config.pages +'/').length)
                      .split('.')
                      .slice(0, -1)
                      .join('.');

        var page = {};
        try {
          page = yfm.read(file).context;
        }
        catch (err) {
          console.trace();
          debug('reading %s %s', file, err);
        }

        pages[urlPath] = page;
      });


      res.menu.extend(pages);

      _navCache = res.menu.tree;
      debug('menu.tree', JSON.stringify(res.menu.tree.guides, null, 2));

      res.locals.navigation = _navCache;
      next();
    });
  }

  function pageEditRender(req, res, next) {
    fs.readFile(app.get('views') +'/partials/page-edit'+ (req.query.edit === 'popup' ? '-popup' : '') +'.tpl', {
      encoding: 'utf8'
    }, function(nope, str) {
      if (nope) { return next(nope); }
      res.locals.page.body = _.template(str, res.locals);
      res.locals.page.title = 'Edit: '+ res.locals.page.title;
      next();
    });
  }

  function docFilepath(req, next) {
    var name = req.url
                .split('?')
                .shift()
                .slice(1)
                ;
    name = name.slice(-1) === '/' ?
            name.slice(0, -1) :
            name;

    var match = config.pages +'/'+ name +'{.md,.html,/index.md,/index.html}';

    debug('doc name "%s" "%s", "%s"', req.url, name, match);

    utils.glob(match, function(err, files) {
      if (err) { return next(err); }
      if (!files.length) {
        return next(new Error('No files in '+ config.pagesBasePath));
      }

      var filename = files.shift();

      debug('filename for "%s" "%s": "%s"', config.pages, name, filename);
      next(filename ? null : new Error('Cannot find '+ match), filename);
    });
  }

  /**
   * Reponse callback for /docs requests
   * @param  {connect.Request}   req  ...
   * @param  {connect.Response}   res  ...
   * @param  {Function} next ...
   */
  function docRequest(req, res, next) {
    debug('docRequest has request user?', req.user);
    // debug('docRequest has request session.passport?', req.session.passport);

    docFilepath(req, function(nope, filename) {
      if (nope) { return res.send(404); }

      if (!_.isUndefined(req.query.edit)) {
        return docEditRequest(req, res, next);
      }

      var content = yfm.read(filename);

      _.extend(res.locals.page, content.context);

      res.locals.page.body = marked.parse(content.content)
                              .replace(/ref:asset:/g, '')
                              .replace(/asset:/g, '')
                              .replace(/ref:/g, config.pagesBasePath)
                              ;
      res.locals.page.header = res.renderPartial('page-header');
      res.locals.page.footer = res.renderPartial('page-footer');

      res.render('default');
    });
  }

  function docEditRequest(req, res, next) {
    debug('docEditRequest has request user?', req.user);
    if (_.isUndefined(req.query.edit)) {
      return next();
    }

    docFilepath(req, function(err, filename) {
      if (err) { return next(err); }
      var content = yfm.read(filename);

      _.extend(res.locals.page, content.context);

      res.locals.page.vars = Object.keys(content.context).concat(['location']);

      res.locals.page.body = content.content;
      res.locals.page.location = req.url.split('?').shift();
      res.locals.page.header = res.renderPartial('page-header');
      res.locals.page.footer = res.renderPartial('page-footer');

      var format = req.accepts(['json', 'html']);

      if (format === 'json') {
        var json = _.clone(res.locals.page);
        // cleanup??
        return res.send(json);
      }<commit_msg>aggregated page<commit_after>/* jshint node: true */
'use strict';

/*
This file was generated by generator-ffwd,
you probably should not modify it
*/

var path = require('path');
var fs = require('fs');
var utils = require('ffwd-utils/server');
var _ = utils._;
var debug = utils.debug('ffwd-documents');
var marked = require('marked');
var yfm = require('yfm');
var toYaml = require('toYaml').toYaml;
var hljs = require('highlight.js');

// Synchronous highlighting with highlight.js
marked.setOptions({
  highlight: function (code) {
    return hljs.highlightAuto(code).value;
  }
});

/**
 * An example of feature implementation
 * @exports example-feature
 * @memberOf FFWD.server
 * @param  {Object} config ...
 * @return {Object}        ...
 */
module.exports = function(config) {
  config = config || {};
  var app = config.app;

  config.pages = config.pages || app.get('pages') || 'pages';
  config.navigation = config.navigation || {};
  var _navCache;


  // stoopid
  config.navigation = config.pages +'/navigation.yml';

  config.cache = typeof config.cache !== 'undefined' ?
                  config.cache :
                  (app.get('env') !== 'development');

  debug('cache documents menu %s %s', config.cache, app.get('env'));


  config.pagesBasePath = typeof config.pagesBasePath === 'undefined' ?
                          'pages' :
                          config.pagesBasePath
                          ;


  /**
   * Builds the navigation for a response.
   * @param  {connect.Request}  req  ...
   * @param  {connect.Response} res  ...
   * @param  {Function} next         ...
   */
  function extendMenu(req, res, next) {
    if (_navCache && config.cache !== false) {
      res.locals.navigation = _navCache;
      return next();
    }


    _navCache = {};
    if (!res.menu) {
      res.locals.navigation = _navCache;
      return next();
    }


    utils.glob(config.pages +'/**/*.{html,md}', function(err, files) {
      var pages = {};
      debug('extendMenu with %s entries', files.length);

      _.each(files, function(file) {
        // converts something like:
        // 'local-pages-folder/yada/doc.md'
        // into
        // 'url-pages-path/yada/doc'
        var urlPath = //config.pagesBasePath +'/'+
                      file
                      .slice((config.pages +'/').length)
                      .split('.')
                      .slice(0, -1)
                      .join('.');

        var page = {};
        try {
          page = yfm.read(file).context;
        }
        catch (err) {
          console.trace();
          debug('reading %s %s', file, err);
        }

        pages[urlPath] = page;
      });


      res.menu.extend(pages);

      _navCache = res.menu.tree;
      // debug('menu.tree', JSON.stringify(res.menu.tree.guides, null, 2));

      res.locals.navigation = _navCache;
      next();
    });
  }


  function pageEditRender(req, res, next) {
    fs.readFile(app.get('views') +'/partials/page-edit'+ (req.query.edit === 'popup' ? '-popup' : '') +'.tpl', {
      encoding: 'utf8'
    }, function(nope, str) {
      if (nope) { return next(nope); }
      res.locals.page.body = _.template(str, res.locals);
      res.locals.page.title = 'Edit: '+ res.locals.page.title;
      next();
    });
  }

  function docFilepath(req, next) {
    var name = req.url
                .split('?')
                .shift()
                .slice(1)
                ;
    name = name.slice(-1) === '/' ?
            name.slice(0, -1) :
            name;

    var match = config.pages +'/'+ name +'{.md,.html,/index.md,/index.html}';

    debug('doc name "%s" "%s", "%s"', req.url, name, match);

    utils.glob(match, function(err, files) {
      if (err) { return next(err); }
      if (!files.length) {
        return next(new Error('No files in '+ config.pagesBasePath));
      }

      var filename = files.shift();

      debug('filename for "%s" "%s": "%s"', config.pages, name, filename);
      next(filename ? null : new Error('Cannot find '+ match), filename);
    });
  }

  /**
   * Reponse callback for /docs requests
   * @param  {connect.Request}   req  ...
   * @param  {connect.Response}   res  ...
   * @param  {Function} next ...
   */
  function docRequest(req, res, next) {
    debug('docRequest has request user?', req.user);
    // debug('docRequest has request session.passport?', req.session.passport);

    docFilepath(req, function(nope, filepath) {
      if (nope) { return res.send(404); }

      if (!_.isUndefined(req.query.edit)) {
        return docEditRequest(req, res, next);
      }

      var content = yfm.read(filepath);

      function done() {
        res.locals.page.header = res.renderPartial('page-header');
        res.locals.page.footer = res.renderPartial('page-footer');

        res.locals.page.body = res.locals.page.body
                                .replace(/ref:asset:/g, '')
                                .replace(/asset:/g, '')
                                .replace(/ref:/g, config.pagesBasePath)
        ;

        res.render(content.context.template || 'default');
      }

      if (!content.context.aggregateChildren) {
        _.extend(res.locals.page, content.context);
        res.locals.page.body = marked.parse(content.content);
        return done();
      }

      var dirname = path.dirname(filepath);
      debug('populate sub pages for %s', dirname);
      utils.glob(dirname +'/**/*.{md,html}', function(err, files) {
        if (err) {
          return next(err);
        }


        debug('analyse %d pages', files.length);
        var subPages = {};
        _.each(files, function(file) {
          var subContent = yfm.read(file);
          if (subContent) {
            var id = _.str.slugify(subContent.context.title);
            subPages[id] = subContent.context;
            subPages[id].id = id;
            subPages[id].body = marked.parse(subContent.content);
          }
        });

        debug('render pages', files.length);
        var categories = {};
        _.each(utils.weightened(subPages), function(file) {
          categories[file.category] = categories[file.category] || [];
          categories[file.category].push(file);
        });

        res.locals.page.body = res.renderPartial('aggregated', {categories: categories});
        done();
      });
    });
  }

  function docEditRequest(req, res, next) {
    debug('docEditRequest has request user?', req.user);
    if (_.isUndefined(req.query.edit)) {
      return next();
    }

    docFilepath(req, function(err, filename) {
      if (err) { return next(err); }
      var content = yfm.read(filename);

      _.extend(res.locals.page, content.context);

      res.locals.page.vars = Object.keys(content.context).concat(['location']);

      res.locals.page.body = content.content;
      res.locals.page.location = req.url.split('?').shift();
      res.locals.page.header = res.renderPartial('page-header');
      res.locals.page.footer = res.renderPartial('page-footer');

      var format = req.accepts(['json', 'html']);

      if (format === 'json') {
        var json = _.clone(res.locals.page);
        // cleanup??
        return res.send(json);
      }
{'fraction_non_alphanumeric': 0.1663621135982945, 'fraction_numerical': 0.0016750418760469012, 'mean_word_length': 2.072514619883041, 'pattern_counts': {'":': 2, '<': 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': '6368177', 'n_tokens_mistral': 4145, 'n_tokens_neox': 3988, 'n_words': 831}