cleanup and refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-11-29 16:07:12 +03:00
parent def671ffba
commit 2e035d0b4b
4 changed files with 62 additions and 80 deletions

View File

@ -573,8 +573,6 @@ object.Constructor('BasePage', {
this.metadata = this.metadata =
{ order: await this.store.sort(this.path, ...cmp) } { order: await this.store.sort(this.path, ...cmp) }
return this }, return this },
// XXX EXPERIMENTAL -- move this to store???
// .sortAs(<name>) // .sortAs(<name>)
// .sortAs([<path>, .. ]) // .sortAs([<path>, .. ])
sortAs: async function(order){ sortAs: async function(order){
@ -585,7 +583,6 @@ object.Constructor('BasePage', {
return pwpath.sanitize(p) }) } return pwpath.sanitize(p) }) }
: { order: (await this.metadata)['order_'+ order] } : { order: (await this.metadata)['order_'+ order] }
return this }, return this },
// XXX EXPERIMENTAL -- move this to store???
// .saveSortAs(<name>) // .saveSortAs(<name>)
// .saveSortAs(<name>, [<path>, .. ]) // .saveSortAs(<name>, [<path>, .. ])
saveSortAs: async function(name, order=null){ saveSortAs: async function(name, order=null){
@ -593,7 +590,6 @@ object.Constructor('BasePage', {
?? (await this.metadata).order ?? (await this.metadata).order
this.metadata = {['order_'+ name]: order} this.metadata = {['order_'+ name]: order}
return this }, return this },
reverse: async function(){ reverse: async function(){
// not sorting single pages... // not sorting single pages...
if(this.length <= 1){ if(this.length <= 1){
@ -744,7 +740,8 @@ function(spec, func){
// function... // function...
func = args.pop() func = args.pop()
// arg sepc... // 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()) && (func.arg_spec = args.pop())
// XXX do we need anything else like .doc, attrs??? // XXX do we need anything else like .doc, attrs???
return func } return func }

View File

@ -478,7 +478,7 @@ module.BaseParser = {
// error... // error...
}catch(err){ }catch(err){
console.error(err) console.error(err)
yield await page.parse( yield page.parse(
// XXX add line number and page path... // XXX add line number and page path...
'@include("./ParseError' '@include("./ParseError'
+':path=' +':path='
@ -495,8 +495,7 @@ module.BaseParser = {
return '%'+ c.charCodeAt().toString(16) }) return '%'+ c.charCodeAt().toString(16) })
.replace(/:/g, '&colon;') .replace(/:/g, '&colon;')
.replace(/=/g, '&equals;') .replace(/=/g, '&equals;')
+'")') +'")') } },
return } },
// recursively resolve and enumerate the ast... // recursively resolve and enumerate the ast...
// //

View File

@ -38,6 +38,7 @@ var makeDecoder = function(name, encode, chars){
return String.fromCharCode('0x'+c) }) } } return String.fromCharCode('0x'+c) }) } }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Path... // Path...
@ -93,22 +94,6 @@ module = {
quote: makeEncoder('quote', 'UNENCODED_PATH'), quote: makeEncoder('quote', 'UNENCODED_PATH'),
unquote: makeDecoder('unquote', '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... // Path utils...
// //
@ -119,12 +104,6 @@ module = {
// NOTE: trailing/leading '/' are represented by '' at end/start of // NOTE: trailing/leading '/' are represented by '' at end/start of
// path list... // path list...
normalize: function(path='.', format='auto'){ normalize: function(path='.', format='auto'){
/*/ XXX NORMCACHE...
if(typeof(path) == 'string'
&& format != 'array'
&& this._normalized_cache.has(path)){
return path }
//*/
format = format == 'auto' ? format = format == 'auto' ?
(path instanceof Array ? (path instanceof Array ?
'array' 'array'
@ -170,19 +149,6 @@ module = {
&& (path.push( && (path.push(
path.pop() path.pop()
.replace(/:*$/, ''))) .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' ? return format == 'string' ?
// special case: root -> keep '/' // special case: root -> keep '/'
((root ((root
@ -191,7 +157,6 @@ module = {
('/'+ path.join('/')) ('/'+ path.join('/'))
: path.join('/')) : path.join('/'))
: path }, : path },
//*/
sanitize: function(path, format='auto'){ sanitize: function(path, format='auto'){
format = format == 'auto' ? format = format == 'auto' ?
(path instanceof Array ? (path instanceof Array ?

View File

@ -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... // Store...
@ -646,25 +667,6 @@ module.BaseStore = {
//
// .exists(<path>)
// -> <promise>
// -> <normalized-path>
// -> 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(<pattern>, <by>, ..) // .sort(<pattern>, <by>, ..)
// .sort([<path>, ..], <by>, ..) // .sort([<path>, ..], <by>, ..)
@ -827,12 +829,31 @@ module.BaseStore = {
.map(function([p]){ .map(function([p]){
return p }) }, return p }) },
//
// .exists(<path>)
// -> <promise>
// -> <normalized-path>
// -> 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 the closest existing alternative path...
find: async function(path, strict=false){ find: function(path, strict=false){
var {path, args} = pwpath.splitArgs(path) var {path, args} = pwpath.splitArgs(path)
args = pwpath.joinArgs('', args) args = pwpath.joinArgs('', args)
return awaitOrDo(
this.names,
function(names){
// build list of existing page candidates... // build list of existing page candidates...
var names = await this.names
var pages = new Set( var pages = new Set(
pwpath.names(path) pwpath.names(path)
.map(function(name){ .map(function(name){
@ -846,7 +867,7 @@ module.BaseStore = {
p.slice(1) p.slice(1)
: '/'+p : '/'+p
if(pages.has(p)){ if(pages.has(p)){
return p+args } } }, return p+args } } }) },
// //
// Resolve page for path // Resolve page for path
// .match(<path>) // .match(<path>)