User:Dixtosa/minorEdits.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

// Description is at: User:Dixtosa/minorEdit
// see the description to see what this script assumes
// <nowiki>

importScript("User:Dixtosa/XMLize.js");

function doGeorgian(wikitext)
{
	//START of FUNCTIONS
	function removeHead(wikitext) {
		wikitext = wikitext.replace(/head\|ka\|noun([^ ])/, "ka-noun$1");
		wikitext = wikitext.replace(/head\|ka\|adverb([^ ])/, "ka-adv$1");
		wikitext = wikitext.replace(/head\|ka\|adjective([^ ])/, "ka-adj$1");
		wikitext = wikitext.replace(/head\|ka\|proper[ _]noun([^ ])/, "ka-proper noun$1");
		return wikitext;
	}
	function addPronunciation(wikitext)
	{
		var exceptionList = ["ალო"];
		if ($.inArray(mw.config.values.wgTitle, exceptionList) >= 0) return wikitext;
		
		var wikixml = WikiXml.parseWikitext(wikitext);
		if (wikixml === undefined) return wikitext; //maybe XMLize can handle this itself
		if (wikixml.find(".Georgian .Pronunciation").find("data").text().indexOf("audio") != -1) return wikitext;
		
		wikixml.find(".Georgian .Pronunciation").remove();
		
		var first_h3 = wikixml.find (".Georgian > section").first();
		if (first_h3.attr("class") == "Alternative forms") first_h3 = first_h3.next();
		if (first_h3.attr("class") == "Etymology") first_h3 = first_h3.next();
		
		var hyphen = "\n* {{ka-hyphen}}";
		if ((mw.config.values.wgTitle.match(/[აეიოუ]/g) || "").length <= 1) hyphen = "";
		
		first_h3.before("<section class = 'Pronunciation' level = '3'><data>\n* {{ka-IPA}}" + hyphen + "\n\n</data></section>");
		return WikiXml.toWikitext(wikixml);
	}
	function fixDeclension(wikitext)
	{
	
		//remove erroneously added declension tables
		wikitext = wikitext.replace (/====[ ]?Declension[ ]?====\n{{ka-decl-adj-auto}}\n/, "");
		wikitext = wikitext.replace (/====[ ]?Declension[ ]?====\n{{ka-adj-decl.*?}}\n/, "");

		wikitext = wikitext.replace(/{{ka-noun-c\|.*plural.*}}/, "{{ka-infl-noun|-}}");
		wikitext = wikitext.replace(/{{ka-noun-c\|.*}}/, "{{ka-infl-noun}}");
		
		
		wikitext = wikitext.replace(/{{ka-noun-a\|.*plural.*}}/, "{{ka-infl-noun|-}}");
		wikitext = wikitext.replace(/{{ka-noun-a\|.*}}/, "{{ka-infl-noun}}");
		
		wikitext = wikitext.replace(/{{ka-noun-o\|.*plural.*}}/, "{{ka-infl-noun|-}}");
		wikitext = wikitext.replace(/{{ka-noun-o\|.*}}/, "{{ka-infl-noun}}");
		
		wikitext = wikitext.replace(/{{ka-noun-u\|.*plural.*}}/, "{{ka-infl-noun|-}}");
		wikitext = wikitext.replace(/{{ka-noun-u\|.*}}/, "{{ka-infl-noun}}");
		
		wikitext = wikitext.replace(/{{ka-noun-e\|.*plural.*}}/, "{{ka-infl-noun|-}}");
		wikitext = wikitext.replace(/{{ka-noun-e\|.*}}/, "{{ka-infl-noun}}");
	
		wikitext = wikitext.replace(/{{ka\-noun\-c\-2\|.*?\|.*?\|(.*?)\|.*plural.*}}/, "{{ka-infl-noun|$1|-}}");
		wikitext = wikitext.replace(/{{ka\-noun\-c\-2\|.*?\|.*?\|(.*?)\|.*}}/, "{{ka-infl-noun|$1}}");
		
		wikitext = wikitext.replace("Declension", "Inflection");
	
		return wikitext;
	}
	//END of FUNCTIONS
	
	wikitext = fixDeclension(wikitext);
	
	var regexReplace = {
		"{{IPA\\|.+?\\|lang=ka}}"  :  "{{ka-IPA}}"
	};
	
	var templateNames = ["ka-noun", "ka-adj", "ka-pron", "ka-adv", "ka-proper noun", "ka-verbal noun", "head\\|ka\\|noun form"];
						
	["ka", "hy", "xcl"].forEach(function(lang, ind, arr) {
		templateNames.push("l\\|" + lang);
		templateNames.push("m\\|" + lang);
	});
	
	templateNames.forEach(function(element, index, array){
		var regex="(" + element + "[^\\}]*?)\\|tr\\=.+?(\\||\\}\\})(.*?)";
		regexReplace[regex] = "$1$2$3";
	});

	
	wikitext = removeHead(wikitext);
	//replaces redundant transliterations in headword and l. also corrects IPA.
	for (var element in regexReplace){
		var re = new RegExp(element, "g");
		wikitext = wikitext.replace(re, regexReplace[element]);
	}
	
	wikitext = addPronunciation(wikitext);
	
	return wikitext;
	//document.editform.submit();
}

function doForAll(wikitext)
{
	function brackets_2_l(wikitext)
	{
		//var lang2code = {'Georgian' : 'ka', 'Russian' : 'ru', 'Armenian' : 'hy'};
		var possibleHeaders = ["Alternative forms", "Synonyms", "Antonyms", "Derived terms", "Related terms", "See also", "Hypernyms"];
		var wikixml = WikiXml.parseWikitext(wikitext, {"wrapTemplate": true});
		if (wikixml === undefined) return wikitext;
		
		var data = wikixml.find("notemplate");
		data.each( function() {
			var elem = $(this);
			if ($.inArray(elem.closest("section").attr("class"), possibleHeaders) == -1) return;
			
			var datatext = elem.text();
			var language = elem.closest('[level="2"]').attr("class");
			if (language == "English") return;
			
			var langcode = '{{subst:#invoke:languages/templates|getByName|' + language + '}}';
			datatext = datatext.replace(/\[\[([^:]*?)\]\]/g, function(whole, captured, line) {
				var ans = '{{l|' + langcode + '|';
				if (captured.indexOf("|") == -1) return ans + captured + '}}';
				captured = captured.split("|");
				var showWord = captured[1];
				var linkWord = captured[0];
				if (linkWord.indexOf("#") != -1)
				{
					var lineLanguage = linkWord.split("#")[1];
					linkWord = linkWord.split("#")[0];
					if (lineLanguage != language) return whole;
				}
				return ans + linkWord + '|' + showWord + '}}';
			});


			elem.text(datatext);
		});
		return WikiXml.toWikitext(wikixml);
	}
	
	return brackets_2_l(wikitext);
}

function clean(wikitext)
{
	wikitext = doForAll(wikitext);
	if (new RegExp("[ა-ჰ ]").test(mw.config.values.wgPageName)) wikitext = doGeorgian(wikitext);//need to change to also understand svan and others using XMLize
	return wikitext;
}
function cleanup()
{
	var wikitext = $("#wpTextbox1").val();
	$("#wpTextbox1").val(clean(wikitext));
}

function initMinorEdit() {
	$ ("#wpDiffWidget")
	.after ($("<span class='oo-ui-widget oo-ui-widget-enabled oo-ui-inputWidget oo-ui-buttonElement oo-ui-buttonElement-framed oo-ui-labelElement oo-ui-buttonInputWidget'></span>")
		.html("<input value = 'Minor Cleanup' class='oo-ui-inputWidget-input oo-ui-buttonElement-button' onclick='cleanup(); return false;' />"));
}

if (
	(mw.config.values.wgAction == "edit" || mw.config.values.wgAction == "submit") &&
	(mw.config.values.wgPageName == "User:Dixtosa/minorEdits/test" || mw.config.values.wgNamespaceNumber === 0)
	)
{
	$(document).ready(initMinorEdit);
}

//</nowiki>