<commit_before>var express = require('express');
var path = require('path');
var fs = require('fs');
var mime = require('mime');
var _ = require('lodash');
var querystring = require('querystring');
var findit = require('findit');
var markdown = require('markdown').markdown;
var app = express();
var argv = require('minimist')(process.argv);

app.set('views', './views');
app.set('view engine', 'pug');

if(!argv['r']){
  console.log('usage: node index.js -r {root_of_dir}')
  return;
}

argv['r'] = _.trimEnd(argv['r'], path.sep);

var root = argv['r'];

app.get('/__*', function(req, res){
  var file_path = querystring.unescape(req.path.substring(3));

  var content_type = mime.lookup(file_path);
  var options = {
    root: path.join(__dirname, "asserts"),
    dotfiles: 'deny',
    headers: {
      'Content-Type': content_type
    }
  }

  res.sendFile(file_path, options);

  /*
  var ret = res.sendFile(file_path, options, function(result){
    if(result != null){
      res.status(400).end();
    }
  });
  */
});

function get_cls_by_state(state){
  if(state.isDirectory()){
    return "folder outline";
  } else if(state.isFile()){
    return "file";
  }

  return "";
}

function get_cls(f){
  var state = fs.lstatSync(path.join(root, f));

  return get_cls_by_state(state);
}

function search_file(file_path, leaf, req, res){
  var finder = findit(leaf);

  var sk = querystring.unescape(req.param('key')).toLowerCase();

  var diritems = [];
  finder.on('path', function(p, stat){
    var bpath = p.substring(leaf.length);
    if(path.basename(bpath).toLowerCase().indexOf(sk) != -1){
      var item = {name: path.basename(p), url: p.substring(root.length), file_type_cls: get_cls_by_state(stat)}
      diritems.push(item);
    }
  });

  finder.on('end', function(){
    res.render('list_dir', { title: file_path, diritems: diritems, bread: create_bread(file_path)});
    res.end();
  });
}

function create_bread(file_path){
    var bread_items = _.trimEnd(file_path, '/').split(path.sep);
    var cur_url = "/";
    var bread = bread_items.map(function(v, i){
      cur_url = path.join(cur_url, v);
      if(v == ''){
        v = "Raspberry Pi";
      }
      return {name: v, url: cur_url, is_last: i == (bread_items.length - 1)}
    });

    return bread;
}

function get_language(ext){
  if(ext == '.html'){
    return "markup";
  }else if(ext == '.md'){
    return 'markdown';
  } else if(ext == '.css'){
    return 'css';
  } else if(ext == '.js'){
    return 'js';
  } else if(ext == '.sh'){
    return 'bash';
  } else if(ext == '.c'){
    return 'c';
  } else if(ext == '.h'){
    return 'cpp';
  } else if(ext == '.cpp' || ext == '.cc'){
    return 'cpp';
  } else if(ext == '.rb'){
    return 'ruby';
  } else if(ext == '.erl'){
    return 'erlang';
  } else if(ext == '.go'){
    return 'go';
  } else if(ext == '.java'){
    return 'java';
  } else if(ext == '.lua'){
    return 'lua';
  } else if(ext == '.mm'){
    return 'objectivec';
  } else if(ext == '.pl'){
    return 'perl';
  } else if(ext == '.php'){
    return 'php';
  } else if(ext == '.py'){
    return 'python';
  } else if(ext == '.swift'){
    return 'swift';
  }

  return null;
}

app.get('*', function(req, res){
  var file_path = querystring.unescape(req.path);
  var leaf = path.join(root, file_path);
  var state = fs.lstatSync(leaf);

  if(state == null){
    res.status(400).end();
    return;
  }

  if(state.isDirectory()){
    if(req.param('key')){
      search_file(file_path, leaf, req, res);
    } else {
      var dirs = fs.readdirSync(leaf);
      var bread = create_bread(file_path);
      var items = dirs.map(function(v){
        var tmp = path.join(file_path, v);
        return {name: v, url: tmp, file_type_cls: get_cls(tmp)};
      });
      res.render('list_dir', { title: file_path, diritems: items, bread: bread});
      res.end();
    }
    return;<commit_msg>appendix / at the end of dir name<commit_after>var express = require('express');
var path = require('path');
var fs = require('fs');
var mime = require('mime');
var _ = require('lodash');
var querystring = require('querystring');
var findit = require('findit');
var markdown = require('markdown').markdown;
var app = express();
var argv = require('minimist')(process.argv);

app.set('views', './views');
app.set('view engine', 'pug');

if(!argv['r']){
  console.log('usage: node index.js -r {root_of_dir}')
  return;
}

argv['r'] = _.trimEnd(argv['r'], path.sep);

var root = argv['r'];

app.get('/__*', function(req, res){
  var file_path = querystring.unescape(req.path.substring(3));

  var content_type = mime.lookup(file_path);
  var options = {
    root: path.join(__dirname, "asserts"),
    dotfiles: 'deny',
    headers: {
      'Content-Type': content_type
    }
  }

  res.sendFile(file_path, options);

  /*
  var ret = res.sendFile(file_path, options, function(result){
    if(result != null){
      res.status(400).end();
    }
  });
  */
});

function get_cls_by_state(state){
  if(state.isDirectory()){
    return "folder outline";
  } else if(state.isFile()){
    return "file";
  }

  return "";
}

function get_cls(f){
  var state = fs.lstatSync(path.join(root, f));

  return get_cls_by_state(state);
}

function search_file(file_path, leaf, req, res){
  var finder = findit(leaf);

  var sk = querystring.unescape(req.param('key')).toLowerCase();

  var diritems = [];
  finder.on('path', function(p, stat){
    var bpath = p.substring(leaf.length);
    if(path.basename(bpath).toLowerCase().indexOf(sk) != -1){
      var item = {name: path.basename(p), url: p.substring(root.length), file_type_cls: get_cls_by_state(stat)}
      diritems.push(item);
    }
  });

  finder.on('end', function(){
    res.render('list_dir', { title: file_path, diritems: diritems, bread: create_bread(file_path)});
    res.end();
  });
}

function create_bread(file_path){
    var bread_items = _.trimEnd(file_path, '/').split(path.sep);
    var cur_url = "/";
    var bread = bread_items.map(function(v, i){
      cur_url = path.join(cur_url, v);
      if(v == ''){
        v = "Raspberry Pi";
      }
      return {name: v, url: cur_url, is_last: i == (bread_items.length - 1)}
    });

    return bread;
}

function get_language(ext){
  if(ext == '.html'){
    return "markup";
  }else if(ext == '.md'){
    return 'markdown';
  } else if(ext == '.css'){
    return 'css';
  } else if(ext == '.js'){
    return 'js';
  } else if(ext == '.sh'){
    return 'bash';
  } else if(ext == '.c'){
    return 'c';
  } else if(ext == '.h'){
    return 'cpp';
  } else if(ext == '.cpp' || ext == '.cc'){
    return 'cpp';
  } else if(ext == '.rb'){
    return 'ruby';
  } else if(ext == '.erl'){
    return 'erlang';
  } else if(ext == '.go'){
    return 'go';
  } else if(ext == '.java'){
    return 'java';
  } else if(ext == '.lua'){
    return 'lua';
  } else if(ext == '.mm'){
    return 'objectivec';
  } else if(ext == '.pl'){
    return 'perl';
  } else if(ext == '.php'){
    return 'php';
  } else if(ext == '.py'){
    return 'python';
  } else if(ext == '.swift'){
    return 'swift';
  }

  return null;
}

app.get('*', function(req, res){
  var file_path = querystring.unescape(req.path);
  var leaf = path.join(root, file_path);
  var state = fs.lstatSync(leaf);

  if(state == null){
    res.status(400).end();
    return;
  }

  if(state.isDirectory()){
    if(req.param('key')){
      search_file(file_path, leaf, req, res);
    } else {
      var dirs = fs.readdirSync(leaf);
      var bread = create_bread(file_path);
      var items = dirs.map(function(v){
        var tmp = path.join(file_path, v);

        var ftc = get_cls(tmp);

        var ret = {name: v, url: tmp, file_type_cls: ftc};
        if(_.startsWith(ftc, 'folder')){
          ret.name += "/";
        }

        return ret;
      });
      res.render('list_dir', { title: file_path, diritems: items, bread: bread});
      res.end();
    }
    return;
{'fraction_non_alphanumeric': 0.19856006062902615, 'fraction_numerical': 0.0022735884804850324, 'mean_word_length': 3.160798738833421, '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': '429525', 'n_tokens_mistral': 2937, 'n_tokens_neox': 2884, 'n_words': 640}