| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * | 
					
						
							| 
									
										
										
										
											2022-04-16 10:53:41 +03:00
										 |  |  | * XXX might be a good idea to try signature based security: | 
					
						
							|  |  |  | * 		- sign changes | 
					
						
							|  |  |  | * 		- sign sync session | 
					
						
							|  |  |  | * 		- refuse changes with wrong signatures | 
					
						
							|  |  |  | * 		- public keys available on client and on server | 
					
						
							|  |  |  | * 			- check signatures localy | 
					
						
							|  |  |  | * 			- check signatures remotely | 
					
						
							|  |  |  | * 		- private key available only with author | 
					
						
							|  |  |  | * 		- keep both the last signed and superceding unsigned version | 
					
						
							|  |  |  | * 		- on sync ask to overwrite unsigned with signed | 
					
						
							|  |  |  | * 		- check if we can use the same mechanics as ssh... | 
					
						
							|  |  |  | * 		- in this view a user in the system is simply a set of keys and  | 
					
						
							|  |  |  | * 			a signature (a page =)) | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | * | 
					
						
							|  |  |  | **********************************************************************/ | 
					
						
							|  |  |  | ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) | 
					
						
							|  |  |  | (function(require){ var module={} // make module AMD/node compatible...
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 22:35:06 +03:00
										 |  |  | // XXX might be a good idea to make this compatible with node's path API...
 | 
					
						
							| 
									
										
										
										
											2022-04-16 12:28:06 +03:00
										 |  |  | var path =  | 
					
						
							|  |  |  | module.path = { | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// The page returned when getting the '/' path...
 | 
					
						
							|  |  |  | 	ROOT_PAGE: 'WikiHome', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// The page returned when listing a path ending with '/'...
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// If set to false treat dirs the same as pages (default)
 | 
					
						
							|  |  |  | 	// XXX revise...
 | 
					
						
							|  |  |  | 	//DEFAULT_DIR: 'pages',
 | 
					
						
							|  |  |  | 	DEFAULT_DIR: false, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ALTERNATIVE_PAGES: [ | 
					
						
							|  |  |  | 		'EmptyPage', | 
					
						
							|  |  |  | 		'NotFound', | 
					
						
							|  |  |  | 	], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	SEARCH_PATHS: [ | 
					
						
							|  |  |  | 		'./Templates', | 
					
						
							|  |  |  | 		'/System', | 
					
						
							|  |  |  | 	], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// NOTE: trailing/leading '/' are represented by '' at end/start of 
 | 
					
						
							|  |  |  | 	// 		path list...
 | 
					
						
							|  |  |  | 	normalize: function(path='.', format='auto'){ | 
					
						
							|  |  |  | 		format = format == 'auto' ? | 
					
						
							|  |  |  | 			(path instanceof Array ? | 
					
						
							|  |  |  | 				'array' | 
					
						
							|  |  |  | 				: 'string') | 
					
						
							|  |  |  | 			: format | 
					
						
							|  |  |  | 		path = (path instanceof Array ? | 
					
						
							|  |  |  | 				path | 
					
						
							|  |  |  | 				// NOTE: this will also trim the path elements...
 | 
					
						
							|  |  |  | 				: path.split(/\s*[\\\/]+\s*/)) | 
					
						
							|  |  |  | 			.reduce(function(res, e){ | 
					
						
							|  |  |  | 				// special case: leading '..' / '.'
 | 
					
						
							|  |  |  | 				if(res.length == 0  | 
					
						
							|  |  |  | 						&& e == '..'){ | 
					
						
							|  |  |  | 					return [e] } | 
					
						
							|  |  |  | 				e == '.' ? | 
					
						
							|  |  |  | 					undefined | 
					
						
							|  |  |  | 				: e == '..'  | 
					
						
							|  |  |  | 						|| res[res.length-1] == '>>' ? | 
					
						
							|  |  |  | 					res.pop() | 
					
						
							|  |  |  | 				// NOTE: the last '>>' will be retained...
 | 
					
						
							|  |  |  | 				: res.push(e) | 
					
						
							|  |  |  | 				return res }, [])  | 
					
						
							|  |  |  | 		return format == 'string' ? | 
					
						
							|  |  |  | 			path.join('/')  | 
					
						
							|  |  |  | 			: path }, | 
					
						
							|  |  |  | 	relative: function(parent, path, format='auto'){ | 
					
						
							|  |  |  | 		format = format == 'auto' ? | 
					
						
							|  |  |  | 			(path instanceof Array ? | 
					
						
							|  |  |  | 				'array' | 
					
						
							|  |  |  | 				: 'string') | 
					
						
							|  |  |  | 			: format | 
					
						
							|  |  |  | 		path = this.normalize(path, 'array') | 
					
						
							|  |  |  | 		// root path...
 | 
					
						
							|  |  |  | 		if(path[0] == ''){ | 
					
						
							|  |  |  | 			return format == 'string' ?  | 
					
						
							|  |  |  | 				path.join('/') | 
					
						
							|  |  |  | 				: path } | 
					
						
							|  |  |  | 		parent = this.normalize(parent, 'array') | 
					
						
							|  |  |  | 		return this.normalize(parent.concat(path), format) }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 01:59:55 +03:00
										 |  |  | 	//paths: function*(path='/', leading_slash=true){
 | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 	paths: function*(path='/'){ | 
					
						
							|  |  |  | 		path = this.normalize(path, 'array') | 
					
						
							|  |  |  | 		// handle '', '.', and '/' paths...
 | 
					
						
							|  |  |  | 		if(path.length == 0  | 
					
						
							|  |  |  | 				|| (path.length == 1 && path[0] == '') | 
					
						
							|  |  |  | 				|| (path.length == 2 && path[0] == '' && path[1] == '')){ | 
					
						
							|  |  |  | 			path = [this.ROOT_PAGE] } | 
					
						
							|  |  |  | 		// normalize relative paths to root...
 | 
					
						
							|  |  |  | 		path[0] != '' | 
					
						
							|  |  |  | 			&& path.unshift('') | 
					
						
							|  |  |  | 		// paths ending in '/' -- dir lister...
 | 
					
						
							|  |  |  | 		if(path[path.length-1] == ''){ | 
					
						
							|  |  |  | 			path.pop() | 
					
						
							|  |  |  | 			this.DEFAULT_DIR | 
					
						
							|  |  |  | 				&& path.push(this.DEFAULT_DIR) } | 
					
						
							|  |  |  | 		// generate path candidates...
 | 
					
						
							|  |  |  | 		for(var page of [path.pop(), ...this.ALTERNATIVE_PAGES]){ | 
					
						
							|  |  |  | 			for(var tpl of ['.', ...this.SEARCH_PATHS]){ | 
					
						
							|  |  |  | 				// search for page up the path...
 | 
					
						
							|  |  |  | 				var p = path.slice() | 
					
						
							|  |  |  | 				while(p.length > 0){ | 
					
						
							|  |  |  | 					yield this.relative(p, tpl +'/'+ page, 'string') | 
					
						
							| 
									
										
										
										
											2022-04-15 01:59:55 +03:00
										 |  |  | 					//yield leading_slash ? 
 | 
					
						
							|  |  |  | 					//	this.relative(p, tpl +'/'+ page, 'string')
 | 
					
						
							|  |  |  | 					//	: this.relative(p, tpl +'/'+ page, 'string').slice(1)
 | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 					// special case: non-relative template/page path...
 | 
					
						
							|  |  |  | 					if(tpl[0] == '/'){ | 
					
						
							|  |  |  | 						break } | 
					
						
							|  |  |  | 					p.pop() } } } }, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //---------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 22:35:06 +03:00
										 |  |  | // NOTE: store keys must be normalized...
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-04-15 11:11:02 +03:00
										 |  |  | // XXX LEADING_SLASH should this be strict about leading '/' in paths???
 | 
					
						
							|  |  |  | // 		...this may lead to duplicate paths created -- '/a/b' and 'a/b'
 | 
					
						
							| 
									
										
										
										
											2022-04-12 02:03:33 +03:00
										 |  |  | // XXX must support store stacks...
 | 
					
						
							| 
									
										
										
										
											2022-04-12 22:35:06 +03:00
										 |  |  | // XXX path macros???
 | 
					
						
							|  |  |  | // XXX should we support page symlinking???
 | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | var store =  | 
					
						
							|  |  |  | module.store = { | 
					
						
							|  |  |  | 	exists: function(path){ | 
					
						
							| 
									
										
										
										
											2022-04-16 12:28:06 +03:00
										 |  |  | 		return module.path.normalize(path, 'string') in this }, | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	paths: function(){ | 
					
						
							| 
									
										
										
										
											2022-04-16 16:56:30 +03:00
										 |  |  | 		return Object.keys(this) }, | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 	pages: function(){ | 
					
						
							|  |  |  | 		var that = this | 
					
						
							| 
									
										
										
										
											2022-04-16 16:56:30 +03:00
										 |  |  | 		return this.paths() | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 			.map(function(p){ | 
					
						
							|  |  |  | 				return [p, that[p]] }) }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 23:32:49 +03:00
										 |  |  | 	// 
 | 
					
						
							|  |  |  | 	// 	Resolve page for path
 | 
					
						
							|  |  |  | 	// 	.match(<path>)
 | 
					
						
							|  |  |  | 	// 		-> <path>
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// 	Match paths (non-strict mode)
 | 
					
						
							|  |  |  | 	// 	.match(<pattern>)
 | 
					
						
							|  |  |  | 	// 	.match(<pattern>, false)
 | 
					
						
							|  |  |  | 	// 		-> [<path>, ...]
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// 	Match pages (paths in strict mode)
 | 
					
						
							|  |  |  | 	// 	.match(<pattern>, true)
 | 
					
						
							|  |  |  | 	// 		-> [<path>, ...]
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// In strict mode the trailing star in the pattern will only match 
 | 
					
						
							|  |  |  | 	// actual existing pages, while in non-strict mode the pattern will 
 | 
					
						
							|  |  |  | 	// match all sub-paths.
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	match: function(path, strict=false){ | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 		// pattern match * / **
 | 
					
						
							|  |  |  | 		if(path.includes('*')  | 
					
						
							|  |  |  | 				|| path.includes('**')){ | 
					
						
							| 
									
										
										
										
											2022-04-15 11:11:02 +03:00
										 |  |  | 			var pattern = new RegExp(`^\\/?${ | 
					
						
							| 
									
										
										
										
											2022-04-16 12:28:06 +03:00
										 |  |  | 				module.path.normalize(path, 'string') | 
					
						
							| 
									
										
										
										
											2022-04-15 23:32:49 +03:00
										 |  |  | 					.replace(/\/$/g, '') | 
					
						
							|  |  |  | 					.replace(/\//g, '\\/') | 
					
						
							|  |  |  | 					.replace(/\*\*/g, '.+') | 
					
						
							|  |  |  | 					.replace(/\*/g, '[^\\/]+') }`)
 | 
					
						
							|  |  |  | 			return [...this.paths() | 
					
						
							|  |  |  | 				.reduce(function(res, p){ | 
					
						
							|  |  |  | 					var m = p.match(pattern) | 
					
						
							|  |  |  | 					m | 
					
						
							|  |  |  | 						&& (!strict  | 
					
						
							|  |  |  | 							|| m[0] == p)  | 
					
						
							|  |  |  | 						&& res.add(m[0]) | 
					
						
							|  |  |  | 					return res }, new Set())] } | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 		// search...
 | 
					
						
							| 
									
										
										
										
											2022-04-16 12:28:06 +03:00
										 |  |  | 		for(var p of module.path.paths(path)){ | 
					
						
							| 
									
										
										
										
											2022-04-15 11:11:02 +03:00
										 |  |  | 			if(p in this  | 
					
						
							|  |  |  | 					// NOTE: all paths at this point and in store are 
 | 
					
						
							|  |  |  | 					// 		absolute, so we check both with the leading '/' 
 | 
					
						
							|  |  |  | 					// 		and without it to make things a bit more 
 | 
					
						
							|  |  |  | 					// 		relaxed...
 | 
					
						
							|  |  |  | 					|| (p[0] == '/' ? | 
					
						
							|  |  |  | 						p.slice(1) in this | 
					
						
							|  |  |  | 						: ('/'+ p) in store)){ | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 				return p } } }, | 
					
						
							| 
									
										
										
										
											2022-04-15 23:32:49 +03:00
										 |  |  | 	// 
 | 
					
						
							|  |  |  | 	// 	Resolve page
 | 
					
						
							|  |  |  | 	// 	.get(<path>)
 | 
					
						
							|  |  |  | 	// 		-> <value>
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// 	Resolve pages (non-strict mode)
 | 
					
						
							|  |  |  | 	// 	.get(<pattern>)
 | 
					
						
							|  |  |  | 	// 	.get(<pattern>, false)
 | 
					
						
							|  |  |  | 	// 		-> [<value>, .. ]
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// 	Get pages (strict mode)
 | 
					
						
							|  |  |  | 	// 	.get(<pattern>, true)
 | 
					
						
							|  |  |  | 	// 		-> [<value>, .. ]
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// In strict mode this will not try to resolve pages and will not 
 | 
					
						
							|  |  |  | 	// return pages at paths that do not explicitly exist.
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2022-04-12 02:03:33 +03:00
										 |  |  | 	// XXX should this call actions???
 | 
					
						
							| 
									
										
										
										
											2022-04-15 23:32:49 +03:00
										 |  |  | 	get: function(path, strict=false){ | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 		var that = this | 
					
						
							| 
									
										
										
										
											2022-04-15 23:32:49 +03:00
										 |  |  | 		path = this.match(path, strict) | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 		return path instanceof Array ? | 
					
						
							|  |  |  |    			path.map(function(p){ | 
					
						
							| 
									
										
										
										
											2022-04-15 23:32:49 +03:00
										 |  |  | 				return that[p]  | 
					
						
							|  |  |  | 					?? that[that.match(p)] }) | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 			: this[path] }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// NOTE: deleting and updating only applies to explicit matching 
 | 
					
						
							|  |  |  | 	// 		paths -- no page acquisition is performed...
 | 
					
						
							| 
									
										
										
										
											2022-04-15 23:32:49 +03:00
										 |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 	// XXX should these return this or the data???
 | 
					
						
							|  |  |  | 	update: function(path, data, mode='update'){ | 
					
						
							| 
									
										
										
										
											2022-04-16 12:28:06 +03:00
										 |  |  | 		path = module.path.normalize('/'+ path, 'string') | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 		path = path[path.length-1] == '/' ? | 
					
						
							|  |  |  | 			path.slice(0, -1) | 
					
						
							|  |  |  | 			: path | 
					
						
							| 
									
										
										
										
											2022-04-15 23:32:49 +03:00
										 |  |  | 		this[path] =  | 
					
						
							|  |  |  | 			mode == 'update' ? | 
					
						
							|  |  |  | 				Object.assign( | 
					
						
							|  |  |  | 					this[path] ?? {},  | 
					
						
							|  |  |  | 					data) | 
					
						
							|  |  |  | 				: data | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 		return this }, | 
					
						
							| 
									
										
										
										
											2022-04-15 23:32:49 +03:00
										 |  |  | 	// XXX revise...
 | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 	delete: function(path){ | 
					
						
							| 
									
										
										
										
											2022-04-16 12:28:06 +03:00
										 |  |  | 		path = module.path.normalize(path, 'string') | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 		path = path[path.length-1] == '/' ? | 
					
						
							|  |  |  | 			path.slice(0, -1) | 
					
						
							|  |  |  | 			: path | 
					
						
							| 
									
										
										
										
											2022-04-15 23:32:49 +03:00
										 |  |  | 		// XXX revise...
 | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 		delete this[path]  | 
					
						
							| 
									
										
										
										
											2022-04-15 23:32:49 +03:00
										 |  |  | 		delete this['/'+ path]  | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 		return this }, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 02:03:33 +03:00
										 |  |  | // XXX need to specify page format....
 | 
					
						
							|  |  |  | // XXX need a way to set the page path...
 | 
					
						
							|  |  |  | var actions =  | 
					
						
							|  |  |  | module.actions = { | 
					
						
							|  |  |  | 	__proto__: store, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// base actions (virtual pages)...
 | 
					
						
							|  |  |  | 	'/System/raw': function(page, path){ | 
					
						
							|  |  |  | 		return { text: this.get(path +'/..') } }, | 
					
						
							|  |  |  | 	// XXX ...
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 02:07:19 +03:00
										 |  |  | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 02:03:33 +03:00
										 |  |  | var relProxy =  | 
					
						
							|  |  |  | function(name){ | 
					
						
							|  |  |  | 	return function(path='.', ...args){ | 
					
						
							|  |  |  | 		return this.store[name]( | 
					
						
							| 
									
										
										
										
											2022-04-16 12:28:06 +03:00
										 |  |  | 			module.path.relative(this.path, path),  | 
					
						
							| 
									
										
										
										
											2022-04-12 02:03:33 +03:00
										 |  |  | 			...args) } }  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // page interface...
 | 
					
						
							|  |  |  | var page = | 
					
						
							|  |  |  | module.page = { | 
					
						
							|  |  |  | 	store: undefined, | 
					
						
							| 
									
										
										
										
											2022-04-14 20:58:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 02:03:33 +03:00
										 |  |  | 	path: undefined, | 
					
						
							| 
									
										
										
										
											2022-04-14 20:58:09 +03:00
										 |  |  | 	referrer: undefined, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 02:03:33 +03:00
										 |  |  | 	text: undefined, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-14 20:58:09 +03:00
										 |  |  | 	// relative proxies to store...
 | 
					
						
							| 
									
										
										
										
											2022-04-12 02:03:33 +03:00
										 |  |  | 	exists: relProxy('exists'),  | 
					
						
							|  |  |  | 	match: relProxy('match'),  | 
					
						
							|  |  |  | 	// XXX should this return page objects???
 | 
					
						
							|  |  |  | 	get: relProxy('get'),  | 
					
						
							|  |  |  | 	update: function(path='.', data, mode){ | 
					
						
							|  |  |  | 		if(arguments.length == 1){ | 
					
						
							|  |  |  | 			data = path | 
					
						
							|  |  |  | 			path = '.' } | 
					
						
							| 
									
										
										
										
											2022-04-16 12:28:06 +03:00
										 |  |  | 		return this.store.update(module.path.relative(this.path, path), data, mode) }, | 
					
						
							| 
									
										
										
										
											2022-04-12 02:03:33 +03:00
										 |  |  | 	delete: relProxy('delete'), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// XXX
 | 
					
						
							|  |  |  | 	clear: function(){ | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 02:07:19 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | //---------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 13:53:33 +03:00
										 |  |  | // XXX add escaping...
 | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | var MACRO_PATTERN_STR = | 
					
						
							| 
									
										
										
										
											2022-04-12 22:35:06 +03:00
										 |  |  | 	[[ | 
					
						
							|  |  |  | 		// @macro(arg ..)
 | 
					
						
							| 
									
										
										
										
											2022-04-13 13:53:33 +03:00
										 |  |  | 		// XXX add support for '\)' in args...
 | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | 		'\\\\?@(?<nameInline>MACROS)\\((?<argsInline>([^)])*)\\)', | 
					
						
							| 
									
										
										
										
											2022-04-12 22:35:06 +03:00
										 |  |  | 		// <macro ..> | <macro ../>
 | 
					
						
							| 
									
										
										
										
											2022-04-13 13:53:33 +03:00
										 |  |  | 		// XXX revise escaped > and />
 | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | 		'<\\s*(?<nameOpen>MACROS)(?<argsOpen>\\s+([^>/])*)?/?>', | 
					
						
							| 
									
										
										
										
											2022-04-12 22:35:06 +03:00
										 |  |  | 		// </macro>
 | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | 		'</\\s*(?<nameClose>MACROS)\\s*>', | 
					
						
							| 
									
										
										
										
											2022-04-13 12:18:13 +03:00
										 |  |  | 	].join('|'), 'smig'] | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | var MACRO_PATTERN | 
					
						
							|  |  |  | var MACRO_PATTERN_GROUPS =  | 
					
						
							|  |  |  | 	'<MACROS>'.split(new RegExp(`(${ MACRO_PATTERN_STR })`)).length-2 | 
					
						
							| 
									
										
										
										
											2022-04-13 13:53:33 +03:00
										 |  |  | // XXX still buggy...
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | var MACRO_ARGS_PATTERN =  | 
					
						
							| 
									
										
										
										
											2022-04-12 22:35:06 +03:00
										 |  |  | 	RegExp('('+[ | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 		// named args...
 | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | 		'(?<nameQuoted>[a-zA-Z-_]+)\\s*=([\'"])(?<valueQuoted>([^\\3]|\\\\3)*)\\3\\s*', | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 		'(?<nameUnquoted>[a-zA-Z-_]+)\\s*=(?<valueUnquoted>[^\\s]*)', | 
					
						
							|  |  |  | 		// positional args...
 | 
					
						
							| 
									
										
										
										
											2022-04-13 13:53:33 +03:00
										 |  |  | 		'([\'"])(?<argQuoted>([^\\8]|\\\\8)*)\\8', | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 		'(?<arg>[^\\s]+)', | 
					
						
							| 
									
										
										
										
											2022-04-13 12:18:13 +03:00
										 |  |  | 	].join('|') +')', 'smig') | 
					
						
							|  |  |  | // XXX do we need basic inline and block commets a-la lisp???
 | 
					
						
							|  |  |  | var COMMENT_PATTERN =  | 
					
						
							|  |  |  | 	RegExp('('+[ | 
					
						
							|  |  |  | 		// <!--[pwiki[ .. ]]-->
 | 
					
						
							|  |  |  | 		'<!--\\[pwiki\\[(?<uncomment>.*)\\]\\]-->', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// <pwiki-comment> .. </pwiki-comment>
 | 
					
						
							|  |  |  | 		'<\\s*pwiki-comment[^>]*>.*<\\/\\s*pwiki-comment\\s*>', | 
					
						
							|  |  |  | 		// <pwiki-comment .. />
 | 
					
						
							|  |  |  | 		'<\\s*pwiki-comment[^\\/>]*\\/>', | 
					
						
							|  |  |  | 	].join('|') +')', 'smig') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var clearComments =  | 
					
						
							|  |  |  | module.clearComments = | 
					
						
							|  |  |  | function(str){ | 
					
						
							|  |  |  | 	return str | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | 		.replace(COMMENT_PATTERN,  | 
					
						
							|  |  |  | 			function(...a){ | 
					
						
							|  |  |  | 				return a.pop().uncomment  | 
					
						
							|  |  |  | 					|| '' }) } | 
					
						
							| 
									
										
										
										
											2022-04-13 12:18:13 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // 	<item> ::=
 | 
					
						
							|  |  |  | // 		<string>
 | 
					
						
							|  |  |  | // 		| {
 | 
					
						
							|  |  |  | // 			name: <string>,
 | 
					
						
							|  |  |  | // 			type: 'inline'
 | 
					
						
							|  |  |  | // 				| 'element'
 | 
					
						
							|  |  |  | // 				| 'opening'
 | 
					
						
							|  |  |  | // 				| 'closing',
 | 
					
						
							|  |  |  | // 			args: {
 | 
					
						
							|  |  |  | // 				<index>: <value>,
 | 
					
						
							|  |  |  | // 				<key>: <value>,
 | 
					
						
							|  |  |  | // 				...
 | 
					
						
							|  |  |  | // 			}
 | 
					
						
							|  |  |  | // 			match: <string>,
 | 
					
						
							|  |  |  | // 		}
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | //
 | 
					
						
							|  |  |  | // NOTE: this internally uses macros' keys to generate the lexing pattern.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // XXX closure: macros
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | // XXX feels a bit ugly...
 | 
					
						
							|  |  |  | var lex = | 
					
						
							|  |  |  | module.lex =  | 
					
						
							| 
									
										
										
										
											2022-04-13 13:53:33 +03:00
										 |  |  | function*(str){ | 
					
						
							| 
									
										
										
										
											2022-04-13 12:18:13 +03:00
										 |  |  | 	// NOTE: we are doing a separate pass for comments to completely 
 | 
					
						
							|  |  |  | 	// 		decouple them from the base macro syntax, making them fully 
 | 
					
						
							|  |  |  | 	// 		transparent...
 | 
					
						
							|  |  |  | 	str = clearComments(str) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var lst = str.split( | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | 		module.MACRO_PATTERN  | 
					
						
							|  |  |  | 			?? (MACRO_PATTERN = module.MACRO_PATTERN =  | 
					
						
							|  |  |  | 				new RegExp( | 
					
						
							|  |  |  | 					'('+ MACRO_PATTERN_STR[0] | 
					
						
							|  |  |  | 						.replace(/MACROS/g,  | 
					
						
							|  |  |  | 							Object.keys(macros).join('|')) +')', | 
					
						
							|  |  |  | 					MACRO_PATTERN_STR[1]))) | 
					
						
							| 
									
										
										
										
											2022-04-13 12:18:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 	var macro = false | 
					
						
							|  |  |  | 	while(lst.length > 0){ | 
					
						
							|  |  |  | 		if(macro){ | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | 			var match = lst.splice(0, MACRO_PATTERN_GROUPS)[0] | 
					
						
							|  |  |  | 			// NOTE: we essentially are parsing the detected macro a 
 | 
					
						
							|  |  |  | 			// 		second time here, this gives us access to named groups
 | 
					
						
							|  |  |  | 			// 		avoiding maintaining match indexes with the .split(..) 
 | 
					
						
							|  |  |  | 			// 		output...
 | 
					
						
							|  |  |  | 			// XXX for some reason .match(..) here returns a list with a string...
 | 
					
						
							|  |  |  | 			var cur = [...match.matchAll(MACRO_PATTERN)][0].groups | 
					
						
							|  |  |  | 			// special case: escaped inline macro -> keep as text...
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 			if(match.startsWith('\\@')){ | 
					
						
							|  |  |  | 				yield match | 
					
						
							|  |  |  | 				macro = false  | 
					
						
							|  |  |  | 				continue } | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | 			// args...
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 			var args = {} | 
					
						
							|  |  |  | 			var i = -1 | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | 			for(var {groups}  | 
					
						
							|  |  |  | 					of (cur.argsInline ?? cur.argsOpen ?? '') | 
					
						
							|  |  |  | 						.matchAll(MACRO_ARGS_PATTERN)){ | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 				i++ | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | 				args[groups.nameQuoted ?? groups.nameUnquoted ?? i] = | 
					
						
							|  |  |  | 					groups.valueQuoted  | 
					
						
							|  |  |  | 					?? groups.valueUnquoted  | 
					
						
							|  |  |  | 					?? groups.argQuoted  | 
					
						
							|  |  |  | 					?? groups.arg } | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 			// macro-spec...
 | 
					
						
							|  |  |  | 			yield { | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | 				name: (cur.nameInline  | 
					
						
							|  |  |  | 						?? cur.nameOpen  | 
					
						
							|  |  |  | 						?? cur.nameClose) | 
					
						
							|  |  |  | 					.toLowerCase(), | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 				type: match[0] == '@' ? | 
					
						
							|  |  |  | 						'inline' | 
					
						
							|  |  |  | 					: match[1] == '/' ? | 
					
						
							|  |  |  | 						'closing' | 
					
						
							|  |  |  | 					: match[match.length-2] == '/' ? | 
					
						
							| 
									
										
										
										
											2022-04-13 12:18:13 +03:00
										 |  |  | 						'element' | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 					: 'opening', | 
					
						
							|  |  |  | 				args,  | 
					
						
							|  |  |  | 				match, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			macro = false | 
					
						
							|  |  |  | 		// normal text...
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			var str = lst.shift() | 
					
						
							|  |  |  | 			// skip empty strings from output...
 | 
					
						
							|  |  |  | 			if(str != ''){ | 
					
						
							|  |  |  | 				yield str } | 
					
						
							|  |  |  | 			macro = true } } } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 13:53:33 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 12:18:13 +03:00
										 |  |  | //
 | 
					
						
							|  |  |  | // 	<item> ::=
 | 
					
						
							|  |  |  | // 		<string>
 | 
					
						
							|  |  |  | // 		| {
 | 
					
						
							|  |  |  | // 			type: 'inline'
 | 
					
						
							|  |  |  | // 				| 'element'
 | 
					
						
							|  |  |  | // 				| 'block',
 | 
					
						
							| 
									
										
										
										
											2022-04-13 20:24:08 +03:00
										 |  |  | // 			block: [
 | 
					
						
							|  |  |  | // 				<item>,
 | 
					
						
							|  |  |  | // 				...
 | 
					
						
							|  |  |  | // 			],
 | 
					
						
							| 
									
										
										
										
											2022-04-13 12:18:13 +03:00
										 |  |  | //
 | 
					
						
							|  |  |  | //			// rest of items are the same as for lex(..)
 | 
					
						
							|  |  |  | // 			...
 | 
					
						
							|  |  |  | // 		}
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | //
 | 
					
						
							|  |  |  | // NOTE: this internaly uses macros to check for propper nesting
 | 
					
						
							| 
									
										
										
										
											2022-04-18 23:35:47 +03:00
										 |  |  | // XXX should we use typrs.js???
 | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | // XXX closure: macros
 | 
					
						
							| 
									
										
										
										
											2022-04-13 20:24:08 +03:00
										 |  |  | // XXX normalize lex to be a generator (???)
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | var group =  | 
					
						
							|  |  |  | module.group = | 
					
						
							|  |  |  | function*(lex, to=false){ | 
					
						
							|  |  |  | 	// NOTE: we are not using for .. of .. here as it depletes the 
 | 
					
						
							|  |  |  | 	// 		generator even if the end is not reached...
 | 
					
						
							|  |  |  | 	while(true){ | 
					
						
							|  |  |  | 		var {value, done} = lex.next() | 
					
						
							| 
									
										
										
										
											2022-04-13 20:24:08 +03:00
										 |  |  | 		// check if unclosed blocks remaining...
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 		if(done){ | 
					
						
							|  |  |  | 			if(to){ | 
					
						
							| 
									
										
										
										
											2022-04-13 20:24:08 +03:00
										 |  |  | 				throw new Error( | 
					
						
							|  |  |  | 					'Premature end of unpit: Expected closing "'+ to +'"') } | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 			return } | 
					
						
							| 
									
										
										
										
											2022-04-13 20:24:08 +03:00
										 |  |  | 		// assert nesting rules...
 | 
					
						
							|  |  |  | 		if(macros[value.name] instanceof Array | 
					
						
							|  |  |  | 				&& macros[value.name].includes(to)){ | 
					
						
							|  |  |  | 			throw new Error( | 
					
						
							|  |  |  | 				'Unexpected "'+ value.name +'" macro'  | 
					
						
							|  |  |  | 					+(to ?  | 
					
						
							|  |  |  | 						' in "'+to+'"'  | 
					
						
							|  |  |  | 						: '')) } | 
					
						
							|  |  |  | 		// open block...
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 		if(value.type == 'opening'){ | 
					
						
							|  |  |  | 			value.body = [...group(lex, value.name)] | 
					
						
							|  |  |  | 			value.type = 'block' | 
					
						
							|  |  |  | 			yield value | 
					
						
							|  |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2022-04-13 20:24:08 +03:00
										 |  |  | 		// close block...
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 		} else if(value.type == 'closing'){ | 
					
						
							|  |  |  | 			if(value.name != to){ | 
					
						
							|  |  |  | 				throw new Error('Unexpected closing "'+ value.name +'"') } | 
					
						
							|  |  |  | 			// NOTE: we are intentionally not yielding the value here...
 | 
					
						
							|  |  |  | 			return }  | 
					
						
							|  |  |  | 		yield value } }  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 12:28:05 +03:00
										 |  |  | var parse =  | 
					
						
							| 
									
										
										
										
											2022-04-13 20:24:08 +03:00
										 |  |  | module.parse = | 
					
						
							| 
									
										
										
										
											2022-04-13 12:28:05 +03:00
										 |  |  | function*(str){ | 
					
						
							|  |  |  | 	yield* group(lex(str)) } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-17 02:13:15 +03:00
										 |  |  | // XXX closure: macros
 | 
					
						
							| 
									
										
										
										
											2022-04-16 22:35:51 +03:00
										 |  |  | var expand = | 
					
						
							|  |  |  | module.expand = | 
					
						
							|  |  |  | function(str){ | 
					
						
							|  |  |  | 	var parser = parse(str) | 
					
						
							|  |  |  | 	while(true){ | 
					
						
							|  |  |  | 		var {elem, done} = parse.next() | 
					
						
							|  |  |  | 		if(done){ | 
					
						
							|  |  |  | 			return } | 
					
						
							|  |  |  | 		// text block...
 | 
					
						
							|  |  |  | 		if(typeof(elem) == 'string'){ | 
					
						
							|  |  |  | 			yield elem } | 
					
						
							| 
									
										
										
										
											2022-04-17 02:13:15 +03:00
										 |  |  | 		// macro...
 | 
					
						
							|  |  |  | 		var {name, args, block} = elem | 
					
						
							|  |  |  | 		// XXX need context...
 | 
					
						
							|  |  |  | 		yield* macros[name](args, block) | 
					
						
							| 
									
										
										
										
											2022-04-16 22:35:51 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 12:18:13 +03:00
										 |  |  | // XXX
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | var expandPage =  | 
					
						
							|  |  |  | module.expandPage = | 
					
						
							|  |  |  | function(page){ | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //---------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | var WIKIWORD_PATTERN = | 
					
						
							|  |  |  | 	RegExp('('+[ | 
					
						
							|  |  |  | 		//'\\\\?(\\/|\\./|\\.\\./|>>|[A-Z][_a-z0-9]+[A-Z/])[_a-zA-Z0-9/]*',
 | 
					
						
							|  |  |  | 		'\\\\?\\/?(\\./|\\.\\./|>>|[A-Z][_a-z0-9]+[A-Z/])[_a-zA-Z0-9/]*', | 
					
						
							|  |  |  | 		'\\\\?\\[[^\\]]+\\]', | 
					
						
							|  |  |  | 	].join('|') +')', 'g') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 12:18:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | //---------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-14 15:41:48 +03:00
										 |  |  | var filters = { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-04-13 12:18:13 +03:00
										 |  |  | var macros = { | 
					
						
							|  |  |  | 	now: function(){}, | 
					
						
							|  |  |  | 	filter: function(){}, | 
					
						
							|  |  |  | 	include: function(){}, | 
					
						
							|  |  |  | 	source: function(){}, | 
					
						
							|  |  |  | 	quote: function(){}, | 
					
						
							|  |  |  | 	macro: function(){}, | 
					
						
							|  |  |  | 	slot: function(){}, | 
					
						
							| 
									
										
										
										
											2022-04-13 20:24:08 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// nesting rules...
 | 
					
						
							|  |  |  | 	'else': ['macro'], | 
					
						
							| 
									
										
										
										
											2022-04-13 12:18:13 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 03:14:21 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 01:31:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							|  |  |  | * vim:set ts=4 sw=4 :                               */ return module }) |