From cedd1bbb79ec6a537e4692435b144fe27607bfed Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Mon, 31 Oct 2022 03:01:10 +0300 Subject: [PATCH] pushed @defmacro(..) to the rear of the todo/ideas stack -- requires to much rework... Signed-off-by: Alex A. Naanou --- pwiki/page.js | 24 ++++++++++++++++++++++++ pwiki/parser.js | 5 +++-- pwiki2.js | 12 ++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/pwiki/page.js b/pwiki/page.js index a2cbe5c..e86f2be 100755 --- a/pwiki/page.js +++ b/pwiki/page.js @@ -1460,6 +1460,30 @@ object.Constructor('Page', BasePage, { if(else_block){ yield this.__parser__.expand(this, else_block, state) } } } }), + /* XXX this is not possible with the current parser as we statically + // define the macro name list (regexp) for the lexer... + // ...so to make this happen we'd need to: + // - update the patterns for parser.lex(..) + // - tweak parser.group(..) + // Like @macro(..) but requires the name argument and will define + // the macros in state.usermacros instead of state.macros... + defmacro: Macro( + ['name', 'src', 'sort', 'text', 'join', 'else', + ['strict', 'isolated', 'inheritmacros' ]], + async function*(args, body, state){ + var name = args.name + if(!name){ + // XXX throw err??? + return '' } + state.usermacros = state.usermacros ?? {} + var s = {} + var res = await this.macros.macro.call(this, args, body, s) + Object.assign( + state.usermacros, + s.macros) + return res }), + //*/ + // nesting rules... 'else': ['macro'], 'join': ['macro'], diff --git a/pwiki/parser.js b/pwiki/parser.js index f401025..e094a0e 100755 --- a/pwiki/parser.js +++ b/pwiki/parser.js @@ -201,9 +201,10 @@ module.BaseParser = { return res }, // XXX should this be here or on page??? callMacro: function(page, name, args, body, state, ...rest){ - return page.macros[name].call(page, + var macro = page.macros[name] + return macro.call(page, this.parseArgs( - page.macros[name].arg_spec + macro.arg_spec ?? [], args), body, diff --git a/pwiki2.js b/pwiki2.js index d742c6d..0bb66b4 100755 --- a/pwiki2.js +++ b/pwiki2.js @@ -18,8 +18,6 @@ * - CLI - * * -* XXX macros: add @defmacro( ..) to be exactly as @macro( ..) -* but defines a @(..) macro... * XXX BUG: parser: * This will break: * await pwiki.parse('@source(.)') @@ -242,6 +240,16 @@ * or render as any other page??? * ...currently they are rendered in the context of the page and * not in their own context... +* XXX macros: add @defmacro( ..) to be exactly as @macro( ..) +* but defines a @(..) macro... +* ...this would be useful for things like: +* +* or: +* +* generating delete and info buttons... +* ...this is not possible because: +* - we statically set the macro name list (regexp) parser.lex(..) +* - we need this list to parser.group(..) * XXX EXPERIMENTAL DOC INHERIT_ARGS added a special-case... * as basename will get appended :$ARGS if no args are given... * ...this only applies to paths referring to the current context