diff options
author | Benjamin Kiessling <mittagessen@l.unchti.me> | 2012-01-27 03:13:47 +0100 |
---|---|---|
committer | Benjamin Kiessling <mittagessen@l.unchti.me> | 2012-01-27 03:13:47 +0100 |
commit | 7dadd20fa765b34c0da2cdeba52da7d4cc139aae (patch) | |
tree | f3a78c22e2b4ad97a1254c2d1d664606d857bc9d | |
parent | 84da8d4b9bfab520a1bc0844773334b7131794c4 (diff) |
Use a swig as a templating system
-rw-r--r-- | config.js | 4 | ||||
-rw-r--r-- | frontend.js | 101 | ||||
-rw-r--r-- | main.js | 2 |
3 files changed, 51 insertions, 56 deletions
@@ -1,8 +1,10 @@ exports.modelBackend = 'fs'; exports.Config = { rootDir: 'slideshows' , + listTemplate: 'index.html', templateFile: 'template.html', descriptionFile: 'desc.js', eventFile: 'events.js', - ignoreDotFiles: 'false' + ignoreDotFiles: 'false', + port: 8080 }; diff --git a/frontend.js b/frontend.js index 8c3d4d6..ee811cd 100644 --- a/frontend.js +++ b/frontend.js @@ -1,7 +1,6 @@ var path = require('path'); var fs = require('fs'); -var sanitize =require('validator').sanitize; - +var cons = require('consolidate'); var model; exports.setModel = function(m) { @@ -9,70 +8,64 @@ exports.setModel = function(m) { }; exports.start = function(config) { - config.app.get('/slides/:id', function(req, res) { - var slideset; + var app = config.app; + app.engine('html', cons.swig); + + // set .html as the default extension + app.set('view engine', 'html'); + app.set('views', path.join(__dirname, config.rootDir)); + + app.get('/slides/:id', function(req, res) { slideset = req.params.id; model.getSlides(slideset, function(err, slides) { if(err) { throw err; } - fs.readFile(path.join(__dirname, config.rootDir, slides['template']) , function(err,data) { - if(err) { res.end(body); } - var body = data.toString(); - var slideStr = ''; - for(var slide in slides['slides']) { - if(!slides['slides'].hasOwnProperty(slide)) { continue; } - var media = sanitize(slides['slides'][slide]['file']).entityEncode(); - if(slides['slides'][slide]['type'] === 'image') { - slideStr = slideStr +'<li><img src="/'+media+'">'; - } - if(slides['slides'][slide]['head'].length > 0) { - var head = slides['slides'][slide]['head']; - slideStr = slideStr+'<div class="slideDesc"><div class="slideHead">'+head+'</div>'; - if(slides['slides'][slide]['text'].length > 0) { - var text = slides['slides'][slide]['text']; - slideStr = slideStr+'<div class="slideText">'+text+'</div>'; - } - slideStr = slideStr + '</div>'; + var slidesAr = []; + for(var slide in slides['slides']) { + if(!slides['slides'].hasOwnProperty(slide)) { continue; } + if(slides['slides'][slide]['type'] === 'image') { + slidesAr.push({ + image: slides['slides'][slide]['file'] + }); + } + if(slides['slides'][slide]['head'].length > 0) { + slidesAr[slidesAr.length - 1]['description'] = slides['slides'][slide]['head']; + if(slides['slides'][slide]['text'].length > 0) { + slidesAr[slidesAr.length - 1]['text'] = slides['slides'][slide]['text']; } - slideStr = slideStr + '</img></li>\n'; } - var eventStr = ''; - for(var date in slides['events']) { - if(!slides['events'].hasOwnProperty(date)) { continue; } - eventStr = eventStr + '<div class="event"><div class="eventDate">'+date+'</div><ul class="eventList">'; - for(var ev in slides['events'][date]) { - if(!slides['events'][date].hasOwnProperty(ev)) { continue; } - eventStr = eventStr + '<li class="eventEl">'; - for(var evF in slides['events'][date][ev]) { - if(!slides['events'][date][ev].hasOwnProperty(evF)) { continue; } - if(typeof slides['events'][date][ev][evF] == 'string') { - var evClass = evF; - var evField = sanitize(slides['events'][date][ev][evF]).entityEncode(); - var evField = evField.replace(/\\n/g, '<br/>'); - eventStr = eventStr + '<div class="eventField ' + evClass + '">' + evField + '</div>'; - } + } + var eventAr = []; + for(var date in slides['events']) { + if(!slides['events'].hasOwnProperty(date)) { continue; } + eventAr.push({ + date: date, + events: [] + }); + for(var ev in slides['events'][date]) { + if(!slides['events'][date].hasOwnProperty(ev)) { continue; } + var eventObj = {}; + for(var evF in slides['events'][date][ev]) { + if(!slides['events'][date][ev].hasOwnProperty(evF)) { continue; } + if(typeof slides['events'][date][ev][evF] == 'string') { + eventObj[evF] = slides['events'][date][ev][evF]; } - eventStr = eventStr + '</li>'; } - eventStr = eventStr + '</ul></div>'; + console.log(eventObj); + eventAr[eventAr.length - 1]['events'].push(eventObj); } - body = body.replace("$IMAGES", slideStr); - body = body.replace("$EVENTS", eventStr); - res.setHeader('Content-Length', body.length); - res.end(body); + } + res.render(path.join(slideset, config.templateFile), { + slides: slidesAr, + events: eventAr }); }); }); - config.app.get('*', function(req, res) { + app.get('*', function(req, res){ model.getAllSlides(function(slidesets) { - var body = '<head></head><body><h3>Slideshows:</h3>'; - for(var slideset in slidesets) { - if(!slidesets.hasOwnProperty(slideset)) { continue; } - body = body + '<a href="/slides/' + slidesets[slideset] + '">'+slidesets[slideset]+'</a><br/>'; - } - body = body + '</body>'; - res.setHeader('Content-Length', body.length); - res.end(body); + res.render(config.listTemplate, { + slides: slidesets + }); }); }); - config.app.listen(8080); + app.listen(config.port); }; @@ -10,7 +10,7 @@ process.chdir(__dirname); var config = require('./config'); var express = require('express'); -var app = express.createServer(); +var app = express(); app.use(express.logger()); app.use(express.staticCache()); |