MediaWiki:Gadget-wikificator.js
Версия от 22:11, 6 января 2023; Андрей Фоменко (обсуждение | вклад)
Замечание. Возможно, после сохранения вам придётся очистить кэш своего браузера, чтобы увидеть изменения.
- Firefox / Safari: Удерживая клавишу Shift, нажмите на панели инструментов Обновить либо нажмите Ctrl-F5 или Ctrl-R (⌘-R на Mac)
- Google Chrome: Нажмите Ctrl-Shift-R (⌘-Shift-R на Mac)
- Internet Explorer: Удерживая Ctrl, нажмите Обновить либо нажмите Ctrl-F5
- Opera: Перейдите в Menu → Настройки (Opera → Настройки на Mac), а затем Безопасность → Очистить историю посещений → Кэшированные изображения и файлы
// <nowiki> mw.loader.using( 'jquery.client', function () { var clientProfile = $.client.profile(); var hotkey = clientProfile.platform === 'mac' ? 'Ctrl+Shift+W' : 'Ctrl+Alt+W'; var strings = { name: 'Викификатор', tooltip: 'Викификатор — автоматический обработчик текста (' + hotkey + ')', summary: 'викификатор', fullText: 'Викификатор обработает ВЕСЬ текст на этой странице. Продолжить?', talkPage: 'Викификатор не обрабатывает страницы обсуждения целиком.\n\nВыделите ваше сообщение — обработано будет только оно.' }; window.wfPlugins = window.wfPlugins || []; window.wfPluginsT = window.wfPluginsT || []; // Function takes an input or text as an argument. If it is absent, it uses $( '#wpTextbox1' ) // as an input. window.Wikify = function ( inputOrText ) { 'use strict'; // FUNCTIONS function r( r1, r2 ) { txt = txt.replace( r1, r2 ); } function processText() { var u = '\u00A0'; // non-breaking space var i; r( / +(\n|\r)/g, '$1' ); // spaces at EOL txt = '\n' + txt + '\n'; r( /[ \t\u00A0]{2,}/g, ' ' ); // double spaces // Entities etc. → Unicode chars if ( mw.config.get( 'wgNamespaceNumber' ) !== 10 ) { r( /&(#x[0-9a-f]{2,4}|#[0-9]{3,4}|[0-9a-z]{2,8});/gi, function ( s ) { var t = document.createElement( 'textarea' ); t.innerHTML = s; var c = t.value; if ( c.length === 1 && c.charCodeAt( 0 ) > 127 || s === ' ' ) { return c; } return s; }); } r( /((?:^|[\s"])\d+(?:[\.,]\d+)?\s*)[xх](\s*\d+(?:[\.,]\d+)?)\s*([мm]{1,2}(?:[\s"\.,;?!]|$))/g, '$1×$2' + u + '$3' ); r( /([\wа-яА-ЯёЁ])'(?=[\wа-яА-ЯёЁ])/g, '$1’' ); // ' r( /«|»|“|”|„/g, '"' ); // temp // Hyphens and en dashes to pretty dashes r( /–/g, '-' ); // – -> hyphen r( /(\s)-{1,3} /g, '$1— ' ); // hyphen -> — r( /(\d)--(\d)/g, '$1—$2' ); // -> — r( /(\s)-(\d)/g, '$1−$2' ); // hyphen -> minus // Insert/delete spaces r( /^([#*:]+)[\u00A0 \t\f\v]*(?!\{\|)([^\u00A0 \t\f\v*#:;])/gm, '$1 $2' ); // space after #*: unless before table r( /(\S)[\u00A0 \t](-{1,3}|—)[\u00A0 \t](\S)/g, '$1' + u + '— $3' ); r( /(^|[^\wА-яЁё\.])([А-ЯЁ][а-яё]+) ([А-ЯЁ]\.) ?([А-ЯЁ]\.)(?! ?[А-ЯЁ][а-яё\.])/g,'$1$2' + u + '$3' + u + '$4' ); // инициалы "Петров А. В." r( /(^|[^\wА-яЁё\.])([А-ЯЁ]\.) ?([А-ЯЁ]\.) ?(?=[А-ЯЁ][а-яё]+)/g, '$1$2' + u + '$3' + u); // инициалы "А. В. Петров" r( /([а-яё]"?\)?[\.\?!:])((?:\x01\d+\x02\|)?(?:[A-QS-ZА-ЯЁ]|R(?!u\b)))/g, '$1 $2' ); // "word. Word"; don't change in cases like "Газета.Ru" r( /([)"a-zа-яё\]²³])\s*([,:])([\[(a-zа-яё])/g, '$1$2 $3' ); // "word, word", "word: word"; except ":" r( /([)a-zа-яё\]²³])\s*([,:])"/g, '$1$2 "' ); r( /([)"a-zа-яё\]²³])[ \u00A0\t]([,;])\s([\[("a-zа-яё])/g, '$1$2 $3' ); r( /([^%\/\wА-Яа-яЁё"]\d+?(?:[\.,]\d+?)?) ?([%‰])(?!-[А-Яа-яЁё])/g, '$1' + u + '$2' ); //5 % r( /(\d) ([%‰])(?=-[А-Яа-яЁё])/g, '$1$2' ); //5%-й r( /([№§])(\s*)(\d)/g, '$1' + u + '$3' ); // inside () r( /\( +/g, '(' ); r( / +\)/g, ')' ); // Plugins for ( i in window.wfPlugins ) { if ( window.wfPlugins.hasOwnProperty( i ) ) { window.wfPlugins[i]( txt, r ); } } // "" → «» for ( i = 1; i <= 2; i++ ) { r( /([\s\x02!|#'"\/([{;+\-])"([^"]*)([^\s"([{|])"([^a-zа-яё])/ig, '$1«$2$3»$4' ); // " } while ( /«[^»]*«/.test( txt ) ) { r( /«([^»]*)«([^»]*)»/g, '«$1„$2“' ); } txt = txt.substr( 1, txt.length - 2 ); // compensation for "txt = '\n' + txt + '\n';" } function processAllText() { txt = $input ? $input.textSelection( 'getContents' ) : text; processText(); if ( $input ) { r( /^[\n\r]+/, '' ); $input.textSelection( 'setContents', txt ); if ( caretPosition ) { $input.textSelection( 'setSelection', { start: caretPosition[0] > txt.length ? txt.length : caretPosition[0] } ); } } else { text = txt; } if ( window.auto_comment && window.insertSummary && !document.editform.wpSection.value ) { window.insertSummary( strings.summary ); } } // MAIN CODE // Check what's in the first parameter var text; var isInput; var $input; if ( typeof inputOrText === 'string' ) { text = inputOrText; } else { isInput = ( inputOrText && ( ( inputOrText.nodeType && inputOrText.value !== undefined ) || // node ( inputOrText.prop && inputOrText.prop( 'nodeType' ) ) // jQuery object ) ); $input = $( isInput ? inputOrText : '#wpTextbox1' ); } var txt = ''; var hidden = []; var winScroll = document.documentElement.scrollTop; var caretPosition; if ( $input ) { $input.focus(); caretPosition = $input.textSelection( 'getCaretPosition', { startAndEnd: true } ); if ( caretPosition ) { var $CodeMirrorVscrollbar = $( '.CodeMirror-vscrollbar' ); var textScroll = ( $CodeMirrorVscrollbar.length ? $CodeMirrorVscrollbar : $input ) .scrollTop(); if ( caretPosition[0] === caretPosition[1] ) { processAllText(); } else { txt = $input.textSelection( 'getSelection' ); processText(); // replaceSelection doesn't work with MediaWiki 1.30 in case this gadget is loaded // from other wiki $input.textSelection( 'encapsulateSelection', { replace: true, peri: txt } ); // In CodeMirror, the selection isn't preserved, so we do it explicitly $input.textSelection( 'setSelection', { start: caretPosition[0], end: caretPosition[0] + txt.length } ); } ( $CodeMirrorVscrollbar.length ? $CodeMirrorVscrollbar : $input ) .scrollTop( textScroll ); // If something went wrong } else if ( confirm( strings.fullText ) ) { processAllText(); } } else { processAllText(); return text; } // scroll back, for 2017 wikitext editor, IE, Opera document.documentElement.scrollTop = winScroll; }; function registerWikificatorTool() { registerTool( { name: 'wikificator', position: 100, title: strings.name, label: strings.tooltip, callback: Wikify, classic: { icon: '//upload.wikimedia.org/wikipedia/commons/0/06/Wikify-toolbutton.png', }, visual: { icon: '//upload.wikimedia.org/wikipedia/commons/thumb/4/41/Wikificator_VE_icon.svg/20px-Wikificator_VE_icon.svg.png', modes: [ 'source' ], addRightAway: true, }, } ); } if ( mw.config.get( 'wgServerName' ) === 'ru.wikipedia.org' ) { registerWikificatorTool(); } else { $.when( mw.loader.using( [ 'mediawiki.util', 'user.options' ] ), $.getScript( 'https://ru.wikipedia.org/w/index.php?title=MediaWiki:Gadget-registerTool.js&action=raw&ctype=text/javascript' ) ).done( registerWikificatorTool ); } $( '#editform' ).on( 'keydown', function ( e ) { // Ctrl+Alt+W on Windows, Ctrl+Shift+W on Mac if ( e.ctrlKey && !e.metaKey && (clientProfile.platform === 'mac' ? e.shiftKey && !e.altKey : !e.shiftKey && e.altKey) && e.keyCode === 87 ) { Wikify(); } } ); }() ); // </nowiki>