From 5172666b45168c746f1a47a334faba565ca34e59 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 28 Jul 2016 09:07:46 +0300 Subject: [PATCH] reworked macros and re-did base teplate pages... Signed-off-by: Alex A. Naanou --- index.html | 23 ++++++-- wiki.js | 152 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 144 insertions(+), 31 deletions(-) diff --git a/index.html b/index.html index f87705b..189200a 100755 --- a/index.html +++ b/index.html @@ -4,6 +4,13 @@ pWiki @@ -37,13 +44,13 @@ var update_editor = function(){ // XXX make this update on enter... $('.title') .on('blur', () => { - //Wiki.title = $('.title').text() - //reload() + Wiki.title = $('.title').text() + reload() }) // live update text... // XXX is this the right way to go for large documents??? - $('.text') + var text = $('.text') .focus(function(){ console.log('EDITING:', Wiki.path) @@ -68,7 +75,10 @@ var update_editor = function(){ reload() }) - $('.raw') + //text.html(Wiki.get(text.attr('saveto')).text) + + + var raw = $('.raw') .focus(function(){ var to = $(this).attr('saveto') || '.' console.log('EDITING:', Wiki.get(to).path) @@ -83,7 +93,9 @@ var update_editor = function(){ .blur(() => { reload() }) - .text($('.raw').html()) + //.text($('.raw').html()) + + //raw.text(Wiki.get(raw.attr('saveto')).raw) } var go = (path) => { @@ -154,6 +166,7 @@ $(() => { } }) }) + diff --git a/wiki.js b/wiki.js index 8d32822..6ee69ab 100755 --- a/wiki.js +++ b/wiki.js @@ -143,7 +143,7 @@ var macro = { // get and prepare the included page... state.include - .push(context.get(path)) + .push([elem, context.get(path)]) // return the marker... return this.__include_marker__ @@ -160,35 +160,68 @@ var macro = { return context.get(path).raw }), + quote: Macro('Include quoted page source (without parsing)', + ['src'], + function(context, elem, state){ + elem = $(elem) + var path = elem.attr('src') + + return elem.text(context.get(path).raw) + }), + + /* // fill/define slot (stage 1)... // // XXX which should have priority the arg text or the content??? - slot: Macro('Define/fill slot', + _slot: Macro('Define/fill slot', ['name', 'text'], - function(context, elem, state){ + function(context, elem, state, parse){ var name = $(elem).attr('name') // XXX text = $(elem).html() text = text == '' ? $(elem).attr('text') : text text = this.parse(context, text, state, true) + //text = parse(elem) if(state.slots[name] == null){ state.slots[name] = text // return a slot macro parsable by stage 2... - return ''+ text +'' + //return '<_slot name="'+name+'">'+ text +'' + return elem } else if(name in state.slots){ state.slots[name] = text return '' } }), + //*/ + // convert @ macro to html-like + parse content... + slot: Macro('Define/fill slot', + ['name', 'text'], + function(context, elem, state, parse){ + elem = $(elem) + var name = elem.attr('name') + + // XXX + text = elem.html() + text = text.trim() == '' ? + elem.html(elem.attr('text') || '').html() + : text + text = parse(elem) + + elem.attr('text', null) + elem.html(text) + + return elem + }), }, // Post macros... // post_macro: { - slot: Macro('', + /* + _slot: Macro('', ['name'], function(context, elem, state){ var name = $(elem).attr('name') @@ -200,6 +233,25 @@ var macro = { return state.slots[name] } }), + //*/ + + /* + // XXX rename to post-include and post-quote + 'page-text': Macro('', + ['src'], + function(context, elem, state){ + elem = $(elem) + + return elem.html(context.get(elem.attr('src')).text) + }), + 'page-raw': Macro('', + ['src'], + function(context, elem, state){ + elem = $(elem) + + return elem.text(context.get(elem.attr('src')).text) + }), + //*/ }, // Filters... @@ -246,7 +298,8 @@ var macro = { // 3) merge and parse included pages: // 1) expand macros // 2) apply filters - // 4) expand post-macros + // 4) fill slots + // 5) expand post-macros // // NOTE: stage 4 parsing is executed on the final merged page only // once. i.e. it is not performed on the included pages. @@ -299,7 +352,13 @@ var macro = { }) // call macro... - return macro[name].call(that, context, elem, state) + var res = macro[name] + .call(that, context, elem, state, + function(elem){ _parse(context, elem, macro) }) + + return res instanceof $ ? res[0].outerHTML + : typeof(res) != typeof('str') ? res.outerHTML + : res } return match @@ -321,7 +380,9 @@ var macro = { // macro match -> call macro... if(name in macro){ - $(e).replaceWith(macro[name].call(that, context, e, state)) + $(e).replaceWith(macro[name] + .call(that, context, e, state, + function(elem){ _parse(context, elem, macro) })) // normal tag -> attrs + sub-tree... } else { @@ -380,9 +441,11 @@ var macro = { parsed .html(parsed.html().replace(include_marker, function(){ var page = state.include.shift() + var elem = $(page.shift()) + page = page.pop() return $('
') - .append($('') + .append(elem//$('') .attr('src', page.path) //.append(page // .parse({ slots: state.slots }, true)) @@ -394,9 +457,34 @@ var macro = { .html() })) - // post macro... + // post processing... if(!skip_post){ - _parse(context, parsed, this.post_macro) + // fill slots... + // XXX need to prevent this from processing slots in editable + // elements... + slots = {} + // get slots... + parsed.find('slot') + .each(function(i, e){ + e = $(e) + var n = e.attr('name') + + n in slots && e.detach() + + slots[n] = e + }) + // place slots... + parsed.find('slot') + .each(function(i, e){ + e = $(e) + var n = e.attr('name') + + e.replaceWith(slots[n]) + }) + + // post-macro... + this.post_macro + && _parse(context, parsed, this.post_macro) } // XXX shuld we get rid of the rot span??? @@ -517,21 +605,32 @@ var data = { 'Templates/_raw': { text: '@source(..)', }, + 'Templates/_view': { text: '\n' - +'\n' +'\n' +'
\n' +' \n' - +'@include(../path) (edit)\n' + +'@include(../path)\n' + +'\n' + +'\n' + +'(edit)\n' + +'\n' + +'\n' +'
\n' +'
\n' +'

' - //+'@include(../title)' - +'@include(../title)' + +'' + +'@source(../title)' + +'' + +'\n' +'

\n' +'
\n' - +'
@include(..)
\n' + +'\n' + +'\n' + +'\n' + +'\n' + +'\n' +'
\n' +'home\n' +'\n', @@ -540,16 +639,17 @@ var data = { text: '\n' +'\n' +'\n' - +'
' - +' \n' - +'@include(../path) (view)\n' - +'
\n' - +'
\n' - +'

@include(../title)

\n' - +'
\n' - +'
@source(../raw)
\n' - +'
\n' - +'home\n' + +'\n' + +'\n' + +'' + +'(view)' + +'\n' + +'\n' + +'\n' + +'
'
+					+''
+				+'
\n' + +'
' +'', }, }