forked from Deuxfleurs/site
Improve render.js code
This commit is contained in:
parent
f3ea5086ed
commit
ef35a40059
4 changed files with 32 additions and 17 deletions
30
render.js
30
render.js
|
@ -4,7 +4,11 @@ const pug = require('pug')
|
|||
const marked = require('marked')
|
||||
const fs = require('fs').promises
|
||||
|
||||
const unit = (...args) => null
|
||||
const log = process.env.VERBOSE ? console.log : unit
|
||||
|
||||
const walk = async (path, filename) => {
|
||||
log('[walk]', path)
|
||||
const type = await fs.lstat(path)
|
||||
if (type.isFile()) return {type: 'file', path: path, name: filename || path}
|
||||
if (!type.isDirectory()) return null
|
||||
|
@ -27,6 +31,7 @@ const suffixl = (...l) => file => l.find(suffix(file))
|
|||
const is_static = suffixl(...ext_static)
|
||||
const is_md = suffixl(...ext_md)
|
||||
const is_pug = suffixl(...ext_pug)
|
||||
const is_templated = f => is_md(f) /* || is_rst(f) */
|
||||
|
||||
const prefix = file => ext => file.substring(0, ext.length) == ext ? ext : null
|
||||
const prefixl = (...l) => file => l.find(prefix(file))
|
||||
|
@ -35,8 +40,9 @@ const rm_prefix = (...l) => file => file.substring(prefixl(...l)(file).length)
|
|||
const rm_suffix = (...l) => file => file.substring(0, file.length - suffixl(...l)(file).length)
|
||||
|
||||
const propagate_md_layout = (tree, markdown_template) => {
|
||||
if (tree.type == 'file' && is_md(tree.name)) {
|
||||
if (tree.type == 'file' && is_templated(tree.name)) {
|
||||
tree.template = markdown_template
|
||||
log('[propagate_md_layout]', tree ? tree.path : null, markdown_template ? markdown_template.path : null)
|
||||
} else if (tree.type == 'folder') {
|
||||
const find_md_tpl = tree.children.filter(c => c.type == 'file' && c.name == '_markdown.pug')
|
||||
const new_md_tpl = find_md_tpl.length > 0 ? find_md_tpl[0] : markdown_template
|
||||
|
@ -45,11 +51,12 @@ const propagate_md_layout = (tree, markdown_template) => {
|
|||
return tree
|
||||
}
|
||||
|
||||
const elagate_templates = tree => {
|
||||
const elagate = tree => {
|
||||
if (tree.type != 'folder') return tree
|
||||
|
||||
tree.children = tree.children.filter(e => !(e.type == 'file' && e.name[0] == '_'))
|
||||
tree.children.forEach(elagate_templates)
|
||||
const lh = e => log('[elegate]', e.path) && false
|
||||
tree.children = tree.children.filter(e => !(e.name[0] == '_') || lh(e))
|
||||
tree.children.forEach(elagate)
|
||||
return tree
|
||||
}
|
||||
|
||||
|
@ -60,6 +67,7 @@ const propagate_nice_name = prefix => tree => {
|
|||
tree.nice_path = splitted.slice(0, -1)
|
||||
tree.nice_name = splitted[splitted.length - 1].split('.')[0]
|
||||
tree.url = without_prefix
|
||||
log('[propagate_nice_name]', [...tree.nice_path, tree.nice_name].join('|'))
|
||||
}
|
||||
|
||||
if (tree.type == 'folder') tree.children.forEach(propagate_nice_name(prefix))
|
||||
|
@ -73,6 +81,7 @@ const prepare_copy = (old_prefix, new_prefix, exts) => tree => {
|
|||
src: tree.path,
|
||||
out: new_prefix + rm_prefix(old_prefix)(tree.path)
|
||||
}
|
||||
log('[prepare_copy]',tree.generate.src,'->',tree.generate.out)
|
||||
} else if (tree.type == 'folder') {
|
||||
tree.children.forEach(prepare_copy(old_prefix, new_prefix, exts))
|
||||
}
|
||||
|
@ -86,6 +95,7 @@ const prepare_pug = (old_prefix, new_prefix) => tree => {
|
|||
src: tree.path,
|
||||
out: new_prefix + rm_prefix(old_prefix)(rm_suffix(...ext_pug)(tree.path)) + '.html'
|
||||
}
|
||||
log('[prepare_pug]',tree.generate.src,'->',tree.generate.out)
|
||||
}
|
||||
else if (tree.type == 'folder') {
|
||||
tree.children.forEach(prepare_pug(old_prefix, new_prefix))
|
||||
|
@ -102,6 +112,7 @@ const prepare_md = (old_prefix, new_prefix) => tree => {
|
|||
markdown: tree.path,
|
||||
out: new_prefix + rm_prefix(old_prefix)(rm_suffix(...ext_md)(tree.path)) + '.html'
|
||||
}
|
||||
log('[prepare_md]',tree.generate.markdown,'+',tree.generate.src,'->',tree.generate.out)
|
||||
}
|
||||
else if (tree.type == 'folder') {
|
||||
tree.children.forEach(prepare_md(old_prefix, new_prefix))
|
||||
|
@ -116,6 +127,7 @@ const prepare_folder = (old_prefix, new_prefix) => tree => {
|
|||
cmd: 'mkdir',
|
||||
out: new_prefix + rm_prefix(old_prefix)(tree.path)
|
||||
}
|
||||
log('[prepare_folder]',tree.generate.out)
|
||||
tree.children.forEach(prepare_folder(old_prefix, new_prefix))
|
||||
}
|
||||
|
||||
|
@ -125,14 +137,16 @@ const prepare_folder = (old_prefix, new_prefix) => tree => {
|
|||
const do_folder = async tree => {
|
||||
if (!tree.generate || tree.generate.cmd != 'mkdir') return tree
|
||||
await fs.mkdir(tree.generate.out, { recursive: true })
|
||||
log('[do_folder]',tree.generate.out)
|
||||
await Promise.all(tree.children.map(do_folder))
|
||||
return tree
|
||||
}
|
||||
|
||||
const do_copy = async tree => {
|
||||
if (tree.generate && tree.generate.cmd == 'copy')
|
||||
if (tree.generate && tree.generate.cmd == 'copy') {
|
||||
await fs.copyFile(tree.generate.src, tree.generate.out)
|
||||
else if (tree.type == 'folder')
|
||||
log('[do_copy]',tree.generate.out)
|
||||
} else if (tree.type == 'folder')
|
||||
await Promise.all(tree.children.map(do_copy))
|
||||
|
||||
return tree
|
||||
|
@ -149,6 +163,7 @@ const do_pug = (prt, root) => async tree => {
|
|||
element: tree
|
||||
})
|
||||
await fs.writeFile(tree.generate.out, html)
|
||||
log('[do_pug]',tree.generate.out)
|
||||
} else if (tree.type == 'folder')
|
||||
await Promise.all(tree.children.map(do_pug(tree,root)))
|
||||
|
||||
|
@ -159,7 +174,7 @@ const do_pug = (prt, root) => async tree => {
|
|||
const conf = { src: './src', dest: './static'}
|
||||
walk(conf.src)
|
||||
.then(propagate_md_layout)
|
||||
.then(elagate_templates)
|
||||
.then(elagate)
|
||||
.then(propagate_nice_name(conf.src))
|
||||
.then(prepare_copy(conf.src, conf.dest))
|
||||
.then(prepare_pug(conf.src, conf.dest))
|
||||
|
@ -168,5 +183,4 @@ walk(conf.src)
|
|||
.then(do_folder)
|
||||
.then(do_copy)
|
||||
.then(do_pug())
|
||||
.then(v => console.log("done"))
|
||||
.catch(console.error)
|
||||
|
|
|
@ -3,14 +3,6 @@ extends ../_layout.pug
|
|||
prepend root
|
||||
- title = element.nice_path[element.nice_path.length - 1]
|
||||
|
||||
mixin menu(o)
|
||||
ul
|
||||
each val in o.children
|
||||
- if (val.type == 'folder')
|
||||
li
|
||||
a(href=val.url)= val.nice_name
|
||||
+menu(val)
|
||||
|
||||
block content
|
||||
.container.spacing
|
||||
nav
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
include _mixin/menu.pug
|
||||
|
||||
block root
|
||||
doctype html
|
||||
head
|
||||
|
|
7
src/_mixin/menu.pug
Normal file
7
src/_mixin/menu.pug
Normal file
|
@ -0,0 +1,7 @@
|
|||
mixin menu(o)
|
||||
ul
|
||||
each val in o.children
|
||||
- if (val.type == 'folder')
|
||||
li
|
||||
a(href=val.url)= val.nice_name
|
||||
+menu(val)
|
Loading…
Reference in a new issue