User:Moondyne/common.js: Difference between revisions

From Wikisource
Jump to navigation Jump to search
Content deleted Content added
OK
add buttons for enhanced toolbar
Line 94: Line 94:




/**
// Running header toolbar button
* WikiEditor specific
*
* Custom buttons ONLY when Prefs set to - Enable enhanced editing toolbar - is selected,
* Enable wizards for... - must not be selected. Show [old]edit toolbar - should be off.
* author - User:Helder.wiki
* based upon - pt.wikibooks.org/wiki/Utilizador:Helder.wiki/Tools/ExtraEditButtons.js
*/
var customizeToolbar = function() {
$( '#wpTextbox1' ).wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'xlink' } )
.wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'ilink' } )
.wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'file' } )
.wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'reference' } )
.wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'signature' } )
.wikiEditor( 'removeFromToolbar', { section: 'characters' } )
.wikiEditor( 'removeFromToolbar', { section: 'advanced' } )
.wikiEditor( 'removeFromToolbar', { section: 'help' } )
.wikiEditor( 'addToToolbar', {
section: 'main',
group: 'format',
tools: {
underline: {
labelMsg: 'wikieditor-toolbar-tool-underline',
label: 'Underline',
id: 'underline',
filters: [ 'body:not(.ns-2, .ns-8, .ns-828)' ],
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/8/8b/Button_underline_he.png',
action: {
type: 'encapsulate',
options: {
pre: "<u>",
periMsg: 'wikieditor-toolbar-tool-underline-example',
post: "</u>"
}
}
},
strikeout: {
labelMsg: 'wikieditor-toolbar-tool-strikeout',
label: 'Strike out',
id: 'strikeout',
filters: [ 'body:not(.ns-2, .ns-8, .ns-828)' ],
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/6/6d/Vector_strikeout.png',
action: {
type: 'encapsulate',
options: {
pre: "<s>",
periMsg: 'wikieditor-toolbar-tool-strikeout-example',
post: "</s>"
}
}
},
emdash: {
labelMsg: 'wikieditor-toolbar-tool-emdash',
label: 'em-dash',
id: 'emdash',
filters: [ 'body:not(.ns-2, .ns-8, .ns-828)' ],
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/8/8b/Button_m-dash.png',
action: {
type: 'replace',
options: {
'peri': '\u2014',
'selectPeri': false
}
}
},
2em: {
labelMsg: 'wikieditor-toolbar-tool-2em',
label: '2em',
id: '2em',
filters: [ 'body:not(.ns-2, .ns-8, .ns-828)' ],
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/b/bd/Button_number_2.png',
action: {
type: 'replace',
options: {
'peri': '{{bar|2}}',
'selectPeri': false
}
}
},
ae: {
labelMsg: 'wikieditor-toolbar-tool-ae',
label: 'ae',
id: 'ae',
filters: [ 'body:not(.ns-2, .ns-8, .ns-828)' ],
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/e/e7/Ligature_ae.svg',
action: {
type: 'replace',
options: {
'peri': 'æ',
'selectPeri': false
}
}
},
'break': {
labelMsg: 'wikieditor-toolbar-tool-break',
label: 'break',
id: 'break',
filters: [ 'body:not(.ns-2, .ns-8, .ns-828)' ],
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/f/f2/Button-br.png',
action: {
type: 'replace',
options: {
'peri': '<br />',
'selectPeri': false
}
}
},
hyphword: {
labelMsg: 'wikieditor-toolbar-tool-hyphword',
label: 'hyphword',
id: 'hyphword',
filters: [ 'body.ns-104' ],
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/5/5a/Button_hyphenated_word.svg',
action: {
type: 'callback',
execute: function(context) {
hyphenated_word();
}
}
}
}
} );
$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
section: 'main',
group: 'insert',
tools: {
'ilink': {
labelMsg: 'wikieditor-toolbar-tool-ilink',
label: 'Wiki link',
id: 'ilink',
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/4/48/Toolbar_Insert_link.png',
// offset: [2, -1582],
action: {
type: 'encapsulate',
options: {
pre: "[[",
periMsg: 'wikieditor-toolbar-tool-ilink-example',
post: "]]"
}
}
},
'xlink': {
labelMsg: 'wikieditor-toolbar-tool-xlink',
label: 'URL link',
id: 'xlink',
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/5/55/Toolbar_insert_xlink.png',
offset: [-70, 2],
action: {
type: 'encapsulate',
options: {
pre: "[",
periMsg: 'wikieditor-toolbar-tool-xlink-example',
post: "]"
}
}
},
'nowiki': {
labelMsg: 'wikieditor-toolbar-tool-nowiki',
label: 'NoWiki',
id: 'nowiki',
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/8/80/Toolbar_insert_nowiki.png',
offset: [-70, -70],
action: {
type: 'encapsulate',
options: {
pre: "<nowiki>",
periMsg: 'wikieditor-toolbar-tool-nowiki-example',
post: "</nowiki>"
}
}
},
redirect: {
labelMsg: 'wikieditor-toolbar-tool-redirect',
label: 'ReDirect',
id: 'redirect',
filters: [ 'body:not(.ns-828, .ns-829)' ],
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/7/76/Insert_redirect.png',
offset: [-70, -142],
action: {
type: 'encapsulate',
options: {
pre: mw.config.get( 'wgWikiEditorMagicWords' ).redirect + ' [[',
periMsg: 'wikieditor-toolbar-tool-redirect-example',
post: "]]",
ownline: true
}
}
},
signature: {
labelMsg: 'wikieditor-toolbar-tool-signature',
label: 'Your Signature',
id: 'signature',
filters: [ 'body.ns-talk, body.ns-4' ],
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/7/79/Insert-signature.png',
offset: [2, -1872],
action: {
type: 'encapsulate',
options: {
pre: '— ~~~~'
}
}
}
}
} );
};
/* Check for edit mode and required modules are available. Then, customize */
if ( $.inArray( mw.config.get( 'wgAction' ), ['edit', 'submit'] ) !== -1 ) {
mw.loader.using( 'user.options', function () {
if ( mw.user.options.get( 'usebetatoolbar' ) ) {
mw.loader.using( 'ext.wikiEditor.toolbar', function () {
$(document).ready( customizeToolbar );
} );
}
} );
}


// From Inductiveload page

/*
* Does a lookup on the previous page to see if there is a hyphenated
* word and tries to complete it
*
* To use this script, add the following to your .js:
* importScript('User:Inductiveload/Hyphenated word.js');
*
*/

function check_hws(data)
{
try {
var editbox = document.getElementsByName('wpTextbox1')[0];
if (editbox && !data.query.pages["-1"]) {
for (var ids in data.query.pages) {
var content = data.query.pages[ids].revisions[0]['*'];
var match = content.match( /{{ *([Hh]ws|[Hh]yphenated word start)\|([^\|}]*)\|?([^\|}]*)?}}/ );

if (match !== undefined && match.length > 3) {
var hws = match[2];
var word = match[3];
var hwe = word.slice(hws.length);
var hwestring = '{{hwe|'+hwe+'|'+word+'}} ';

editbox.value = hwestring + editbox.value;
}

break;
}
}
}
catch (err) { }
}

function hyphenated_word()
{
var r = new RegExp("(\\d+)$");
var page = Number(r.exec(mw.config.get('wgPageName'))[1]) - 1; //previous page
var pagename = mw.config.get('wgPageName').replace(/\d+$/g, page);

var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath')
+ "/api.php?action=query&prop=revisions&callback=check_hws&rvprop=content"
+ "&format=json&titles=" + encodeURIComponent(pagename);

$('body').append('<script type="text/javascript" src="' + url + '"/>');
}

/*// Running header toolbar button
importScript('User:Inductiveload/Running header.js');
importScript('User:Inductiveload/Running header.js');


Line 145: Line 423:
} );
} );
}
}
*/

Revision as of 04:42, 28 August 2014

/* sidebar code for use with User:Pathoschild's Regex code */

function rmflinks() {
        regexTool('Cleanup','cleanup()');
        regexTool('Smallcaps','smallcaps()');
}

// Cleanup script
function cleanup() {
	var editbox = document.getElementsByName('wpTextbox1')[0];

        // remove trailing whitespace at the end of each line
	editbox.value = editbox.value.replace(/ \n/g, '\n');

        // remove trailing whitespace at the end of input
	editbox.value = editbox.value.replace(/\s+$/g, '');

        // remove spacing around mdash, but only if it has spaces on both sides
        // (we don't want to remove the trailing space from "...as follows:— "
	editbox.value = editbox.value.replace(/ +— +/g, '—');

        // join words that are hyphenated across a line break
	editbox.value = editbox.value.replace(/-\n/g, '');

        // stuff to do only if the page doesn't contain a <poem> tag:
        if (-1 == editbox.value.indexOf("<poem>")) {
                // remove single line breaks; preserve multiple.
                editbox.value = editbox.value.replace(/([^>\n])\n([^<\n])/g, '$1 $2');

                // collapse sequences of spaces into a single space
                editbox.value = editbox.value.replace(/  +/g, ' ');
        }

        // remove unwanted spaces around punctuation marks
        editbox.value = editbox.value.replace(/ ([;:\?!,])/g, '$1');
	editbox.value = editbox.value.replace(/ ;/g, ';');

        // Replace smart double quotes with straight double quotes
        editbox.value = editbox.value.replace(/[\u201C\u201D\u201E\u201F\u2033\u2036]/g, '"');   

        // Replace smart single quotes with straight single quote (apostrophe)
        editbox.value = editbox.value.replace(/[\u2018\u2019\u201A\u201B\u2032\u2035]/g, "'");   

        // OCR SCANNOS
        editbox.value = editbox.value.replace(/tlie/g, 'the');             // "the", "them", "their", etcetera
        editbox.value = editbox.value.replace(/([a-z])U/g, '$1ll');        // "U" -> "ll" when preceded by a lowercase letter.
	editbox.value = editbox.value.replace(/--/g, '—');                 // convert double-hyphen to mdash
	editbox.value = editbox.value.replace(/-—/g, '—');                 // convert ndash mdash to mdash
  	editbox.value = editbox.value.replace(/—-/g, '—');                 // convert mdash ndash to mdash
	editbox.value = editbox.value.replace(/[il]([0-9])/g, '1$1');      // convert i9 to 19, etc.
	editbox.value = editbox.value.replace(/labcur/g, 'labour');
 	editbox.value = editbox.value.replace(/I-I/g, 'H');
	editbox.value = editbox.value.replace(/Fromantle/g, 'Fremantle');
	editbox.value = editbox.value.replace(/Eremantle/g, 'Fremantle');
	editbox.value = editbox.value.replace(/Oolonel/g, 'Colonel');
	editbox.value = editbox.value.replace(/ aud /g, ' and ');
	editbox.value = editbox.value.replace(/Govermnent/g, 'Government');
	editbox.value = editbox.value.replace(/dicult/g, 'difficult');
	editbox.value = editbox.value.replace(/ ocer/g, ' officer');
	editbox.value = editbox.value.replace(/ ocial/g, ' official');
	editbox.value = editbox.value.replace(/ \uFFFD(?=[1-9])/g, ' \u00A3');   // � --> £
	editbox.value = editbox.value.replace(/ iu/g, ' in');
	editbox.value = editbox.value.replace(/ tile/g, ' the');
	editbox.value = editbox.value.replace(/Oouucil/g, 'Council');
	editbox.value = editbox.value.replace(/ laud/g, ' land');
	editbox.value = editbox.value.replace(/ Loudon/g, ' London');
	editbox.value = editbox.value.replace(/ ou /g, ' on ');
	editbox.value = editbox.value.replace(/\u005E/g, ',');                   // ^ --> ,
	editbox.value = editbox.value.replace(/Digitized by\s/g, '');
	editbox.value = editbox.value.replace(/Google/g, '');	

}

function smallcaps() {
	var editbox = document.getElementsByName('wpTextbox1')[0];
        editbox.focus();
        var selStart = editbox.selectionStart;
        var selEnd = editbox.selectionEnd;
        var pre = editbox.value.substring(selStart, selEnd);
 
        // Applying small-caps to all-caps text is pointless...
        // ... unless the all-caps is OCR of text that is actually small-caps.
        // Check if text is all-caps, and if it is, convert it to title case before applying small-caps.
        if (pre == pre.toUpperCase()) {
            pre = titlecase(pre)
        }
 
        var post = '{{small-caps|'+pre+'}}';
        editbox.value = editbox.value.substring(0, selStart)
                      + post
                      + editbox.value.substring(selEnd);
        editbox.setSelectionRange(selStart+post.length, selStart+post.length);
}


/**
 * WikiEditor specific
 *
 * Custom buttons ONLY when Prefs set to - Enable enhanced editing toolbar - is selected,
 * Enable wizards for... - must not be selected. Show [old]edit toolbar - should be off.
 * author - User:Helder.wiki
 * based upon - pt.wikibooks.org/wiki/Utilizador:Helder.wiki/Tools/ExtraEditButtons.js
 */
var customizeToolbar = function() {
	$( '#wpTextbox1' ).wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'xlink' } )
	.wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'ilink' } )
	.wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'file' } )
	.wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'reference' } )
	.wikiEditor( 'removeFromToolbar', { section: 'main', group: 'insert', tool: 'signature' } )
	.wikiEditor( 'removeFromToolbar', { section: 'characters' } )
	.wikiEditor( 'removeFromToolbar', { section: 'advanced' } )
	.wikiEditor( 'removeFromToolbar', { section: 'help' } )
	.wikiEditor( 'addToToolbar', {
		section: 'main',
		group: 'format',
		tools: {
			underline: {
				labelMsg: 'wikieditor-toolbar-tool-underline',
				label: 'Underline',
				id: 'underline',
				filters: [ 'body:not(.ns-2, .ns-8, .ns-828)' ],
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/8/8b/Button_underline_he.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: "<u>",
						periMsg: 'wikieditor-toolbar-tool-underline-example',
						post: "</u>"
					}
				}
			},
			strikeout: {
				labelMsg: 'wikieditor-toolbar-tool-strikeout',
				label: 'Strike out',
				id: 'strikeout',
				filters: [ 'body:not(.ns-2, .ns-8, .ns-828)' ],
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/6/6d/Vector_strikeout.png',
				action: {
					type: 'encapsulate',
					options: {
						pre: "<s>",
						periMsg: 'wikieditor-toolbar-tool-strikeout-example',
						post: "</s>"
					}
				}
			},
			emdash: {
				labelMsg: 'wikieditor-toolbar-tool-emdash',
				label: 'em-dash',
				id: 'emdash',
				filters: [ 'body:not(.ns-2, .ns-8, .ns-828)' ],
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/8/8b/Button_m-dash.png',
				action: {
					type: 'replace',
					options: {
						'peri': '\u2014',
						'selectPeri': false
					}
				}
			},
			2em: {
				labelMsg: 'wikieditor-toolbar-tool-2em',
				label: '2em',
				id: '2em',
				filters: [ 'body:not(.ns-2, .ns-8, .ns-828)' ],
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/b/bd/Button_number_2.png',
				action: {
					type: 'replace',
					options: {
						'peri': '{{bar|2}}',
						'selectPeri': false
					}
				}
			},
			ae: {
				labelMsg: 'wikieditor-toolbar-tool-ae',
				label: 'ae',
				id: 'ae',
				filters: [ 'body:not(.ns-2, .ns-8, .ns-828)' ],
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/e/e7/Ligature_ae.svg',
				action: {
					type: 'replace',
					options: {
						'peri': 'æ',
						'selectPeri': false
					}
				}
			},
			'break': {
				labelMsg: 'wikieditor-toolbar-tool-break',
				label: 'break',
				id: 'break',
				filters: [ 'body:not(.ns-2, .ns-8, .ns-828)' ],
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/f/f2/Button-br.png',
				action: {
					type: 'replace',
					options: {
						'peri': '<br />',
						'selectPeri': false
					}
				}
			},
			hyphword: {
				labelMsg: 'wikieditor-toolbar-tool-hyphword',
				label: 'hyphword',
				id: 'hyphword',
				filters: [ 'body.ns-104' ],
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/5/5a/Button_hyphenated_word.svg',
				action: {
					type: 'callback',
					execute: function(context) {
						hyphenated_word();
					}
				}
			}
		}
	} );
	$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
		section: 'main',
		group: 'insert',
		tools: {
			'ilink': {
				labelMsg: 'wikieditor-toolbar-tool-ilink',
				label: 'Wiki link',
				id: 'ilink',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/4/48/Toolbar_Insert_link.png',
//				offset: [2, -1582],
				action: {
					type: 'encapsulate',
					options: {
						pre: "[[",
						periMsg: 'wikieditor-toolbar-tool-ilink-example',
						post: "]]"
					}
				}
			},
			'xlink': {
				labelMsg: 'wikieditor-toolbar-tool-xlink',
				label: 'URL link',
				id: 'xlink',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/5/55/Toolbar_insert_xlink.png',
				offset: [-70, 2],
				action: {
					type: 'encapsulate',
					options: {
						pre: "[",
						periMsg: 'wikieditor-toolbar-tool-xlink-example',
						post: "]"
					}
				}
			},
			'nowiki': {
				labelMsg: 'wikieditor-toolbar-tool-nowiki',
				label: 'NoWiki',
				id: 'nowiki',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/8/80/Toolbar_insert_nowiki.png',
				offset: [-70, -70],
				action: {
					type: 'encapsulate',
					options: {
						pre: "<nowiki>",
						periMsg: 'wikieditor-toolbar-tool-nowiki-example',
						post: "</nowiki>"
					}
				}
			},
			redirect: {
				labelMsg: 'wikieditor-toolbar-tool-redirect',
				label: 'ReDirect',
				id: 'redirect',
				filters: [ 'body:not(.ns-828, .ns-829)' ],
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/7/76/Insert_redirect.png',
				offset: [-70, -142],
				action: {
					type: 'encapsulate',
					options: {
						pre: mw.config.get( 'wgWikiEditorMagicWords' ).redirect + ' [[',
						periMsg: 'wikieditor-toolbar-tool-redirect-example',
						post: "]]",
						ownline: true
					}
				}
			},
			signature: {
				labelMsg: 'wikieditor-toolbar-tool-signature',
				label: 'Your Signature',
				id: 'signature',
				filters: [ 'body.ns-talk, body.ns-4' ],
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/7/79/Insert-signature.png',
				offset: [2, -1872],
				action: {
					type: 'encapsulate',
					options: {
						pre: '— ~~~~'
					}
				}
			}
		}
	} );
};
/* Check for edit mode and required modules are available. Then, customize */
if ( $.inArray( mw.config.get( 'wgAction' ), ['edit', 'submit'] ) !== -1 ) {
	mw.loader.using( 'user.options', function () {
		if ( mw.user.options.get( 'usebetatoolbar' ) ) {
			mw.loader.using( 'ext.wikiEditor.toolbar', function () {
				$(document).ready( customizeToolbar );
			} );
		}
	} );
}


// From Inductiveload page

/*
 * Does a lookup on the previous page to see if there is a hyphenated
 * word and tries to complete it
 *
 * To use this script, add the following to your .js:
 *     importScript('User:Inductiveload/Hyphenated word.js');
 *
 */

function check_hws(data)
{
    try {
        var editbox = document.getElementsByName('wpTextbox1')[0];
        if (editbox && !data.query.pages["-1"]) {
            for (var ids in data.query.pages) {
                var content = data.query.pages[ids].revisions[0]['*'];
                var match = content.match( /{{ *([Hh]ws|[Hh]yphenated word start)\|([^\|}]*)\|?([^\|}]*)?}}/ );

                if (match !== undefined && match.length > 3) {
                    var hws = match[2];
                    var word = match[3];
                    var hwe = word.slice(hws.length);
                    var hwestring = '{{hwe|'+hwe+'|'+word+'}} ';

                    editbox.value = hwestring + editbox.value;
                }

                break;
            }
        }
    }
    catch (err) { }
}

function hyphenated_word()
{
    var r = new RegExp("(\\d+)$");
    var page = Number(r.exec(mw.config.get('wgPageName'))[1]) - 1; //previous page
    var pagename = mw.config.get('wgPageName').replace(/\d+$/g, page);

    var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath')
        + "/api.php?action=query&prop=revisions&callback=check_hws&rvprop=content"
        + "&format=json&titles=" + encodeURIComponent(pagename);

    $('body').append('<script type="text/javascript" src="' + url + '"/>');
}

/*// Running header toolbar button
importScript('User:Inductiveload/Running header.js');


// Extra toolbar buttons
self.proofreadpage_disable_wheelzoom = true;
var addExtraButtons = function(){
 
mw.toolbar.addButtons(
{
     "imageFile": "http://upload.wikimedia.org/wikipedia/commons/8/8b/Button_m-dash.png",
     "speedTip": "M-dash",
     "tagOpen": '—',
     "tagClose": '',
     "sampleText": ""
},
{
     "imageFile": "http://upload.wikimedia.org/wikipedia/commons/b/bd/Button_number_2.png",
     "speedTip": "2em",
     "tagOpen": '{{bar|2}}',
     "tagClose": '',
     "sampleText": ""
},
{
     "imageFile": "http://upload.wikimedia.org/wikipedia/commons/e/e7/Ligature_ae.svg",
     "speedTip": "ae",
     "tagOpen": 'æ',
     "tagClose": '',
     "sampleText": ""
},
{
     "imageFile": "http://upload.wikimedia.org/wikipedia/commons/f/f2/Button-br.png",
     "speedTip": "Break",
     "tagOpen": '<br />',
     "tagClose": '',
     "sampleText": ""
}
);
 
};  //end of var addExtraButtons


if( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
        mw.loader.using( 'user.options', function () {
                if ( ! mw.user.options.get( 'usebetatoolbar' ) ) {
                        mw.loader.using( 'mediawiki.action.edit', function(){
                                $( addExtraButtons );
                        } );
                }
        } );
}
*/