User:Ricordisamoa/Rhyme.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/* <nowiki>
* Rhyme.js by [[User:Ricordisamoa]]
* --------------------experimental--------------------
*/
$(document).ready(function(){
if($('div.poem').length < 1){
return;
}
var firstanzas = $('div.poem').first().clone(true).find('.numeroriga').remove().end().text().match(/^\n*\s*((\S.+\n){2}(\S.+)\n{2,}(\S.+\n){2}(\S.+))\s*\n/)[1].trim(),
endings = firstanzas.match(/\w+\W*(\n|$)/g),
accent = ['aà', 'eèé', 'iì', 'oò', 'uù'],
colors = ['blue', 'brown', 'green'],
rh = {};
endings = $.map(endings, function(e){
return e.trim().match(/^(\w+)\b/)[1];
});
console.log(endings);
$.get(
'//it.wiktionary.org/w/api.php',
{
action: 'query',
format: 'json',
titles: endings.join('|'),
prop: 'revisions',
rvprop: 'content'
},
function(data){
var regex = /(^|\n)\s*\{\{\s*\-sill\-\s*\}\}\n\s*\;?\'*\s*(\S+\s*(\s*\|\s*\S+)*)\s*\'*\s*(\n|$)/;
$.each(data.query.pages, function(pageid, page){
if(page.revisions === undefined){
return;
}
var sill = page.revisions[0]['*'].match(regex);
if(sill !== null){
sill = sill[2].replace(/\s*\|\s*/g, '');
var sillv;
for(var z = 0; z < sill.length; z++){
$.each(accent, function(i, e){
for(i = 1; i < e.length; i++){
if(sill[z] === e[i]){
var y = e[0] + sill.slice(z + 1);
if(y in rh){
rh[y].push(page.title);
}
else{
rh[y] = [page.title];
}
// console.log(page.title + ': ' + y);
sillv = sill.slice(0, z) + y;
}
}
});
}
/*
if(sillv === page.title){
console.log(' ' + sillv + ': ' + sill);
}
else{
console.log(' ' + sill);
}
*/
}
});
$.each(rh, function(k, v){
$.each(v, function(w, word){
$('div.poem').first()
.find('a,span,p').each(function(){
$(this).contents()
.filter(function(){
return this.nodeType === 3;
})
.each(function(){
var find = this.textContent.indexOf(word);
if(find !== -1){
var lio = word.lastIndexOf(k),
node = this.splitText(find + lio);
$(node.splitText(word.length - lio).previousSibling)
.wrap('<span>').parent()
.css('color', colors[Object.keys(rh).indexOf(k) % colors.length]);
}
});
});
});
});
},
'jsonp'
);
});
/* </nowiki> */