mirror of
https://github.com/flynx/pWiki.git
synced 2025-10-29 18:10:09 +00:00
experimenting with wikiwords...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
d8814fd182
commit
7a1a89e904
@ -7,8 +7,86 @@
|
|||||||
(function(require){ var module={} // make module AMD/node compatible...
|
(function(require){ var module={} // make module AMD/node compatible...
|
||||||
/*********************************************************************/
|
/*********************************************************************/
|
||||||
|
|
||||||
|
var WIKIWORD_PATTERN =
|
||||||
|
RegExp('('+[
|
||||||
|
//'\\\\?(\\/|\\./|\\.\\./|>>|[A-Z][_a-z0-9]+[A-Z/])[_a-zA-Z0-9/]*',
|
||||||
|
'\\\\?\\/?(\\./|\\.\\./|>>|[A-Z][_a-z0-9]+[A-Z/])[_a-zA-Z0-9/]*',
|
||||||
|
'\\\\?\\[[^\\]]+\\]',
|
||||||
|
].join('|') +')', 'g')
|
||||||
|
|
||||||
|
// XXX RENAME...
|
||||||
|
// XXX REVISE...
|
||||||
|
var setWikiWords =
|
||||||
|
module.setWikiWords =
|
||||||
|
function(text, show_brackets=true, skip){
|
||||||
|
skip = skip ?? []
|
||||||
|
skip = skip instanceof Array ?
|
||||||
|
skip
|
||||||
|
: [skip]
|
||||||
|
return text
|
||||||
|
// set new...
|
||||||
|
.replace(
|
||||||
|
WIKIWORD_PATTERN,
|
||||||
|
function(l){
|
||||||
|
// check if WikiWord is escaped...
|
||||||
|
if(l[0] == '\\'){
|
||||||
|
return l.slice(1) }
|
||||||
|
|
||||||
|
var path = l[0] == '[' ?
|
||||||
|
l.slice(1, -1)
|
||||||
|
: l
|
||||||
|
var i = [].slice.call(arguments).slice(-2)[0]
|
||||||
|
|
||||||
|
// XXX HACK check if we are inside a tag...
|
||||||
|
var rest = text.slice(i+1)
|
||||||
|
if(rest.indexOf('>') < rest.indexOf('<')){
|
||||||
|
return l }
|
||||||
|
|
||||||
|
return skip.indexOf(l) < 0 ?
|
||||||
|
('<a '
|
||||||
|
+'class="wikiword" '
|
||||||
|
+'href="#'+ path +'" '
|
||||||
|
+'bracketed="'+ (show_brackets && l[0] == '[' ? 'yes' : 'no') +'" '
|
||||||
|
//+'onclick="event.preventDefault(); go($(this).attr(\'href\').slice(1))" '
|
||||||
|
+'>'
|
||||||
|
+ (!!show_brackets ? path : l)
|
||||||
|
+'</a>')
|
||||||
|
: l })}
|
||||||
|
|
||||||
|
|
||||||
|
// XXX move this to a better spot....
|
||||||
|
var iterText =
|
||||||
|
module.iterText =
|
||||||
|
function*(root, skip_empty=true){
|
||||||
|
for(var node of root.childNodes){
|
||||||
|
if(node.nodeType == Node.TEXT_NODE
|
||||||
|
&& (!skip_empty || node.nodeValue.trim().length != 0)){
|
||||||
|
yield node
|
||||||
|
} else {
|
||||||
|
yield* iterText(node) } } }
|
||||||
|
|
||||||
|
|
||||||
|
// handle wikiwords...
|
||||||
|
//
|
||||||
|
// this will skip:
|
||||||
|
// a
|
||||||
|
// [wikiwords=no]
|
||||||
|
//
|
||||||
|
var wikiword =
|
||||||
|
module.wikiword =
|
||||||
|
function(root){
|
||||||
|
var tmp = document.createElement('div')
|
||||||
|
iterText(pwiki.dom)
|
||||||
|
.forEach(function(text){
|
||||||
|
// skip stuff...
|
||||||
|
if(text.parentNode.nodeName == 'a'
|
||||||
|
|| text.parentNode.getAttribute('wikiwords') == 'no'){
|
||||||
|
return }
|
||||||
|
var t = text.nodeValue
|
||||||
|
var n = setWikiWords(text.nodeValue)
|
||||||
|
if(t != n){
|
||||||
|
tmp.innerHTML = n
|
||||||
|
text.replaceWith(...tmp.childNodes) } }) }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -20,65 +20,6 @@ function(...args){
|
|||||||
return func }
|
return func }
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
|
||||||
// XXX this does not seem to handle html well...
|
|
||||||
|
|
||||||
var WIKIWORD_PATTERN =
|
|
||||||
RegExp('('+[
|
|
||||||
//'\\\\?(\\/|\\./|\\.\\./|>>|[A-Z][_a-z0-9]+[A-Z/])[_a-zA-Z0-9/]*',
|
|
||||||
'\\\\?\\/?(\\./|\\.\\./|>>|[A-Z][_a-z0-9]+[A-Z/])[_a-zA-Z0-9/]*',
|
|
||||||
'\\\\?\\[[^\\]]+\\]',
|
|
||||||
].join('|') +')', 'g')
|
|
||||||
|
|
||||||
// XXX REVISE...
|
|
||||||
var setWikiWords =
|
|
||||||
module.setWikiWords =
|
|
||||||
function(text, show_brackets=true, skip){
|
|
||||||
skip = skip ?? []
|
|
||||||
skip = skip instanceof Array ?
|
|
||||||
skip
|
|
||||||
: [skip]
|
|
||||||
return text
|
|
||||||
// set new...
|
|
||||||
.replace(
|
|
||||||
WIKIWORD_PATTERN,
|
|
||||||
function(l){
|
|
||||||
// check if WikiWord is escaped...
|
|
||||||
if(l[0] == '\\'){
|
|
||||||
return l.slice(1) }
|
|
||||||
|
|
||||||
var path = l[0] == '[' ?
|
|
||||||
l.slice(1, -1)
|
|
||||||
: l
|
|
||||||
var i = [].slice.call(arguments).slice(-2)[0]
|
|
||||||
|
|
||||||
// XXX HACK check if we are inside a tag...
|
|
||||||
var rest = text.slice(i+1)
|
|
||||||
if(rest.indexOf('>') < rest.indexOf('<')){
|
|
||||||
return l }
|
|
||||||
|
|
||||||
return skip.indexOf(l) < 0 ?
|
|
||||||
('<a '
|
|
||||||
+'class="wikiword" '
|
|
||||||
+'href="#'+ path +'" '
|
|
||||||
+'bracketed="'+ (show_brackets && l[0] == '[' ? 'yes' : 'no') +'" '
|
|
||||||
//+'onclick="event.preventDefault(); go($(this).attr(\'href\').slice(1))" '
|
|
||||||
+'>'
|
|
||||||
+ (!!show_brackets ? path : l)
|
|
||||||
+'</a>')
|
|
||||||
: l })}
|
|
||||||
|
|
||||||
module.wikiWord =
|
|
||||||
Filter(
|
|
||||||
{quote: 'quote-wikiword'},
|
|
||||||
function(source){
|
|
||||||
return setWikiWords(source) })
|
|
||||||
module.quoteWikiWord =
|
|
||||||
function(source){
|
|
||||||
// XXX
|
|
||||||
return source }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**********************************************************************
|
/**********************************************************************
|
||||||
* vim:set ts=4 sw=4 : */ return module })
|
* vim:set ts=4 sw=4 : */ return module })
|
||||||
|
|||||||
@ -548,8 +548,9 @@ object.Constructor('Page', BasePage, {
|
|||||||
return source
|
return source
|
||||||
.replace(/test/g, 'TEST') },
|
.replace(/test/g, 'TEST') },
|
||||||
|
|
||||||
wikiword: filters.wikiWord,
|
// XXX
|
||||||
'quote-wikiword': filters.quoteWikiWord,
|
wikiword: function(){},
|
||||||
|
'quote-wikiword': function(){},
|
||||||
|
|
||||||
markdown: markdown.markdown,
|
markdown: markdown.markdown,
|
||||||
'quote-markdown': markdown.quoteMarkdown,
|
'quote-markdown': markdown.quoteMarkdown,
|
||||||
@ -1005,6 +1006,7 @@ object.Constructor('Page', BasePage, {
|
|||||||
state = text
|
state = text
|
||||||
text = null }
|
text = null }
|
||||||
state = state ?? {}
|
state = state ?? {}
|
||||||
|
|
||||||
return this.__parser__.parse(this, text, state) },
|
return this.__parser__.parse(this, text, state) },
|
||||||
|
|
||||||
// true if page has an array value but is not a pattern page...
|
// true if page has an array value but is not a pattern page...
|
||||||
@ -1102,6 +1104,7 @@ object.Constructor('DOMPage', Page, {
|
|||||||
wikiword: undefined,
|
wikiword: undefined,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
// events...
|
// events...
|
||||||
//
|
//
|
||||||
// XXX might be a good idea to move this up to Page and trigger when
|
// XXX might be a good idea to move this up to Page and trigger when
|
||||||
|
|||||||
16
pwiki2.html
16
pwiki2.html
@ -88,13 +88,16 @@ document.pwikiloaded = new Event('pwikiloaded')
|
|||||||
|
|
||||||
|
|
||||||
// start loading pWiki...
|
// start loading pWiki...
|
||||||
require(['./browser'], function(m){
|
require(['./browser', './pwiki/dom/wikiword'], function(pwiki, wikiword){
|
||||||
window.pwiki = m.pwiki
|
pwiki = window.pwiki = pwiki.pwiki
|
||||||
|
wikiword = window.wikiword = wikiword.wikiword
|
||||||
|
|
||||||
|
// XXX for some reason this sets to undefined...
|
||||||
|
var iterText = window.iterText = wikiword.iterText
|
||||||
|
|
||||||
// XXX make a pWikiDom page to manage this...
|
// XXX make a pWikiDom page to manage this...
|
||||||
pwiki.dom = document.querySelector('#pWiki')
|
pwiki.dom = document.querySelector('#pWiki')
|
||||||
|
|
||||||
|
|
||||||
// handle location.hash (both directions)
|
// handle location.hash (both directions)
|
||||||
var _debounceHashChange = false
|
var _debounceHashChange = false
|
||||||
pwiki.onNavigate(async function(){
|
pwiki.onNavigate(async function(){
|
||||||
@ -109,7 +112,12 @@ require(['./browser'], function(m){
|
|||||||
// XXX do we need to use a MutationObserver here to trigger this
|
// XXX do we need to use a MutationObserver here to trigger this
|
||||||
// after the above is done loading???
|
// after the above is done loading???
|
||||||
// (see: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver)
|
// (see: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver)
|
||||||
pwiki.dom.dispatchEvent(document.pwikiloaded) })
|
pwiki.dom.dispatchEvent(document.pwikiloaded)
|
||||||
|
|
||||||
|
// wikiwords
|
||||||
|
// XXX this should be controllable from the page...
|
||||||
|
wikiword(pwiki.dom)
|
||||||
|
})
|
||||||
|
|
||||||
window.addEventListener('hashchange', function(evt){
|
window.addEventListener('hashchange', function(evt){
|
||||||
evt.preventDefault()
|
evt.preventDefault()
|
||||||
|
|||||||
@ -29,7 +29,7 @@
|
|||||||
* - migrate bootstrap
|
* - migrate bootstrap
|
||||||
* - store topology
|
* - store topology
|
||||||
* - markdown -- DONE??
|
* - markdown -- DONE??
|
||||||
* - WikiWord --
|
* - WikiWord -- DONE-ish
|
||||||
* currently this is broken as it does not know how to deal with HTML
|
* currently this is broken as it does not know how to deal with HTML
|
||||||
* this can be solved by one of:
|
* this can be solved by one of:
|
||||||
* - make this a dom filter and only handle text nodes (as v1-2)
|
* - make this a dom filter and only handle text nodes (as v1-2)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user