User:Ghybu/vector.js

/* Yay, a rhymes editor that automatically updates the entry for the added rhyme.

To do: * Make less ugly * Ability to remove/edit rhymes * Put rhyme template at the bottom of pronunciation section? * Fix jillion bugs (none of which I'm aware of yet, but I'll find them...) * Add qualifier button

// var ccc, numberOfRhymesAdded = 0;

function setUpRhymeAdderFor(qq, ii) { var findNumberOfHeaders = parseInt(new mw.Uri($(qq).find("a").get(0).href).query.section), qqq; editor = new Editor; new AdderWrapper(editor, {		'createForm': function {			return qqq = newNode('form', newNode('label', '+Add new rhyme: ',					newNode('input', { 'name': 'rhyme' })				),				newNode('input', {					'type': 'submit',					'value': 'Add'				}) );		},		'fields': {			'rhyme': function(txt, error) {				return txt || error('Please specify a rhyme.');			}		},		'onsubmit': function(values, render) {			var langname = mw.config.get("wgTitle").split('/')[0];			var lutils = new LanguageUtilsAsync;			lutils.GetWiktionaryCodeByCanonicalName(langname).then(function(langcode) { qqq.getElementsByTagName('input')[0].value = "";

var newItem = ''; render(newItem, function(newhtml) {					editor.addEdit({ 'edit': function(wikitext) { var wikixml = WikiXml.parseWikitext(wikitext); var hdr = $(wikixml.find("section").get(findNumberOfHeaders - 1)); var headerData = $.trim(hdr.find("data").get(0).textContent); var rhymes = headerData.split(/\r?\n/);

rhymes.push("* " + newItem); var canSort = true; rhymes.forEach(function(line) {								if (!/^\* $/.test(line)) canSort = false;							});

if (canSort) rhymes.sort;

$(hdr.find("data").get(0)).text("\n" + rhymes.join("\n") + "\n\n");

return WikiXml.toWikitext(wikixml); },						'redo': function { $(ii).append($("").html(newhtml)); $(ii).find("li").sort(function(a, b) {								return $(a).text > $(b).text;							}).appendTo(ii); },						'undo': function { $(ii).find("li a:contains('" + values.rhyme + "')").closest("li").remove; },						'summary': "+rhyme " + values.rhyme + "", 'after_save': function { numberOfRhymesAdded++; document.body.style.cursor = 'wait'; JsMwApi.page(values.rhyme).edit(								function(text, saveFunction, editRes) {									var rhymehome = mw.config.get("wgTitle").split("/")[1];									if (text.match('==' + langname + '==') && !text.match("\\{\\{rhymes(\\|lang=" + langcode + "\\|" + util.escapeRe(rhymehome) + "|\\|" + util.escapeRe(rhymehome) + "\\|lang=" + langcode + "|\\|" + util.escapeRe(rhymehome) + ")\\}\\}")) {										if (!String(text.match("==" + langname + "==[\\s\\S]*?(|$)")).match("=Pronunciation=")) {											text = text.replace(RegExp("(==" + langname + "==[\\s\\S]*?(?=\n=+(?=[^=])(?!(Etymology=|Alternative))))"), "$1\n===Pronunciation===\n")										}										text = text.replace( RegExp('(==' + langname + '==[\\s\\S]*?=+Pronunciation=+[\\s\\S]*?\n(?![\:\*]))'), '$1* {' + '{rhymes|' + rhymehome + "|lang=" + langcode + '}}\n' )									}									if (text) {										saveFunction(text, { summary: "+rhyme " + "-" + rhymehome + "" }, function { document.body.style.cursor = --numberOfRhymesAdded ? 'wait' : ''; });									} else {										document.body.style.cursor = --numberOfRhymesAdded ? 'wait' : '';									}								}							) }					});				});			});		}	}, qq.parentNode, ii.nextSibling) }

function setUpRhymeAdder { if (		/*mw.config.get("wgNamespaceNumber") == 106 && */		mw.config.get("wgAction") == "view"		/* && !window.dontLoadRhymesAdder*/	) { for (var i = 0, q = document.getElementsByTagName('h3'); i < q.length; i++) { if (q[i].innerHTML.indexOf("syllable") != -1) { var ii = q[i].nextSibling; while (ii && (!ii.nodeName || !(ii.nodeName == "UL" || ii.nodeName == "DIV" || ii.nodeName == "H3"))) { ii = ii.nextSibling }				if (!ii || !ii.nodeName) { ii = q[i].parentNode.appendChild(newNode('ul', newNode('li', {						'style': 'display:none;'					}))); }				if (ii.nodeName == "H3") { ii = ii.parentNode.insertBefore(newNode('ul', newNode('li', {						'style': 'display:none;'					})), ii); }				try { setUpRhymeAdderFor(q[i], ii) } catch (e) {} }		}	} }

$(function {	mw.loader.using(["ext.gadget.Editor", "ext.gadget.LanguageUtils"], function { $.getScript("//en.wiktionary.org/w/index.php?title=User:Dixtosa/XMLize.js&action=raw", setUpRhymeAdder); }); });

//