User:Mr. Stradivarius/chessboardfix.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Mr. Stradivarius/chessboardfix. |
// <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>