From b810c625c9b7fff0665bacca0690b1707a10579c Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 10 Aug 2022 15:29:45 +0300 Subject: [PATCH] thinking of how to resolve he relative url base issue (see: pwiki2.js) Signed-off-by: Alex A. Naanou --- browser.js | 1 - pwiki/page.js | 20 ++++++++++++++++++-- pwiki/path.js | 39 ++++++++++++++++++++++++++++++++++++--- pwiki2.html | 29 ++++++++++++++++++++++------- pwiki2.js | 43 ++++++++++++++++++++++--------------------- 5 files changed, 98 insertions(+), 34 deletions(-) diff --git a/browser.js b/browser.js index 4cc8ab2..2c690d4 100755 --- a/browser.js +++ b/browser.js @@ -35,7 +35,6 @@ store.update('System', var pwiki = module.pwiki = // XXX - //page.DOMPage('/', '/', store) //page.Page('/', '/', store) page.pWikiPageElement('/', '/', store) diff --git a/pwiki/page.js b/pwiki/page.js index 0e57882..e2edfe7 100755 --- a/pwiki/page.js +++ b/pwiki/page.js @@ -25,7 +25,11 @@ var relProxy = function(name){ var func = function(path='.', ...args){ return this.store[name]( + /* XXX RELATIVE + pwpath.relative(this.location+'/', path), + /*/ pwpath.relative(this.location, path), + //*/ ...args) } Object.defineProperty(func, 'name', {value: name}) return func } @@ -36,7 +40,11 @@ function(name){ strict = path path = '.' } return this.store[name]( + /* XXX RELATIVE + pwpath.relative(this.location+'/', path), + /*/ pwpath.relative(this.location, path), + //*/ strict) } Object.defineProperty(func, 'name', {value: name}) return func } @@ -161,10 +169,10 @@ object.Constructor('BasePage', { // XXX should these be writable??? get name(){ - return pwpath.split(this.path).pop() }, + return pwpath.basename(this.location) }, //set name(value){ }, get dir(){ - return pwpath.relative(this.location, '..') }, + return pwpath.dirname(this.location) }, //set dir(value){ }, get isPattern(){ return this.location.includes('*') }, @@ -215,7 +223,11 @@ object.Constructor('BasePage', { __update__: function(data){ return this.store.update(this.location, data) }, __delete__: function(path='.'){ + /* XXX RELATIVE + return this.store.delete(pwpath.relative(this.location+'/', path)) }, + /*/ return this.store.delete(pwpath.relative(this.location, path)) }, + //*/ // page data... // @@ -292,7 +304,11 @@ object.Constructor('BasePage', { strict = path path = '.' } return this.store.find( + /* XXX RELATIVE + //pwpath.relative(this.location+'/', path), strict) }, + /*/ pwpath.relative(this.location, path), strict) }, + //*/ // // .get([, ]) diff --git a/pwiki/path.js b/pwiki/path.js index d0e1ffe..941dd36 100755 --- a/pwiki/path.js +++ b/pwiki/path.js @@ -98,10 +98,34 @@ module = { .join('/'), 'string') }, basename: function(path){ - return this.split(path).pop() }, + path = this.split(path) + return path.length == 1 ? + path[0] + : (path.at(-1) == '' ? + path.at(-2) + : path.at(-1)) }, dirname: function(path){ - return this.relative(path, '..', 'string') }, + path = this.split(path) + path = path.length == 1 ? + '.' + : path.length == 2 ? + path[0] + : (path.at(-1) == '' ? + path.slice(0, -2) + : path.slice(0, -1)) + .join('/') + return path == '' ? + '/' + : path }, + // XXX BUG? which is more correct?? + // .relative('a/b/c', 'x') + // -> 'a/b/c/x' (current) + // or: + // .relative('a/b/c', 'x') + // -> 'a/b/x' + // ...not sure about this yet... + // XXX REVISE... relative: function(parent, path, format='auto'){ format = format == 'auto' ? (path instanceof Array ? @@ -118,6 +142,13 @@ module = { path = path instanceof Array ? path : path.split(/\s*[\\\/]+\s*/) + // NOTE: relative paths are siblings and not children unless the + // parent is explicitly a directory (i.e. ends in '/')... + /* XXX RELATIVE -- leading @ in path is the same as a trailing / in parent... + path[0] == '@' ? + path.shift() + : parent.pop() + //*/ return this.normalize([...parent, ...path], format) }, // Build alternative paths for page acquisition... @@ -172,7 +203,9 @@ module = { while(base.length > 0){ var p = base.slice() while(p.length > 0){ - var cur = this.relative(p, tpl +'/'+ pg, 'string') + // NOTE: we are adding '' to path here to get things + // relative to it and not relative to basedir... + var cur = this.relative([...p, ''], tpl +'/'+ pg, 'string') if(!seen.has(cur)){ seen.add(cur) yield cur } diff --git a/pwiki2.html b/pwiki2.html index 72abea7..0071d90 100755 --- a/pwiki2.html +++ b/pwiki2.html @@ -98,24 +98,39 @@ require(['./browser'], function(pwiki){ evt.preventDefault() var [path, hash] = location.hash.slice(1).split('#') path = path.trim() == '' ? - '/' + pwiki.path + //'/' : path + // XXX treat links as absolute unless explicitly relative... + path = /^\.\.?([\\\/].*)?$/.test(path) ? + path + : '/'+path + // NOTE: .hash needs to be set before .path, otherwise the path + // handlers will not see it.. + pwiki.hash = hash pwiki.path = path }) pwiki .onNavigate(function(){ // NOTE: we do not need to directly update location.hash here as // that will push an extra history item... - history.replaceState({path: pwiki.path}, pwiki.title, '#'+pwiki.path) + history.replaceState( + {path: this.path}, + this.title, + '#'+this.path + +(this.hash ? + '#'+this.hash + : '')) // NOTE: we are intentionally not awaiting for this -- this // separates the navigate and load events... - pwiki.refresh() }) + this.refresh() }) .onLoad(function(evt){ // handle title... document.querySelector('title').innerHTML = this.title - - // XXX when loaded us .scrollIntoView() to get to hash... - // ...would also be nice to keep hash within location.hash... - }) + // scroll to anchor element... + this.hash + && this.dom + .querySelector('#'+ this.hash) + .scrollIntoView() }) // show current page... pwiki.path = location.hash.slice(1) diff --git a/pwiki2.js b/pwiki2.js index 6af579a..8cd67a4 100755 --- a/pwiki2.js +++ b/pwiki2.js @@ -1,15 +1,23 @@ /********************************************************************** * * -* XXX wikiword filter seems to act up on / -* XXX BUG? /test/wikiword -- produces nested links... -* to reproduce: -* await p.pwiki.get('/test/wikiword').text -* ..as-is, seems not to deal well with tags... -* ...also might be a good idea to try and moke wiki-word generation -* a DOM thing instead of a filter, this seems logical as we might -* need it only within a UI and I do not think I'll be working on -* a non DOM interface (curses, ...) any time soon... +* XXX BUG: .get('/*').raw hangs... +* XXX RELATIVE relative urls are a bit odd... +* Path/to/page opens Moo -> Path/to/Page/Moo +* should be (???): +* Path/to/page opens Moo -> Path/to/Moo +* this boils down to how path.relative(..) works, treating the base +* as a directory always (current) vs. only if '/' is at the end, on +* one hand the current approach is more uniform with less subtle ways +* to make mistakes but on the other hand this may introduce a lot +* of complexity to the user writing links, e.g. how should the +* following be interpreted? +* page: /SomePage +* link: SomeOtherPage +* -> /SomeOtherPage +* -> /SomePage/SomeOtherPage (current) +* the current approach does not seem to be intuitive... +* can this be fixed uniformly across the whole system??? * XXX add action to reset overloaded (bootstrap) pages... * - per page * - global @@ -29,18 +37,11 @@ * - migrate bootstrap * - store topology * - markdown -- DONE?? -* - WikiWord -- DONE-ish -* currently this is broken as it does not know how to deal with HTML -* this can be solved by one of: -* - make this a dom filter and only handle text nodes (as v1-2) -* - add a way to go around tags (as pwiki/parser) -* the first approach looks more promising... -* - dom filters ??? -* does this need to be a pWiki level thing or just a js call/load?? -* ...this can be used to contain all to page-side stuff like: -* - hash handling / navigation -* - editors -* - wikiwords +* - WikiWord -- DONE +* - dom filters -- DONE +* - wikiword -- DONE +* - path2link (wikiword?) -- +* - editor * - configuration * - defaults * - System/config (global)