2022-08-08 12:29:02 +03:00
|
|
|
/**********************************************************************
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
|
|
|
|
|
(function(require){ var module={} // make module AMD/node compatible...
|
|
|
|
|
/*********************************************************************/
|
|
|
|
|
|
2022-08-08 23:04:42 +03:00
|
|
|
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')
|
2022-08-08 12:29:02 +03:00
|
|
|
|
2022-08-08 23:04:42 +03:00
|
|
|
// 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) }
|
2022-08-08 12:29:02 +03:00
|
|
|
|
2022-08-08 23:04:42 +03:00
|
|
|
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]
|
|
|
|
|
//
|
2022-08-10 11:42:31 +03:00
|
|
|
var wikiWordText =
|
|
|
|
|
module.wikiWordText =
|
|
|
|
|
function(elem){
|
2022-08-08 23:04:42 +03:00
|
|
|
var tmp = document.createElement('div')
|
2022-08-10 11:42:31 +03:00
|
|
|
iterText(elem)
|
2022-08-08 23:04:42 +03:00
|
|
|
.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) } }) }
|
2022-08-08 12:29:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
|
* vim:set ts=4 sw=4 : */ return module })
|