added macro else handling + basic testing...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2026-07-06 13:08:25 +03:00
parent 31b22be60b
commit 26073fb713
2 changed files with 29 additions and 6 deletions

View File

@ -1244,6 +1244,8 @@ module.parser = {
function(st){
return ((st ?? state).slots ?? {})[name] },
{slot: name}) }) })),
// XXX do not like this name...
content: ['slot'],
//
@ -1566,6 +1568,7 @@ module.parser = {
// set macro...
if(name && body){
;(state.macros ??= {})[name] = body
// get macro...
} else if(name){
body = (state.macros ?? {})[name] }
@ -1573,10 +1576,18 @@ module.parser = {
// else...
if(args.src
&& !page.get(args.src).exists()){
// <else> .. </else>
for(var elem of body){
if(elem.name == 'else'){
body = elem.body
return that.expand(page.get(args.src), elem.body, state) } } }
return that.expand(page.get(args.src), body, state) } }
// args.else...
return args['else'] ?
that.expand(page.get(args.src), args['else'], state)
: [] }
// join macro???
// XXX
return args.src && body ?
// run macro...
@ -1584,12 +1595,9 @@ module.parser = {
function(page, body, path, text, state){
return this.expand(page.get(path), body, state) })
: '' }) })),
// nesting rules...
'else': ['macro'],
join: ['macro'],
// XXX do not like this name...
content: ['slot', 'include', 'quote', 'macro'],
},
}

View File

@ -53,6 +53,7 @@ module.exports.P = {
get basepath(){
return path.dirname(this.path ?? '') },
// XXX should this return current path if it does not exixt???
get matched(){
var path_action_pattern = /\/path\/?/
var path = this.path.replace(path_action_pattern, '')
@ -74,6 +75,7 @@ module.exports.P = {
:p]
: [] })
.flat() }
// XXX should this return current path if it does not exixt???
return [this.path] },
exists: function(path){
var that = this
@ -330,8 +332,21 @@ test.Setups({
// for inline quoting see: test.Modifiers.quote
// XXX
// XXX macro...
// XXX
// macro...
macro: function(assert, path='/page', res){
return {
page: P,
code: [
'<macro src="'+ path +'">[[ @source(./path) ]]</macro>',
res ?? ('[[ '+ path +' ]]'), ], } },
macro_404: function(assert){
return this.macro(assert, '/not_found', '') },
macro_else: function(assert){
return {
page: P,
code: [
'<macro src="/not_found">[[ @source(./path) ]]<else>ERR</else></macro>',
'ERR', ], } },
// XXX var...
// XXX