ro work to me universal
This commit is contained in:
parent
985fd3c901
commit
62119c8742
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,7 +1,9 @@
|
||||
/content
|
||||
#/settings.js
|
||||
|
||||
# node
|
||||
/node_modules
|
||||
|
||||
# other stuff
|
||||
/.setings
|
||||
/.settings
|
||||
/.vscode
|
||||
|
||||
109
app.js
Normal file
109
app.js
Normal file
@ -0,0 +1,109 @@
|
||||
const express = require('express')
|
||||
const siteMap = require('./siteMap')
|
||||
const compile = require('./compile');
|
||||
const settings = require('./settings');
|
||||
const { render } = require('ejs');
|
||||
|
||||
console.log(siteMap)
|
||||
|
||||
const app = express()
|
||||
app.set('view engine', 'ejs');
|
||||
app.set('views', __dirname + '/views')
|
||||
|
||||
app.use('/:vak/static', renderStatic)
|
||||
app.use('/:vak/:page', renderPage)
|
||||
app.use('/:vak', renderVak)
|
||||
app.use('/', renderIndex)
|
||||
app.use(render404)
|
||||
|
||||
|
||||
function onError404(req, res, next){
|
||||
return function(err){
|
||||
render404(req, res, next)
|
||||
}
|
||||
}
|
||||
|
||||
function renderStatic(req, res, next){
|
||||
if(typeof siteMap[req.params.vak] == 'undefined') return next();
|
||||
vak = siteMap[req.params.vak]
|
||||
|
||||
dir = __dirname.split('/'); dir.pop() // remove dir of this code
|
||||
dir = `${dir.join('/')}/${vak.title}/notes/static${req.path}`
|
||||
|
||||
res.sendFile(dir, onError404(req, res, next))
|
||||
}
|
||||
|
||||
function renderPage(req, res, next){
|
||||
if(typeof siteMap[req.params.vak] == 'undefined') return next();
|
||||
vak = siteMap[req.params.vak]
|
||||
|
||||
if(typeof vak.pages[req.params.page] == 'undefined') return next();
|
||||
page = vak.pages[req.params.page]
|
||||
|
||||
|
||||
compile(page, function(html){
|
||||
if(typeof html != 'string'){ return next() }
|
||||
|
||||
res.render('main', {
|
||||
siteMap,
|
||||
title: "notes",
|
||||
topNav: vak.pages,
|
||||
vak: req.params.vak,
|
||||
page: req.params.page,
|
||||
main: html
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function renderVak(req, res, next){
|
||||
if(typeof siteMap[req.params.vak] == 'undefined') return next();
|
||||
vak = siteMap[req.params.vak]
|
||||
|
||||
if(vak.index == null){
|
||||
res.render('main', {
|
||||
siteMap,
|
||||
title: "notes",
|
||||
topNav: vak.pages,
|
||||
vak: req.params.vak,
|
||||
main: `<h1>${vak.title}</h1>`
|
||||
})
|
||||
}else{
|
||||
compile(vak.index, function(html){
|
||||
if(typeof html != 'string'){ return next() }
|
||||
|
||||
res.render('main', {
|
||||
siteMap,
|
||||
title: "notes",
|
||||
topNav: vak.pages,
|
||||
vak: req.params.vak,
|
||||
main: html
|
||||
}, onError404(req, res, next))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function renderIndex(req, res, next){
|
||||
res.render('main', {
|
||||
siteMap,
|
||||
title: "notes",
|
||||
topNav: siteMap,
|
||||
main: "<h1>Hoi</h1>"
|
||||
}, onError404(req, res, next))
|
||||
}
|
||||
|
||||
function render404(req, res, next){
|
||||
res.status(404)
|
||||
res.render('main', {
|
||||
siteMap,
|
||||
title: "notes",
|
||||
topNav: vak.pages,
|
||||
vak: req.params.vak,
|
||||
main: "<h1>404 - Page not found</h1>"
|
||||
}, (err) => {
|
||||
if(err){
|
||||
res.send("<h1>404 - Page not found</h1>")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
app.listen(settings.serverPort, ()=>{console.log("server listening on", settings.serverPort)})
|
||||
149
compile.js
149
compile.js
@ -1,13 +1,22 @@
|
||||
const showdown = require('showdown')
|
||||
const express = require('express')
|
||||
const fs = require('fs')
|
||||
|
||||
const siteMap = require('./siteMap')
|
||||
module.exports = function compile(page, fn){
|
||||
switch(page.type){
|
||||
case 'md':
|
||||
compileMD(page.file, fn)
|
||||
break;
|
||||
default:
|
||||
fn(-2)
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// == compiles ============================
|
||||
// ========================================
|
||||
|
||||
/* =============================================================
|
||||
* === Markdown ================================================
|
||||
* =============================================================
|
||||
*/
|
||||
|
||||
const showdown = require('showdown')
|
||||
const MDCompile = new showdown.Converter({tables: true})
|
||||
|
||||
function compileMD(file, fn){
|
||||
@ -27,131 +36,3 @@ function compileMD(file, fn){
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function compilePDF(file, fn){
|
||||
fs.readFile(file, 'utf-8', function(err, pdf){
|
||||
if(err){
|
||||
console.error("ERROR: could not read file:", file)
|
||||
if(typeof fn == 'function')
|
||||
fn(-1)
|
||||
}
|
||||
return fn("<h1>PDF not supoted yet</h1>")
|
||||
// pdf = MDCompile.makeHtml(markdown);
|
||||
// if(typeof fn == 'function')
|
||||
// fn(html)
|
||||
// else
|
||||
// fs.writeFile(fn, html, function(err){
|
||||
// if(err)
|
||||
// console.error(err)
|
||||
// })
|
||||
})
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// == webserver ===========================
|
||||
// ========================================
|
||||
|
||||
const app = express()
|
||||
app.set('view engine', 'ejs');
|
||||
app.set('views', __dirname + '/views')
|
||||
|
||||
app.use('/img', function(req, res, next){
|
||||
res.sendFile(__dirname + '/img' + req.path)
|
||||
})
|
||||
|
||||
app.use('/:vak/static', renderStatic)
|
||||
app.use('/:vak/:page', renderPage)
|
||||
app.use('/:vak', renderVak)
|
||||
app.use('/', renderIndex)
|
||||
app.use(render404)
|
||||
|
||||
function onError404(req, res, next){
|
||||
return function(err){
|
||||
render404(req, res, next)
|
||||
}
|
||||
}
|
||||
|
||||
function renderStatic(req, res, next){
|
||||
if(typeof siteMap[req.params.vak] == 'undefined') return next();
|
||||
vak = siteMap[req.params.vak]
|
||||
|
||||
dir = __dirname.split('/'); dir.pop() // remove dir of this code
|
||||
dir = `${dir.join('/')}/${vak.title}/notes/static${req.path}`
|
||||
|
||||
res.sendFile(dir, onError404(req, res, next))
|
||||
}
|
||||
|
||||
function renderPage(req, res, next){
|
||||
if(typeof siteMap[req.params.vak] == 'undefined') return next();
|
||||
vak = siteMap[req.params.vak]
|
||||
|
||||
if(typeof vak.pages[req.params.page] == 'undefined') return next();
|
||||
page = vak.pages[req.params.page]
|
||||
|
||||
|
||||
compileMD(page.file, function(html){
|
||||
if(typeof html != 'string'){ return next() }
|
||||
|
||||
res.render('mainV2', {
|
||||
siteMap,
|
||||
title: "notes",
|
||||
topNav: vak.pages,
|
||||
vak: req.params.vak,
|
||||
page: req.params.page,
|
||||
main: html
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function renderVak(req, res, next){
|
||||
if(typeof siteMap[req.params.vak] == 'undefined') return next();
|
||||
vak = siteMap[req.params.vak]
|
||||
|
||||
if(vak.index == null){
|
||||
res.render('mainV2', {
|
||||
siteMap,
|
||||
title: "notes",
|
||||
topNav: vak.pages,
|
||||
vak: req.params.vak,
|
||||
main: `<h1>${vak.title}</h1>`
|
||||
})
|
||||
}else{
|
||||
compileMD(vak.index, function(html){
|
||||
if(typeof html != 'string'){ return next() }
|
||||
|
||||
res.render('mainV2', {
|
||||
siteMap,
|
||||
title: "notes",
|
||||
topNav: vak.pages,
|
||||
vak: req.params.vak,
|
||||
main: html
|
||||
}, onError404(req, res, next))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function renderIndex(req, res, next){
|
||||
res.render('mainV2', {
|
||||
siteMap,
|
||||
title: "notes",
|
||||
topNav: siteMap,
|
||||
main: "<h1>Hoi</h1>"
|
||||
}, onError404(req, res, next))
|
||||
}
|
||||
|
||||
function render404(req, res, next){
|
||||
res.status(404)
|
||||
res.render('mainV2', {
|
||||
siteMap,
|
||||
title: "notes",
|
||||
topNav: vak.pages,
|
||||
vak: req.params.vak,
|
||||
main: "<h1>404 - Page not found</h1>"
|
||||
}, (err) => {
|
||||
if(err){
|
||||
res.send("<h1>404 - Page not found</h1>")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
app.listen(8081, ()=>{console.log("server started")})
|
||||
|
||||
1875
package-lock.json
generated
1875
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
116
siteMap.js
116
siteMap.js
@ -1,85 +1,69 @@
|
||||
const settings = require('./settings')
|
||||
const fs = require('fs')
|
||||
|
||||
function collectVakken(dir, child=null){
|
||||
files = fs.readdirSync(dir)
|
||||
//TODO: catch errors
|
||||
//if(err) { console.error("getVakken: readdir:" + err) }
|
||||
|
||||
siteMap = {};
|
||||
//TODO: make async
|
||||
module.exports = scanDirectory(settings.siteTitle, __dirname + settings.searchDir)
|
||||
|
||||
function scanPages(dir, files){
|
||||
var pages = {}
|
||||
|
||||
for(i=files.length-1; i >= 0; i--){
|
||||
if(files[i] == 'notes') continue;
|
||||
Dfile = files[i]
|
||||
var file, path, urlName, extention, fileName
|
||||
file = files[i] // full file name
|
||||
path = `${dir}/${file}` // full path to file
|
||||
fileName = file.split('.')
|
||||
extention = fileName.pop() // extention
|
||||
fileName = fileName.join('.') // filename without extention
|
||||
urlName = fileName.toLowerCase().replace(/\ /g, '-') // url friendly file name
|
||||
|
||||
//TODO: handel archief
|
||||
|
||||
link = Dfile.toLowerCase().replace(/\ /g, '-')
|
||||
p = colectPages(`${dir}/${Dfile}`, link)
|
||||
|
||||
siteMap[link] = {
|
||||
title: Dfile,
|
||||
url: `/${link}`,
|
||||
index: p.index,
|
||||
pages: p.pages
|
||||
}
|
||||
}
|
||||
return siteMap
|
||||
}
|
||||
|
||||
function colectPages(dir, baseURL){
|
||||
var pages = {index: null, pages: {}}
|
||||
|
||||
try {
|
||||
files = fs.readdirSync(dir + '/notes')
|
||||
} catch (error) {
|
||||
return []
|
||||
}
|
||||
|
||||
for(i=files.length-1; i >= 0; i--){
|
||||
if(files[i] == 'static') continue;
|
||||
|
||||
if(files[i] == 'index.md'){
|
||||
pages[0] = `${dir}/${files[i]}`
|
||||
// skip files called index
|
||||
if(fileName == 'index'){
|
||||
continue
|
||||
}
|
||||
|
||||
file = files[i].split('.')
|
||||
extention = file.pop()
|
||||
fileName = file.join('.')
|
||||
file = files[i]
|
||||
fileDir = `${dir}/notes/${file}`
|
||||
friendlyURL = fileName.toLowerCase().replace(/\ /g, '-')
|
||||
|
||||
type = 'unknown'
|
||||
switch(extention){
|
||||
case 'pdf':
|
||||
case 'md':
|
||||
type = extention
|
||||
break;
|
||||
}
|
||||
|
||||
if(type == 'unknown'){
|
||||
console.warn(`501 - file type not suported (${extention}):`, fileDir)
|
||||
// if directory
|
||||
if(fs.lstatSync(path).isDirectory()){
|
||||
pages[urlName] = scanDirectory(file, path)
|
||||
continue
|
||||
}
|
||||
|
||||
pages.pages[friendlyURL] = {
|
||||
// is file
|
||||
pages[urlName] = {
|
||||
title: fileName,
|
||||
url: `/${baseURL}/${friendlyURL}`,
|
||||
file: fileDir,
|
||||
type: type
|
||||
type: extention,
|
||||
file: path
|
||||
}
|
||||
}
|
||||
return pages
|
||||
}
|
||||
|
||||
function main(){
|
||||
dir = __dirname.split('/')
|
||||
dir.pop()
|
||||
dir = dir.join('/')
|
||||
function scanDirectory(file, path){
|
||||
var index
|
||||
var files = fs.readdirSync(path);
|
||||
|
||||
siteMap = collectVakken(dir)
|
||||
return siteMap
|
||||
if((index = files.indexOf('index.md')) != -1){
|
||||
var fileName, extention
|
||||
fileName = files[index].split('.')
|
||||
extention = fileName.pop()
|
||||
fileName = fileName.join('.')
|
||||
index = {
|
||||
title: fileName,
|
||||
type: extention,
|
||||
file: `${path}/${files[index]}`
|
||||
}
|
||||
}else{
|
||||
index = false
|
||||
}
|
||||
|
||||
return {
|
||||
title: file,
|
||||
type: "directory",
|
||||
file: path,
|
||||
index: (index) ? index : {
|
||||
title: file,
|
||||
type: 'index',
|
||||
file: path
|
||||
},
|
||||
subPages: scanPages(path, files)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = main()
|
||||
|
||||
2
start.sh
2
start.sh
@ -1,2 +1,2 @@
|
||||
cleancss --level 2 -o views/style.min.css views/style.css
|
||||
node compile.js
|
||||
node app.js
|
||||
Reference in New Issue
Block a user