var Connect = require('connect'); var http = require('http'); var walk = require('walk'); var fs = require('fs'); var url = require('url'); var path = require('path'); var files = []; var walker = walk.walk('./slideshows',{ followLinks: false }); walker.on("directories", function (root, dirAr, next) { var lastDir = path.join(__dirname, root).split('/'); if((lastDir.length - __dirname.split('/').length) < 2) { dirAr.forEach(function(dir) { files[dir.name] = []; }); } next(); }).on("file", function (root, fileStats, next) { if(fileStats.name == "desc") { next(); } else if(fileStats.name == "template.html") { var lastDir = root.split('/'); files[lastDir[lastDir.length - 1]].push({ template: fileStats.name}); next(); } else { var lastDir = path.join(root).split('/'); if(files.hasOwnProperty(lastDir[lastDir.length - 1])) { files[lastDir[lastDir.length - 1]].push({ file: fileStats.name, head: '', text: ''}); } next(); } }).on("errors", function (root, nodeStatsArray, next) { next(); }).on("end", function () { for(var slides in files) { if(!files.hasOwnProperty(slides)) { continue; } var data = fs.readFileSync(__dirname+'/slideshows/'+slides+'/desc'); var descs = data.toString().split('\n'); for(var file in slides) { if(!files[slides].hasOwnProperty(file)) { continue; } if(!files[slides][file].hasOwnProperty('file')) { continue; } for(var desc in descs) { if(!descs.hasOwnProperty(desc)) { continue; } var info = descs[desc].split('|'); if(info[0] === files[slides][file]['file']) { files[slides][file]['head'] = info[1]; files[slides][file]['text'] = info[2]; } } } } Connect.createServer( Connect.logger(), // Log responses to the terminal using Common Log Format. Connect.responseTime(), // Add a special header with timing information. Connect.staticCache(), Connect.static( __dirname + '/slideshows'), // Serve all static files in the current dir. function(req, res) { var purl = url.parse(req.url, true); var slideset = 'default'; if(purl['query'].hasOwnProperty('slides')) { slideset = purl['query']['slides']; } if(!files.hasOwnProperty(slideset)) { res.end(); return; } fs.readFile('./slideshows/'+slideset+'/template.html', function(err,data){ if(err) { res.end(body); } var body = data.toString(); var slides = ''; for(var file in files[slideset]) { if (!files[slideset].hasOwnProperty(file)) { continue; } if (!files[slideset][file].hasOwnProperty('file')) { continue; } slides = slides+'
  • ' if(files[slideset][file]['text']) { slides = slides+'

    '+files[slideset][file]['head']+' - '+files[slideset][file]['text']+'

  • '; } } body = body.replace("$IMAGES", slides); res.setHeader('Content-Length', body.length); res.end(body); }); } ).listen(8080); });