Jump to content

User:Mr. Stradivarius/chessboardfix.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
/* This script fixes duplicate parameters in chess template invocations. To
* install it, put the following code in [[Special:MyPage/skin.js]]:
 
importScript('User:Mr. Stradivarius/chessboardfix.js'); // Linkback: [[User:Mr. Stradivarius/chessboardfix.js]]

* To use the script, open the page you want to fix in edit view, highlight the
* chess template invocation that you want to fix, and click the
* "Fix chessboards" link in the toolbar (below "what links here").
* If the chess template uses the previous standard invocation style, then it
* will be converted to the new style. In other words, template invocations like
* this:

{{Chess diagram
| tright
| 
|= 

 8 |rd|nd|bd|qd|kd|bd|nd|rd|=
 7 |pd|pd|pd|pd|  |pd|pd|pd|=
 6 |  |  |  |  |  |  |  |  |=
 5 |  |  |  |  |pd|  |  |  |=
 4 |  |  |  |  |pl|pl|  |  |=
 3 |  |  |  |  |  |  |  |  |=
 2 |pl|pl|pl|pl|  |  |pl|pl|=
 1 |rl|nl|bl|ql|kl|bl|nl|rl|=
    a  b  c  d  e  f  g  h  

| The King's Gambit
}}

* Will be transformed into template invocations like this:

{{Chess diagram
| tright
| 

|rd|nd|bd|qd|kd|bd|nd|rd
|pd|pd|pd|pd|  |pd|pd|pd
|  |  |  |  |  |  |  |  
|  |  |  |  |pd|  |  |  
|  |  |  |  |pl|pl|  |  
|  |  |  |  |  |  |  |  
|pl|pl|pl|pl|  |  |pl|pl
|rl|nl|bl|ql|kl|bl|nl|rl

| The King's Gambit
}}

* Please check that the script produces the correct output; there is a chance
* that it may mangle the page code, particularly if it is used on unusual types
* of chessboard template or on normal wikitext.
*/

var myContent = document.getElementsByName( 'wpTextbox1' )[0];

function replaceSelection( replace ) {
	// Replace currently selected text with the replace variable. The replace
	// variable can be a string or a callback function. The callback function
	// takes the selection text as its first and only argument, and must return
	// the string to replace the selection with.
	var len = myContent.value.length;
	var start = myContent.selectionStart;
	var end = myContent.selectionEnd;
	var sel = myContent.value.substring( start, end );

	if ( typeof( replace ) == 'function' ) {
		replace = replace( sel );
	}

	myContent.value = myContent.value.substring( 0, start )
		+ replace
		+ myContent.value.substring( end, len );
}

function chessboardFix() {
	replaceSelection( function ( sel ) {
		sel = sel.replace( /\n *\d* *((?:\| *.. *)+)\| *= */g, '\n$1' );   // " 8 |rd|  |bd|  |  |rd|  |  |=" --> "|rd|  |bd|  |  |rd|  |  "
		sel = sel.replace( /\n *\|= *\n+/g, '\n\n' );                      // "|=" --> ""
		sel = sel.replace( /\n *(?:[a-zA-Z][a-zA-Z]?\b *)+\n+/g, '\n\n' ); // " a b c d e f g h" --> ""
		sel = sel.replace( /\n *[bB]oard *\w+ *\n+/g, '\n' );              // "   Board A  " --> ""
		sel = sel.replace( / *\|= *\n/g, '\n' );                           // "{{Chess diagram |=" --> "{{Chess diagram"
		return sel;
	} );
}

function addChessboardPortletLink() {
	var portletLink = mw.util.addPortletLink(
		'p-tb',
		'#',
		'Fix chessboards',
		't-chessboardfix'
	);
	$( portletLink ).click( function ( e ) {
		e.preventDefault();
		chessboardFix();
	});
}

if ( mw.config.get( 'wgNamespaceNumber' ) != -1 && myContent ) {
	jQuery( document ).ready( addChessboardPortletLink );
}

// </nowiki>