mirror of
				https://github.com/flynx/pWiki.git
				synced 2025-10-30 18:40:08 +00:00 
			
		
		
		
	added @var(..)/@vars(..) + cleanup and tweaking...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
		
							parent
							
								
									5e7707696a
								
							
						
					
					
						commit
						cdb2fe63c0
					
				
							
								
								
									
										160
									
								
								pwiki/page.js
									
									
									
									
									
								
							
							
						
						
									
										160
									
								
								pwiki/page.js
									
									
									
									
									
								
							| @ -1294,6 +1294,45 @@ object.Constructor('Page', BasePage, { | |||||||
| 						{slot: name}) }),  | 						{slot: name}) }),  | ||||||
| 		'content': ['slot'], | 		'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 '' }, | ||||||
|  | 
 | ||||||
| 		// 	
 | 		// 	
 | ||||||
| 		// 	<macro src=<url>> .. </macro>
 | 		// 	<macro src=<url>> .. </macro>
 | ||||||
| 		//
 | 		//
 | ||||||
| @ -1319,7 +1358,7 @@ object.Constructor('Page', BasePage, { | |||||||
| 		// XXX SORT sorting not implemented yet....
 | 		// XXX SORT sorting not implemented yet....
 | ||||||
| 		macro: Macro( | 		macro: Macro( | ||||||
| 			['name', 'src', 'sort', 'text', 'join', 'else',  | 			['name', 'src', 'sort', 'text', 'join', 'else',  | ||||||
| 				['strict', 'isolated', 'inheritmacros' ]], | 				['strict', 'isolated', 'inheritmacros', 'inheritvars' ]], | ||||||
| 			async function*(args, body, state){ | 			async function*(args, body, state){ | ||||||
| 				var that = this | 				var that = this | ||||||
| 
 | 
 | ||||||
| @ -1348,6 +1387,9 @@ object.Constructor('Page', BasePage, { | |||||||
| 				var macros = state.macros =  | 				var macros = state.macros =  | ||||||
| 					state.macros  | 					state.macros  | ||||||
| 						?? {} | 						?? {} | ||||||
|  | 				var vars = state.vars =  | ||||||
|  | 					state.vars  | ||||||
|  | 						?? {} | ||||||
| 				var depends = state.depends =  | 				var depends = state.depends =  | ||||||
| 					state.depends  | 					state.depends  | ||||||
| 						?? new Set() | 						?? new Set() | ||||||
| @ -1408,6 +1450,11 @@ object.Constructor('Page', BasePage, { | |||||||
| 							args.inheritmacros | 							args.inheritmacros | ||||||
| 							: iargs.inheritmacros) | 							: iargs.inheritmacros) | ||||||
| 						?? true | 						?? true | ||||||
|  | 				var inheritvars =  | ||||||
|  | 					('inheritvars' in args ? | ||||||
|  | 							args.inheritvars | ||||||
|  | 							: iargs.inheritvars) | ||||||
|  | 						?? true | ||||||
| 
 | 
 | ||||||
| 				if(src){ | 				if(src){ | ||||||
| 					src = await base.parse(src, state) | 					src = await base.parse(src, state) | ||||||
| @ -1447,6 +1494,9 @@ object.Constructor('Page', BasePage, { | |||||||
| 								macros: inheritmacros ? | 								macros: inheritmacros ? | ||||||
| 									{__proto__: macros} | 									{__proto__: macros} | ||||||
| 									: {}, | 									: {}, | ||||||
|  | 								vars: inheritvars ? | ||||||
|  | 									{__proto__: vars} | ||||||
|  | 									: {}, | ||||||
| 							} | 							} | ||||||
| 							yield this.__parser__.parse(page,  | 							yield this.__parser__.parse(page,  | ||||||
| 								this.__parser__.expand(page,  | 								this.__parser__.expand(page,  | ||||||
| @ -1460,30 +1510,6 @@ object.Constructor('Page', BasePage, { | |||||||
| 						if(else_block){ | 						if(else_block){ | ||||||
| 							yield this.__parser__.expand(this, else_block, state) } } } }), | 							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...
 | 		// nesting rules...
 | ||||||
| 		'else': ['macro'], | 		'else': ['macro'], | ||||||
| 		'join': ['macro'], | 		'join': ['macro'], | ||||||
| @ -1501,10 +1527,6 @@ object.Constructor('Page', BasePage, { | |||||||
| 		...module.BasePage.prototype.actions, | 		...module.BasePage.prototype.actions, | ||||||
| 
 | 
 | ||||||
| 		'!': true, | 		'!': true, | ||||||
| 
 |  | ||||||
| 		// XXX DEBUG -- remove these...
 |  | ||||||
| 		testDirect: true, |  | ||||||
| 		'testDirect!': true,	 |  | ||||||
| 	}, | 	}, | ||||||
| 
 | 
 | ||||||
| 	'!': Object.assign( | 	'!': Object.assign( | ||||||
| @ -1512,23 +1534,6 @@ object.Constructor('Page', BasePage, { | |||||||
| 			return this.get('..:$ARGS', {energetic: true}).raw }, | 			return this.get('..:$ARGS', {energetic: true}).raw }, | ||||||
| 		{energetic: true}), | 		{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...
 | 	// events...
 | ||||||
| 	//
 | 	//
 | ||||||
| @ -2159,7 +2164,31 @@ module.System = { | |||||||
| 	tree: { | 	tree: { | ||||||
| 		text: object.doc` | 		text: object.doc` | ||||||
| 			<slot title/> | 			<slot title/> | ||||||
| 			<macro src="../*:$ARGS"> | 
 | ||||||
|  | 			<macro tree src="../*:$ARGS"> | ||||||
|  | 				<var path "@source(s ./path)"/> | ||||||
|  | 
 | ||||||
|  | 				<div> | ||||||
|  | 					<div class="item"> | ||||||
|  | 						<a class="tree-page-title" href="#@var(path)">@source(./title)</a> | ||||||
|  | 						<a class="show-on-hover" href="#@var(path)/info">🛈</a> | ||||||
|  | 						<a class="show-on-hover"  | ||||||
|  | 							href="javascript:pwiki.delete('@var(path)')" | ||||||
|  | 							>×</a> | ||||||
|  | 					</div> | ||||||
|  | 					<div style="padding-left: 30px"> | ||||||
|  | 						@macro(tree "./*:$ARGS") | ||||||
|  | 					</div> | ||||||
|  | 				</div> | ||||||
|  | 			</macro>` }, | ||||||
|  | 	/* XXX @var(..) vs. multiple @source(..) calls are not that different... | ||||||
|  | 	tree2: { | ||||||
|  | 		text: object.doc` | ||||||
|  | 			<slot title/> | ||||||
|  | 
 | ||||||
|  | 			<i>This is a comparison with [../tree] -- \\@var(..) vs direct macro call...</i><br><br> | ||||||
|  | 
 | ||||||
|  | 			<macro tree src="../*:$ARGS"> | ||||||
| 				<div> | 				<div> | ||||||
| 					<div class="item"> | 					<div class="item"> | ||||||
| 						<a class="tree-page-title" href="#@source(s ./path)">@source(./title)</a> | 						<a class="tree-page-title" href="#@source(s ./path)">@source(./title)</a> | ||||||
| @ -2169,10 +2198,11 @@ module.System = { | |||||||
| 							>×</a> | 							>×</a> | ||||||
| 					</div> | 					</div> | ||||||
| 					<div style="padding-left: 30px"> | 					<div style="padding-left: 30px"> | ||||||
| 						@include("./tree:$ARGS") | 						@macro(tree "./*:$ARGS") | ||||||
| 					</div> | 					</div> | ||||||
| 				</div> | 				</div> | ||||||
| 			</macro>` }, | 			</macro>` }, | ||||||
|  | 	//*/
 | ||||||
| 	all: { | 	all: { | ||||||
| 		text: `@include("../**/path:$ARGS" join="@source(line-separator)")`}, | 		text: `@include("../**/path:$ARGS" join="@source(line-separator)")`}, | ||||||
| 	info: { | 	info: { | ||||||
| @ -2282,37 +2312,8 @@ module.System = { | |||||||
| 			: undefined }, | 			: undefined }, | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 	// XXX DEBUG -- remove these...
 | 	/* XXX need a stable way to redirect after the action... | ||||||
| 	testPage: { | 	//		...not sure if these are needed vs. pwiki.delete(..) and friends...
 | ||||||
| 		text: object.doc`<pre>
 |  | ||||||
| 			location: @source(./location) |  | ||||||
| 			path: @source(./path) |  | ||||||
| 			args: <args/> |  | ||||||
| 		</pre>`}, |  | ||||||
| 	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 }, |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 	// actions...
 | 	// actions...
 | ||||||
| 	//
 | 	//
 | ||||||
| 	// XXX should ** be the default here...
 | 	// XXX should ** be the default here...
 | ||||||
| @ -2369,6 +2370,7 @@ module.System = { | |||||||
| 		// 		redirected page...
 | 		// 		redirected page...
 | ||||||
| 		return '' }, | 		return '' }, | ||||||
| 	// XXX copy/...
 | 	// XXX copy/...
 | ||||||
|  | 	//*/
 | ||||||
| 
 | 
 | ||||||
| 	// XXX System/sort
 | 	// XXX System/sort
 | ||||||
| 	// XXX System/reverse
 | 	// XXX System/reverse
 | ||||||
|  | |||||||
| @ -23,6 +23,7 @@ | |||||||
| * 			await pwiki.parse('<macro src=../tags join=", ">@source(.)</macro>') | * 			await pwiki.parse('<macro src=../tags join=", ">@source(.)</macro>') | ||||||
| *		This will not: | *		This will not: | ||||||
| * 			await pwiki.parse('<macro src="../tags" join=", ">@source(.)</macro>') | * 			await pwiki.parse('<macro src="../tags" join=", ">@source(.)</macro>') | ||||||
|  | * 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 Q: do we need a way to index a list item via path??? | ||||||
| * XXX STYLE: should style loading be done via the event mechanics  | * XXX STYLE: should style loading be done via the event mechanics  | ||||||
| * 		(see: pwiki2.html) or via the base templates (see: pwiki/page.js:_view  | * 		(see: pwiki2.html) or via the base templates (see: pwiki/page.js:_view  | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user