pageOptions working, toc introduced
This commit is contained in:
82
dist/main.js
vendored
82
dist/main.js
vendored
@ -4,26 +4,68 @@ const fs = require("fs");
|
||||
const config = require("./config");
|
||||
const logger = require("./log");
|
||||
const express = require("express");
|
||||
function getPageOptions(contentStr) {
|
||||
let pageOptions = {};
|
||||
// logger.info(`contentStr: ${contentStr}`)
|
||||
try {
|
||||
let lines = contentStr.split("\n");
|
||||
let firstLine = lines[0];
|
||||
// logger.info(`firstLine: ${firstLine}`)
|
||||
let pageOptionsStr = firstLine.replace(/^\s*<!-- (\{.+\}) -->.*$/, "\$1");
|
||||
// logger.info(`pageOptionsStr: ${pageOptionsStr}`)
|
||||
pageOptions = JSON.parse(pageOptionsStr);
|
||||
// logger.info(JSON.stringify(pageOptions))
|
||||
}
|
||||
catch (_a) {
|
||||
// logger.info("No pageOptions found")
|
||||
}
|
||||
return pageOptions;
|
||||
}
|
||||
function getToc() {
|
||||
logger.info("building toc");
|
||||
let toc = "";
|
||||
let posts = fs.readdirSync('./docroot/posts');
|
||||
posts.forEach((v) => {
|
||||
let content = fs.readFileSync(`./docroot/posts/${v}/article.pag`);
|
||||
let contentStr = content.toString();
|
||||
let pageOptions = getPageOptions(contentStr);
|
||||
toc += `<li><a href="posts/${v}">${v} - ${pageOptions.title}</a></li>`;
|
||||
});
|
||||
return toc;
|
||||
}
|
||||
logger.info("Homepage starting");
|
||||
let app = express();
|
||||
let masterTmpl;
|
||||
let toc;
|
||||
let cache = {};
|
||||
app.engine('pag', (filePath, options, callback) => {
|
||||
fs.readFile(filePath, (err, content) => {
|
||||
if (err) {
|
||||
return callback(new Error(err.message));
|
||||
}
|
||||
let contentStr = content.toString();
|
||||
let pageOptionsStr = contentStr.replace(/^\s+<!-- (\{.*\}) -->.*$/, "\$1");
|
||||
logger.info(`contentStr: ${contentStr}`);
|
||||
logger.info(`pageOptionsStr: ${pageOptionsStr}`);
|
||||
let pageOptions = JSON.parse(pageOptionsStr);
|
||||
let renderedPhase1 = contentStr
|
||||
.replace('#bla#', 'blu');
|
||||
let renderedPhase2 = masterTmpl
|
||||
.replace('#maincontent#', renderedPhase1)
|
||||
.replace('#title#', pageOptions.title);
|
||||
return callback(null, renderedPhase2);
|
||||
});
|
||||
if (!(filePath in cache)) {
|
||||
logger.info(`${filePath} not yet in cache`);
|
||||
fs.readFile(filePath, (err, content) => {
|
||||
if (err) {
|
||||
return callback(new Error(err.message));
|
||||
}
|
||||
let contentStr = content.toString();
|
||||
let pageOptions = getPageOptions(contentStr);
|
||||
let renderedPhase1 = contentStr
|
||||
.replace('#bla#', 'blu');
|
||||
let renderedPhase2 = masterTmpl
|
||||
.replace('#maincontent#', renderedPhase1);
|
||||
for (let key in pageOptions) {
|
||||
let value = pageOptions[key];
|
||||
if (key == 'toc' && value == 'compute') {
|
||||
value = toc;
|
||||
}
|
||||
renderedPhase2 = renderedPhase2.replace(`#${key}#`, value);
|
||||
}
|
||||
cache[filePath] = renderedPhase2;
|
||||
return callback(null, renderedPhase2);
|
||||
});
|
||||
}
|
||||
else {
|
||||
logger.info(`${filePath} served from cache`);
|
||||
return callback(null, cache[filePath]);
|
||||
}
|
||||
});
|
||||
app.set('views', './docroot');
|
||||
app.set('view engine', 'pag');
|
||||
@ -36,7 +78,15 @@ app.get('/posts/:date', (req, res) => {
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Hello world!');
|
||||
});
|
||||
app.get('/reload', (req, res) => {
|
||||
logger.info('truncating cache');
|
||||
cache = {};
|
||||
toc = getToc();
|
||||
res.send('reload triggered');
|
||||
});
|
||||
app.use('/files', express.static('files'));
|
||||
masterTmpl = fs.readFileSync(config.dict.masterTmpl).toString();
|
||||
toc = getToc();
|
||||
app.listen(config.dict.httpPort, () => {
|
||||
logger.info("Homepage is listening");
|
||||
});
|
||||
|
Reference in New Issue
Block a user