diff --git a/pwiki/page.js b/pwiki/page.js index 9912996..e3f3827 100755 --- a/pwiki/page.js +++ b/pwiki/page.js @@ -1294,6 +1294,45 @@ object.Constructor('Page', BasePage, { {slot: name}) }), 'content': ['slot'], + // XXX EXPERIMENTAL... + 'var': Macro( + ['name', 'text', + ['shown', 'hidden']], + async function(args, body, state){ + var name = args.name + if(!name){ + return '' } + name = await this.parse(name, state) + var text = args.text + ?? body + var vars = state.vars = + state.vars + ?? {} + + // set... + if(text){ + text = vars[name] = + await this.parse(text, state) + var show = args.shown + || ('hidden' in args + && !args.hidden) + || false + return show ? + text + : '' + // get... + } else { + return vars[name] + ?? '' } }), + vars: async function(args, body, state){ + var vars = state.vars = + state.vars + ?? {} + for(var [name, value] of Object.entries(args)){ + vars[await this.parse(name, state)] = + await this.parse(value, state) } + return '' }, + // // > .. // @@ -1319,7 +1358,7 @@ object.Constructor('Page', BasePage, { // XXX SORT sorting not implemented yet.... macro: Macro( ['name', 'src', 'sort', 'text', 'join', 'else', - ['strict', 'isolated', 'inheritmacros' ]], + ['strict', 'isolated', 'inheritmacros', 'inheritvars' ]], async function*(args, body, state){ var that = this @@ -1348,6 +1387,9 @@ object.Constructor('Page', BasePage, { var macros = state.macros = state.macros ?? {} + var vars = state.vars = + state.vars + ?? {} var depends = state.depends = state.depends ?? new Set() @@ -1408,6 +1450,11 @@ object.Constructor('Page', BasePage, { args.inheritmacros : iargs.inheritmacros) ?? true + var inheritvars = + ('inheritvars' in args ? + args.inheritvars + : iargs.inheritvars) + ?? true if(src){ src = await base.parse(src, state) @@ -1447,6 +1494,9 @@ object.Constructor('Page', BasePage, { macros: inheritmacros ? {__proto__: macros} : {}, + vars: inheritvars ? + {__proto__: vars} + : {}, } yield this.__parser__.parse(page, this.__parser__.expand(page, @@ -1460,30 +1510,6 @@ 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'], @@ -1501,10 +1527,6 @@ object.Constructor('Page', BasePage, { ...module.BasePage.prototype.actions, '!': true, - - // XXX DEBUG -- remove these... - testDirect: true, - 'testDirect!': true, }, '!': Object.assign( @@ -1512,23 +1534,6 @@ object.Constructor('Page', BasePage, { return this.get('..:$ARGS', {energetic: true}).raw }, {energetic: true}), - // XXX DEBUG -- remove these... - testDirect: function(){ - console.log('testDirect:', this.location, - this.args, - (this.renderer ?? {}).args, - (this.root ?? {}).args) - //console.log(' :', this, this.renderer) - return this.location }, - 'testDirect!': Object.assign( - function(){ - console.log('testDirect!:', this.location, - this.args, - (this.renderer ?? {}).args, - (this.root ?? {}).args) - //console.log(' :', this, this.renderer) - return this.location }, - {energetic: true}), // events... // @@ -2159,7 +2164,31 @@ module.System = { tree: { text: object.doc` - + + + + +
+ +
+ @macro(tree "./*:$ARGS") +
+
+
` }, + /* XXX @var(..) vs. multiple @source(..) calls are not that different... + tree2: { + text: object.doc` + + + This is a comparison with [../tree] -- \\@var(..) vs direct macro call...

+ +
@source(./title) @@ -2169,10 +2198,11 @@ module.System = { >×
- @include("./tree:$ARGS") + @macro(tree "./*:$ARGS")
` }, + //*/ all: { text: `@include("../**/path:$ARGS" join="@source(line-separator)")`}, info: { @@ -2282,37 +2312,8 @@ module.System = { : undefined }, - // XXX DEBUG -- remove these... - testPage: { - text: object.doc`
-			location: @source(./location)
-			path: @source(./path)
-			args: 
-		
`}, - testAction: function(){ - console.log('testAction:', this.location, - this.args, - (this.renderer ?? {}).args, - (this.root ?? {}).args) - //console.log(' :', this, this.renderer) - return this.location }, - 'testAction!': Object.assign( - function(){ - console.log('testAction!:', this.location, - this.args, - (this.renderer ?? {}).args, - (this.root ?? {}).args) - //console.log(' :', this, this.renderer) - return this.location }, - {energetic: true}), - _testPage: { - text: object.doc` - @source(./path) - `}, - _testAction: function(){ - return this.path }, - - + /* XXX need a stable way to redirect after the action... + // ...not sure if these are needed vs. pwiki.delete(..) and friends... // actions... // // XXX should ** be the default here... @@ -2369,6 +2370,7 @@ module.System = { // redirected page... return '' }, // XXX copy/... + //*/ // XXX System/sort // XXX System/reverse diff --git a/pwiki2.js b/pwiki2.js index 0bb66b4..eaabc66 100755 --- a/pwiki2.js +++ b/pwiki2.js @@ -23,6 +23,7 @@ * await pwiki.parse('@source(.)') * This will not: * await pwiki.parse('@source(.)') +* XXX parser: error handling: must output to page and be informative... * XXX Q: do we need a way to index a list item via path??? * XXX STYLE: should style loading be done via the event mechanics * (see: pwiki2.html) or via the base templates (see: pwiki/page.js:_view