// TODO: Index should likely show the index for the current location if it also contains a directory var fs = require('fs'); module.exports = function(qwiki) { qwiki.rule('index', '@@CONTENT@@', function(req, res, instance, next) { var path = 'wiki/'+req.area; qwiki.readDirectory(path, function(err, files) { if (err) { console.log('err: ' + err); if (err.errno == -2) { // ENOENT res.write('Cannot index "' + req.area + '" as it does not exist'); // FIXME: do actual errors } res.write('Error: ' + err); next(); return; } files.sort(function(a, b) { if (a.is_dir == true && b.is_dir == false) return -1; else if (a.is_dir == false && b.is_dir == true) return 1; else if (a.is_dir == false && b.is_dir == false) { var a_ext = qwiki.getExt(a.name); var b_ext = qwiki.getExt(b.name); if (a_ext == 'qwk' && b_ext != 'qwk') return -1; if (a_ext != 'qwk' && b_ext == 'qwk') return 1; else return 0; } else { return 0; } }); /*files.sort(function(a, b) { var a_ext = qwiki.getExt(a.name); var b_ext = qwiki.getExt(b.name); if (a_ext == 'qwk' && b_ext != 'qwk') return -1; if (a_ext != 'qwk' && b_ext == 'qwk') return 1; else return 0; });*/ res.write('

Index of '+req.area+'

'); res.write(''); next(); }); }); qwiki.rule('index', '@@CONTROLS@@', function(req, res, instance, next) { res.write('
  • View
  • Upload
  • '); next(); }); qwiki.act('index', function(req, res) { res.writeHead(200, "OK", { "Content-Type": "text/html", }); qwiki.parsePage('index', '', req, res); }); };