Code formatting with 2 space indent
This commit is contained in:
+132
-132
@@ -8,155 +8,155 @@ const path = require('path');
|
||||
* @type {{warn: string, ok: string, error: string}}
|
||||
*/
|
||||
const cons = {
|
||||
ok: '\x1b[32m✔\x1b[0m %s',
|
||||
warn: '\x1b[33m⚠\x1b[0m %s',
|
||||
error: '\x1b[31m✘\x1b[0m %s',
|
||||
ok: '\x1b[32m✔\x1b[0m %s',
|
||||
warn: '\x1b[33m⚠\x1b[0m %s',
|
||||
error: '\x1b[31m✘\x1b[0m %s',
|
||||
};
|
||||
|
||||
module.exports = (config) => {
|
||||
const fw = require('./file_walker')(config);
|
||||
const renderer = require('./renderer')(config);
|
||||
const fw = require('./file_walker')(config);
|
||||
const renderer = require('./renderer')(config);
|
||||
|
||||
// set view engine from configuration
|
||||
app.set('view engine', config['view_engine']);
|
||||
// reroute the views folder to the root folder
|
||||
app.set('views', path.join(__dirname, '..'));
|
||||
// set view engine from configuration
|
||||
app.set('view engine', config['view_engine']);
|
||||
// reroute the views folder to the root folder
|
||||
app.set('views', path.join(__dirname, '..'));
|
||||
|
||||
const articles = {};
|
||||
const articles = {};
|
||||
|
||||
/**
|
||||
* Fetch articles from the data folder and send success as a response
|
||||
* @param callback
|
||||
*/
|
||||
const reload = (callback) => {
|
||||
fw.fetchArticles((err, dict) => {
|
||||
if (err) {
|
||||
callback(false);
|
||||
return console.error(cons.error, 'loading articles : ' + err);
|
||||
}
|
||||
Object.keys(articles).forEach((key) => delete articles[key]);
|
||||
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
|
||||
const nb = Object.keys(articles).length;
|
||||
if (nb > 0)
|
||||
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''}`);
|
||||
else
|
||||
console.log(cons.warn, `no articles loaded, check your configuration`);
|
||||
callback(true);
|
||||
});
|
||||
};
|
||||
if (config['test'])
|
||||
app.reload = reload;
|
||||
|
||||
/**
|
||||
* Render the page with the view engine and catch errors
|
||||
* @param res
|
||||
* @param vPath - path of the view
|
||||
* @param data - data to pass to the view
|
||||
* @param code - code to send along the page
|
||||
*/
|
||||
const render = (res, vPath, data, code = 200) => {
|
||||
res.render(vPath, data, (err, html) => {
|
||||
if (err) {
|
||||
res.sendStatus(500);
|
||||
console.log(cons.error, `failed to render ${vPath} : ${err}`);
|
||||
} else
|
||||
res.status(code).send(html);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Show an error with the correct page
|
||||
* @param resPath - the page of the original error
|
||||
* @param code - error code
|
||||
* @param res
|
||||
*/
|
||||
const showError = (resPath, code, res) => {
|
||||
const errorPath = path.join(config['data_dir'], config['home']['error']);
|
||||
fs.access(errorPath, fs.constants.R_OK, (err) => {
|
||||
if (err)
|
||||
res.sendStatus(code);
|
||||
else
|
||||
render(res, errorPath, {error: code, path: resPath}, code);
|
||||
});
|
||||
};
|
||||
|
||||
// home endpoint : send the correct index page or error if not existing
|
||||
app.get('/', (req, res) => {
|
||||
const homePath = path.join(config['data_dir'], config['home']['index']);
|
||||
fs.access(homePath, fs.constants.R_OK, (err) => {
|
||||
if (err)
|
||||
showError(req.path, 404, res);
|
||||
else
|
||||
render(res, homePath, {articles: Object.values(articles)});
|
||||
});
|
||||
/**
|
||||
* Fetch articles from the data folder and send success as a response
|
||||
* @param callback
|
||||
*/
|
||||
const reload = (callback) => {
|
||||
fw.fetchArticles((err, dict) => {
|
||||
if (err) {
|
||||
callback(false);
|
||||
return console.error(cons.error, 'loading articles : ' + err);
|
||||
}
|
||||
Object.keys(articles).forEach((key) => delete articles[key]);
|
||||
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
|
||||
const nb = Object.keys(articles).length;
|
||||
if (nb > 0)
|
||||
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''}`);
|
||||
else
|
||||
console.log(cons.warn, `no articles loaded, check your configuration`);
|
||||
callback(true);
|
||||
});
|
||||
};
|
||||
if (config['test'])
|
||||
app.reload = reload;
|
||||
|
||||
// catch all article urls and render them
|
||||
app.get('*', (req, res, next) => {
|
||||
if (/^\/\d{4}\/\d{2}\/\d{2}\/(\w*\/)?$/.test(req.path)) {
|
||||
const articlePath = req.path.substr(1, 10);
|
||||
const article = articles[articlePath];
|
||||
if (!article)
|
||||
showError(req.path, 404, res);
|
||||
else {
|
||||
renderer.render(path.join(article.realPath, config['article']['index']), (err, html) => {
|
||||
if (err) {
|
||||
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
||||
return showError(req.path, 500, res);
|
||||
}
|
||||
article.content = html;
|
||||
const templatePath = path.join(config['data_dir'], config['article']['template']);
|
||||
fs.access(templatePath, fs.constants.R_OK, (err) => {
|
||||
if (err) {
|
||||
console.log(cons.error, `no template found at ${templatePath}`);
|
||||
showError(req.path, 500, res);
|
||||
} else
|
||||
render(res, templatePath, {article: article});
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
/**
|
||||
* Render the page with the view engine and catch errors
|
||||
* @param res
|
||||
* @param vPath - path of the view
|
||||
* @param data - data to pass to the view
|
||||
* @param code - code to send along the page
|
||||
*/
|
||||
const render = (res, vPath, data, code = 200) => {
|
||||
res.render(vPath, data, (err, html) => {
|
||||
if (err) {
|
||||
res.sendStatus(500);
|
||||
console.log(cons.error, `failed to render ${vPath} : ${err}`);
|
||||
} else
|
||||
res.status(code).send(html);
|
||||
});
|
||||
};
|
||||
|
||||
// catch all hidden file type and return 404
|
||||
app.get('*', (req, res, next) => {
|
||||
if (config['home']['hidden'].includes(path.extname(req.path)))
|
||||
showError(req.path, 404, res);
|
||||
else
|
||||
next();
|
||||
/**
|
||||
* Show an error with the correct page
|
||||
* @param resPath - the page of the original error
|
||||
* @param code - error code
|
||||
* @param res
|
||||
*/
|
||||
const showError = (resPath, code, res) => {
|
||||
const errorPath = path.join(config['data_dir'], config['home']['error']);
|
||||
fs.access(errorPath, fs.constants.R_OK, (err) => {
|
||||
if (err)
|
||||
res.sendStatus(code);
|
||||
else
|
||||
render(res, errorPath, {error: code, path: resPath}, code);
|
||||
});
|
||||
};
|
||||
|
||||
// serve all static files via get
|
||||
app.get('*', express.static(config['data_dir']));
|
||||
// catch express.static errors (mostly not found) by displaying 404
|
||||
app.get('*', (req, res) => {
|
||||
// home endpoint : send the correct index page or error if not existing
|
||||
app.get('/', (req, res) => {
|
||||
const homePath = path.join(config['data_dir'], config['home']['index']);
|
||||
fs.access(homePath, fs.constants.R_OK, (err) => {
|
||||
if (err)
|
||||
showError(req.path, 404, res);
|
||||
else
|
||||
render(res, homePath, {articles: Object.values(articles)});
|
||||
});
|
||||
});
|
||||
|
||||
// catch all other methods and return 400
|
||||
app.all('*', (req, res) => {
|
||||
res.status(400).send('bad request');
|
||||
});
|
||||
|
||||
app.use((err, req, res, next) => {
|
||||
console.log(cons.error, `error when handling ${req.path} request : ${err}`);
|
||||
console.error(err.stack);
|
||||
next(err);
|
||||
});
|
||||
|
||||
// must be use in a server.js to start the server
|
||||
app.start = () => {
|
||||
reload((res) => {
|
||||
if (res)
|
||||
app.listen(config['node_port'], () => {
|
||||
console.log(cons.ok, `gitblog.md server listening on port ${config['node_port']}`);
|
||||
});
|
||||
// catch all article urls and render them
|
||||
app.get('*', (req, res, next) => {
|
||||
if (/^\/\d{4}\/\d{2}\/\d{2}\/(\w*\/)?$/.test(req.path)) {
|
||||
const articlePath = req.path.substr(1, 10);
|
||||
const article = articles[articlePath];
|
||||
if (!article)
|
||||
showError(req.path, 404, res);
|
||||
else {
|
||||
renderer.render(path.join(article.realPath, config['article']['index']), (err, html) => {
|
||||
if (err) {
|
||||
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
||||
return showError(req.path, 500, res);
|
||||
}
|
||||
article.content = html;
|
||||
const templatePath = path.join(config['data_dir'], config['article']['template']);
|
||||
fs.access(templatePath, fs.constants.R_OK, (err) => {
|
||||
if (err) {
|
||||
console.log(cons.error, `no template found at ${templatePath}`);
|
||||
showError(req.path, 500, res);
|
||||
} else
|
||||
render(res, templatePath, {article: article});
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
return app;
|
||||
// catch all hidden file type and return 404
|
||||
app.get('*', (req, res, next) => {
|
||||
if (config['home']['hidden'].includes(path.extname(req.path)))
|
||||
showError(req.path, 404, res);
|
||||
else
|
||||
next();
|
||||
});
|
||||
|
||||
// serve all static files via get
|
||||
app.get('*', express.static(config['data_dir']));
|
||||
// catch express.static errors (mostly not found) by displaying 404
|
||||
app.get('*', (req, res) => {
|
||||
showError(req.path, 404, res);
|
||||
});
|
||||
|
||||
// catch all other methods and return 400
|
||||
app.all('*', (req, res) => {
|
||||
res.status(400).send('bad request');
|
||||
});
|
||||
|
||||
app.use((err, req, res, next) => {
|
||||
console.log(cons.error, `error when handling ${req.path} request : ${err}`);
|
||||
console.error(err.stack);
|
||||
next(err);
|
||||
});
|
||||
|
||||
// must be use in a server.js to start the server
|
||||
app.start = () => {
|
||||
reload((res) => {
|
||||
if (res)
|
||||
app.listen(config['node_port'], () => {
|
||||
console.log(cons.ok, `gitblog.md server listening on port ${config['node_port']}`);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return app;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,9 @@
|
||||
"home": {
|
||||
"index": "index.ejs",
|
||||
"error": "error.ejs",
|
||||
"hidden": [".ejs"]
|
||||
"hidden": [
|
||||
".ejs"
|
||||
]
|
||||
},
|
||||
"article": {
|
||||
"index": "index.md",
|
||||
|
||||
+17
-17
@@ -8,24 +8,24 @@ const fs = require('fs');
|
||||
* @returns {*}
|
||||
*/
|
||||
const merge = (ref, src) => {
|
||||
if (typeof ref !== typeof src) {
|
||||
return ref;
|
||||
} else if (typeof ref === 'object') {
|
||||
const out = {};
|
||||
Object.keys(ref).forEach((key) =>out[key] = merge(ref[key], src[key]));
|
||||
return out;
|
||||
} else {
|
||||
return src;
|
||||
}
|
||||
if (typeof ref !== typeof src) {
|
||||
return ref;
|
||||
} else if (typeof ref === 'object') {
|
||||
const out = {};
|
||||
Object.keys(ref).forEach((key) => out[key] = merge(ref[key], src[key]));
|
||||
return out;
|
||||
} else {
|
||||
return src;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = () => {
|
||||
try {
|
||||
let configData = fs.readFileSync('config.json', {encoding:'UTF-8'});
|
||||
let config = JSON.parse(configData);
|
||||
return merge(refConfig, config);
|
||||
} catch (error) {
|
||||
console.error('Failed to load config.json : '+error);
|
||||
return refConfig;
|
||||
}
|
||||
try {
|
||||
let configData = fs.readFileSync('config.json', {encoding: 'UTF-8'});
|
||||
let config = JSON.parse(configData);
|
||||
return merge(refConfig, config);
|
||||
} catch (error) {
|
||||
console.error('Failed to load config.json : ' + error);
|
||||
return refConfig;
|
||||
}
|
||||
};
|
||||
+78
-78
@@ -7,29 +7,29 @@ const path = require('path');
|
||||
* @param cb
|
||||
*/
|
||||
const getFileTree = (dir, cb) => {
|
||||
let list = [];
|
||||
let remaining = 0;
|
||||
fs.readdir(dir, {withFileTypes: true}, (err, items) => {
|
||||
if (err)
|
||||
let list = [];
|
||||
let remaining = 0;
|
||||
fs.readdir(dir, {withFileTypes: true}, (err, items) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
items.forEach((item) => {
|
||||
if (item.isDirectory()) {
|
||||
remaining++;
|
||||
getFileTree(path.join(dir, item.name), (err, out) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
items.forEach((item) => {
|
||||
if (item.isDirectory()) {
|
||||
remaining++;
|
||||
getFileTree(path.join(dir, item.name), (err, out) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
list.push(...out);
|
||||
remaining--;
|
||||
if (remaining === 0)
|
||||
cb(null, list);
|
||||
});
|
||||
} else {
|
||||
list.push(path.join(dir, item.name));
|
||||
}
|
||||
});
|
||||
if (remaining === 0)
|
||||
list.push(...out);
|
||||
remaining--;
|
||||
if (remaining === 0)
|
||||
cb(null, list);
|
||||
});
|
||||
} else {
|
||||
list.push(path.join(dir, item.name));
|
||||
}
|
||||
});
|
||||
if (remaining === 0)
|
||||
cb(null, list);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -39,70 +39,70 @@ const getFileTree = (dir, cb) => {
|
||||
* @param cb
|
||||
*/
|
||||
const readIndexFile = (path, thumbnailTag, cb) => {
|
||||
fs.readFile(path, {encoding: 'UTF-8'}, (err, data) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
fs.readFile(path, {encoding: 'UTF-8'}, (err, data) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
|
||||
let info = {};
|
||||
let info = {};
|
||||
|
||||
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
|
||||
info.title = regRes1 ? regRes1[2].trim() : undefined;
|
||||
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
|
||||
info.title = regRes1 ? regRes1[2].trim() : undefined;
|
||||
|
||||
const thumbnailRegEx = new RegExp(`!\\[${thumbnailTag}]\\(([^)]*)\\)`, 'i');
|
||||
const regRes2 = data.match(thumbnailRegEx);
|
||||
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
|
||||
const thumbnailRegEx = new RegExp(`!\\[${thumbnailTag}]\\(([^)]*)\\)`, 'i');
|
||||
const regRes2 = data.match(thumbnailRegEx);
|
||||
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
|
||||
|
||||
cb(null, info);
|
||||
});
|
||||
cb(null, info);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = (config) => {
|
||||
return {
|
||||
fileTree: config['test'] ? getFileTree : undefined,
|
||||
readIndexFile: config['test'] ? readIndexFile : undefined,
|
||||
/**
|
||||
* find and read all articles inside the data directory
|
||||
* @param cb
|
||||
*/
|
||||
fetchArticles: (cb) => {
|
||||
getFileTree(config['data_dir'], (err, fileList) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
const paths = fileList
|
||||
.map((path) => path.substr(config['data_dir'].length))
|
||||
.filter((path) => path.indexOf(config['article']['index']) === path.length - config['article']['index'].length)
|
||||
.map((path) => path.substr(0, path.length - config['article']['index'].length))
|
||||
.map((path) => path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/))
|
||||
.filter((matches) => matches && matches.length > 1);
|
||||
if (paths.length === 0)
|
||||
cb(null, {});
|
||||
const articles = {};
|
||||
let remaining = 0;
|
||||
paths.forEach((matches) => {
|
||||
const article = {
|
||||
path: path.join(matches[1], matches[2], matches[3]),
|
||||
realPath: path.join(config['data_dir'], matches[1], matches[2], matches[3]),
|
||||
year: parseInt(matches[1]),
|
||||
month: parseInt(matches[2]),
|
||||
day: parseInt(matches[3])
|
||||
};
|
||||
article.date = new Date(article.year, article.month, article.day);
|
||||
remaining++;
|
||||
readIndexFile(path.join(article.realPath, config['article']['index']), config['article']['thumbnail_tag'], (err, info) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
article.title = info.title || config['article']['default_title'];
|
||||
article.thumbnail = info.thumbnail ? path.join(article.path, info.thumbnail) : config['article']['default_thumbnail'];
|
||||
article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_');
|
||||
article.url = '/' + path.join(article.path, article.escapedTitle) + '/';
|
||||
articles[article.path] = article;
|
||||
remaining--;
|
||||
if (remaining === 0)
|
||||
cb(null, articles);
|
||||
});
|
||||
});
|
||||
return {
|
||||
fileTree: config['test'] ? getFileTree : undefined,
|
||||
readIndexFile: config['test'] ? readIndexFile : undefined,
|
||||
/**
|
||||
* find and read all articles inside the data directory
|
||||
* @param cb
|
||||
*/
|
||||
fetchArticles: (cb) => {
|
||||
getFileTree(config['data_dir'], (err, fileList) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
const paths = fileList
|
||||
.map((path) => path.substr(config['data_dir'].length))
|
||||
.filter((path) => path.indexOf(config['article']['index']) === path.length - config['article']['index'].length)
|
||||
.map((path) => path.substr(0, path.length - config['article']['index'].length))
|
||||
.map((path) => path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/))
|
||||
.filter((matches) => matches && matches.length > 1);
|
||||
if (paths.length === 0)
|
||||
cb(null, {});
|
||||
const articles = {};
|
||||
let remaining = 0;
|
||||
paths.forEach((matches) => {
|
||||
const article = {
|
||||
path: path.join(matches[1], matches[2], matches[3]),
|
||||
realPath: path.join(config['data_dir'], matches[1], matches[2], matches[3]),
|
||||
year: parseInt(matches[1]),
|
||||
month: parseInt(matches[2]),
|
||||
day: parseInt(matches[3])
|
||||
};
|
||||
article.date = new Date(article.year, article.month, article.day);
|
||||
remaining++;
|
||||
readIndexFile(path.join(article.realPath, config['article']['index']), config['article']['thumbnail_tag'], (err, info) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
article.title = info.title || config['article']['default_title'];
|
||||
article.thumbnail = info.thumbnail ? path.join(article.path, info.thumbnail) : config['article']['default_thumbnail'];
|
||||
article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_');
|
||||
article.url = '/' + path.join(article.path, article.escapedTitle) + '/';
|
||||
articles[article.path] = article;
|
||||
remaining--;
|
||||
if (remaining === 0)
|
||||
cb(null, articles);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
+14
-14
@@ -3,28 +3,28 @@ const path = require('path');
|
||||
const ncp = require('ncp').ncp;
|
||||
|
||||
const copy = (src, dest) => {
|
||||
ncp(src, dest, function (err) {
|
||||
if (err)
|
||||
console.error(err);
|
||||
else
|
||||
console.log(`copied ${src} to ${dest}`);
|
||||
});
|
||||
ncp(src, dest, function (err) {
|
||||
if (err)
|
||||
console.error(err);
|
||||
else
|
||||
console.log(`copied ${src} to ${dest}`);
|
||||
});
|
||||
};
|
||||
|
||||
copy(path.join('src', 'config.default.json'), 'config.example.json');
|
||||
|
||||
if (!fs.existsSync('data')) {
|
||||
fs.mkdirSync('data');
|
||||
fs.mkdirSync('data');
|
||||
|
||||
copy(path.join('sample_data','home'), 'data');
|
||||
copy(path.join('sample_data', 'home'), 'data');
|
||||
|
||||
const pad0 = (n) =>('0' + n).substr(-2);
|
||||
const pad0 = (n) => ('0' + n).substr(-2);
|
||||
|
||||
const datetime = new Date();
|
||||
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
|
||||
const datetime = new Date();
|
||||
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
|
||||
|
||||
if (!fs.existsSync(dir))
|
||||
fs.mkdirSync(dir, {recursive: true});
|
||||
if (!fs.existsSync(dir))
|
||||
fs.mkdirSync(dir, {recursive: true});
|
||||
|
||||
copy(path.join('sample_data','article'), dir);
|
||||
copy(path.join('sample_data', 'article'), dir);
|
||||
}
|
||||
+11
-11
@@ -2,15 +2,15 @@ const fs = require('fs');
|
||||
const showdown = require('showdown');
|
||||
|
||||
module.exports = (config) => {
|
||||
const converter = new showdown.Converter(config['showdown']);
|
||||
return {
|
||||
render : (file, cb) => {
|
||||
fs.readFile(file, {encoding:'UTF-8'}, (err, data) => {
|
||||
if(err)
|
||||
return cb(err);
|
||||
const html = converter.makeHtml(data);
|
||||
cb(null,html);
|
||||
});
|
||||
}
|
||||
};
|
||||
const converter = new showdown.Converter(config['showdown']);
|
||||
return {
|
||||
render: (file, cb) => {
|
||||
fs.readFile(file, {encoding: 'UTF-8'}, (err, data) => {
|
||||
if (err)
|
||||
return cb(err);
|
||||
const html = converter.makeHtml(data);
|
||||
cb(null, html);
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user