making path queries more consistent...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-04-21 11:43:53 +03:00
parent 27088e7a70
commit 6f3a0c75a6

View File

@ -170,9 +170,11 @@ module.store = {
// pattern match * / ** // pattern match * / **
if(path.includes('*') if(path.includes('*')
|| path.includes('**')){ || path.includes('**')){
// NOTE: we are matching full paths only here so leading and
// trainling '/' are optional...
var pattern = new RegExp(`^\\/?${ var pattern = new RegExp(`^\\/?${
module.path.normalize(path, 'string') module.path.normalize(path, 'string')
.replace(/\/$/g, '') .replace(/^\/|\/$/g, '')
.replace(/\//g, '\\/') .replace(/\//g, '\\/')
.replace(/\*\*/g, '.+') .replace(/\*\*/g, '.+')
.replace(/\*/g, '[^\\/]+') }`) .replace(/\*/g, '[^\\/]+') }`)
@ -186,15 +188,18 @@ module.store = {
return res }, new Set())] } return res }, new Set())] }
// search... // search...
for(var p of module.path.paths(path)){ for(var p of module.path.paths(path)){
if(p in this if(p in this){
// NOTE: all paths at this point and in store are return p }
// absolute, so we check both with the leading '/' // NOTE: all paths at this point and in store are absolute,
// and without it to make things a bit more // so we check both with the leading '/' and without
// relaxed... // it to make things a bit more relaxed and return the
|| (p[0] == '/' ? // actual matching path...
p.slice(1) in this if(p[0] == '/'
: ('/'+ p) in store)){ && p.slice(1) in this){
return p } } }, return p.slice(1) }
if(p[0] != '/'
&& ('/'+p) in this){
return '/'+p } } },
// //
// Resolve page // Resolve page
// .get(<path>) // .get(<path>)
@ -213,11 +218,16 @@ module.store = {
// return pages at paths that do not explicitly exist. // return pages at paths that do not explicitly exist.
// //
// XXX should this call actions??? // XXX should this call actions???
// XXX should this return a map for pattern matches???
get: function(path, strict=false){ get: function(path, strict=false){
var that = this var that = this
path = this.match(path, strict) path = this.match(path, strict)
return path instanceof Array ? return path instanceof Array ?
// XXX should we return matched paths???
path.map(function(p){ path.map(function(p){
// NOTE: p can match a non existing page at this point,
// this can be the result of matching a/* in a a/b/c
// and returning a a/b which can be undefined...
return that[p] return that[p]
?? that[that.match(p)] }) ?? that[that.match(p)] })
: this[path] }, : this[path] },
@ -514,7 +524,7 @@ function*(str){
// XXX closure: macros // XXX closure: macros
var expand = var expand =
module.expand = module.expand =
function*(ast){ function*(ast, state={}){
// XXX PAGE... // XXX PAGE...
var page var page
while(true){ while(true){
@ -526,10 +536,12 @@ function*(ast){
yield value yield value
continue } continue }
// macro... // macro...
var {name, args, body, match} = value var {name, args, body} = value
// XXX PAGE... // XXX PAGE...
var res = var res =
macros[name].call(page, args, body, match) // XXX need to hav eaccess to expand(..) in the macro to be
// able to uniformly parse the body...
macros[name].call(page, args, body, state)
?? '' ?? ''
// XXX test if iterable... // XXX test if iterable...
if(res instanceof Array){ if(res instanceof Array){