var fs = require('fs');
module.exports = function(qwiki) {
qwiki.rule('', '@@CONTENT@@', function(req, res, instance, next) {
if (req.area == '/' || req.area == '') {
qwiki.rules['updates']['@@CONTENT@@'](req, res, instance, next);
return;
} else {
for (var listing in qwiki.listings) {
if (req.area.substring(1, listing.length+1) == listing) {
req.listing = listing;
qwiki.rules['listing']['@@CONTENT@@'](req, res, instance, next);
return;
}
}
}
// instance.pos, instance.offset, instance.indices
var cache_path = 'cache/'+req.area+'.html';
res.write('
');
res.write('
');
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.area+'.qwk';
fs.stat(wiki_path, function(err, stats) {
// TODO: stats.isFile()
if (err && err.code == 'ENOENT') {
// the wiki entry does not exist
res.write(req.area + ' does not exist yet');
res.write('
');
next();
} else if (err) {
// error while statting wiki entry
res.write(req.area + ': ' + err.code);
res.write('');
next();
} else {
// wiki entry exists!
fs.readFile(wiki_path, function(err, data) {
if (err) {
res.write(req.area + ': ' + err.code);
res.write('');
next();
} else {
qwiki.convertAndSave(req.area, (req.area in qwiki.wiki_index.pages ? qwiki.wiki_index.pages[req.area].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(req.area + ': ' + err.code);
res.write('');
next();
} else {
res.write('');
next();
}
});
});
}
});
}
});
} else if (err) {
res.write(req.area + ': ' + err.code);
res.write('');
next();
} else {
res.write('');
next();
}
});
});
qwiki.rule('', '@@TITLE@@', function(req, res, instance, next) {
res.write('qwiki ' + req.area);
next();
});
qwiki.rule('', '@@PAGE@@', function(req, res, instance, next) {
res.write(req.area);
next();
});
qwiki.rule('', '@@CRUMBS@@', function(req, res, instance, next) {
var parts = req.area.split('/');
var path = '';
parts[0] = '>';
for (var i = 0; i < parts.length; i++) {
path += (i == 0 ? '/' : parts[i]);
res.write('