From 9a0b8d1ad2e0a77a8bf9bdc072223f8870dbdefd Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sat, 5 Nov 2022 01:49:39 +0300 Subject: [PATCH] reworked count handling by @macro(..), still tweaking and fixing... Signed-off-by: Alex A. Naanou --- pwiki/page.js | 100 +++++++++++++++++++++++++++++++++++++++++++----- pwiki/parser.js | 9 ++++- pwiki2.js | 11 +++++- 3 files changed, 108 insertions(+), 12 deletions(-) diff --git a/pwiki/page.js b/pwiki/page.js index 6544a64..0436055 100755 --- a/pwiki/page.js +++ b/pwiki/page.js @@ -1299,29 +1299,70 @@ object.Constructor('Page', BasePage, { 'content': ['slot'], // XXX EXPERIMENTAL... + // XXX INC_DEC do we need inc/dec and parent??? 'var': Macro( ['name', 'text', + // XXX INC_DEC + ['shown', 'hidden', 'inc', 'dec', 'parent']], + /*/ ['shown', 'hidden']], + //*/ async function(args, body, state){ var name = args.name if(!name){ return '' } name = await this.parse(name, state) + // XXX INC_DEC + var inc = args.inc + var dec = args.dec + //*/ var text = args.text ?? body + // NOTE: .hidden has priority... + var show = + ('hidden' in args ? + !args.hidden + : undefined) + ?? args.shown + var vars = state.vars = state.vars ?? {} + // XXX INC_DEC + if(args.parent && name in vars){ + while(!vars.hasOwnProperty(name) + && vars.__proto__ !== Object.prototype){ + vars = vars.__proto__ } } + + // inc/dec... + if(inc || dec){ + if(!(name in vars) + || isNaN(parseInt(vars[name]))){ + return '' } + var cur = parseInt(vars[name]) + cur += + inc === true ? + 1 + : !inc ? + 0 + : parseInt(inc) + cur -= + dec === true ? + 1 + : !dec ? + 0 + : parseInt(dec) + vars[name] = cur + '' + return show ?? true ? + vars[name] + : '' } + //*/ // set... if(text){ text = vars[name] = await this.parse(text, state) - var show = args.shown - || ('hidden' in args - && !args.hidden) - || false - return show ? + return show ?? false ? text : '' // get... @@ -1359,9 +1400,15 @@ object.Constructor('Page', BasePage, { // // // - // XXX SORT sorting not implemented yet.... + // NOTE: this handles src count argument internally partially + // overriding .match(..)'s implementation, this is done + // because @macro(..) needs to account for arbitrary nesting + // that .match(..) can not know about... + // XXX should we do the same for offset??? + // + // XXX SORT sorting not implemented yet... macro: Macro( - ['name', 'src', 'sort', 'text', 'join', 'else', + ['name', 'src', 'sort', 'text', 'join', 'else', ['strict', 'isolated', 'inheritmacros', 'inheritvars' ]], async function*(args, body, state){ var that = this @@ -1504,9 +1551,36 @@ object.Constructor('Page', BasePage, { 'no' : value } + // XXX COUNT + // handle count... + // NOTE: this duplicates .match(..)'s functionality + // because we need to account for arbitrary macro + // nesting that .match(..) does not know about... + // XXX we still end up with NaN in some cases... + var count = pwpath.splitArgs(src).args.count + if(count == 'inherit' + && !('macro:count' in vars)){ + vars['macro:count'] = + vars['macro:count'] + ?? parseInt(this.args.count) } + if(count + && count != 'inherit'){ + vars['macro:count'] = parseInt(count) } + //*/ + // expand matches... var first = true for await(var page of match.asPages(strict)){ + // XXX COUNT + // handle count... + if(vars['macro:count'] <= 0){ + break } + if('macro:count' in vars){ + var v = vars + while(!v.hasOwnProperty('macro:count')){ + v = v.__proto__ } + v['macro:count']-- } + //*/ if(join && !first){ yield join } first = false @@ -1528,6 +1602,9 @@ object.Constructor('Page', BasePage, { text, _state), _state) } else { yield this.__parser__.expand(page, text, state) } } + // XXX COUNT + // cleanup... + delete vars['macro:count'] // else... if(first && (text || args['else'])){ @@ -2226,11 +2303,16 @@ module.System = { // ...for this we'll need to be able to either: // - count our own pages or // - keep a global count + // ...with offset the issue is not solvable because we will not + // see/count the children of skipped nodes -- the only way to + // solve this is to completely handle offset in macro... tree: { text: object.doc` + - + + @var(path "@source(s ./path)")
@@ -2241,7 +2323,7 @@ module.System = { >×
- @macro(tree "./*:$ARGS") + @macro(tree "./*:$ARGS:count=inherit")
` }, diff --git a/pwiki/parser.js b/pwiki/parser.js index e094a0e..d9e3efb 100755 --- a/pwiki/parser.js +++ b/pwiki/parser.js @@ -35,7 +35,7 @@ module.BaseParser = { // MACRO_ARGS: ['(\\s*(',[ // arg='val' | arg="val" | arg=val - '(?[a-z:-]+)\\s*=\\s*(?'+([ + '(?[a-z:-_]+)\\s*=\\s*(?'+([ // XXX CHROME/NODE BUG: this does not work yet... //'\\s+(?[\'"])[^\\k]*\\k', '"(?(\\"|[^"])*?)"', @@ -539,6 +539,13 @@ module.BaseParser = { return await this.resolve(page, ast, state) // filters... .map(function(section){ + // normalize types... + section = + typeof(section) == 'number' ? + section + '' + : section == null ? + '' + : section return ( // expand section... typeof(section) != 'string' ? diff --git a/pwiki2.js b/pwiki2.js index a4d827a..3d511e4 100755 --- a/pwiki2.js +++ b/pwiki2.js @@ -25,8 +25,15 @@ * - * * -* XXX macro-vars: should the vars be defined as macro: (current) or -* simply as ??? +* XXX not yet sure why /tree's root macro needs to have count=inherit... +* for an example see: +* /Tests/MacroPageCountInheritTest:count=3 +* ...adding :count=inherit after $ARGS fixes the issue... +* not sure why this works in this context: +* /Tests/MacroCountTest +* XXX should @macro(..) handle offset in the same manner as count??? +* XXX BUG: @var(macro:count) can still be NaN in some cases... +* XXX BUG: count does not appear to affext /Test/list/generator and /Test/list/static... * XXX macros: should vars and url args be unified??? * ...likely no but need tho think about it some more... * XXX should @quote(..)'s expandactions be on by default???