minor refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-07-21 17:03:43 +03:00
parent f5d2ec7b7b
commit 2992634e54

128
wiki.js
View File

@ -58,6 +58,13 @@ var setWikiWords = function(text, show_brackets, skip){
/*********************************************************************/ /*********************************************************************/
function Macro(doc, args, func){
func.doc = doc
func.macro_args = args
return func
}
// XXX should inline macros support named args??? // XXX should inline macros support named args???
var macro = { var macro = {
@ -75,7 +82,7 @@ var macro = {
// ..text.. // ..text..
// </macro> // </macro>
// //
// // XXX should inline macros support named args???
__macro__pattern__: __macro__pattern__:
/<([a-zA-Z-_:]+)(.|[\n\r])*?(>(.|[\n\r])*?<\/\1>|\/>)|@([a-zA-Z-_]+)\(([^)]*)\)/mg, /<([a-zA-Z-_:]+)(.|[\n\r])*?(>(.|[\n\r])*?<\/\1>|\/>)|@([a-zA-Z-_]+)\(([^)]*)\)/mg,
@ -88,24 +95,21 @@ var macro = {
// Macros... // Macros...
// //
// XXX do not like how args are defined...
// ...putting them in the same pot as the macro-handlers is
// error-prone, need a bit more separation -- constructor?
// Macro( <doc>, <args>, <func> )
macro: { macro: {
// select filter to post-process text... // select filter to post-process text...
filter_args: ['name'], filter: Macro('Filter to post-process text',
filter: function(context, args, text, state){ ['name'],
var filter = args[0] || args.name function(context, args, text, state){
var filter = args[0] || args.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...
// //
@ -118,72 +122,66 @@ var macro = {
// ...if required this can be done via global and local // ...if required this can be done via global and local
// filters... (now filters are only local) // filters... (now filters are only local)
// XXX do we need to render just one slot??? (slot arg) // XXX do we need to render just one slot??? (slot arg)
// XXX might be a good idea to wrap the result in a tag to enable // e.g. include PageX SlotY
// in-place editing... include: Macro('Include page',
include_args: ['src'], ['src'],
include: function(context, args, _, state){ function(context, args, _, state){
var path = args.src var path = args.src
// get and prepare the included page... // get and prepare the included page...
state.include state.include
.push(context.get(path)) .push(context.get(path))
/*
// XXX do we need to quote the path here??? ...might get wikiword-ed ;)
.push('<span class="include" src="'+path+'">'
+context.get(path).text
+'</span>')
//*/
// return the marker... // return the marker...
return this.__include_marker__ return this.__include_marker__
}, }),
/*
// NOTE: this is similar to include, the difference is that this // NOTE: this is similar to include, the difference is that this
// includes the page source to the current context while // includes the page source to the current context while
// include works in an isolated context // include works in an isolated context
// XXX currently this will not parse the target... source: Macro('Include page source (without parsing)',
source_args: ['src'], ['src'],
source: function(context, args, _, state){ function(context, args, _, state){
var path = args.src var path = args.src
return context.get(path).raw return context.get(path).raw
}, }),
//*/
// fill/define slot (stage 1)... // fill/define slot (stage 1)...
slot_args: ['name'], slot: Macro('Define/fill slot',
slot: function(context, args, text, state){ ['name'],
var name = args.name function(context, args, text, state){
var name = args.name
text = this.parse(context, text, state, true) text = this.parse(context, text, state, true)
if(state.slots[name] == null){ if(state.slots[name] == null){
state.slots[name] = text state.slots[name] = text
// return a slot macro parsable by stage 2... // return a slot macro parsable by stage 2...
return '<slot name="'+name+'">'+ text +'</slot>' return '<slot name="'+name+'">'+ text +'</slot>'
} else if(name in state.slots){ } else if(name in state.slots){
state.slots[name] = text state.slots[name] = text
return '' return ''
} }
}, }),
}, },
// Post macros... // Post macros...
// //
post_macro: { post_macro: {
slot_args: ['name'], slot: Macro('',
slot: function(context, args, text, state){ ['name'],
var name = args.name function(context, args, text, state){
var name = args.name
if(state.slots[name] == null){ if(state.slots[name] == null){
return text return text
} else if(name in state.slots){ } else if(name in state.slots){
return state.slots[name] return state.slots[name]
} }
}, }),
}, },
// Filters... // Filters...
@ -231,7 +229,7 @@ var macro = {
var a = d[2].split(/\s+/g) var a = d[2].split(/\s+/g)
a.forEach(function(e, i){ a.forEach(function(e, i){
args[(stage[res.name + '_args'] || [])[i]] = e args[((stage[res.name] || {}).macro_args || [])[i]] = e
}) })
// html-like... // html-like...
@ -309,7 +307,8 @@ var macro = {
}) })
// merge includes... // merge includes...
// XXX need to check for errors (includes too short/long)... // XXX need to check for errors (includes list shorter/longer
// than number of markers)...
text = text.replace(RegExp(this.__include_marker__, 'g'), function(){ text = text.replace(RegExp(this.__include_marker__, 'g'), function(){
var page = state.include.shift() var page = state.include.shift()
// NOTE: we are quoting html here, this is done to prevent // NOTE: we are quoting html here, this is done to prevent
@ -319,7 +318,6 @@ var macro = {
return $('<span>') return $('<span>')
.addClass('include') .addClass('include')
.attr('src', page.path) .attr('src', page.path)
// XXX need to pass the state.slots to parser...
.html(page.parse({ slots: state.slots }, true))[0] .html(page.parse({ slots: state.slots }, true))[0]
.outerHTML .outerHTML
}) })