refactoring + bugfixes...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-05-20 10:56:44 +03:00
parent 12845cbf9e
commit c8d45d3337

110
pwiki2.js
View File

@ -60,23 +60,26 @@ var types = require('ig-types')
var path = var path =
module.path = { module.path = {
// The page returned when getting the '/' path...
ROOT_PAGE: 'WikiHome',
// The page returned when listing a path ending with '/'... // The page returned when listing a path ending with '/'...
// //
// If set to false treat dirs the same as pages (default) // If set to false treat dirs the same as pages (default)
// XXX revise... //INDEX_PAGE: 'index',
//DEFAULT_DIR: 'pages', INDEX_PAGE: false,
DEFAULT_DIR: false,
// The page returned when getting the '/' path...
//
// NOTE: this is the same as .INDEX_PAGE but only for '/'
ROOT_PAGE: 'WikiHome',
ALTERNATIVE_PAGES: [ ALTERNATIVE_PAGES: [
'EmptyPage', 'EmptyPage',
'NotFound', 'NotFound',
], ],
// NOTE: if a path here is relative it is also searched relative to
// the target path.
SEARCH_PATHS: [ SEARCH_PATHS: [
'./Theme/CLI', //'./Theme/CLI',
'./Templates', './Templates',
'/System', '/System',
], ],
@ -122,6 +125,20 @@ module.path = {
('/'+ path.join('/')) ('/'+ path.join('/'))
: path.join('/')) : path.join('/'))
: path }, : path },
split: function(path){
return this.normalize(path, 'array') },
join: function(...parts){
return this.normalize(
(parts[0] instanceof Array ?
parts[0]
: parts)
.join('/'),
'string') },
basename: function(path){
return this.split(path).pop() },
dirname: function(path){
return this.relative(path, '..', 'string') },
relative: function(parent, path, format='auto'){ relative: function(parent, path, format='auto'){
format = format == 'auto' ? format = format == 'auto' ?
(path instanceof Array ? (path instanceof Array ?
@ -140,49 +157,54 @@ module.path = {
: path.split(/\s*[\\\/]+\s*/) : path.split(/\s*[\\\/]+\s*/)
return this.normalize([...parent, ...path], format) }, return this.normalize([...parent, ...path], format) },
paths: function*(path='/'){ // Build alternative paths for page acquisition...
path = this.normalize(path, 'array') //
// handle '', '.', and '/' paths... // NOTE: if seen is given (when called recursively) this will not
if(path.length == 0 // search for .ALTERNATIVE_PAGES...
|| (path.length == 1 && path[0] == '') // XXX should we normalize '' to '/' here???
|| (path.length == 2 && path[0] == '' && path[1] == '')){ paths: function*(path='/', seen){
path = [this.ROOT_PAGE] } var alt_pages = !seen
seen = seen
?? new Set()
path = this.normalize(path, 'string')
// special case: root...
if(path == '/' || path == ''){
// XXX should we normalize '' to '/' here???
//path = '/'
// as-is...
seen.add(path)
yield path
// special case: root page...
if(this.ROOT_PAGE){
yield* this.paths(this.normalize('/'+ this.ROOT_PAGE, 'string'), seen) }}
// NOTE: since path is already normalized we can trust the delimiter...
path = path.split(/\//g)
// normalize relative paths to root... // normalize relative paths to root...
path[0] != '' path[0] != ''
&& path.unshift('') && path.unshift('')
// paths ending in '/' -- dir lister... // paths ending in '/' -- dir lister...
if(path[path.length-1] == ''){ if(path[path.length-1] == ''){
path.pop() path.pop()
this.DEFAULT_DIR this.INDEX_PAGE
&& path.push(this.DEFAULT_DIR) } && path.push(this.INDEX_PAGE) }
// generate path candidates... // search for page...
for(var page of [path.pop(), ...this.ALTERNATIVE_PAGES]){ var page = path.pop()
for(var tpl of ['.', ...this.SEARCH_PATHS]){ for(var tpl of ['.', ...this.SEARCH_PATHS]){
// search for page up the path... // search for page up the path...
var p = path.slice() var p = path.slice()
while(p.length > 0){ while(p.length > 0){
yield this.relative(p, tpl +'/'+ page, 'string') var cur = this.relative(p, tpl +'/'+ page, 'string')
//yield leading_slash ? if(!seen.has(cur)){
// this.relative(p, tpl +'/'+ page, 'string') seen.add(cur)
// : this.relative(p, tpl +'/'+ page, 'string').slice(1) yield cur }
// special case: non-relative template/page path... // special case: non-relative template/page path...
if(tpl[0] == '/'){ if(tpl[0] == '/'){
break } break }
p.pop() } } } }, p.pop() } }
// alternative pages...
split: function(path){ if(alt_pages){
return this.normalize(path, 'array') }, for(var page of [...this.ALTERNATIVE_PAGES]){
join: function(...parts){ yield* this.paths(path.concat(page), seen) }} },
return this.normalize(
(parts[0] instanceof Array ?
parts[0]
: parts)
.join('/'),
'string') },
basename: function(path){
return this.split(path).pop() },
dirname: function(path){
return this.relative(path, '..', 'string') },
} }