From 492f9753aec153d521f28f8c6f2fb3edd9afe921 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 12 May 2022 09:04:33 +0300 Subject: [PATCH] started work on path variables... Signed-off-by: Alex A. Naanou --- pwiki2.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/pwiki2.js b/pwiki2.js index 6a7302a..d644c73 100755 --- a/pwiki2.js +++ b/pwiki2.js @@ -583,8 +583,47 @@ object.Constructor('BasePage', { // root page used to clone new instances via the .clone(..) method... //root: undefined, + // Path variables... + // + // XXX should these be here??? + // other places path variables can be resolved: + // - navigation (below) + // - macro expansion... + // XXX + path_vars: { + NOW: function(){ + return Date.now() }, + PATH: function(){ + return this.path }, + NAME: function(){ + return this.name }, + DIR: function(){ + return this.dir }, + //TITLE: function(){ + // return this.title }, + + /*/ XXX this needs: + // - macro context... + // - sort order... + INDEX: function(context){ + return context.index }, + //*/ + }, + + resolvePathVars: function(path, context={}){ + var that = this + return Object.entries(this.path_vars) + .reduce(function(res, [key, func]){ + return res + .replace( + new RegExp('(\\${'+key+'}|\\$'+key+')', 'g'), + func.call(that, context)) + }, path) }, + // page location... // + // NOTE: path variables are resolved relative to the page BEFORE + // navigation... __location: undefined, get location(){ return this.__location ?? '/' }, @@ -592,9 +631,10 @@ object.Constructor('BasePage', { set location(path){ this.referrer = this.location var cur = this.__location = - module.path.relative( - this.location, - path) + this.resolvePathVars( + module.path.relative( + this.location, + path)) //* XXX HISTORY... if(this.history !== false){ this.history.includes(this.__location) @@ -1631,6 +1671,10 @@ object.Constructor('Page', BasePage, { set text(value){ this.store.update(this.location, {text: value}) }, + /*/ XXX + get index(){}, + set index(value){}, + //*/ })