now args now working...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-05-16 11:54:21 +03:00
parent d40b9a96b1
commit a0b377d542

118
pwiki2.js
View File

@ -106,7 +106,6 @@ module.path = {
parent = this.normalize(parent, 'array') parent = this.normalize(parent, 'array')
return this.normalize(parent.concat(path), format) }, return this.normalize(parent.concat(path), format) },
//paths: function*(path='/', leading_slash=true){
paths: function*(path='/'){ paths: function*(path='/'){
path = this.normalize(path, 'array') path = this.normalize(path, 'array')
// handle '', '.', and '/' paths... // handle '', '.', and '/' paths...
@ -146,6 +145,10 @@ module.path = {
: parts) : parts)
.join('/'), .join('/'),
'string') }, 'string') },
basename: function(path){
return this.split(path).pop() },
dirname: function(path){
return this.relative(path, '..', 'string') },
} }
@ -948,14 +951,6 @@ module.BaseParser = {
return filter[0] != '-' }) return filter[0] != '-' })
.filter(function(filter){ .filter(function(filter){
return !skip.has(filter) })}, return !skip.has(filter) })},
/*/ XXX is this still used???
posArgs: function(args){
return Object.entries(args)
.reduce(function(res, [key, value]){
/^[0-9]+$/.test(key)
&& (res[key*1] = value)
return res }, []) },
//*/
// //
// Spec format: // Spec format:
// [<orderd>, ... [<keyword>, ...]] // [<orderd>, ... [<keyword>, ...]]
@ -1295,6 +1290,7 @@ module.parser = {
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// XXX should these be something more generic like Object.assign(..) ???
// XXX revise... // XXX revise...
var Filter = var Filter =
@ -1305,6 +1301,19 @@ function(...args){
&& Object.assign(func, args.pop()) && Object.assign(func, args.pop())
return func } return func }
// XXX do we need anything else like .doc, attrs???
var Macro =
module.Macro =
function(spec, func){
var args = [...arguments]
// function...
func = args.pop()
// arg sepc...
;(args.length > 0 && args[args.length-1] instanceof Array)
&& (func.arg_spec = args.pop())
// XXX do we need anything else like .doc, attrs???
return func }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -1405,7 +1414,7 @@ object.Constructor('Page', BasePage, {
[outer_filters] } [outer_filters] }
// merge in new filters... // merge in new filters...
var local = Object.values(args) var local = Object.keys(args)
filters.splice(filters.length, 0, ...local) filters.splice(filters.length, 0, ...local)
// trigger quote-filter... // trigger quote-filter...
@ -1460,11 +1469,13 @@ object.Constructor('Page', BasePage, {
// XXX should we track recursion via the resolved (current) path // XXX should we track recursion via the resolved (current) path
// or the given path??? // or the given path???
// XXX should this be lazy??? // XXX should this be lazy???
include: function(args, body, state, key='included', handler){ include: Macro(
['src', 'recursive', ['isolated']],
function(args, body, state, key='included', handler){
// positional args... // positional args...
var src = args.src //|| args[0] var src = args.src
var recursive = args.recursive || body var recursive = args.recursive || body
var isolated = this.__parser__.posArgs(args).includes('isolated') var isolated = args.isolated
if(!src){ if(!src){
return '' } return '' }
@ -1505,13 +1516,15 @@ object.Constructor('Page', BasePage, {
} else { } else {
delete state[key] } delete state[key] }
return res }, return res }),
source: function(args, body, state){ source: Macro(
var src = args.src //|| args[0] ['src'],
function(args, body, state){
var src = args.src
return this.macros.include.call(this, return this.macros.include.call(this,
args, body, state, 'sources', args, body, state, 'sources',
function(){ function(){
return this.__parser__.parse(this, this.get(src).raw +'', state) }) }, return this.__parser__.parse(this, this.get(src).raw +'', state) }) }),
// //
// @quote(<src>) // @quote(<src>)
// //
@ -1531,7 +1544,9 @@ object.Constructor('Page', BasePage, {
// not expanded... // not expanded...
// //
// XXX need a way to escape macros -- i.e. include </quote> in a quoted text... // XXX need a way to escape macros -- i.e. include </quote> in a quoted text...
quote: function(args, body, state){ quote: Macro(
['src', 'filter', 'text'],
function(args, body, state){
var src = args.src //|| args[0] var src = args.src //|| args[0]
var text = args.text var text = args.text
?? body ?? body
@ -1572,12 +1587,12 @@ object.Constructor('Page', BasePage, {
return this.macros.filter return this.macros.filter
.call(this, filters, text, state, false) .call(this, filters, text, state, false)
.call(this, state) } .call(this, state) }
return text } }, return text } }),
// very similar to @filter(..) but will affect @quote(..) filters... // very similar to @filter(..) but will affect @quote(..) filters...
'quote-filter': function(args, body, state){ 'quote-filter': function(args, body, state){
var filters = state.quote_filters = var filters = state.quote_filters =
state.quote_filters ?? [] state.quote_filters ?? []
filters.splice(filters.length, 0, ...Object.values(args)) }, filters.splice(filters.length, 0, ...Object.keys(args)) },
// //
// <slot name=<name>/> // <slot name=<name>/>
// //
@ -1601,7 +1616,9 @@ object.Constructor('Page', BasePage, {
// //
// XXX how do we handle a slot defined within a slot???? // XXX how do we handle a slot defined within a slot????
// ...seems that we'll fall into recursion on definition... // ...seems that we'll fall into recursion on definition...
slot: function(args, body, state){ slot: Macro(
['name', 'text', ['shown', 'hidden']],
function(args, body, state){
var name = args.name var name = args.name
var text = args.text var text = args.text
?? body ?? body
@ -1615,12 +1632,11 @@ object.Constructor('Page', BasePage, {
//var hidden = name in slots //var hidden = name in slots
// XXX EXPERIMENTAL // XXX EXPERIMENTAL
var pos = this.__parser__.posArgs(args)
var hidden = var hidden =
// 'hidden' has priority... // 'hidden' has priority...
(pos.includes('hidden') || args.hidden) args.hidden
// explicitly show... () // explicitly show... ()
|| ((pos.includes('shown') || args.shown) ? || (args.shown ?
false false
// show first instance... // show first instance...
: name in slots) : name in slots)
@ -1630,10 +1646,12 @@ object.Constructor('Page', BasePage, {
return hidden ? return hidden ?
'' ''
: function(state){ : function(state){
return state.slots[name] } }, return state.slots[name] } }),
// XXX sorting not implemented yet.... // XXX sorting not implemented yet....
macro: function(args, body, state){ macro: Macro(
['name', 'src', 'sort', 'text'],
function(args, body, state){
var that = this var that = this
var name = args.name //?? args[0] var name = args.name //?? args[0]
var src = args.src var src = args.src
@ -1685,8 +1703,7 @@ object.Constructor('Page', BasePage, {
return pages return pages
.map(function(page){ .map(function(page){
return [...that.__parser__.expand(page, text, state)] }) return [...that.__parser__.expand(page, text, state)] })
.flat() .flat() } }),
} },
// nesting rules... // nesting rules...
'else': ['macro'], 'else': ['macro'],
@ -1834,51 +1851,6 @@ module.pwiki =
Page('/', '/', store) Page('/', '/', store)
// XXX should we also convert values??
// ...like:
// "true" -> true
// "123" -> 123
// ...
var parseArgs = function(spec, args){
// spec...
var order = spec.slice()
var bools = new Set(
order[order.length-1] instanceof Array ?
order.pop()
: [])
order = order
.filter(function(k){
return !(k in args) })
var res = {}
var pos = Object.entries(args)
// stage 1: populate res with explicit data and place the rest in pos...
.reduce(function(pos, [key, value]){
/^[0-9]+$/.test(key) ?
(bools.has(value) ?
// bool...
(res[value] = true)
// positional...
: (pos[key*1] = value))
// keyword...
: (res[key] = value)
return pos }, [])
// stage 2: populate implicit values from pos...
.forEach(function(e, i){
order.length == 0 ?
(res[e] = true)
: (res[order.shift()] = e) })
return res }
console.log('---',
parseArgs(
['src', 'bam', ['first', 'second']],
{1: 'first', 2: '..', src2: 'second', moo: 'third'}))
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// XXX experiments and testing... // XXX experiments and testing...