pageOptions working, toc introduced
This commit is contained in:
94
src/main.ts
94
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 += `<li><a href="posts/${v}">${v} - ${pageOptions.title}</a></li>`
|
||||
})
|
||||
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")
|
||||
|
Reference in New Issue
Block a user