From 54edbc70b1f6dc164252b0662cd33c105a4370f0 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 17 Jul 2016 15:58:50 +0300 Subject: [PATCH] tweaking and refactoring -- most of the parser is done, not yet hooked in... Signed-off-by: Alex A. Naanou --- index.html | 28 +---------------- wiki.js | 91 +++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 77 insertions(+), 42 deletions(-) diff --git a/index.html b/index.html index 45e10a4..0af638f 100755 --- a/index.html +++ b/index.html @@ -111,7 +111,7 @@ var reload = () => { }) } - $('.text').html(activateWikiWords(text, filters.indexOf('show_link_brackets') < 0 && editing)) + $('.text').html(setWikiWords(text, filters.indexOf('show_link_brackets') < 0 && editing)) // XXX save... localStorage['wiki-data'] = JSON.stringify(Wiki.__wiki_data) @@ -131,32 +131,6 @@ var go = (path) => { reload() } -var clearWikiWords = elem => { - // clear existing... - elem.find('.WikiWord').each(function(){ - $(this).attr('braced') == 'yes' ? - $(this).replaceWith(['['].concat(this.childNodes, [']'])) - : $(this).replaceWith(this.childNodes) - }) - return elem -} - -var activateWikiWords = (text, show_brackets) => - text - // set new... - .replace( - Wiki.__wiki_link__, - function(l){ - return '' - + (show_brackets && l[0] == '[' ? l.slice(1, -1) : l) - +'' - }) - - $(() => { $(window).on('popstate', function(evt){ event.state diff --git a/wiki.js b/wiki.js index 7ab9daf..478db82 100755 --- a/wiki.js +++ b/wiki.js @@ -6,6 +6,32 @@ //var DEBUG = DEBUG != null ? DEBUG : true +var clearWikiWords = elem => { + // clear existing... + elem.find('.WikiWord').each(function(){ + $(this).attr('braced') == 'yes' ? + $(this).replaceWith(['['].concat(this.childNodes, [']'])) + : $(this).replaceWith(this.childNodes) + }) + return elem +} + +var setWikiWords = (text, show_brackets) => + text + // set new... + .replace( + Wiki.__wiki_link__, + function(l){ + return '' + + (show_brackets && l[0] == '[' ? l.slice(1, -1) : l) + +'' + }) + + /*********************************************************************/ @@ -498,11 +524,27 @@ var Wiki = { var macro = { - // XXX this misses nested tags (i.e. pattern within a tag) within - // one line for some reason.... + + // Abstract macro syntax: + // Inline macro: + // @macro(arg ..) + // + // HTML-like: + // + // + // HTML-like with body: + // + // ..text.. + // + // __macro__pattern__: /<([a-zA-Z-_:]+)(.|[\n\r])*?(>(.|[\n\r])*?<\/\1>|\/>)|@([a-zA-Z-_]+)\(([^)]*)\)/mg, + + // default filters... + // + // NOTE: these are added AFTER the user defined filters... __filters__: [ + 'wikiword', ], context: null, @@ -513,10 +555,12 @@ var macro = { filter: { default: 'html', - html: function(text){ return $('
').html(text) }, + html: function(text){ return $('
').html(text).html() }, json: 'text', - text: function(text){ return $('
').text(text) }, + text: function(text){ return $('
').text(text).html() }, + + wikiword: function(text){ return setWikiWords(text) }, }, // macro stage 1... @@ -527,7 +571,11 @@ var macro = { filter: function(args, text, _, filters){ var filter = args[0] || args.name - filters.push(filter) + filter[0] == '-' ? + // disabled -- keep at head of list... + filters.unshift(filter) + // normal -- tail... + : filters.push(filter) return '' }, @@ -609,7 +657,6 @@ var macro = { return res }, - // XXX add support for disabled filters -- -filter // XXX do we need to parse the contents of tags here??? (nested patterns?) parse: function(text, context){ var that = this @@ -638,15 +685,29 @@ var macro = { text = _parse(text, this.macro2) // filter stage.... - filters.forEach(function(k){ - var seen = [] - // get filter aliases... - while(typeof(k) == typeof('str') && seen.indexOf(k) == -1){ - seen.push(k) - k = that.filter[k] - } - text = k.call(that, text) - }) + filters + .concat(this.__filters__) + // unique -- leave last occurance.. + .filter(function(k, i, lst){ + return k[0] != '-' + // filter dupplicates... + && lst.slice(i+1).indexOf(k) == -1 + // filter disabled... + && lst.slice(0, i).indexOf('-' + k) == -1 + }) + // unique -- leave first occurance.. + //.filter(function(k, i, lst){ return lst.slice(0, i).indexOf(k) == -1 }) + // apply the filters... + .forEach(function(k){ + // get filter aliases... + var seen = [] + while(typeof(k) == typeof('str') && seen.indexOf(k) == -1){ + seen.push(k) + k = that.filter[k] + } + // call the filter + text = k.call(that, text) + }) return text },