mirror of
https://github.com/flynx/pWiki.git
synced 2025-12-17 08:31:38 +00:00
new parser working...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
a7c31969c2
commit
bb1aea885c
213
wiki.js
213
wiki.js
@ -386,12 +386,163 @@ var macro = {
|
|||||||
|
|
||||||
return text
|
return text
|
||||||
},
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// XXX should inline macros support named args???
|
||||||
|
var macro2 = {
|
||||||
|
|
||||||
|
__include_marker__: '__include_marker__',
|
||||||
|
|
||||||
|
// Abstract macro syntax:
|
||||||
|
// Inline macro:
|
||||||
|
// @macro(arg ..)
|
||||||
|
//
|
||||||
|
// HTML-like:
|
||||||
|
// <macro arg=value ../>
|
||||||
|
//
|
||||||
|
// HTML-like with body:
|
||||||
|
// <macro arg=value ..>
|
||||||
|
// ..text..
|
||||||
|
// </macro>
|
||||||
|
//
|
||||||
|
// XXX should inline macros support named args???
|
||||||
|
__macro__pattern__:
|
||||||
|
[[
|
||||||
|
// @macro(arg ..)
|
||||||
|
'@([a-zA-Z-_]+)\\(([^)]*)\\)'
|
||||||
|
].join('|'), 'mg'],
|
||||||
|
|
||||||
|
// default filters...
|
||||||
|
//
|
||||||
|
// NOTE: these are added AFTER the user defined filters...
|
||||||
|
__filters__: [
|
||||||
|
'wikiword',
|
||||||
|
],
|
||||||
|
|
||||||
|
// Macros...
|
||||||
|
//
|
||||||
|
macro: {
|
||||||
|
// select filter to post-process text...
|
||||||
|
filter: Macro('Filter to post-process text',
|
||||||
|
['name'],
|
||||||
|
function(context, elem, state){
|
||||||
|
var filter = $(elem).attr('name')
|
||||||
|
|
||||||
|
filter[0] == '-' ?
|
||||||
|
// disabled -- keep at head of list...
|
||||||
|
state.filters.unshift(filter)
|
||||||
|
// normal -- tail...
|
||||||
|
: state.filters.push(filter)
|
||||||
|
|
||||||
|
return ''
|
||||||
|
}),
|
||||||
|
|
||||||
|
// include page/slot...
|
||||||
|
//
|
||||||
|
// NOTE: this will render the page in the caller's context.
|
||||||
|
// NOTE: included pages are rendered completely independently
|
||||||
|
// from the including page.
|
||||||
|
//
|
||||||
|
// XXX do we need to control the rendering of nested pages???
|
||||||
|
// ...currently I do not think so...
|
||||||
|
// ...if required this can be done via global and local
|
||||||
|
// filters... (now filters are only local)
|
||||||
|
// XXX do we need to render just one slot??? (slot arg)
|
||||||
|
// e.g. include PageX SlotY
|
||||||
|
include: Macro('Include page',
|
||||||
|
['src'],
|
||||||
|
function(context, elem, state){
|
||||||
|
var path = $(elem).attr('src')
|
||||||
|
|
||||||
|
// get and prepare the included page...
|
||||||
|
state.include
|
||||||
|
.push(context.get(path))
|
||||||
|
|
||||||
|
// return the marker...
|
||||||
|
return this.__include_marker__
|
||||||
|
}),
|
||||||
|
|
||||||
|
// NOTE: this is similar to include, the difference is that this
|
||||||
|
// includes the page source to the current context while
|
||||||
|
// include works in an isolated context
|
||||||
|
source: Macro('Include page source (without parsing)',
|
||||||
|
['src'],
|
||||||
|
function(context, elem, state){
|
||||||
|
var path = $(elem).attr('src')
|
||||||
|
|
||||||
|
return 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',
|
||||||
|
['name', 'text'],
|
||||||
|
function(context, elem, state){
|
||||||
|
var name = $(elem).attr('name')
|
||||||
|
|
||||||
|
// XXX
|
||||||
|
text = $(elem).html()
|
||||||
|
text = text == '' ? $(elem).attr('text') : text
|
||||||
|
text = this.parse(context, text, state, true)
|
||||||
|
|
||||||
|
if(state.slots[name] == null){
|
||||||
|
state.slots[name] = text
|
||||||
|
// return a slot macro parsable by stage 2...
|
||||||
|
return '<slot name="'+name+'">'+ text +'</slot>'
|
||||||
|
|
||||||
|
} else if(name in state.slots){
|
||||||
|
state.slots[name] = text
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
|
||||||
|
// Post macros...
|
||||||
|
//
|
||||||
|
post_macro: {
|
||||||
|
slot: Macro('',
|
||||||
|
['name'],
|
||||||
|
function(context, elem, state){
|
||||||
|
var name = $(elem).attr('name')
|
||||||
|
|
||||||
|
if(state.slots[name] == null){
|
||||||
|
return $(elem).html()
|
||||||
|
|
||||||
|
} else if(name in state.slots){
|
||||||
|
return state.slots[name]
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
|
||||||
|
// Filters...
|
||||||
|
//
|
||||||
|
// Signature:
|
||||||
|
// filter(text) -> html
|
||||||
|
//
|
||||||
|
// XXX
|
||||||
|
filter: {
|
||||||
|
default: 'html',
|
||||||
|
|
||||||
|
html: function(context, elem){ return $(elem) },
|
||||||
|
|
||||||
|
json: 'text',
|
||||||
|
text: function(context, elem){ return $('<div>').html($(elem).text()) },
|
||||||
|
|
||||||
|
// XXX
|
||||||
|
//nl2br: function(context, text){ return $('<div>').html(text.replace(/\n/g, '<br>\n')) },
|
||||||
|
|
||||||
|
wikiword: function(context, elem){
|
||||||
|
return $('<span>')
|
||||||
|
.html(setWikiWords($(elem).html(), true, this.__include_marker__)) },
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
// XXX this expect a different macro signature:
|
// XXX this expect a different macro signature:
|
||||||
// macro(context, element, state)
|
// macro(context, element, state)
|
||||||
// -> text
|
// -> text
|
||||||
_parse: function(context, text, state, skip_post, pattern){
|
parse: function(context, text, state, skip_post, pattern){
|
||||||
var that = this
|
var that = this
|
||||||
|
|
||||||
state = state || {}
|
state = state || {}
|
||||||
@ -399,11 +550,15 @@ var macro = {
|
|||||||
state.slots = state.slots || {}
|
state.slots = state.slots || {}
|
||||||
state.include = state.include || []
|
state.include = state.include || []
|
||||||
|
|
||||||
// XXX update .__macro__pattern__ to only support the @macro style...
|
//pattern = pattern || RegExp('@([a-zA-Z-_]+)\\(([^)]*)\\)', 'mg')
|
||||||
//var pattern = pattern || this.__macro__pattern__
|
pattern = pattern || RegExp.apply(null, this.__macro__pattern__)
|
||||||
pattern = pattern || RegExp('@([a-zA-Z-_]+)\\(([^)]*)\\)', 'mg')
|
|
||||||
|
|
||||||
var parsed = $('<span>').html(text)
|
// XXX need to quote regexp chars...
|
||||||
|
var include_marker = RegExp(this.__include_marker__, 'g')
|
||||||
|
|
||||||
|
var parsed = typeof(text) == typeof('str') ?
|
||||||
|
$('<span>').html(text)
|
||||||
|
: text
|
||||||
|
|
||||||
var _parseText = function(context, text, macro){
|
var _parseText = function(context, text, macro){
|
||||||
return text.replace(pattern, function(match){
|
return text.replace(pattern, function(match){
|
||||||
@ -430,7 +585,7 @@ var macro = {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// call macro...
|
// call macro...
|
||||||
return macro[name].call(context, elem, state)
|
return macro[name].call(that, context, elem, state)
|
||||||
}
|
}
|
||||||
|
|
||||||
return match
|
return match
|
||||||
@ -440,11 +595,12 @@ var macro = {
|
|||||||
var _parse = function(context, parsed, macro){
|
var _parse = function(context, parsed, macro){
|
||||||
$(parsed).contents().each(function(_, e){
|
$(parsed).contents().each(function(_, e){
|
||||||
// #text node -> parse the @ macros...
|
// #text node -> parse the @ macros...
|
||||||
if(e.nodeType == 3){
|
if(e.nodeType == e.TEXT_NODE){
|
||||||
// parse text...
|
$(e).replaceWith(_parseText(context, $(e).text(), macro))
|
||||||
var t = $(e)
|
|
||||||
|
|
||||||
t.replaceWith(_parseText(context, t.text(), macro))
|
// comment node -> parse the @ macros...
|
||||||
|
} else if(e.nodeType == e.COMMENT_NODE){
|
||||||
|
$(e).replaceWith(_parseText(context, '<!--'+e.textContent+'-->', macro))
|
||||||
|
|
||||||
// node -> html-style + attrs...
|
// node -> html-style + attrs...
|
||||||
} else {
|
} else {
|
||||||
@ -452,7 +608,7 @@ var macro = {
|
|||||||
|
|
||||||
// macro match -> call macro...
|
// macro match -> call macro...
|
||||||
if(name in macro){
|
if(name in macro){
|
||||||
$(e).replaceWith(macro[name].call(context, e, state))
|
$(e).replaceWith(macro[name].call(that, context, e, state))
|
||||||
|
|
||||||
// normal tag -> attrs + sub-tree...
|
// normal tag -> attrs + sub-tree...
|
||||||
} else {
|
} else {
|
||||||
@ -474,12 +630,9 @@ var macro = {
|
|||||||
|
|
||||||
|
|
||||||
// macro stage...
|
// macro stage...
|
||||||
// XXX need to update .macro...
|
|
||||||
_parse(context, parsed, this.macro)
|
_parse(context, parsed, this.macro)
|
||||||
|
|
||||||
// filter stage...
|
// filter stage...
|
||||||
// XXX need to update .filters to take html and return html...
|
|
||||||
/*
|
|
||||||
state.filters
|
state.filters
|
||||||
.concat(this.__filters__)
|
.concat(this.__filters__)
|
||||||
// unique -- leave last occurance..
|
// unique -- leave last occurance..
|
||||||
@ -509,7 +662,6 @@ var macro = {
|
|||||||
// use the filter...
|
// use the filter...
|
||||||
parsed = k.call(that, context, parsed)
|
parsed = k.call(that, context, parsed)
|
||||||
})
|
})
|
||||||
//*/
|
|
||||||
|
|
||||||
// merge includes...
|
// merge includes...
|
||||||
//
|
//
|
||||||
@ -524,14 +676,22 @@ var macro = {
|
|||||||
// XXX I do not like that we are reparsing the whole page here...
|
// XXX I do not like that we are reparsing the whole page here...
|
||||||
// ...the only alternative I see is traversing the whole
|
// ...the only alternative I see is traversing the whole
|
||||||
// page agin -- _parse(..) stage 1.5???...
|
// page agin -- _parse(..) stage 1.5???...
|
||||||
.html(parsed.html().replace(this.__include_marker__, function(){
|
.html(parsed.html().replace(include_marker, function(){
|
||||||
var page = state.include.shift()
|
var page = state.include.shift()
|
||||||
|
|
||||||
return $('<include/>')
|
return $('<div>')
|
||||||
|
.append($('<include/>')
|
||||||
.attr('src', page.path)
|
.attr('src', page.path)
|
||||||
.append(page
|
//.append(page
|
||||||
.parse({ slots: state.slots }, true))
|
// .parse({ slots: state.slots }, true))
|
||||||
|
.append(that
|
||||||
|
.parse(context,
|
||||||
|
page.raw,
|
||||||
|
{ slots: state.slots },
|
||||||
|
true)))
|
||||||
|
.html()
|
||||||
}))
|
}))
|
||||||
|
/* XXX do we need this???
|
||||||
// tag includes...
|
// tag includes...
|
||||||
.find('include').each(function(i, elem){
|
.find('include').each(function(i, elem){
|
||||||
var src = $(elem).attr('src')
|
var src = $(elem).attr('src')
|
||||||
@ -545,12 +705,17 @@ var macro = {
|
|||||||
// XXX this uses old Wiki.parse(..) method/parser...
|
// XXX this uses old Wiki.parse(..) method/parser...
|
||||||
$(elem)
|
$(elem)
|
||||||
.empty()
|
.empty()
|
||||||
.append(that.get(src)
|
//.append(that.get(src)
|
||||||
.parse({ slots: state.slots }, true))
|
// .parse({ slots: state.slots }, true))
|
||||||
|
.append(that
|
||||||
|
.parse(context,
|
||||||
|
context.get(src).raw,
|
||||||
|
{ slots: state.slots },
|
||||||
|
true))
|
||||||
})
|
})
|
||||||
|
//*/
|
||||||
|
|
||||||
// post macro...
|
// post macro...
|
||||||
// XXX need to update .post_macro...
|
|
||||||
if(!skip_post){
|
if(!skip_post){
|
||||||
_parse(context, parsed, this.post_macro)
|
_parse(context, parsed, this.post_macro)
|
||||||
}
|
}
|
||||||
@ -744,6 +909,8 @@ var Wiki = {
|
|||||||
'\\[[^\\]]+\\]',
|
'\\[[^\\]]+\\]',
|
||||||
].join('|') +')', 'g'),
|
].join('|') +')', 'g'),
|
||||||
|
|
||||||
|
__macro_parser__: macro2,
|
||||||
|
|
||||||
|
|
||||||
// Resolve '.' and '..' relative to current page...
|
// Resolve '.' and '..' relative to current page...
|
||||||
//
|
//
|
||||||
@ -961,7 +1128,7 @@ var Wiki = {
|
|||||||
parse: function(state, skip_post){
|
parse: function(state, skip_post){
|
||||||
// special case: if we are getting ./raw then do not parse text...
|
// special case: if we are getting ./raw then do not parse text...
|
||||||
return this.title == 'raw' ? this.raw
|
return this.title == 'raw' ? this.raw
|
||||||
: macro.parse(this, this.raw, state, skip_post)
|
: this.__macro_parser__.parse(this, this.raw, state, skip_post)
|
||||||
},
|
},
|
||||||
get text(){
|
get text(){
|
||||||
return this.parse() },
|
return this.parse() },
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user