میڈیا وکی:Gadget-Extra-Editbuttons-urduwikitools.js

آزاد انسائیکلوپیڈیا، وکیپیڈیا توں

نوٹ: بچان مگروں توانوں اپنے براؤزر دے کاشے توں بار جانا پوے گا تبدیلیاں ویکھن لئی۔

  • Firefox / Safari: Shift پھڑی رکھو ریلوڈ تے کلکنگ کردیاں ہویاںCtrl-F5 or Ctrl-R (Command-R میک تے)
  • گوکل کروم: دباؤ Ctrl-Shift-R (Command-Shift-R میک تے)

Internet Explorer: hold Ctrl کلک کردیاں Refresh, یا دباؤ Ctrl-F5 Konqueror:کلک ریلوڈ یا F5 دباؤ۔ Opera: کاشے نوں صاف کرو آوزار → تانگاں

// <nowiki> // DO NOT REMOVE THIS LINE EVER
/**
 * Wikipedia specific Urdu text style improvement tools
 * Tests: [[میڈیا وکی:Gadget-Extra-Editbuttons-tests.js]] [[وپ:تجربہ]]
 * See also: [[میڈیا وکی:Gadget-Extra-Editbuttons-urdutools.js]]
 */
/*global urduTools, urduToolsDictionary, console*/
var urduWikiTools = (function () {
	'use strict';
 
	function descendingFromComparetor(x, y) {
		return x.from - y.from;
	}
 
	function replaceExcept(text, callback, excepts) {
		var match, result = [], i, ranges, minRange, to, min, max;
		while (text !== '') {
			ranges = [];
 
			for (i in excepts) {
				if (excepts.hasOwnProperty(i)) {
					// a global regex should be reset before calls
					excepts[i].lastIndex = 0;
					match = excepts[i].exec(text);
					if (match !== null) {
						ranges.push({
							from: match.index,
							to: match.index + match[0].length
						});
					}
				}
			}
 
			// so nothing is matched
			if (ranges.length === 0) {
				result.push(callback(text));
				break;
			}
 
			minRange = ranges.sort(descendingFromComparetor)[0];
			min = minRange.from;
 
			to = [];
			for (i in ranges) {
				if (ranges.hasOwnProperty(i)) {
					if (ranges[i].from <= minRange.to) {
						to.push(ranges[i].to);
					}
				}
			}
			max = Math.max.apply(null, to);
 
			result.push(callback(text.substr(0, min)));
			result.push(text.substr(min, max - min));
			// console.log('Excepted: "' + text.substr(min, max - min) + '"');
			text = text.substr(max);
		}
		return result.join('');
	}
 
	var patterns = {
		arabicDigitsEnglishContext: /[a-z][a-z %"'\._:\|,\-\\\/\(\)\#\^\+\d><–\[\]&?]*\d|(\d|[a-z])[a-z %"'\._:\|,\-\\\/\(\)\#\^\+\d><–\[\]&?]*[a-z]\d*/gi,
		arabicTagEnclosed: /\{\{(?:|عربی متن آغاز |ع )\}\}([\s\S]*?)\{\{(?: عربی متن اختتام|ڑ)\}\}/g,
		argumentsBlacklist: /(?:accessdate|namespace|doi|style|آئی ایس بی این|show|bibcode|isbn|pmid|arxiv)\s*\=\s*[^\|\}]*/gi,
		color: /#(?:[abcdef0-9]{8}|[abcdef0-9]{6}|[abcdef0-9]{3})/gi,
		colorAsParameter: /\=\s*(?:[abcdef0-9]{8}|[abcdef0-9]{6}|[abcdef0-9]{3})(?:[\s\|\}]|$)/gi,
		// space, ", \t, \n, {, |, }, ... they will interfere with wiki markup
		decodeUriBlacklist: /(?:%20|%5C|%5E|%60|%23|%25|%3C|%3E|%5B|%5D|%22|%09|%0A|%7B|%7C|%7D)/gi,
		englishDate: /\d{1,2},? [a-z]{3,} \d{2,4}/gi, // 3, May 2013
		fileNames: /(?:File|File|فائل|فائل|Image)\:.*?(?=\||\]|\n|$)/gi, // don't capture | after
		fileParameter: /\|\s*(image|فائل|فائل)\s*\=\s*.*/g,
		ipSign: /\[\[خاص:شراکتاں۔*?\]\]/g,
		isbn: /ISBN [\d\-]*/gi,
		galleryTag: /<gallery.*?>[\s\S]*?<\/gallery>/g,
		htmlAttributes: /(?:style|colspan|color|rowspan|cellpadding|cellspacing|height|width|size|border|thumbtime|name|perrow)\s*\=\s*(?:['\"].*?['\"]|[\da-z]+)/gi,
		htmlEntity: /&#\d+;/,
		imagePixelSize: /[\|=] *[x\d]+?(px|px)[\]\|\s]/g, // means it will capture |10px| and |10x10px|
		wikilinkTargets: /\[[^\[|\]]+/g,
		linksOnEnglishContext: /[a-z][\:\,\. ]*\[\[[\da-z\-\, ]*/gi,
		mathTag: /<math.*?>[\s\S]*?<\/math>/g,
		otherLanguagesInline: /\{\{(?:وچ.+?|انگریزی وچ |انگریزی|عربی|عربی وچ )[\s\S]*?\}\}/g,
		parameter: /\{\{\{\d+/gi,
		parenthesesAfterDigits: /\w\s?\([\w\s\.\-]*?\)/g,
		parenthesesHa: /\)ها/g,
		ref: /(?:<ref[^\/]*?>[\s\S]*?<\/ref>|<ref[^\/]*?\/>)/g, // inside <ref></ref> and <ref/>
		signatures: /\[\[(?:صارف|User|[_ ]خیال[ _]صارف|User[_ ]talk)\:.*?\]\]/gi,
		sourceTag: /(<source.*?>[\s\S]*?<\/source>|<syntaxhighlight.*?>[\s\S]*?<\/syntaxhighlight>|<code.*?>[\s\S]*?<\/code>)/g,
		tagNames: /<\/?[a-zA-Z\d]*/g,
		templateEnglishName: /(سانچہ|Template):[a-z][a-z\d\-\+_]+/gi,
		templateParameterName: /\|\s*(?=[a-z_]*\d)[a-z_\d]*\s*\=/gi,
		translatedUrl: /.(کوم|نٹ|آرگ)/g,
		url: /\/\/.*?(?=[\s\n\|\}\]<]|$)/gi	 // بغیر https?: بھی ممکن ہے
	};
 
	function wikiConvertToUrduCharacters(text) {
		return replaceExcept(
			text,
			urduTools.toStandardUrduCharacters,
			[patterns.otherLanguagesInline, patterns.arabicTagEnclosed, patterns.fileNames, patterns.signatures, patterns.url]
		);
	}
 
	if (!String.prototype.trim) { // if is not available currently
		String.prototype.trim = function () {
			return this.replace(/^\s+|\s+$/g, '');
		};
	}
 
	function wikiPunctuation(text) {
		return replaceExcept(
			text,
			function (text) {
				text = text.replace(
					new RegExp('([' + urduTools.urduCharactersNoVowels + '])ـ+([' + urduTools.urduCharactersNoVowels + '])', 'g'),
					'$1$2'
				);
				return urduTools.punctuation(text)
					.replace(/^([*#]+)([^*#\:\s])/mg, '$1 $2') // Adds a space after the # or * for lists
					.replace(/^([*#]+) {2,}([^*#\:\s])/mg, '$1 $2'); // Trim more that one space after the # or * for lists
			},
			[patterns.fileNames, patterns.url, patterns.wikilinkTargets, patterns.galleryTag, patterns.sourceTag, patterns.translatedUrl, patterns.parenthesesHa]
		)
			.replace(/\n{3,}/g, '\n\n') // Cosmetic changes bot replaces
			.replace(/\[\[(گٹھ|سانچہ|ویکیپیڈیا)\: +/g, '[[$1:')
			.replace(/[\n\s]*\{\{[•·ن](w?)\}\}\s*/g, '{{•$1}} ')
			.replace(/ *(<\/? ?br ?\/?>|\{\{بر\}\}) */g, '{{نس}}')
			.replace(/(<\/ref>)\s+(<ref)/g, '$1$2')
			.replace(/[\s\n]+<ref(?!erences)/g, '<ref')
			.replace(/(\n?)[\s\n]+?<\/ref>/g, '$1</ref>')
			.replace(/([^=])\n+(\=.*?\=\n+)/g, '$1\n\n$2')
			.replace(/^(\={2,}) +[\:,;>&\^#@•→←↔↑↓—–…~٫،؛ٔ]/mg, '$1') // Cleanup headers
			.replace(/[\:,;<&\^#@•→←↔↑↓—–…~٫،؛ٔ] +(\={2,})$/mg, '$1')
			.replace(/^(\={2,}\s*)(«)([^\n«»]*?)(»)(\s*\={2,})/mg, '$1 $3 $5')
			.replace(/^(\={2,}) *'+(.*?)'+ *(\={2,})/mg, '$1 $2 $3')
			.replace(/^[●⚫⬤]/mg, '*') // Wikify bullets in start of lines
			.replace(/^#\s*(REDIRECT|redirect)/gi, '#مڑجوڑ')
			.replace(/^#رجوع_مکرر(?=\S)/g, '#مڑجوڑ ') // Adds a space after #REDIRECT
			.replace(/(\={2,}) *([^\n\r]*?) *(\={2,})/g, '$1 $2 $3') // Format headings level 2 and above
			// روابط میں اضافی اسپیس کی حذف شدگی
			.replace(/\[\[\s*(.*?)\s*\]\]/g, '[[$1]]')
			// تبدیل به نویسه / یکی کردن فاصله های مجازی پشت سرهم
			.replace(/(\{\{فم\}\}|\&zwnj\;|\u200c+)/g, '\u200c')
			// Full stop and comma should be before citation. See en:WP:REFPUNC
			.replace(/ *((?:<ref[^\/]*?>.*?<\/ref>)+)([\.،,])?/g, '$2$1')
			.replace(/ *((?:<ref[^\/]*?\/>)+)([\.،,])/g, '$2$1')
			.trim();
	}
 
	function wikiUrlMinifier(text) {
		return text
			.replace(patterns.url, function (x) {
				return replaceExcept(
					x,
					function (x) {
						try {
							x = decodeURI(x);
						} catch (e) { console.error(e); }
						return x;
					},
					[patterns.decodeUriBlacklist]
				);
			})
 
			// Strip the http(s) prefix
			.replace(/\[(https?\:)(?=\/\/(?:[\w\-]+)\.(wiki(pedia|media|data|source|news|oyage|quote)|wiktionary)\.org\/[^\s\]]*)/g, '[');
	}
 
	function wikiTextDigitsToUrdu(text) {
		text = replaceExcept(
			text,
			urduTools.toUrduDigits,
			[patterns.url, patterns.argumentsBlacklist, patterns.mathTag, patterns.imagePixelSize, patterns.fileNames, patterns.ref,
				patterns.sourceTag, patterns.arabicDigitsEnglishContext, patterns.signatures, patterns.htmlEntity,
				patterns.htmlAttributes, patterns.fileParameter, patterns.templateParameterName, patterns.ipSign,
				patterns.parenthesesAfterDigits, patterns.otherLanguagesInline, patterns.isbn, patterns.englishDate,
				patterns.parameter, patterns.color, patterns.templateEnglishName, patterns.linksOnEnglishContext,
				patterns.colorAsParameter, patterns.tagNames]
		);
		return text
			// Decimal point, and thousands' separator
			.replace(/([1234567890])\.([1234567890])/g, '$1٫$2')
			.replace(/([1234567890]),([1234567890])/g, '$1٬$2');
	}
 
	function dictationReplace(x, y, extensions, text) {
		return text.replace(
			new RegExp(
				'(^|[^' + urduTools.urduCharacters + '])(\\s|\u200c|_|)(' + x + ')(\\s|_)(' + y + ')(\\s|\u200c|_|)(' +
					extensions + ')($|[^' + urduTools.urduCharacters + '])',
				'g'
			),
			'$1$2$3\u200c$5$6$7$8'
		);
	}
 
	function wikiDictation(text) {
		return replaceExcept(
			text,
			dictation,
			[patterns.fileNames, patterns.signatures, patterns.url, patterns.galleryTag]
		);
	}
 
 
	return {
		convertDigitsToUrdu: urduTools.toStandardUrduCharacters,
		cleanUselessZwnj: urduTools.normalizeZwnj,
		dictation: dictation,
		wikiConvertToUrduCharacters: wikiConvertToUrduCharacters,
		wikiDictation: wikiDictation,
		wikiPunctuation: wikiPunctuation,
		wikiUrlMinifier: wikiUrlMinifier,
		wikiTextDigitsToUrdu: wikiTextDigitsToUrdu
	};
}());
if (typeof window !== "undefined") {
	window.urduWikiTools = urduWikiTools;
}