refactoring....

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-03-31 01:18:09 +03:00
parent f0f4e9a086
commit 70b9976536

104
wiki.js
View File

@ -138,13 +138,11 @@ var macro = {
['name'], ['name'],
function(context, elem, state){ function(context, elem, state){
var filter = $(elem).attr('name') var filter = $(elem).attr('name')
filter[0] == '-' ? filter[0] == '-' ?
// disabled -- keep at head of list... // disabled -- keep at head of list...
state.filters.unshift(filter) state.filters.unshift(filter)
// normal -- tail... // normal -- tail...
: state.filters.push(filter) : state.filters.push(filter)
return '' }), return '' }),
// include page/slot... // include page/slot...
@ -156,11 +154,9 @@ var macro = {
['src', 'isolated', 'text'], ['src', 'isolated', 'text'],
function(context, elem, state){ function(context, elem, state){
var path = $(elem).attr('src') var path = $(elem).attr('src')
// get and prepare the included page... // get and prepare the included page...
state.include state.include
.push([elem, context.get(path)]) .push([elem, context.get(path)])
// return the marker... // return the marker...
return this.__include_marker__ }), return this.__include_marker__ }),
@ -171,7 +167,6 @@ var macro = {
['src'], ['src'],
function(context, elem, state){ function(context, elem, state){
var path = $(elem).attr('src') var path = $(elem).attr('src')
return context.get(path) return context.get(path)
.map(function(page){ .map(function(page){
return page.raw }) return page.raw })
@ -182,7 +177,6 @@ var macro = {
function(context, elem, state){ function(context, elem, state){
elem = $(elem) elem = $(elem)
var path = elem.attr('src') var path = elem.attr('src')
return $(context.get(path) return $(context.get(path)
.map(function(page){ .map(function(page){
return elem return elem
@ -329,15 +323,13 @@ var macro = {
'page-text': Macro('', 'page-text': Macro('',
['src'], ['src'],
function(context, elem, state){ function(context, elem, state){
elem = $(elem) return $(elem)
.html(context.get(elem.attr('src')).text) }),
return elem.html(context.get(elem.attr('src')).text) }),
'page-raw': Macro('', 'page-raw': Macro('',
['src'], ['src'],
function(context, elem, state){ function(context, elem, state){
elem = $(elem) return $(elem)
.text(context.get(elem.attr('src')).text) }),
return elem.text(context.get(elem.attr('src')).text) }),
//*/ //*/
}, },
@ -372,7 +364,10 @@ var macro = {
wikiword: function(context, elem){ wikiword: function(context, elem){
return $('<span>') return $('<span>')
.html(setWikiWords($(elem).html(), true, this.__include_marker__)) }, .html(setWikiWords(
$(elem).html(),
true,
this.__include_marker__)) },
// XXX need to remove all on* event handlers... // XXX need to remove all on* event handlers...
noscript: function(context, elem){ noscript: function(context, elem){
return $(elem) return $(elem)
@ -524,9 +519,15 @@ var macro = {
var a = d[2] var a = d[2]
.split(/((['"]).*?\2)|\s+/g) .split(/((['"]).*?\2)|\s+/g)
// cleanup... // cleanup...
.filter(function(e){ return e && e != '' && !/^['"]$/.test(e)}) .filter(function(e){
return e
&& e != ''
&& !/^['"]$/.test(e)})
// remove quotes... // remove quotes...
.map(function(e){ return /^(['"]).*\1$/.test(e) ? e.slice(1, -1) : e }) .map(function(e){
return /^(['"]).*\1$/.test(e) ?
e.slice(1, -1)
: e })
// add the attrs to the element... // add the attrs to the element...
name != '*' name != '*'
@ -543,10 +544,13 @@ var macro = {
return res instanceof jQuery ? return res instanceof jQuery ?
// merge html of the returned set of elements... // merge html of the returned set of elements...
res.map(function(i, e){ return e.outerHTML }) res
.map(function(i, e){
return e.outerHTML })
.toArray() .toArray()
.join('\n') .join('\n')
: typeof(res) != typeof('str') ? res.outerHTML : typeof(res) != typeof('str') ?
res.outerHTML
: res } : res }
return match }) } return match }) }
@ -675,7 +679,8 @@ var macro = {
var n = e.attr('name') var n = e.attr('name')
n in slots && e.detach() n in slots
&& e.detach()
slots[n] = e }) slots[n] = e })
// place slots... // place slots...
@ -720,11 +725,16 @@ var macro = {
// ...is changing .path a good idea for redirecting??? // ...is changing .path a good idea for redirecting???
var BaseData = { var BaseData = {
// Macro acces to standard page attributes (paths)... // Macro acces to standard page attributes (paths)...
'System/title': function(){ return this.get('..').title }, 'System/title': function(){
'System/path': function(){ return this.dir }, return this.get('..').title },
'System/dir': function(){ return this.get('..').dir }, 'System/path': function(){
'System/location': function(){ return this.dir }, return this.dir },
'System/resolved': function(){ return this.get('..').acquire() }, 'System/dir': function(){
return this.get('..').dir },
'System/location': function(){
return this.dir },
'System/resolved': function(){
return this.get('..').acquire() },
// page data... // page data...
// //
@ -733,8 +743,10 @@ var BaseData = {
// .get('./raw').text // .get('./raw').text
// is the same as: // is the same as:
// .get('.').raw // .get('.').raw
'System/raw': function(){ return this.get('..').raw }, 'System/raw': function(){
'System/text': function(){ return this.get('..').text }, return this.get('..').raw },
'System/text': function(){
return this.get('..').text },
// XXX move this to Wiki.children + rename... // XXX move this to Wiki.children + rename...
'System/list': function(){ 'System/list': function(){
@ -760,13 +772,18 @@ var BaseData = {
var wiki = this.__wiki_data var wiki = this.__wiki_data
Object.keys(wiki).forEach(function(k){ Object.keys(wiki).forEach(function(k){
;(wiki[k].links || []).forEach(function(l){ ;(wiki[k].links || [])
(l == p || that.get(path2lst(l).slice(0, -1)).acquire('./'+path2lst(l).pop()) == p) .forEach(function(l){
;(l == p
|| that
.get(path2lst(l).slice(0, -1))
.acquire('./'+path2lst(l).pop()) == p)
&& res.push([l, k]) }) }) && res.push([l, k]) }) })
return res return res
//.map(function(e){ return '['+ e[0] +'] <i>from page: ['+ e[1] +']</i>' }) //.map(function(e){ return '['+ e[0] +'] <i>from page: ['+ e[1] +']</i>' })
.map(function(e){ return '['+ e[1] +'] <i>-&gt; ['+ e[0] +']</i>' }) .map(function(e){
return '['+ e[1] +'] <i>-&gt; ['+ e[0] +']</i>' })
.sort() .sort()
.join('<br>') }, .join('<br>') },
@ -1272,11 +1289,16 @@ var Wiki = {
// //
get raw(){ get raw(){
var data = this.data var data = this.data
data = data instanceof Function ? data.call(this, this) : data data = data instanceof Function ?
data.call(this, this)
: data
return typeof(data) == typeof('str') ? data return typeof(data) == typeof('str') ?
data
: data != null ? : data != null ?
('raw' in data ? data.raw : data.text) ('raw' in data ?
data.raw
: data.text)
: '' }, : '' },
set raw(value){ set raw(value){
var l = this.location var l = this.location
@ -1313,9 +1335,13 @@ var Wiki = {
var links = data.links = data.links var links = data.links = data.links
|| (this.raw.match(this.__wiki_link__) || []) || (this.raw.match(this.__wiki_link__) || [])
// unwrap explicit links... // unwrap explicit links...
.map(function(e){ return e[0] == '[' ? e.slice(1, -1) : e }) .map(function(e){
return e[0] == '[' ?
e.slice(1, -1)
: e })
// unique... // unique...
.filter(function(e, i, l){ return l.slice(0, i).indexOf(e) == -1 }) .filter(function(e, i, l){
return l.slice(0, i).indexOf(e) == -1 })
return links }, return links },
@ -1362,7 +1388,9 @@ var Wiki = {
var data = this.__wiki_data var data = this.__wiki_data
var _get = function(path, title, lst){ var _get = function(path, title, lst){
lst = (lst == null || lst.length == 0) ? [''] : lst lst = (lst == null || lst.length == 0) ?
['']
: lst
for(var i=0; i < lst.length; i++){ for(var i=0; i < lst.length; i++){
var p = path.concat([lst[i], title]) var p = path.concat([lst[i], title])
if(that.exists(p)){ if(that.exists(p)){
@ -1385,10 +1413,14 @@ var Wiki = {
path.pop() } path.pop() }
// default paths... // default paths...
var p = _get(path, title, post_acquire_from) var p = _get(
path,
title,
post_acquire_from)
// system path... // system path...
|| this.__system__ || this.__system__
&& _get([this.__system__], title) && _get([this.__system__],
title)
// NOTE: this may be null... // NOTE: this may be null...
return p return p