User:Robin Lionheart/common.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.

This JavaScript is executed for Robin Lionheart on every page load.


/* global $, mw, pathoschild */

/**
 * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
 * @see https://meta.wikimedia.org/wiki/TemplateScript
 * @update-token [[File:Pathoschild/templatescript.js]]
 */
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
	pathoschild.TemplateScript.add([
		{
			name: 'Grease New eo form',
			script: neweoform
		},
		//{
		//	name: 'standardization and cleanup',
		//	script: standardize
		//},
		//{
		//	name: 'new eo animal',
		//	script: new_eo_animal
		//},
		//{
		//	name: 'new eo animal adj',
		//	script: new_eo_animal_a
		//},
		//{
		//	name: 'new eo animal ido',
		//	script: new_eo_animal_ido
		//}
	]);
});

// ==UserScript==
// @name           Wiktionary Esperanto Forms
// @namespace      http://www.robinlionheart.com/
// @include        http://en.wiktionary.org/w/index.php?title=*
// ==/UserScript==

function debug(msg) {
	console.log(msg);
	//alert(msg);
}

function neweoform(editor) {
	// sanity check: check if we're running this on the wrong page
	if(editor.action !== 'edit') {
		debug("abort: no edit form");
		return;
	}

	var pagetitle = mw.config.get('wgPageName');

	// if this looks like a new topic cat, fill in topic cat template
	var preventry = editor.get();
	if (preventry == "") {
		debug("no entry, check for preload text");
		preventry = mw.util.getParamValue('preloadtext');
		debug("preventry: " + preventry);
	}
	if (pagetitle.substr(0,9) == "Category:" && pagetitle.charAt(11) == ':') {
		var lang = pagetitle.substr(9,2);
		var tcat = pagetitle.substr(12);
		//debug("Yes, it is");

		editor.options({ minor: true });

		if (preventry == "") {
			editor
				.set("{"+"{topic cat|" + lang + "|" + tcat + "}}")
				.setEditSummary("New topic cat for " + tcat);
		}
		else
			debug("abort: previous category entry");

		return;
	}

	// don't get involved with anything else with a colon
	// ex. "User talk:", "Wiktionary:"
	if (pagetitle.indexOf(':') != -1) {
		debug("abort: page name has colon");
		return;
	}

	// if a subst:new eo form's there, our work here is already done
	if (preventry.indexOf("{\{subst:new eo form") != -1) {
		debug("abort: subst form already there");
		return;
	}

	// if a part of speech template is already there, abort
	if ((preventry.indexOf("{\{eo-noun") != -1)
	    || (preventry.indexOf("{\{eo-proper noun") != -1)
	    || (preventry.indexOf("{\{eo-verb") != -1)
	    || (preventry.indexOf("{\{eo-adj") != -1)
	    || (preventry.indexOf("{\{eo-adv") != -1)
	    || (preventry.indexOf("{\{eo-con") != -1)
	    || (preventry.indexOf("{\{eo-intj") != -1)
	    || (preventry.indexOf("{\{eo-part-sublemma") != -1)
	    || (preventry.indexOf("{\{eo-phrase") != -1)
	   ) {
		debug("abort: part of speech template found");
		return;
	}

	// if no header for a part of speech with endings, abort
	if (preventry != ""
	    && (preventry.indexOf("===Noun===") == -1)
	    && (preventry.indexOf("===Proper noun===") == -1)
	    && (preventry.indexOf("===Verb===") == -1)
	    && (preventry.indexOf("===Adjective===") == -1)
	    && (preventry.indexOf("===Adverb===") == -1)
	   ) {
		debug("abort: no header for a part of speech with endings");
		return;
	}

	// are we editing a whole page with an Esperanto subsection?
	debug("checking: is there an Esperanto subsection?");
	var before = "", after = "";
	var strbegin = preventry.indexOf("==Esperanto==");
	if (strbegin > 0) {
		//debug("Esperanto subsection detected.");
		window.status = "Please edit the Esperanto section directly next time.";

		// save everything before ==Esperanto==,
		// for when we rewrite the textbox contents
		before = preventry.substr(0, strbegin);
		preventry = preventry.substr(strbegin);
	}
	strbegin = 0;

	// anything afterward we need to preserve?
	debug("checking: anything after to preserve?");
	const preserveafter = [
		"----", 		// following section?
		"\n{{count page|",	// counter?
		"\n[[",			// interwiki links?
		"\n#*", 		// quotation?
		"\n#:"			// sample text?
	];
	var minpos = preventry.length;
	for (i in preserveafter) {
		pos = preventry.indexOf(preserveafter[i])
		if (pos != -1 && pos < minpos) minpos = pos;
	}
	// save everything afterward then, for when we replace textbox
	after = preventry.substr(minpos);
	preventry = preventry.substr(0,minpos);
	// if (before != "" || after != "") debug("before: " + before + " after: " + after);

	var stem, ending, templatecall;

	// if there's an eo-form-of, grab parameters from that
	debug("checking: is there a eo-form-of template already?");
	strbegin = preventry.indexOf("{\{eo-form of|");
	if (strbegin != -1) {
		debug("found 'eo-form of'");
		// just stick the contents of that into new eo form, then
		if (
			preventry.substr(0, strbegin) == ("==Esperanto==\n\n===Noun===\n'''" + pagetitle + "'''\n\n# ")
			|| preventry.substr(0, strbegin) == ("==Esperanto==\n\n===Adjective===\n'''" + pagetitle + "'''\n\n# ")
			|| preventry.substr(0, strbegin) == ("==Esperanto==\n\n===Verb===\n'''" + pagetitle + "'''\n\n# ")
		) {
			//debug("before: " + before + "\nafter: " + after + "\n\nerasing pre-form-of matter");
			// nothing before the form of that needs saving
			preventry = preventry.substr(strbegin);
			strbegin = 0;
		}

		strbegin += 13;

		var strend = preventry.indexOf("}}", strbegin);
		stem = preventry.substring(strbegin, strend);
		ending = "";
		preventry = preventry.substr(strend + 2);

		templatecall = "subst:new eo form|" + stem;

		// trim linefeeds
		while (preventry.charAt(0) == ' ' || preventry.charAt(0) == '\n') preventry = preventry.substr(1);

		// anything unaccounted for? speak up about it
		var debugmsg = "extracted '" + stem + "' from eo-form of";
		if (preventry) debugmsg += "\nleftovers: '" + preventry + "'";
		debug(debugmsg);
	} else {
		debug("no, do it the hard way");
		ending = pagetitle;

		// check endings
		const endings = [
/*
			'antajn', 'antojn',
			'intajn', 'intojn',
			'ontajn', 'ontojn',
			// 'untajn', 'untojn', // uncomment if you need them
			'antaj', 'antan', 'antoj', 'anton',
			'intaj', 'intan', 'intoj', 'inton',
			'ontaj', 'ontan', 'ontoj', 'onton',
			'atojn', 'itojn', 'otojn',
			// 'untaj', 'untan', 'untoj', 'unton',
			'anta', 'anto', 'ante',
			'ataj', 'atan', 'atoj', 'aton',
			'inta', 'into', 'inte',
			'itaj', 'itan', 'itoj', 'iton',
			'onta', 'onto', 'onte',
			'otaj', 'otan', 'otoj', 'oton',
			// 'unta', 'unto', 'unte',
			// 'utaj', 'utan', 'utoj', 'uton',
			'ata', 'ato',
			'ita', 'ito',
			'ota', 'oto',
			// 'uta', 'uto',
*/
			'ajn', 'ojn',
			'as', 'is', 'os', 'us',
			'aj', 'an', 'oj', 'on',
			'u',
			'a', 'o', 'e', 'i', '-' ];

		// debug("checking endings");
		for (i in endings) {
			// debug("checking " + endings[i]); // debug
			if (ending.length < endings[i].length) continue;
			if (ending.length != endings[i].length) {
				stem = pagetitle.substring(0, pagetitle.length - endings[i].length);
				ending = pagetitle.substring(pagetitle.length - endings[i].length);
			}
			if (ending != endings[i]) continue;
			debug("found ending: " + endings[i]);
			break;
		}

		if (ending != endings[i]) {
			debug("abort: no ending found");
			return;
		}

		// if initial capital, assume proper
		var proper = (pagetitle.charAt(0) == pagetitle.charAt(0).toUpperCase());
		// scan for uncountable flag
		var unc = (preventry.indexOf("|unc=yes") != -1);

		// may change ending if proper
		if (proper) {
			if (ending == "on") ending = "on-proper";
			else if (ending == "oj" || ending == "ojn") ending += "-properpl";
		}

		templatecall = "subst:new eo form|"
			+ stem
			+ "|"
			+ ending;

		if (unc) templatecall += "|unc=yes";
	}

	if (editor.val() != "") {
		// prompt to erase incorrect Verb entries
		if (
			((preventry.indexOf("===Verb===") != -1 && ending.length > 2) || preventry.indexOf("'''") != -1)
			&& confirm("BEFORE: " + before  + "\nBETWEEN: " + preventry+ "\nAFTER: " + after + "\nreplace what's in between?")) {
			preventry = "";
			editor.set("");
		}
		// prompt to continue
		else if (preventry.indexOf("{\{head|eo|") != -1 && !confirm("BEFORE: " + before  + "\nBETWEEN: " + preventry+ "\nAFTER: " + after + "\ncontinue?")) {
			return;
		}
	}

	// fill in form
	editor.options({ minor: true, watch: true });

//	if (after == "") after = "\n{\{count page|[[Wiktionary:Page count]]}}";

	if (editor.get() != "" && preventry != "") {
		// if there is unaccounted for material,
		// then put it all after a section break
		editor.set(before + "{\{" + templatecall + "}}\n----\n" + preventry + after);
	} else {
		editor.set(before + "{\{" + templatecall + "}}\n" + after);
	}

	editor.setEditSummary("/* Esperanto */ generating with " + templatecall + " ([[User:Robin Lionheart/common.js|Greased]])");
}

// uncomment the following line to make this a Greasemonkey script
//$(function() { neweoform(pathoschild.TemplateScript.Context); });