experimenting with macros...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-07-10 02:41:31 +03:00
parent b8174abd5a
commit 2260f75e8c
2 changed files with 70 additions and 7 deletions

View File

@ -21,7 +21,57 @@ var reload = () => {
$('.title').text(Wiki.title)
$('.text').html(activateWikiWords(Wiki.text))
// process text...
var text = Wiki.text
// expand macros...
if($('.text').prop('contenteditable') != 'true'){
text = text
// Syntax:
// @<macro>( <args> )
.replace(/@([a-zA-Z-_]+)\(([^)]*)\)/g, function(_, macro, args){
args = $('<div>')
.html(args)
.text()
.trim()
.split(/\s+/g)
//console.log('>>>>>', macro, args)
if(macro == 'include' && args.length == 1){
w = Object.create(Wiki)
w.location = args[0]
return w.text
}
return _
})
// html-like macro syntax...
.replace(/\&lt;[a-zA-Z-_:]+.*?\/?\&gt;/g, function(t){
var elem = $($('<div>').html(t).text())
var macro = elem.prop('tagName').toLowerCase()
var args = {}
var a = elem.prop('attributes')
for(var i=0; i<a.length; i++){
args[a[i].name] = a[i].value
}
//console.log('>>>>>', macro, args)
if(macro == 'include' && args.src != null){
w = Object.create(Wiki)
w.location = args.src
return w.text
}
return t
})
}
$('.text').html(activateWikiWords(text))
// XXX save...
localStorage['wiki-data'] = JSON.stringify(Wiki.__wiki_data)
@ -85,11 +135,21 @@ $(() => {
// live update text...
// XXX is this the right way to go for large documents???
$('.text')
.focus(() => {
$('.text').prop('contenteditable', $('.text').prop('contenteditable') != 'true')
reload()
})
.on('keyup', () => {
Wiki.text = clearWikiWords($('.text').clone()).html() })
if($('.text').prop('contenteditable') == 'true'){
Wiki.text = clearWikiWords($('.text').clone()).html()
}
})
// XXX do this live, but on a timeout after user input...
// XXX need to place the cursor in the same position...
.blur(() => { reload() })
.blur(() => {
$('.text').prop('contenteditable', false)
reload()
})
})
</script>
@ -98,7 +158,7 @@ $(() => {
<div class="dir"></div>
<hr>
<h1 class="title" contenteditable tabindex=0></h1>
<div class="text" contenteditable tabindex=0></div>
<div class="text" tabindex=0></div>
</body>
</html>

View File

@ -21,7 +21,8 @@ var BaseData = {
}
return null
})
.filter((e) => e != null)
.filter(e => e != null)
.map(e => '['+ e +']')
.join('<br>')
},
'Templates/tree': function(){
@ -34,7 +35,8 @@ var BaseData = {
}
return null
})
.filter((e) => e != null)
.filter(e => e != null)
.map(e => '['+ e +']')
.join('<br>')
},
'Templates/links': function(){
@ -53,7 +55,8 @@ var BaseData = {
})
return res
.map(e => '['+ e[0] +'] <i>from page: '+ e[1] +'</i>')
//.map(e => '['+ e[0] +'] <i>from page: ['+ e[1] +']</i>')
.map(e => '['+ e[1] +'] <i>-&gt; ['+ e[0] +']</i>')
.join('<br>')
},