Тизим:Сайқаллаш.жс
Qiyofa
// [[Ziyosfera:Sayqallash]]
// MediaWiki:Sayqallash.js
// Oʻzbek tili uchun matnni avtomatik sayqallash (Vikifikator / Wikificator)
// MediaWiki 1.45+, WikiEditor va CodeMirror bilan toʻliq moslashtirilgan.
/* <pre> */
/* global mw, jQuery, $ */
( function () {
'use strict';
// 1. Asosiy matnni sayqallash (Vikifikatsiya) funksiyasi
function processWikitext( rawText ) {
if ( !rawText ) {
return '';
}
var txt = rawText;
var hidden = [];
function hide( re ) {
txt = txt.replace( re, function ( match ) {
return '\x01' + ( hidden.push( match ) - 1 ) + '\x02';
} );
}
// A. Oʻzgartirilmasligi kerak boʻlgan teglarni yashirish
var tagsToHide = [
'nowiki', 'pre', 'syntaxhighlight', 'source', 'math', 'chem',
'gallery', 'timeline', 'score', 'categorytree', 'hiero', 'graph'
];
for ( var i = 0; i < tagsToHide.length; i++ ) {
hide( new RegExp( '<' + tagsToHide[ i ] + '(?:\\s[^>]*)?>[\\s\\S]*?<\\/' + tagsToHide[ i ] + '>', 'gi' ) );
}
// HTML izohlar (<!-- ... -->)
hide( /<!--[\s\S]*?-->/g );
// B. Andozalarni ({{...}}) ichma-ich (nested) toʻgʻri yashirish
var pos = 0;
var stack = [];
while ( true ) {
var left = txt.indexOf( '{{', pos );
var right = txt.indexOf( '}}', pos );
if ( left === -1 && right === -1 && !stack.length ) {
break;
}
if ( left !== -1 && ( left < right || right === -1 ) ) {
stack.push( left );
pos = left + 2;
} else {
left = stack.pop();
if ( typeof left === 'undefined' ) {
if ( right === -1 ) {
pos += 2;
continue;
} else {
left = 0;
}
}
if ( right === -1 ) {
right = txt.length;
}
right += 2;
var tpl = txt.substring( left, right );
var ph = '\x01' + ( hidden.push( tpl ) - 1 ) + '\x02';
txt = txt.substring( 0, left ) + ph + txt.substr( right );
pos = left + ph.length;
}
}
// C. Tashqi URL manzillarni yashirish
hide( /(?:https?|ftp):\/\/[^\s\[\]<>"'\(\)]+/g );
// D. Ichki havolalarni qayta ishlash: [[Fayl:...]] va [[Turkum:...]] toʻliq yashiriladi
txt = txt.replace( /\[\[([\s\S]*?)\]\]/g, function ( match, inner ) {
if ( /^(?:Fayl|File|Image|Rasm|Turkum|Category|Media):/i.test( inner ) ) {
return '\x01' + ( hidden.push( match ) - 1 ) + '\x02';
}
if ( inner.indexOf( '|' ) !== -1 ) {
var parts = inner.split( '|' );
var target = parts[ 0 ];
var anchor = parts.slice( 1 ).join( '|' );
var targetPh = '\x01' + ( hidden.push( target ) - 1 ) + '\x02';
return '[[' + targetPh + '|' + anchor + ']]';
} else {
return '\x01' + ( hidden.push( match ) - 1 ) + '\x02';
}
} );
// E. HTML teglar va ularning atributlarini yashirish (<tag attr="value">)
hide( /<[^>]+>/g );
// -------------------------------------------------------------
// 2. TIPOGRAFIKA VA OʻZBEK TILI IMLO QOIDALARINI QOʻLLASH
// -------------------------------------------------------------
// Sarlavhalardagi boʻshliqlar: ==Sarlavha== -> == Sarlavha ==
txt = txt.replace( /^(=+)[ \t]*(.*?)[ \t]*(=+)$/gm, '$1 $2 $1' );
// Oʻzbekcha Oʻ va Gʻ harflari (U+02BB - ʻ)
// Har xil notoʻgʻri apostrof belgilarini toʻgʻrilash: ', `, ‘, ’, ‛, ′
txt = txt.replace( /([OoGg])['`‘\u2019\u201b\u2032]/g, '$1ʻ' );
txt = txt.replace( /([OoGg])"{1,2}/g, '$1ʻ' );
txt = txt.replace( /([OoGg])''([a-zA-Z])/g, '$1ʻ$2' );
// Tutuq belgisi (U+02BC - ʼ)
// Harflar orasida: ma'lumot -> maʼlumot, ta'lim -> taʼlim, san'at -> sanʼat, Qur'on -> Qurʼon
txt = txt.replace( /([a-zA-Z\u0400-\u04FF])['`‘\u2019\u201b\u2032]([a-zA-Z\u0400-\u04FF])/g, '$1ʼ$2' );
// Soʻz boshida: e'lon -> eʼlon, e'tibor -> eʼtibor, a'zo -> aʼzo
txt = txt.replace( /(^|[\s\("«])([EeAa])['`‘\u2019\u201b\u2032]([a-zA-Z\u0400-\u04FF])/g, '$1$2ʼ$3' );
// Darajalar ² va ³
txt = txt.replace( /(?:<sup>2<\/sup>|²)/g, '²' );
txt = txt.replace( /(?:<sup>3<\/sup>|³)/g, '³' );
txt = txt.replace( /\^2(?=\D|$)/g, '²' );
txt = txt.replace( /\^3(?=\D|$)/g, '³' );
// Matematik va maxsus belgilar: ±, ≈, °, °C, °F
txt = txt.replace( /(?:\+--?|±)/g, '±' );
txt = txt.replace( /~=/g, '≈' );
txt = txt.replace( /°/g, '°' );
txt = txt.replace( /(^|[\s\(=≈≠≤≥<>\|])([+±−\-]?\d+(?:[.,]\d+)?)\s*(?:[°^*]\s*|°\s*)?[CС](?=[\s\)\'".,;!?\|]|$)/gm, '$1$2\u00A0°C' );
txt = txt.replace( /(^|[\s\(=≈≠≤≥<>\|])([+±−\-]?\d+(?:[.,]\d+)?)\s*(?:[°^*]\s*|°\s*)?F(?=[\s\)\'".,;!?\|]|$)/gm, '$1$2\u00A0°F' );
// Koʻp nuqta (...) -> ellips (…)
txt = txt.replace( /(?:\.{3}|&(?:hellip|#133);)/g, '…' );
// Tire va chiziqchalar (lookbehind siz, barcha brauzerlar uchun xavfsiz)
txt = txt.replace( /&(?:#151|[nm]dash);/g, '—' );
txt = txt.replace( /(\s)(?:--|–|—)(\s)/g, '$1—$2' );
txt = txt.replace( /(\S)\s+(?:--|–|—)\s+(\S)/g, '$1\u00A0— $2' );
txt = txt.replace( /(\d)\s*--\s*(\d)/g, '$1—$2' );
// Maxsus ramzlar: ©, ®, ™, §, €, ¥, £
txt = txt.replace( /©/gi, '©' );
txt = txt.replace( /(?:\(r\)|®)/gi, '®' );
txt = txt.replace( /(?:\((?:tm|тм)\)|™)/gi, '™' );
txt = txt.replace( /(?:\(p\)|§)/gi, '§' );
txt = txt.replace( /€/gi, '€' );
txt = txt.replace( /¥/gi, '¥' );
txt = txt.replace( /£/gi, '£' );
// Sonlar va birliklar orasida boʻlinmas probel (\u00A0)
txt = txt.replace( /(\d)\s*(mln|mlrd|trln|mln\.|mlrd\.|trln\.|million|milliard|trillion)(?=[^\wʻʼ]|$)/gi, '$1\u00A0$2' );
txt = txt.replace( /(\d)\s*(ming|nusxa|dona|kishi|nafar|foiz|metr|sm|mm|km|kg|tonna|yil|asr)(?=[^\wʻʼ]|$)/gi, '$1\u00A0$2' );
txt = txt.replace( /(\d)\s*%/g, '$1%' );
txt = txt.replace( /([№§])\s*(\d)/g, '$1\u00A0$2' );
// Bosh harflar (initsiallar): A. Navoiy -> A. Navoiy
txt = txt.replace( /([A-ZА-ЯЁ]\.)\s*([A-ZА-ЯЁ]\.)\s*([A-ZА-ЯЁ][a-zа-яё])/g, '$1\u00A0$2\u00A0$3' );
txt = txt.replace( /([A-ZА-ЯЁ]\.)\s*([A-ZА-ЯЁ][a-zа-яё])/g, '$1\u00A0$2' );
// Tinish belgilaridan keyin probel qoʻyish (vergul, nuqta-vergul)
txt = txt.replace( /([a-zA-Z\u0400-\u04FF\)])([,;])([a-zA-Z\u0400-\u04FF\(\"«])/g, '$1$2 $3' );
// Qoʻshtirnoqlar: barchasini " ga keltirib, tashqi «...» va ichki „...“ ga aylantirish
txt = txt.replace( /[«»“”„"]|&(?:[lr]aquo|[bl]dquo|#132|#147|#148|quot);/g, '"' );
txt = txt.replace( /<<(.+?)>>/g, '"$1"' );
txt = txt.replace( /(^|[\s\(\x01])"([^"\n]+?)"(?=[\s\)\x01.,;:!?]|$)/g, '$1«$2»' );
while ( /«([^»]*)«([^»]*)»/g.test( txt ) ) {
txt = txt.replace( /«([^»]*)«([^»]*)»/g, '«$1„$2“' );
}
// 3. Yashirilgan elementlarni teskari tartibda qayta tiklash
for ( var j = hidden.length - 1; j >= 0; j-- ) {
var placeholder = '\x01' + j + '\x02';
txt = txt.split( placeholder ).join( hidden[ j ] );
}
return txt;
}
// 3. Avtokapitalizatsiya (gap oxiridagi tinish belgilaridan keyin bosh harf)
function initAutoCapitalization() {
var $textbox = $( '#wpTextbox1' );
if ( !$textbox.length ) {
return;
}
$textbox.on( 'keyup', function ( event ) {
if ( event.keyCode !== 32 ) {
return;
}
var txtarea = this;
var startPos = txtarea.selectionStart || 0;
var endPos = txtarea.selectionEnd || 0;
var scrollTop = txtarea.scrollTop;
var letterPattern = /[0-9a-z`'ʻʼ‘’]/i;
var letter = '';
var letterPos = 0;
var pointfound = false;
var spacefound = false;
var localTxt = '. ' + txtarea.value;
var i, k;
for ( i = startPos - 1; i >= 0; i-- ) {
k = localTxt.substring( i, i + 1 );
if ( letterPattern.test( k ) && !spacefound ) {
letter = k;
letterPos = i - 2;
} else if ( k === ' ' || k === '\n' ) {
spacefound = true;
} else if ( ( k === '.' || k === '!' || k === '?' ) && spacefound ) {
pointfound = true;
break;
} else {
break;
}
}
if ( pointfound && letter && letter !== letter.toUpperCase() && letterPos >= 0 ) {
txtarea.value =
txtarea.value.substring( 0, letterPos ) +
letter.toUpperCase() +
txtarea.value.substring( letterPos + 1 );
txtarea.selectionStart = startPos;
txtarea.selectionEnd = endPos;
txtarea.scrollTop = scrollTop;
}
} );
}
// 4. Sayqallashni tahrir maydonida ishga tushirish (CodeMirror va WikiEditor bilan)
function executeSayqallash() {
var $textbox = $( '#wpTextbox1' );
if ( !$textbox.length ) {
return;
}
// CodeMirror instance mavjudligini tekshirish
var cm = null;
var cmElem = document.querySelector( '.CodeMirror' );
if ( cmElem && cmElem.CodeMirror ) {
cm = cmElem.CodeMirror;
}
var hasSelection = false;
var originalText = '';
// A. Matn va tanlovni aniqlash
if ( cm ) {
hasSelection = cm.somethingSelected();
if ( hasSelection ) {
originalText = cm.getSelection();
} else {
originalText = cm.getValue();
}
} else if ( $.fn.textSelection ) {
var sel = $textbox.textSelection( 'getSelection' );
if ( sel && sel.length > 0 ) {
hasSelection = true;
originalText = sel;
} else {
originalText = $textbox.textSelection( 'getContents' );
}
} else {
var domEl = $textbox[ 0 ];
if ( domEl.selectionStart !== undefined && domEl.selectionEnd !== undefined && domEl.selectionStart !== domEl.selectionEnd ) {
hasSelection = true;
originalText = domEl.value.substring( domEl.selectionStart, domEl.selectionEnd );
} else {
originalText = domEl.value;
}
}
if ( !originalText ) {
mw.notify( 'Sayqallash uchun matn topilmadi.', { title: 'Sayqallash', type: 'warn' } );
return;
}
// B. Qayta ishlash
var processedText = processWikitext( originalText );
// C. Natijani qaytarib yozish
if ( cm ) {
if ( hasSelection ) {
cm.replaceSelection( processedText );
} else {
var cursor = cm.getCursor();
var scrollInfo = cm.getScrollInfo();
cm.setValue( processedText );
cm.setCursor( cursor );
cm.scrollTo( scrollInfo.left, scrollInfo.top );
}
$textbox.val( cm.getValue() ).trigger( 'input' ).trigger( 'change' );
} else if ( $.fn.textSelection ) {
if ( hasSelection ) {
$textbox.textSelection( 'encapsulateSelection', {
replace: true,
peri: processedText
} );
} else {
$textbox.textSelection( 'setContents', processedText );
$textbox.trigger( 'input' ).trigger( 'change' );
}
} else {
var el = $textbox[ 0 ];
if ( hasSelection ) {
var start = el.selectionStart;
var end = el.selectionEnd;
el.value = el.value.substring( 0, start ) + processedText + el.value.substring( end );
el.selectionStart = start;
el.selectionEnd = start + processedText.length;
} else {
el.value = processedText;
}
$textbox.trigger( 'input' ).trigger( 'change' );
}
// D. Foydalanuvchiga xabarnoma chiqarish
if ( originalText === processedText ) {
mw.notify( 'Matnda oʻzgartirish talab qilinadigan holat topilmadi.', { title: 'Sayqallash', type: 'info' } );
} else {
if ( hasSelection ) {
mw.notify( 'Tanlangan matn muvaffaqiyatli sayqallandi!', { title: 'Sayqallash', type: 'success' } );
} else {
mw.notify( 'Barcha matn muvaffaqiyatli sayqallandi!', { title: 'Sayqallash', type: 'success' } );
}
}
}
// 5. Tugma va UI panelini qoʻshish
function addSayqallashUI() {
if ( document.getElementById( 'sayqallashButton' ) ) {
return;
}
var btnHtml =
'<a id="sayqallashButton" href="#" title="Matnni avtomatik sayqallash (Ctrl+Alt+W)" ' +
'style="display:inline-block;cursor:pointer;text-decoration:none;color:#1e3a8a;font-weight:bold;font-size:15px;border:1px solid #93c5fd;background:#dbeafe !important;padding:2px 8px !important;border-radius:4px;margin-right:6px;" ' +
'accesskey="w">Sayqallash</a>' +
'<span style="color:#4b5563;font-size:13px;">— matndagi apostrof (oʻ, gʻ), tutuq belgisi (ʼ), tire, qoʻshtirnoq va imlo belgilarini avtomatik tuzatadi ' +
'[<a href="/ziyo/Ziyosfera:Sayqallash" target="_blank" title="Sayqallash haqida yordam sahifasi">?</a>]</span>';
if ( $( '#sayqallashPlaceHolder' ).length ) {
$( '#sayqallashPlaceHolder' ).html( btnHtml );
} else if ( $( '.editOptions' ).length ) {
$( '.editOptions' ).before( '<div id="sayqallashPlaceHolder" style="margin:8px 0;">' + btnHtml + '</div>' );
} else if ( $( '#editform' ).length ) {
$( '#editform' ).prepend( '<div id="sayqallashPlaceHolder" style="margin:8px 0;">' + btnHtml + '</div>' );
}
}
// WikiEditor uskunalar paneliga tugma qoʻshish
function addToolbarButton() {
var $textbox = $( '#wpTextbox1' );
if ( $.fn.wikiEditor ) {
$textbox.wikiEditor( 'addToToolbar', {
section: 'main',
group: 'format',
tools: {
sayqallash: {
label: 'Sayqallash (Ctrl+Alt+W)',
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/0/06/Wikify-toolbutton.png',
action: {
type: 'callback',
execute: function () {
executeSayqallash();
}
}
}
}
} );
}
}
// 6. Skriptni ishga tushirish
$( function () {
initAutoCapitalization();
addSayqallashUI();
mw.loader.using( [ 'jquery.textSelection', 'mediawiki.util' ], function () {
addSayqallashUI();
addToolbarButton();
// Tugmaga bosilganda (event delegation)
$( document ).on( 'click', '#sayqallashButton', function ( e ) {
e.preventDefault();
executeSayqallash();
} );
// Klaviatura yorliqlari (Ctrl+Alt+W yoki Ctrl+Shift+W)
$( document ).on( 'keydown', function ( e ) {
if ( ( e.ctrlKey && e.altKey && ( e.key === 'w' || e.key === 'W' || e.keyCode === 87 ) ) ||
( e.ctrlKey && e.shiftKey && ( e.key === 'w' || e.key === 'W' || e.keyCode === 87 ) ) ) {
e.preventDefault();
executeSayqallash();
}
} );
} );
} );
// Global obyektlar
window.Sayqallash = executeSayqallash;
window.Wikify = executeSayqallash;
}() );
/* </pre> */