diff --git a/dist/main.js b/dist/main.js
index 145e1f5..a9e6aaa 100644
--- a/dist/main.js
+++ b/dist/main.js
@@ -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 += `
diff --git a/files/default.css b/files/default.css
new file mode 100644
index 0000000..ec96aa4
--- /dev/null
+++ b/files/default.css
@@ -0,0 +1,7 @@
+body {
+ background-color: white;
+ font-family: "verdana", sans-serif;
+ font-size: 16px;
+}
+
+
\ No newline at end of file
diff --git a/src/main.ts b/src/main.ts
index fef6038..a99b444 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -6,33 +6,82 @@ import * as logger from './log'
import * as express from 'express'
+function getPageOptions(contentStr: string) : any {
+ let pageOptions : any = {}
+ // 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 {
+ // logger.info("No pageOptions found")
+ }
+ return pageOptions
+}
+
+function getToc() : string {
+ logger.info("building toc")
+ let toc : string = ""
+ 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 += `
${v} - ${pageOptions.title}`
+ })
+ return toc
+}
+
+
+
logger.info("Homepage starting")
let app = express()
let masterTmpl: string
+let toc: string
+let cache: any = {}
+
+
app.engine('pag', (filePath: string, options: any, callback: any) => {
- 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')
+ if (! (filePath in cache)) {
+ logger.info(`${filePath} not yet in cache`)
- let renderedPhase2 = masterTmpl
- .replace('#maincontent#', renderedPhase1)
- .replace('#title#', pageOptions.title)
+ 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)
+ }
- return callback(null, renderedPhase2)
- })
+ cache[filePath] = renderedPhase2
+
+ return callback(null, renderedPhase2)
+ })
+ } else {
+ logger.info(`${filePath} served from cache`)
+ return callback(null, cache[filePath])
+ }
})
app.set('views', './docroot')
@@ -50,7 +99,18 @@ 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")