User:Phe/common.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

mw.loader.load('//fr.wikisource.org/w/index.php?title=User:Phe/hocr.js&action=raw&ctype=text/javascript');

/*importScript('User:Inductiveload/Visibility.js');*/

importScript('User:Phe/Running header.js');
/*importScript('User:Phe/Author fill.js');*/

var ws_export_server = 'http://wsexport.zaniah.virgus/index.php/en/book/epub/';
mw.util.addPortletLink('p-coll-print_export', ws_export_server + encodeURIComponent(mw.config.get('wgPageName').replace(/\//g, '%2F')) + ".epub", 'Epub', 'coll-epub', 'Accès à l’export d’epub');


/* == Lupin's popup == */
/*
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Lupin/popups.js&action=raw&ctype=text/javascript');
*/
simplePopups=true;
popupDelay=0.7;
popupStructure='original';
popupOnEditSelection=false;
popupAdminLinks=true;

/*proofreadpage_default_layout='horizontal';*/


// install [[User:Cacycle/wikEdDiff]] enhanced diff view using ajax
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Cacycle/wikEdDiff.js&action=raw&ctype=text/javascript');

/**
 * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
 * @see https://meta.wikimedia.org/wiki/TemplateScript
 * @update-token [[File:pathoschild/templatescript.js]]
 */
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
	pathoschild.TemplateScript.add([
		{
			name: 'clean up',
			script: cleanup
		},
		{
			name: 'Works about',
			scriptUrl: 'User:Phe/Works about.js',
			script: function(editor) {
				works_about();
			},
			forNamespaces: 'author'
		},
		{
			name: 'Adding interwiki',
			scriptUrl: 'User:Phe/Interwiki.js',
			script: function(editor) {
				add_interwiki();
			},
			forNamespaces: 'author'
		},
		{
			name: 'Sort authors',
			scriptUrl: 'User:Phe/Sort author.js',
			script: function(editor) {
				sort_author();
			},
			enabled: mw.config.get('wgPageName').search('Wikisource:Authors-') === 0
		},
		//{
		//	name: 'Move refs',
		//	scriptUrl: 'User:Phe/Move refs.js',
		//	script: function(editor) {
		//		get_references();
		//	}
		//}
	]);
});

function cleanup(editor) {
	editor
		// remove trailing whitespace at the end of each line
		.replace(/ \n/g, '\n')
		
		// remove trailing whitespace at the end of input
		.replace(/\s+$/g, '')
		
		// convert double-hyphen to mdash
		.replace(/--/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:— "
		.replace(/ +— +/g, '—')
		
		// join words that are hyphenated across a line break
		.replace(/-\n/g, '');

	// stuff to do only if the page doesn't contain a <poem> tag:
	if (editor.get().indexOf("<poem>") === -1) {
		editor
			// remove single line breaks; preserve multiple.
			//.replace(/([^>\n])\n([^<\n])/g, '$1 $2')
			
			// collapse sequences of spaces into a single space
			.replace(/  +/g, ' ');
	}
	
	editor
		// remove unwanted spaces around punctuation marks
		.replace(/ ([;:\?!,])/g, '$1')

		//OCR fixes
		// convert i9 to 19, etc.
		.replace(/[il]([0-9])/g, '1$1')
		
		// "the", "them", "their", etcetera
		.replace(/tlie/g, 'the');
}

/*
 * Author: w:fr:Phe
 *
 * Extract the text layer of a djvu file.
 */

var extract_text_layer = {
    callback : function (data) {
        if (data['error'] == 0) {
            $('#wpRobot-extract-text > a').text('ok')
            $('#wpRobot-extract-text > a').attr('href', '/w/index.php?title=User:' + encodeURIComponent(mw.config.get('wgUserName')) + '/Text');
        } else {
            $('#wpRobot-extract-text > a').text(data['text']);
        }
    },

    do_extract : function () {
        var url = 'http://toolserver.org/~phe/extract_text_layer.php?cmd=extract&title='
          + mw.config.get('wgTitle') + '&lang=' + mw.config.get('wgContentLanguage') + '&user=' + mw.config.get('wgUserName');

        $.getJSON(url, function(data) { extract_text_layer.callback(data); } );
    },

    add_extract_button : function () {
        var id_name = self.mw.config.get('skin') == "vector" ? '#p-namespaces' : '#p-cactions';
        var $a = $(id_name + ' ul');
        if (!$a.length)
            return false;
        $a[0].innerHTML = $a[0].innerHTML  + '<li id="ca-extract">' + '<span id="wpRobot-extract-text"><a href="javascript:extract_text_layer.do_extract();"><span>extract text</span></a></span></li>';
    },

    setup : function () {
        extract_text_layer.add_extract_button();
    },
};

/* FIXME: don't use an hard-coded namespace number */
if (mw.config.get("wgNamespaceNumber") == 106 /*&& mw.config.get("wgAction") == "edit"*/)
    $(extract_text_layer.setup);