experimenting with wikiwords...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-08-08 23:04:42 +03:00
parent d8814fd182
commit 7a1a89e904
5 changed files with 96 additions and 66 deletions

View File

@ -7,8 +7,86 @@
(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) } }) }

View File

@ -20,65 +20,6 @@ function(...args){
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 })

View File

@ -548,8 +548,9 @@ object.Constructor('Page', BasePage, {
return source
.replace(/test/g, 'TEST') },
wikiword: filters.wikiWord,
'quote-wikiword': filters.quoteWikiWord,
// XXX
wikiword: function(){},
'quote-wikiword': function(){},
markdown: markdown.markdown,
'quote-markdown': markdown.quoteMarkdown,
@ -1005,6 +1006,7 @@ object.Constructor('Page', BasePage, {
state = text
text = null }
state = state ?? {}
return this.__parser__.parse(this, text, state) },
// true if page has an array value but is not a pattern page...
@ -1102,6 +1104,7 @@ object.Constructor('DOMPage', Page, {
wikiword: undefined,
},
// events...
//
// XXX might be a good idea to move this up to Page and trigger when

View File

@ -88,13 +88,16 @@ document.pwikiloaded = new Event('pwikiloaded')
// start loading pWiki...
require(['./browser'], function(m){
window.pwiki = m.pwiki
require(['./browser', './pwiki/dom/wikiword'], function(pwiki, wikiword){
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...
pwiki.dom = document.querySelector('#pWiki')
// handle location.hash (both directions)
var _debounceHashChange = false
pwiki.onNavigate(async function(){
@ -109,7 +112,12 @@ require(['./browser'], function(m){
// XXX do we need to use a MutationObserver here to trigger this
// after the above is done loading???
// (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){
evt.preventDefault()

View File

@ -29,7 +29,7 @@
* - migrate bootstrap
* - store topology
* - markdown -- DONE??
* - WikiWord --
* - WikiWord -- DONE-ish
* currently this is broken as it does not know how to deal with HTML
* this can be solved by one of:
* - make this a dom filter and only handle text nodes (as v1-2)