refactoring + fixed most of the path args issues...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-09-07 16:37:26 +03:00
parent b5e19f6821
commit 6f4e2caa7a
4 changed files with 43 additions and 41 deletions

View File

@ -23,7 +23,7 @@ var markdown = require('./filters/markdown')
var relProxy = var relProxy =
function(name){ function(name){
var func = function(path='.', ...args){ var func = function(path='.:$ARGS', ...args){
return this.store[name]( return this.store[name](
pwpath.relative(this.path, path), pwpath.relative(this.path, path),
...args) } ...args) }
@ -31,10 +31,10 @@ function(name){
return func } return func }
var relMatchProxy = var relMatchProxy =
function(name){ function(name){
var func = function(path='.', strict=this.strict){ var func = function(path='.:$ARGS', strict=this.strict){
if(path === true || path === false){ if(path === true || path === false){
strict = path strict = path
path = '.' } path = '.:$ARGS' }
return this.store[name]( return this.store[name](
pwpath.relative(this.path, path), pwpath.relative(this.path, path),
strict) } strict) }
@ -372,6 +372,7 @@ object.Constructor('BasePage', {
// -> path // -> path
// -> undefined // -> undefined
// //
// XXX ARGS preserve args...
find: function(path='.', strict=false){ find: function(path='.', strict=false){
if(path === true || path === false){ if(path === true || path === false){
strict = path strict = path
@ -403,7 +404,7 @@ object.Constructor('BasePage', {
// we really have to... // we really have to...
path = path ? path = path ?
pwpath.relative(this.path, path) pwpath.relative(this.path, path)
: this.path : this.location
var paths = path.includes('*') var paths = path.includes('*')
// XXX ENERGETIC... // XXX ENERGETIC...
&& !(await this.energetic && !(await this.energetic
@ -844,7 +845,8 @@ object.Constructor('Page', BasePage, {
key = key ?? 'included' } key = key ?? 'included' }
var base = this.get(this.path.split(/\*/).shift()) var base = this.get(this.path.split(/\*/).shift())
var src = args.src var src = args.src
&& await base.parse(args.src, state) //&& await base.parse(args.src, state)
&& this.resolvePathVars(await base.parse(args.src, state))
if(!src){ if(!src){
return } return }
var recursive = args.recursive ?? body var recursive = args.recursive ?? body
@ -875,7 +877,8 @@ object.Constructor('Page', BasePage, {
yield join } yield join }
first = false first = false
var full = page.path //var full = page.path
var full = page.location
// handle recursion... // handle recursion...
var parent_seen = 'seen' in state var parent_seen = 'seen' in state
@ -1266,7 +1269,7 @@ object.Constructor('Page', BasePage, {
'!': Object.assign( '!': Object.assign(
function(){ function(){
return this.get('.', {energetic: true}).raw }, return this.get('.:$ARGS', {energetic: true}).raw },
{energetic: true}), {energetic: true}),
// XXX DEBUG -- remove these... // XXX DEBUG -- remove these...
@ -1315,17 +1318,6 @@ object.Constructor('Page', BasePage, {
state = state ?? {} state = state ?? {}
return this.__parser__.parse(this, text, state) }, return this.__parser__.parse(this, text, state) },
// true if page has an array value but is not a pattern page...
//
// XXX the split into pattern and array pages feels a bit overcomplicated...
// ...can we merge the two and simplify things???
// XXX EXPERIMENTAL
get isArray(){ return (async function(){
return !this.isPattern
// NOTE: we can't only use .data here as it can be a function
// that will return an array...
&& await this.raw instanceof Array }).call(this) },
// raw page text... // raw page text...
// //
// NOTE: writing to .raw is the same as writing to .text... // NOTE: writing to .raw is the same as writing to .text...
@ -1379,7 +1371,7 @@ object.Constructor('Page', BasePage, {
// actions... // actions...
// //
// XXX revise name... // XXX revise name...
asPages: async function*(path='.', strict=false, noexpandactions=false){ asPages: async function*(path='.:$ARGS', strict=false, noexpandactions=false){
// options... // options...
var args = [...arguments] var args = [...arguments]
var opts = typeof(args.at(-1)) == 'object' ? var opts = typeof(args.at(-1)) == 'object' ?
@ -1389,7 +1381,7 @@ object.Constructor('Page', BasePage, {
...opts, ...opts,
path: typeof(args[0]) == 'string' ? path: typeof(args[0]) == 'string' ?
args.shift() args.shift()
: '.', : '.:$ARGS',
strict: args.shift() strict: args.shift()
?? false, ?? false,
} }

View File

@ -315,7 +315,6 @@ module = {
splitArgs: function(path){ splitArgs: function(path){
path = this.normalize(path, 'string') path = this.normalize(path, 'string')
var [path, ...args] = path.split(/(?<!\\):/g) var [path, ...args] = path.split(/(?<!\\):/g)
return { return {
path, path,
args: args.reduce(function(res, arg){ args: args.reduce(function(res, arg){

View File

@ -241,7 +241,8 @@ module.BaseStore = {
/*/ /*/
path = pwpath.normalize(path, 'string') path = pwpath.normalize(path, 'string')
//*/ //*/
return (await this.__exists__(path)) var {path, args} = pwpath.splitArgs(path)
var res = (await this.__exists__(path))
// NOTE: all paths at this point and in store are // NOTE: all paths at this point and in store are
// absolute, so we check both with the leading // absolute, so we check both with the leading
// '/' and without it to make things a bit more // '/' and without it to make things a bit more
@ -266,10 +267,15 @@ module.BaseStore = {
: ('/'+ path)))) : ('/'+ path))))
//*/ //*/
// normalize the output... // normalize the output...
|| false }, || false
if(!res){
return false }
return pwpath.joinArgs(res, args) },
// find the closest existing alternative path... // find the closest existing alternative path...
// XXX CACHED.... // XXX CACHED....
find: async function(path, strict=false){ find: async function(path, strict=false){
var {path, args} = pwpath.splitArgs(path)
args = pwpath.joinArgs('', args)
// build list of existing page candidates... // build list of existing page candidates...
var names = await this.names() var names = await this.names()
var pages = new Set( var pages = new Set(
@ -280,12 +286,12 @@ module.BaseStore = {
// select accessible candidate... // select accessible candidate...
for(var p of pwpath.paths(path, !!strict)){ for(var p of pwpath.paths(path, !!strict)){
if(pages.has(p)){ if(pages.has(p)){
return p } return p+args }
p = p[0] == '/' ? p = p[0] == '/' ?
p.slice(1) p.slice(1)
: '/'+p : '/'+p
if(pages.has(p)){ if(pages.has(p)){
return p } } }, return p+args } } },
/*/ /*/
find: async function(path, strict=false){ find: async function(path, strict=false){
for(var p of pwpath.paths(path, !!strict)){ for(var p of pwpath.paths(path, !!strict)){
@ -317,12 +323,14 @@ module.BaseStore = {
if(path.includes('*') if(path.includes('*')
|| path.includes('**')){ || path.includes('**')){
var order = (this.metadata(path) ?? {}).order || [] var order = (this.metadata(path) ?? {}).order || []
var {path, args} = pwpath.splitArgs(path)
args = pwpath.joinArgs('', args)
// NOTE: we are matching full paths only here so leading and // NOTE: we are matching full paths only here so leading and
// trainling '/' are optional... // trainling '/' are optional...
// NOTE: we ensure that we match full names and always split // NOTE: we ensure that we match full names and always split
// at '/' only... // at '/' only...
var pattern = new RegExp(`^\\/?${ var pattern = new RegExp(`^\\/?${
pwpath.normalize(path, 'string') path
.replace(/^\/|\/$/g, '') .replace(/^\/|\/$/g, '')
.replace(/\//g, '\\/') .replace(/\//g, '\\/')
.replace(/\*\*/g, '.*') .replace(/\*\*/g, '.*')
@ -353,7 +361,9 @@ module.BaseStore = {
m[0].slice(1) m[0].slice(1)
: m[0]) : m[0])
return res }, new Set())] return res }, new Set())]
.sortAs(order) } .sortAs(order)
.map(function(p){
return p+args })}
// direct search... // direct search...
return this.find(path, strict) }, return this.find(path, strict) },
// //
@ -378,8 +388,9 @@ module.BaseStore = {
// pattern match * / ** // pattern match * / **
if(path.includes('*') if(path.includes('*')
|| path.includes('**')){ || path.includes('**')){
path = pwpath.split(path) var p = pwpath.splitArgs(path)
var p = path.slice() var args = pwpath.joinArgs('', p.args)
p = pwpath.split(p.path)
var tail = [] var tail = []
while(!p.at(-1).includes('*')){ while(!p.at(-1).includes('*')){
tail.unshift(p.pop()) } tail.unshift(p.pop()) }
@ -387,7 +398,7 @@ module.BaseStore = {
if(tail.length > 0){ if(tail.length > 0){
return (await this.match(p.join('/'), strict)) return (await this.match(p.join('/'), strict))
.map(function(p){ .map(function(p){
return pwpath.join(p, tail) }) } } return pwpath.join(p, tail) + args }) } }
// direct... // direct...
return this.match(path, strict) }, return this.match(path, strict) },
// //
@ -419,6 +430,7 @@ module.BaseStore = {
// XXX SANITIZE... // XXX SANITIZE...
path = pwpath.sanitize(path, 'string') path = pwpath.sanitize(path, 'string')
//*/ //*/
var path = pwpath.splitArgs(path).path
path = path.includes('*') path = path.includes('*')
&& (energetic == true ? && (energetic == true ?
await this.find(path) await this.find(path)
@ -461,6 +473,7 @@ module.BaseStore = {
// NOTE: setting/removing metadata is done via .update(..) / .delete(..) // NOTE: setting/removing metadata is done via .update(..) / .delete(..)
// NOTE: this uses .__get__(..) internally... // NOTE: this uses .__get__(..) internally...
metadata: async function(path, ...args){ metadata: async function(path, ...args){
path = pwpath.splitArgs(path).path
// set... // set...
if(args.length > 0){ if(args.length > 0){
return this.update(path, ...args) } return this.update(path, ...args) }
@ -491,6 +504,7 @@ module.BaseStore = {
/*/ /*/
|| pwpath.normalize(path, 'string') || pwpath.normalize(path, 'string')
//*/ //*/
path = pwpath.splitArgs(path).path
data = data instanceof Promise ? data = data instanceof Promise ?
await data await data
: data : data
@ -517,6 +531,7 @@ module.BaseStore = {
// read-only... // read-only...
if(this.__delete__ == null){ if(this.__delete__ == null){
return this } return this }
path = pwpath.splitArgs(path).path
path = await this.exists(path) path = await this.exists(path)
if(path){ if(path){
await this.__delete__(path) await this.__delete__(path)

View File

@ -19,6 +19,7 @@
* examples: * examples:
* // NOTE: .text is redered via _view and thus is on a different * // NOTE: .text is redered via _view and thus is on a different
* // level to .raw... * // level to .raw...
* // XXX should renderer.args be the same in .text and .parse(..)???
* // XXX for .text and .parse(..), the action is called twice... * // XXX for .text and .parse(..), the action is called twice...
* // root path: /System/testAction:a:b:c * // root path: /System/testAction:a:b:c
* await pwiki.get('/path/testDirect:x:y:z').raw * await pwiki.get('/path/testDirect:x:y:z').raw
@ -29,28 +30,23 @@
* .args - x y z * .args - x y z
* .renderer.args - a b c * .renderer.args - a b c
* .root.args - a b c * .root.args - a b c
* await pwiki.get('/path/testDirect:x:y:z').parse('@include(.)') * await pwiki.get('/path/testDirect:x:y:z').parse('@include(.:$ARGS)')
* .args - empty * .args - x y z
* .renderer.args - a b c * .renderer.args - a b c
* .root.args - a b c * .root.args - a b c
* XXX losing args on this path...
* XXX triggered twice... * XXX triggered twice...
* await pwiki.get('/path/testAction:x:y:z').parse('@include(.)') * await pwiki.get('/path/testAction:x:y:z').parse('@include(.:$ARGS)')
* .args - empty * .args - x y z
* .renderer.args - a b c * .renderer.args - a b c
* .root.args - a b c * .root.args - a b c
* XXX losing args on this path...
* XXX triggered twice... * XXX triggered twice...
* XXX .text uses .parse(..) internally...
* await pwiki.get('/path/testDirect:x:y:z').text * await pwiki.get('/path/testDirect:x:y:z').text
* .args - empty * .args - x y z
* .renderer.args - x y z * .renderer.args - x y z
* .root.args - a b c * .root.args - a b c
* XXX args should be local...
* ... @include(.:$ARGS) not working???
* XXX triggered twice... * XXX triggered twice...
* await pwiki.get('/path/testAction:x:y:z').text * await pwiki.get('/path/testAction:x:y:z').text
* .args - empty * .args - x y z
* .renderer.args - x y z * .renderer.args - x y z
* .root.args - a b c * .root.args - a b c
* XXX triggered twice... * XXX triggered twice...