#!/usr/bin/node //--------------------------------------------------------------------- var path = require('path') var test = require('ig-test') var serialize = require('ig-serialize') var parser = require('../parser').parser //--------------------------------------------------------------------- var PAGES = module.exports.PAGES = { '/blank': '', '/page': 'Page', '/async/page': Promise.resolve('Page'), '/includePage': '@include(/page)', '/isolated': '@slot(slot original)', '/recursive/Self': '<< @include(/recursive/Self) >>', '/recursive/OtherSelf': '<< @include(/recursive/SelfOther) >>', '/recursive/SelfOther': '<< @include(/recursive/OtherSelf) >>', '/async/recursive/Self': Promise.resolve('<< @include(/async/recursive/Self) >>'), '/async/recursive/OtherSelf': Promise.resolve('<< @include(/async/recursive/SelfOther) >>'), '/async/recursive/SelfOther': Promise.resolve('<< @include(/async/recursive/OtherSelf) >>'), '/multi/page': [ 'A', 'B', 'C' ], '/async/multi/page': Promise.resolve([ 'A', 'B', 'C' ]), /* XXX are these actually possible??? // ...and should these be handlable?? '/multi/async/page': [ Promise.resolve('A'), Promise.resolve('B'), Promise.resolve('C'), ], '/multi/async/multi/page': Promise.resolve([ Promise.resolve('A'), Promise.resolve('B'), Promise.resolve('C'), ]), //*/ } var P = module.exports.P = { __pages__: PAGES, __parser__: parser, path: '/', 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, '') path = path == '' ? '/' : path var path_action = path != this.path if(path.includes('*')){ var that = this var re = new RegExp(path .replace(/\//, '\\/') .replace(/\*/g, '[^\\/]+')) return Object.keys(this.__pages__) .map(function(p){ return re.test(p) ? [path_action ? p +'/path' :p] : [] }) .flat() } // XXX should this return current path if it does not exixt??? return [this.path] }, exists: function(path){ var that = this return (path ? this.get(path).matched : this.matched) .filter(function(p){ return p in that.__pages__ }) .length > 0 }, // * -> a promise or a list of promises??? get raw(){ var res = [] var path_action_pattern = /\/path\/?/ var path_action = path_action_pattern.test(this.path) for(var path of this.matched){ res.push( path_action ? ((path = path.replace(path_action_pattern, '')) == '' ? '/' : path) : this.__pages__[path]) } return res.length == 1 ? res[0] /*/ if at least one page is a promise wait for all... : Promise .iter(res) .sync() }, /*/ : res }, //*/ // XXX should this return an arrya for a multi-match??? get text(){ return this.__parser__.parse(this, this.raw, {}) }, get: function(p){ return { __proto__: P, path: path.resolve(this.path, p), } }, // XXX resolvePathVars: function(path){ return path }, } //--------------------------------------------------------------------- test.Setups({ // general parser... empty: function(assert){ return { code: [ '', '' ] }}, // slot... slot_empty: function(assert){ return {code: [ '@slot(slot)', '@slot("slot")', '@slot(\'slot\')', '@slot(name=slot)', '@slot(name="slot")', '@slot(name=\'slot\')', '', '', '', '', '', '', '' ]} }, slot_value: function(assert){ return {code: [ '', '', '@slot(slot value)', '@slot(slot text=value)', 'value' ]} }, slot_fill: function(assert){ var ins = this.slot_value(assert).code.slice(0, -1) return {code: [ ...ins.map(function(e){ return e + '@slot(slot other)' }), ...ins.map(function(e){ return e + '' }), ...ins.map(function(e){ return e + 'other' }), 'other' ]} }, slot_fill_fill: function(assert){ var ins = this.slot_fill(assert).code.slice(0, -1) return {code: [ ...ins.map(function(e){ return e + '@slot(slot third)' }), ...ins.map(function(e){ return e + '' }), ...ins.map(function(e){ return e + 'third' }), 'third' ]} }, // nested... slot_nested: function(assert){ return {code: [ '[[ ]]@slot(slot.content value)', '[[ value ]]' ]} }, slot_nested_overwrite: function(assert){ return {code: [ '[[ ]]@slot(slot value)', 'value' ]} }, // nested-self... slot_nested_nested: function(assert){ return {code: [ '[[ ]]', '[[ ]]', 'value' ]} }, slot_nested_shown_nested: function(assert){ return {code: [ '[[ ]]', '[[ ]]' ]} }, // slot content... slot_content: function(assert){ return {code: [ '[[ ]]', '[[ ]]' ]} }, slot_content_multi: function(assert){ return {code: [ '[[ ]]', '[[ moo moo moo ]]' ]} }, slot_content_filled: function(assert){ var ins = this.slot_content(assert).code.slice(0, -1) return {code: [ ...ins.map(function(e){ return '' + e }), '[[ content ]]' ]} }, slot_content_content_filled: function(assert){ var ins = this.slot_content_filled(assert).code var res = ins.pop() return {code: [ ...ins.map(function(e){ return e +'(( ))' }), '(( '+ res +' ))' ]} }, slot_content_nested: function(assert){ return {code: [ 'moo[[ ]]', '[[ moo ]]' ]} }, // shown/hidden.. slot_shown: function(assert){ var ins = this.slot_value(assert).code var expect = ins.pop() return {code: [ ...ins.map(function(i){ return i +' @slot(slot that shown)' }), 'that that' ]} }, slot_hidden: function(assert){ return {code: [ '