Участник:Менделеевец99/WikifyTemplates.js

Материал из Википедии — свободной энциклопедии
///////////////////////////////////////////////////////////////////////////////////////////////////
//          SCRIPT «WIKIFY TEMPLATES»          //          СКРИПТ «ВИКИФИКАЦИЯ ШАБЛОНОВ»         //
// ------------------------------------------- // ---------------------------------------------- //
//   Autor: [[ru:User:SmesharikiAreTheBest]]   //    Автор: [[Участник:SmesharikiAreTheBest]]    //
///////////////////////////////////////////////////////////////////////////////////////////////////

// Повтор символов
function copy (symbol, count) {
	var result = '';
	for (var i = 0; i < count; i ++) result += symbol;
	return result;
}

// Нажатие кнопки - [[MediaWiki:Wikificator.js]]
function wikifyTemplateButtonClick () {
	// Получение выделенного фрагмента
	textBox = document.getElementById ('wpTextbox1');
	var startPos = textBox.selectionStart;
	var endPos = textBox.selectionEnd;
	text = textBox.value.substring (startPos, endPos);

	// Собственно викификация
	text = text.replace (/[\r\n]([^\r\n]*)\|/g, '$1|');
	text = text.replace (/\|([^\r\n]*)[\r\n]/g, '|$1');
	for (var i = 0; i < 100; i ++) text = text.replace (/(\s\|)|(\|\s)/g, '|');
	for (i = 0; i < 100; i ++) text = text.replace (/(\s=)|(=\s)/g, '=');
	text = text.replace (/\|/g, '\r\n| ');
	text = text.replace (/=/g, ' = ');
	text = text.replace (/}}/g, '\r\n}}');

	var strs = text.split ("\r\n");
	var max = 0;
	for (i = 0; i < strs.length; i ++) max = (max >= strs [i].indexOf ('=')) ? max : strs [i].indexOf ('=');

	var text = '';
	for (i = 0; i < strs.length; i ++) text += strs [i].replace ('=', copy (' ', max - strs [i].indexOf ('='))+'=') + '\r\n';
	text = text.replace (/\r\n$/g, '');

	// Запись
	textBox.value = textBox.value.substring (0, startPos) + text + textBox.value.substring (endPos);
}

// Добавление кнопки - [[MediaWiki:Gadget-refToolbar.js]]
function addWikifyTemplateButton () {
	var button = document.createElement ('a');
	button.href = 'javascript:wikifyTemplateButtonClick()';
	button.title = 'Оформление шаблонов';
	button.id = 'wikifyTemplates';
	var textNode = document.createTextNode ('Оформление шаблонов');
	button.appendChild (textNode);

	var toolbar = document.getElementById('p-tb');
	if (toolbar) toolbar.appendChild (button);
}
 
// Выполняется после загрузки страницы
if (mw.config.get('wgAction') == 'edit' || mw.config.get('wgAction') == 'submit') {
//	addOnloadHook (addWikifyTemplateButton)
	

jQuery( function( $ ) {
  // ваш код
} );

$( addWikifyTemplateButton );

}