diff --git a/pwiki/page.js b/pwiki/page.js index 39979d2..ea4c97b 100755 --- a/pwiki/page.js +++ b/pwiki/page.js @@ -573,8 +573,6 @@ object.Constructor('BasePage', { this.metadata = { order: await this.store.sort(this.path, ...cmp) } return this }, - - // XXX EXPERIMENTAL -- move this to store??? // .sortAs() // .sortAs([, .. ]) sortAs: async function(order){ @@ -585,7 +583,6 @@ object.Constructor('BasePage', { return pwpath.sanitize(p) }) } : { order: (await this.metadata)['order_'+ order] } return this }, - // XXX EXPERIMENTAL -- move this to store??? // .saveSortAs() // .saveSortAs(, [, .. ]) saveSortAs: async function(name, order=null){ @@ -593,7 +590,6 @@ object.Constructor('BasePage', { ?? (await this.metadata).order this.metadata = {['order_'+ name]: order} return this }, - reverse: async function(){ // not sorting single pages... if(this.length <= 1){ @@ -744,7 +740,8 @@ function(spec, func){ // function... func = args.pop() // arg sepc... - ;(args.length > 0 && args[args.length-1] instanceof Array) + ;(args.length > 0 + && args[args.length-1] instanceof Array) && (func.arg_spec = args.pop()) // XXX do we need anything else like .doc, attrs??? return func } diff --git a/pwiki/parser.js b/pwiki/parser.js index 397c89e..fd948a0 100755 --- a/pwiki/parser.js +++ b/pwiki/parser.js @@ -478,7 +478,7 @@ module.BaseParser = { // error... }catch(err){ console.error(err) - yield await page.parse( + yield page.parse( // XXX add line number and page path... '@include("./ParseError' +':path=' @@ -495,8 +495,7 @@ module.BaseParser = { return '%'+ c.charCodeAt().toString(16) }) .replace(/:/g, ':') .replace(/=/g, '=') - +'")') - return } }, + +'")') } }, // recursively resolve and enumerate the ast... // diff --git a/pwiki/path.js b/pwiki/path.js index bd11364..fa3fd9b 100755 --- a/pwiki/path.js +++ b/pwiki/path.js @@ -38,6 +38,7 @@ var makeDecoder = function(name, encode, chars){ return String.fromCharCode('0x'+c) }) } } + //--------------------------------------------------------------------- // Path... @@ -93,22 +94,6 @@ module = { quote: makeEncoder('quote', 'UNENCODED_PATH'), unquote: makeDecoder('unquote', 'quote', 'UNENCODED_PATH'), - /*/ XXX NORMCACHE... - __normalized_cache_threshold: 100, - __normalized_cache_size: 4096, - __normalized_cache: undefined, - get _normalized_cache(){ - var norm = this.__normalized = - this.__normalized - ?? new Set() - // trim to size... - var l = norm.size - var lim = this.__normalized_cache_size ?? 1000 - var t = this.__normalized_cache_threshold ?? 100 - if(l > lim){ - norm = this.__normalized = new Set([...norm].slice(Math.max(l - lim - t, t))) } - return norm }, - //*/ // Path utils... // @@ -119,12 +104,6 @@ module = { // NOTE: trailing/leading '/' are represented by '' at end/start of // path list... normalize: function(path='.', format='auto'){ - /*/ XXX NORMCACHE... - if(typeof(path) == 'string' - && format != 'array' - && this._normalized_cache.has(path)){ - return path } - //*/ format = format == 'auto' ? (path instanceof Array ? 'array' @@ -170,19 +149,6 @@ module = { && (path.push( path.pop() .replace(/:*$/, ''))) - /*/ XXX NORMCACHE... - var res = format == 'string' ? - // special case: root -> keep '/' - ((root - && path.length == 1 - && path[0] == '') ? - ('/'+ path.join('/')) - : path.join('/')) - : path - typeof(res) == 'string' - && this._normalized_cache.add(res) - return res }, - /*/ return format == 'string' ? // special case: root -> keep '/' ((root @@ -191,7 +157,6 @@ module = { ('/'+ path.join('/')) : path.join('/')) : path }, - //*/ sanitize: function(path, format='auto'){ format = format == 'auto' ? (path instanceof Array ? diff --git a/pwiki/store/base.js b/pwiki/store/base.js index 76329f4..385c393 100755 --- a/pwiki/store/base.js +++ b/pwiki/store/base.js @@ -158,6 +158,27 @@ object.Constructor('JournalDB', { }) +var awaitOrDo = +module.awaitOrDo = +function(data, func){ + if(arguments.length > 2){ + data = [...arguments] + func = data.pop() + var promise = data + .reduce(function(res, e){ + return res + || e instanceof Promise }, false) + return promise ? + Promise.all(data) + .then(function(res){ + return func(...res) }) + : func(...data) + // single data item... + } else { + return data instanceof Promise ? + data.then(func) + : func(data) } } + //--------------------------------------------------------------------- // Store... @@ -646,25 +667,6 @@ module.BaseStore = { - // - // .exists() - // -> - // -> - // -> false - // - exists: function(path){ - var {path, args} = - pwpath.splitArgs( - pwpath.sanitize(path, 'string')) - var test = function(paths){ - return paths.includes(path) ? - pwpath.joinArgs(path, args) - : undefined } - var paths = this.paths - return paths instanceof Promise ? - paths.then(test) - : test(paths) }, - // // .sort(, , ..) // .sort([, ..], , ..) @@ -827,26 +829,45 @@ module.BaseStore = { .map(function([p]){ return p }) }, + // + // .exists() + // -> + // -> + // -> false + // + exists: function(path){ + var {path, args} = + pwpath.splitArgs( + pwpath.sanitize(path, 'string')) + return awaitOrDo( + this.paths, + function(paths){ + return paths.includes(path) ? + pwpath.joinArgs(path, args) + : undefined }) }, + // find the closest existing alternative path... - find: async function(path, strict=false){ + find: 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( - pwpath.names(path) - .map(function(name){ - return names[name] ?? [] }) - .flat()) - // select accessible candidate... - for(var p of pwpath.paths(path, !!strict)){ - if(pages.has(p)){ - return p+args } - p = p[0] == '/' ? - p.slice(1) - : '/'+p - if(pages.has(p)){ - return p+args } } }, + return awaitOrDo( + this.names, + function(names){ + // build list of existing page candidates... + var pages = new Set( + pwpath.names(path) + .map(function(name){ + return names[name] ?? [] }) + .flat()) + // select accessible candidate... + for(var p of pwpath.paths(path, !!strict)){ + if(pages.has(p)){ + return p+args } + p = p[0] == '/' ? + p.slice(1) + : '/'+p + if(pages.has(p)){ + return p+args } } }) }, // // Resolve page for path // .match()