ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

//Dokumentation unter [[Benutzer:Schnark/js/veAutocorrect]] <nowiki>
/*global mediaWiki, OO, ve*/
(function (mw, $) {
"use strict";

var VISUAL = 1, SOURCE = 2;

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

	//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
	//mode defaults to both VISUAL and SOURCE
	function autoCorrectFromTo (from, to, 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, 0, true);
		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 = {
		de: ['„', '"'],
		en: ['"', '"']
	};

	//for all languages and projects
//chars

	autoCorrectFromTo('ی ', 'ي');
	//الاختصارات
	autoCorrectFromTo('ان=', '==انظر أيضا==\n*[[',SOURCE);
	autoCorrectFromTo('مر=', '==المراجع==\n{{مراجع}}',SOURCE);
	autoCorrectFromTo('وص=', '==الوصلات الخارجية==\n*[',SOURCE);

	autoCorrectFromTo(/(?:^|[^!])(--)$/, '–'); //don't mess with <!--
	autoCorrectFromTo('–>', '-->', SOURCE); //fix --> (only needed for source code)
	//autoCorrectFromTo('–-', '—'); //--- with -- turned into –
	autoCorrectFromTo('...', '…');
	//autoCorrectFromTo('<<', '"');
	//autoCorrectFromTo('>>', '"');
	autoCorrectFromTo('->', '→');
	autoCorrectFromTo(' 1/2 ', ' ½ ');
	autoCorrectFromTo(' 1/4 ', ' ¼ ');
	autoCorrectFromTo(' 3/4 ', ' ¾ ');
	autoCorrectFromTo('+-', '±');
	autoCorrectFromTo(/\d(')$/, '′');
	autoCorrectFromTo(/\D(')$/, '’', VISUAL);
	autoCorrectFromTo(/[^0-9'](')$/, '’', SOURCE); //italic/bold syntax
	autoCorrectFromTo(/([′’]\')$/, '\'\'', SOURCE); //fix italic/bold syntax
	autoCorrectFromTo(/\d(")$/, '″', VISUAL);
	autoCorrectFromTo(/(?:^|=\s*"[^"]*")[^=]*(?:=\s*(?:[^" ]|"[^"]*")[^=]*)*\d(")$/, '″', 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], VISUAL);
		autoCorrectFromTo(/(?:^|=\s*"[^"]*")[^=]*(?:=\s*(?:[^" ]|"[^"]*")[^=]*)*(?:^|[( \n])(")$/, quotes[lang][0], SOURCE);
		autoCorrectFromTo(/[^\d( \n](")$/, quotes[lang][1], VISUAL);
		autoCorrectFromTo(/(?:^|=\s*"[^"]*")[^=]*(?:=\s*(?:[^" ]|"[^"]*")[^=]*)*[^\d( \n=](")$/, quotes[lang][1], SOURCE);
	}
	//depending on the wiki
	/*jshint onecase: true*/
	switch (wiki) {
	case 'arwiki':
		autoCorrectFromTo([{type: 'paragraph'}, 'ا', 'ن', '='], [
			{type: 'heading', attributes: {level: 2}},
			'ا', 'ن', 'ظ', 'ر', ' ', 'أ', 'ي', 'ض',  'ا',
			{type: '/heading'},
			{type: 'paragraph'},
			'*'

		], VISUAL); 
		autoCorrectFromTo([{type: 'paragraph'}, 'م', 'ر', '='], [
			{type: 'heading', attributes: {level: 2}},
			'ا', 'ل', 'و', 'ص', 'ل', 'ا', 'ت', ' ',  'ا',  'ل',  'خ',  'ا',  'ر',  'ج',  'ي',  'ة',
			{type: '/heading'},
			{type: 'paragraph'},
			'*'
		], VISUAL);
	}
}

//this script should be loaded via mw.libs.ve.addPlugin, so activation should be in progress
if ($('html').hasClass('ve-activated')) {
	//wait for main module and initialize
	mw.loader.using(['ext.visualEditor.core']).done(function () {
		initAutoCorrect(mw.config.get('wgContentLanguage'), mw.config.get('wgDBname'));
	});
} else {
	mw.log.warn('You have to load "Benutzer:Schnark/js/veAutocorrect.js" via mw.libs.ve.addPlugin!');
}

})(mediaWiki, jQuery);
//</nowiki>