<commit_before>'use strict';

/* global __dirname */

var express = require('express'),
    fs      = require('fs'),
    path    = require('path'),
    router  = express.Router();

var base = path.resolve(__dirname, '../../examples/data');

function docPath(req, addPath, ending) {
  return base + '/' + addPath + '/' + req.params.doc + '.' + ending;
}

var contentTypes = {
  'xml' : 'text/xml; charset=utf-8',
  'json': 'application/json; charset=utf-8',
  'html': 'text/html; charset=utf-8'
};

function sendFile(req, res, addPath, ending) {
  res.header('Content-Type', contentTypes[ending]);
  res.sendFile(docPath(req, addPath, ending));
}

function writeFile(req, res, addPath, ending) {
  var doc = '';
  req.on('data', function(data) { doc += data; });
  req.on('end', function() {
    var path = docPath(req, addPath, ending);
    fs.writeFile(path, doc, function() { res.end(); });
  });
}

function get(route, fileType) {
  return function(req, res) { sendFile(req, res, route, fileType); };
}

function post(route, fileType) {
  return function(req, res) { writeFile(req, res, route, fileType); };
}

var exampleFileRoutes = {
  'treebanks': 'xml',
  'translations': 'json',
  'tei' : 'xml',
  'oa' : 'json',

  'treebanks/phaidra': 'json',
  'translations/phaidra': 'json'
};

for (var route in exampleFileRoutes) {
  var fileType = exampleFileRoutes[route];
  router.get( '/' + route + '/:doc', get(route, fileType));
  router.post('/' + route + '/:doc', post(route, fileType));<commit_msg>Prettify when writing to disc<commit_after>'use strict';

/* global __dirname */

var express = require('express'),
    fs      = require('fs'),
    path    = require('path'),
    pd      = require('pretty-data').pd,
    router  = express.Router();

var base = path.resolve(__dirname, '../../examples/data');

function docPath(req, addPath, ending) {
  return base + '/' + addPath + '/' + req.params.doc + '.' + ending;
}

var contentTypes = {
  'xml' : 'text/xml; charset=utf-8',
  'json': 'application/json; charset=utf-8',
  'html': 'text/html; charset=utf-8'
};

function sendFile(req, res, addPath, ending) {
  res.header('Content-Type', contentTypes[ending]);
  res.sendFile(docPath(req, addPath, ending));
}

function prettify(str, req) {
  var type = req.headers['content-type'];
  if (type.match(/xml/))  return pd.xml(str);
  if (type.match(/json/)) return pd.json(str);
  return str;
}

function writeFile(req, res, addPath, ending) {
  var doc = '';
  req.on('data', function(data) { doc += data; });
  req.on('end', function() {
    var path = docPath(req, addPath, ending);
    fs.writeFile(path, prettify(doc, req), function() { res.end(); });
  });
}

function get(route, fileType) {
  return function(req, res) { sendFile(req, res, route, fileType); };
}

function post(route, fileType) {
  return function(req, res) { writeFile(req, res, route, fileType); };
}

var exampleFileRoutes = {
  'treebanks': 'xml',
  'translations': 'json',
  'tei' : 'xml',
  'oa' : 'json',

  'treebanks/phaidra': 'json',
  'translations/phaidra': 'json'
};

for (var route in exampleFileRoutes) {
  var fileType = exampleFileRoutes[route];
  router.get( '/' + route + '/:doc', get(route, fileType));
  router.post('/' + route + '/:doc', post(route, fileType));
{'fraction_non_alphanumeric': 0.19944852941176472, 'fraction_numerical': 0.001838235294117647, 'mean_word_length': 4.0153609831029184, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '3188213', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1146, 'n_words': 264}