kettek2/acts/listing.js

99 lines
3.8 KiB
JavaScript

var fs = require('fs');
module.exports = function(qwiki) {
qwiki.rule('listing', '@@CONTENT@@', function(req, res, instance, next) {
var article = req.area.substring(req.listing.length+2);
if (article == '') {
res.write('<div class="content_row" id="bot">');
res.write('<div class="right">');
res.write('<h1>'+(req.listing.charAt(0).toUpperCase()+req.listing.slice(1))+'</h1>');
res.write('</div>');
res.write('</div>');
for (var i in qwiki.listings[req.listing]) {
res.write('<div class="content_row" id="bot">');
res.write('<div class="left">');
res.write('<h2>'+qwiki.listings[req.listing][i].date+'</h2>');
res.write('<h3>'+qwiki.listings[req.listing][i].time+'</h3>');
res.write('</div>');
res.write('<div class="right">');
res.write('<a href="'+req.listing+'/'+i+'">'+qwiki.listings[req.listing][i].title+'</a>');
res.write('</div>');
res.write('</div>');
}
next();
} else {
var entry = qwiki.listings[req.listing][article];
//article = article;
var cache_path = 'cache/'+req.listing+'/'+article+'.html';
var title = article;
var date = '00-00-00';
var time = '00:00:00';
if (typeof entry !== 'undefined') {
title = (typeof entry.title !== 'undefined' ? entry.title : article);
date = (typeof entry.date !== 'undefined' ? entry.date : '0');
time = (typeof entry.time !== 'undefined' ? entry.time : '0');
}
res.write('<div class="content_row" id="bot">');
res.write('<div class="left">');
res.write('<h2>'+date+'</h2>');
res.write('<h3>'+time+'</h3>');
res.write('</div>');
res.write('<div class="right">');
res.write('<h1>'+title+'</h1>');
qwiki.readFile(res, cache_path, function(type, err) {
if (type == 'FNF') {
// cache does not exist - is there a wiki source?
var wiki_path = 'wiki/'+req.listing+'/'+article+'.qwk';
fs.stat(wiki_path, function(err, stats) {
// TODO: stats.isFile()
if (err && err.code == 'ENOENT') {
// the wiki entry does not exist
res.write(article + ' does not exist yet');
res.write('</div></div>');
next();
} else if (err) {
// error while statting wiki entry
res.write(article + ': ' + err.code);
res.write('</div></div>');
next();
} else {
// wiki entry exists!
fs.readFile(wiki_path, function(err, data) {
if (err) {
res.write(article + ': ' + err.code);
res.write('</div></div>');
next();
} else {
qwiki.convertAndSave(req.listing+'/'+article, (article in qwiki.wiki_index.pages ? qwiki.wiki_index.pages[article].format : qwiki.getDefault('format')), data, function() {
qwiki.readFile(res, wiki_path, function(type, err) {
if (type == 'FNF') {
res.write('error while creating cache');
res.write('</div></div>');
next();
} else if (err) {
res.write(article + ': ' + err.code);
res.write('</div></div>');
next();
} else {
res.write('</div></div>');
next();
}
});
});
}
});
}
});
} else if (err) {
res.write(article + ': ' + err.code);
res.write('</div></div>');
next();
} else {
res.write('</div></div>');
next();
}
});
}
});
};