User:Amousey/monobook.js

// de Wikipedia veAutocorrect.js // actual script in monobook.js which // needs editing for script name // and domain names because it won't run on all wikis // invoke monobook.js here //Dokumentation unter Benutzer:Schnark/js/veAutocorrect /*global mediaWiki, OO, ve*/ (function (mw) { "use strict";

var VISUAL = 1, SOURCE = 2, deps;

function initAutoCorrect (lang, wiki) { var autoCorrectCommandCount = 0, quotes, i;

//like ve.ui.Sequence, with the difference that for regular expressions //of the form /foo(bar)/ only the parentheses is used as Range, not the whole expression function ReSequence { ReSequence.parent.apply(this, arguments); }	OO.inheritClass(ReSequence, ve.ui.Sequence); ReSequence.prototype.match = function (data, offset, plaintext) { var execResult; if (this.data instanceof RegExp) { execResult = this.data.exec(plaintext); return execResult && new ve.Range(offset - execResult[1].length, offset); }		return ReSequence.parent.prototype.match.apply(this, arguments); };

//when the user enters "from" change it to "to" //from can be a string, a regular expression of the form /foo(bar)/ or an array of data //to can be a string or an array of data //strip is the number of chars to strip //mode defaults to both VISUAL and SOURCE function autoCorrectFromTo (from, to, strip, mode) { //get a unique name, we use it for both the command and the sequence var name = 'schnarkAutoCorrectCommand-' + (autoCorrectCommandCount++), seq; //create and register the command ve.ui.commandRegistry.register(			//1st true: annotate, 2nd true: collapse to end			new ve.ui.Command(name, 'content', 'insert', {args: [to, true, true]})		); //create and register the sequence seq = new ReSequence(/*sequence*/ name, /*command*/ name, from, strip); if (!mode || mode === VISUAL) { ve.ui.sequenceRegistry.register(seq); }		if ((!mode || mode === SOURCE) && ve.ui.wikitextSequenceRegistry) { ve.ui.wikitextSequenceRegistry.register(seq); }	}

//define what should be autocorrected quotes = { en: ['“', '”'] };

//for all languages and projects autoCorrectFromTo(/(?:^|[^!])(--)$/, '–', 2); //don't mess with ', 2, SOURCE); //fix --> (only needed for source code)	autoCorrectFromTo(/\|date=2007-8\|$/, /|date=August 2007|/, 1);	autoCorrectFromTo(/\|date=2007-8\|$/, /|date=August 2007|/, 2);

autoCorrectFromTo(/(?:^|=\s*"[^"]*")[^=]*(?:=\s*(?:[^" ]|"[^"]*")[^=]*)*\d(")$/, '″', 1, SOURCE); //not in attributes //(?:^|=\s*"[^"]*") - anchor at the start of the line or a quoted attribute	//[^=]* - skip chars that aren't equal signs	//=\s*(?:[^" ]|"[^"]*") - equal sign either not followed by quote mark or with complete quoted attribute	//depending on the content language	if (quotes.hasOwnProperty(lang)) {		autoCorrectFromTo(/(?:^|[( \n])(")$/, quotes[lang][0], 1, VISUAL);		autoCorrectFromTo(/(?:^|=\s*"[^"]*")[^=]*(?:=\s*(?:[^" ]|"[^"]*")[^=]*)*(?:^|[( \n])(")$/, quotes[lang][0], 1, SOURCE); autoCorrectFromTo(/[^\d( \n](")$/, quotes[lang][1], 1, VISUAL);		autoCorrectFromTo(/(?:^|=\s*"[^"]*")[^=]*(?:=\s*(?:[^" ]|"[^"]*")[^=]*)*[^\d( \n=](")$/, quotes[lang][1], 1, SOURCE);	}	//depending on the wiki	/*jshint onecase: true*/	switch (wiki) {	case 'dewiki':		autoCorrectFromTo([{type: 'paragraph'}, '=', 'w'], [			{type: 'heading', attributes: {level: 2}},			'W', 'e', 'b', 'l', 'i', 'n', 'k', 's',			{type: '/heading'},			{type: 'paragraph'}		], 2, VISUAL); //actually just for proof of concept	} }

deps = ['ext.visualEditor.core']; if (mw.libs.ve.isWikitextAvailable) { deps.push('ext.visualEditor.mwwikitext'); }

mw.loader.using(deps).then(function {	if (mw.config.get('wgPageContentModel') === 'wikitext') {		//I use VE for JS/CSS, too, don't enable there		initAutoCorrect(mw.config.get('wgContentLanguage'), mw.config.get('wgDBname'));	}	mw.hook('userjs.script-ready.common').fire; });

})(mediaWiki); //