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('
');
res.write('
');
res.write('
'+(req.listing.charAt(0).toUpperCase()+req.listing.slice(1))+'
');
res.write('');
res.write('
');
for (var i in qwiki.listings[req.listing]) {
res.write('');
res.write('
');
res.write('
'+qwiki.listings[req.listing][i].date+'
');
res.write(''+qwiki.listings[req.listing][i].time+'
');
res.write('');
res.write('
');
res.write('
');
}
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('');
res.write('
');
res.write('
'+date+'
');
res.write(''+time+'
');
res.write('');
res.write('
');
res.write('
'+title+'
');
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(' ');
next();
} else if (err) {
// error while statting wiki entry
res.write(article + ': ' + err.code);
res.write('');
next();
} else {
// wiki entry exists!
fs.readFile(wiki_path, function(err, data) {
if (err) {
res.write(article + ': ' + err.code);
res.write('');
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('');
next();
} else if (err) {
res.write(article + ': ' + err.code);
res.write('');
next();
} else {
res.write('');
next();
}
});
});
}
});
}
});
} else if (err) {
res.write(article + ': ' + err.code);
res.write('');
next();
} else {
res.write('');
next();
}
});
}
});
};