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 =
function(name){
var func = function(path='.', ...args){
var func = function(path='.:$ARGS', ...args){
return this.store[name](
pwpath.relative(this.path, path),
...args) }
@ -31,10 +31,10 @@ function(name){
return func }
var relMatchProxy =
function(name){
var func = function(path='.', strict=this.strict){
var func = function(path='.:$ARGS', strict=this.strict){
if(path === true || path === false){
strict = path
path = '.' }
path = '.:$ARGS' }
return this.store[name](
pwpath.relative(this.path, path),
strict) }
@ -372,6 +372,7 @@ object.Constructor('BasePage', {
// -> path
// -> undefined
//
// XXX ARGS preserve args...
find: function(path='.', strict=false){
if(path === true || path === false){
strict = path
@ -403,7 +404,7 @@ object.Constructor('BasePage', {
// we really have to...
path = path ?
pwpath.relative(this.path, path)
: this.path
: this.location
var paths = path.includes('*')
// XXX ENERGETIC...
&& !(await this.energetic
@ -844,7 +845,8 @@ object.Constructor('Page', BasePage, {
key = key ?? 'included' }
var base = this.get(this.path.split(/\*/).shift())
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){
return }
var recursive = args.recursive ?? body
@ -875,7 +877,8 @@ object.Constructor('Page', BasePage, {
yield join }
first = false
var full = page.path
//var full = page.path
var full = page.location
// handle recursion...
var parent_seen = 'seen' in state
@ -1266,7 +1269,7 @@ object.Constructor('Page', BasePage, {
'!': Object.assign(
function(){
return this.get('.', {energetic: true}).raw },
return this.get('.:$ARGS', {energetic: true}).raw },
{energetic: true}),
// XXX DEBUG -- remove these...
@ -1315,17 +1318,6 @@ object.Constructor('Page', BasePage, {
state = 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...
//
// NOTE: writing to .raw is the same as writing to .text...
@ -1379,7 +1371,7 @@ object.Constructor('Page', BasePage, {
// actions...
//
// XXX revise name...
asPages: async function*(path='.', strict=false, noexpandactions=false){
asPages: async function*(path='.:$ARGS', strict=false, noexpandactions=false){
// options...
var args = [...arguments]
var opts = typeof(args.at(-1)) == 'object' ?
@ -1389,7 +1381,7 @@ object.Constructor('Page', BasePage, {
...opts,
path: typeof(args[0]) == 'string' ?
args.shift()
: '.',
: '.:$ARGS',
strict: args.shift()
?? false,
}

View File

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

View File

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

View File

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