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){ function(st){
return ((st ?? state).slots ?? {})[name] }, return ((st ?? state).slots ?? {})[name] },
{slot: name}) }) })), {slot: name}) }) })),
// XXX do not like this name...
content: ['slot'],
// //
@ -1566,6 +1568,7 @@ module.parser = {
// set macro... // set macro...
if(name && body){ if(name && body){
;(state.macros ??= {})[name] = body ;(state.macros ??= {})[name] = body
// get macro... // get macro...
} else if(name){ } else if(name){
body = (state.macros ?? {})[name] } body = (state.macros ?? {})[name] }
@ -1573,10 +1576,18 @@ module.parser = {
// else... // else...
if(args.src if(args.src
&& !page.get(args.src).exists()){ && !page.get(args.src).exists()){
// <else> .. </else>
for(var elem of body){ for(var elem of body){
if(elem.name == 'else'){ if(elem.name == 'else'){
body = elem.body 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 ? return args.src && body ?
// run macro... // run macro...
@ -1584,12 +1595,9 @@ module.parser = {
function(page, body, path, text, state){ function(page, body, path, text, state){
return this.expand(page.get(path), body, state) }) return this.expand(page.get(path), body, state) })
: '' }) })), : '' }) })),
// nesting rules... // nesting rules...
'else': ['macro'], 'else': ['macro'],
join: ['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(){ get basepath(){
return path.dirname(this.path ?? '') }, return path.dirname(this.path ?? '') },
// XXX should this return current path if it does not exixt???
get matched(){ get matched(){
var path_action_pattern = /\/path\/?/ var path_action_pattern = /\/path\/?/
var path = this.path.replace(path_action_pattern, '') var path = this.path.replace(path_action_pattern, '')
@ -74,6 +75,7 @@ module.exports.P = {
:p] :p]
: [] }) : [] })
.flat() } .flat() }
// XXX should this return current path if it does not exixt???
return [this.path] }, return [this.path] },
exists: function(path){ exists: function(path){
var that = this var that = this
@ -330,8 +332,21 @@ test.Setups({
// for inline quoting see: test.Modifiers.quote // for inline quoting see: test.Modifiers.quote
// XXX // XXX
// XXX macro... // macro...
// XXX 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 var...
// XXX // XXX