151 lines
4.4 KiB
JavaScript
151 lines
4.4 KiB
JavaScript
var QWiki = require('./QWiki/index.js');
|
|
var chokidar = require('chokidar');
|
|
var fs = require('fs');
|
|
var path = require('path');
|
|
|
|
var port = 8089;
|
|
process.argv.forEach(function (val, index, array) {
|
|
var parts = val.split('=');
|
|
var key = '';
|
|
var value = '';
|
|
if (parts[0].substr(0, 2) == '--') {
|
|
key = parts[0].substr(2);
|
|
value = parts[1];
|
|
}
|
|
if (key == 'port') {
|
|
if (value == '') {
|
|
console.log(key + ' needs a value');
|
|
} else {
|
|
port = Number(value);
|
|
}
|
|
}
|
|
});
|
|
|
|
var qwiki = new QWiki();
|
|
|
|
qwiki.addMIMEtype('png', 'image/png');
|
|
qwiki.addMIMEtype('jpg', 'image/jpg');
|
|
qwiki.addMIMEtype('svg', 'image/svg+xml');
|
|
qwiki.addMIMEtype('html', 'text/html');
|
|
qwiki.addMIMEtype('css', 'text/css');
|
|
|
|
var m_markdownit = require('markdown-it')({
|
|
typographer: true
|
|
});
|
|
m_markdownit.use(require('markdown-it-anchor'));
|
|
m_markdownit.use(require('markdown-it-table-of-contents-with-heading'), {includeLevel: [1,2,3,4,5,6,7,8], forceFullToc: true});
|
|
m_markdownit.use(require('markdown-it-deflist'));
|
|
m_markdownit.use(require('markdown-it-header-sections'));
|
|
m_markdownit.use(require('markdown-it-sup'));
|
|
qwiki.addFormat('md', 'Markdown', function(source) {
|
|
// source is a Buffer, but render seems to need a String
|
|
return m_markdownit.render(source.toString());
|
|
});
|
|
|
|
qwiki.setDefault('format', 'md');
|
|
|
|
function updateQat(index) {
|
|
if (index < 0 || index >= qwiki.qats.length);
|
|
fs.readFile(qwiki.qats[index].path, function(err, data) {
|
|
if (err) throw err;
|
|
qwiki.qats[index].source = data.toString();
|
|
qwiki.qats[index].render = m_markdownit.render(qwiki.qats[index].source);
|
|
});
|
|
};
|
|
|
|
function updateUpdate(index) {
|
|
if (index < 0 || index >= qwiki.updates.length);
|
|
fs.readFile(qwiki.updates[index].path, function(err, data) {
|
|
if (err) throw err;
|
|
var filename = path.basename(qwiki.updates[index].path);
|
|
filename = filename.substr(0, filename.lastIndexOf('.'));
|
|
var parts = filename.split('_', 2);
|
|
qwiki.updates[index].date = (parts.length > 0 ? parts[0] : '');
|
|
qwiki.updates[index].time = (parts.length > 1 ? parts[1].replace(/-/g, ':') : '');
|
|
qwiki.updates[index].source = data.toString();
|
|
qwiki.updates[index].render = m_markdownit.render(qwiki.updates[index].source);
|
|
});
|
|
};
|
|
|
|
function updateArticles(path) {
|
|
fs.readFile(path, function(err, data) {
|
|
if (err) throw err;
|
|
qwiki.articles = JSON.parse(data);
|
|
});
|
|
};
|
|
|
|
function updateSoftware(path) {
|
|
fs.readFile(path, function(err, data) {
|
|
if (err) throw err;
|
|
qwiki.softwares = JSON.parse(data);
|
|
});
|
|
};
|
|
|
|
function updateListing(name, path) {
|
|
fs.readFile(path, function(err, data) {
|
|
if (err) throw err;
|
|
qwiki.listings[name] = JSON.parse(data);
|
|
});
|
|
};
|
|
|
|
qwiki.listen(port, function() {
|
|
console.log('Watching wiki/qat');
|
|
qwiki.qats = [];
|
|
chokidar.watch('wiki/qat/', {}).on('all', function(event, path) {
|
|
if (event == 'add') {
|
|
var qat = { path: path, source: '', render: '' };
|
|
qwiki.qats.push(qat);
|
|
updateQat(qwiki.qats.length-1);
|
|
} else if (event == 'unlink') {
|
|
for (var i = 0, len = qwiki.qats.length; i < len; i++) {
|
|
if (qwiki.qats[i].path == path) {
|
|
qwiki.qats.splice(i, 1);
|
|
break;
|
|
}
|
|
}
|
|
} else if (event == 'change') {
|
|
var qat;
|
|
for (var i = 0, len = qwiki.qats.length; i < len; i++) {
|
|
if (qwiki.qats[i].path == path) {
|
|
qat = qwiki.qats[i];
|
|
updateQat(i);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
console.log('Watching wiki/updates');
|
|
qwiki.updates = [];
|
|
chokidar.watch('wiki/updates/', {}).on('all', function(event, path) {
|
|
if (event == 'add') {
|
|
var update = { path: path, source: '', render: '' };
|
|
qwiki.updates.push(update);
|
|
updateUpdate(qwiki.updates.length-1);
|
|
} else if (event == 'unlink') {
|
|
for (var i = 0, len = qwiki.updates.length; i < len; i++) {
|
|
if (qwiki.updates[i].path == path) {
|
|
qwiki.updates.splice(i, 1);
|
|
break;
|
|
}
|
|
}
|
|
} else if (event == 'change') {
|
|
var update;
|
|
for (var i = 0, len = qwiki.updates.length; i < len; i++) {
|
|
if (qwiki.updates[i].path == path) {
|
|
update = qwiki.updates[i];
|
|
updateUpdate(i);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
console.log('Watching wiki/articles.json');
|
|
qwiki.listings = {};
|
|
chokidar.watch('wiki/articles.json', {}).on('all', function(event, path) {
|
|
if (event == 'add' || event == 'change') {
|
|
updateListing('articles', path);
|
|
}
|
|
});
|
|
});
|