Aller au contenu

Utilisateur:Ostrea/Testscript.js

La bibliothèque libre.

Note : après avoir enregistré vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : Maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ou Ctrl-R (⌘-R sur un Mac) ;
  • Google Chrome : Appuyez sur Ctrl-Maj-R (⌘-Shift-R sur un Mac) ;
  • Internet Explorer : Maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ;
  • Opera : Allez dans Menu → Settings (Opera → Préférences sur un Mac) et ensuite à Confidentialité & sécurité → Effacer les données d'exploration → Images et fichiers en cache.
/*



This page defines a TemplateScript library. It's not meant to be referenced
directly. See [[Wikisource:TemplateScript]] for usage.



*/
/* global $, pathoschild */


/**
 * 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]]
 */
// <nowiki>
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
	/*********
	** Define library
	*********/
	pathoschild.TemplateScript.library.define({
		key: 'wikisource.proofreading',
		name: 'Proofreading tools',
		url: '//en.wikisource.org/wiki/Wikisource:TemplateScript#Proofreading',
		description: 'A set of scripts for <a href="/wiki/Help:Proofreading">proofreading works in the <tt>Page:</tt> namespace</a>. This includes tools for cleaning up OCR, generating page templates, and adding common text formatting.',
		categories: [
			{
				name: 'Page tools',
				scripts: [
					{ key: 'add-header', name: 'Add header', script: function(editor) { addPageHeader(editor); }, forNamespaces: 'page' },
					{ key: 'add-footer', name: 'Add footer', script: function(editor) { addPageFooter(editor); }, forNamespaces: 'page' },
					{ key: 'cleanup-ocr', name: 'Clean up OCR', script: function(editor) { pageCleanup(editor); }, forNamespaces: 'page' },
					{ key: 'make-ref', name: 'Make reference', script: function(editor) { makeReference(editor); }, forNamespaces: 'page' },
					{ key: 'smallcaps', name: 'Convert to small-caps', script: function(editor) { smallcaps(editor); }, forNamespaces: 'page' },
					{ key: 'uppercase', name: 'Convert to uppercase', script: function(editor) { upper(editor); }, forNamespaces: 'page' }
				]
			}
		]
	});

	/*********
	** Page context
	*********/
	var state = {
		initialised: false,  // whether the page context has been initialised
		page: {
			number: null,   // the djvu page number extracted from the URL
			proofed: null
		},
		specialFormats: [] // work-specific header template formats
	};

	/*********
	** Private methods
	*********/
	/**
	 * Initialise the data needed by the page tools.
	 */
	var _initialise = function() {
		// only initialise once
		if(state.initialised)
			return;
		state.initialised = true;

		// get page metadata
		var pn = /\.djvu\/([0-9]+)&action=edit/g.exec(location.href);
		var pq = document.getElementById('pagequality');
		state.page = {
			number: pn !== null ? parseInt(pn[1], 10) : null,
			proofed: pq && pq.getAttribute('class') && pq.getAttribute('class').match(/quality0|quality[2-4]/)
		};

		// get user-defined work formats
		// expected format:
		//   {
		//      title: /History of England /,
		//      evenHeader: '{{rh|...}}',
		//      oddHeader: '{{rh|...}}',
		//      footer: '',
		//      footerWithReferences: '{{smallrefs}}'
	 	//   }
		state.specialFormats = [];
		if(window.specialFormats)
			state.specialFormats = state.specialFormats.concat(window.specialFormats);
	};
	
	/**
	 * Convert the text to title case based on English rules.
	 * @param {string} text The text to convert.
	 */
	var _titlecase = function(text) {
		// split text into individual words and examine them one by one
		var words = text.toLowerCase().split(" ");
		$.each(function(i, word) {
			switch(word) {
				case "a":
				case "an":
				case "and":
				case "as":
				case "at":
				case "but":
				case "by":
				case "etcetera":
				case "etc.":
				case "for":
				case "from":
				case "in":
				case "nor":
				case "of":
				case "o'":
				case "on":
				case "or":
				case "the":
				case "to":
				case "with":
				case "versus":
				case "vs.":
				case "v.":
				case "yet":
					break; // don't capitalise articles, "to" as part of an infinitive, prepositions or short conjunctions
				default: // capitalise everything else
					words[i] = word.substring(0, 1).toUpperCase() + word.substring(1, words[i].length);
					break;
			}
		});

		// capitalise first word regardless
		words[0] = words[0].substring(0, 1).toUpperCase() + words[0].substring(1, words[0].length);

		// capitalise last word regardless
		var last = words.length-1;
		words[last] = words[last].substring(0, 1).toUpperCase() + words[last].substring(1, words[last].length);

		// reconstruct title
		return words.join(' ');
	};

	/*********
	** Script methods
	*********/
	/**
	 * Add a {{running header}} template to the page.
	 * @param {object} editor The script helpers for the page.
	 */
	var addPageHeader = function(editor) {
		_initialise();
		
		if(state.page.number === null)
			return;

		var isEven = (state.page.number % 2 === 0);
		var generic = true;
		var headertext = '';
		
		for (var f in state.specialFormats) {
			var format = state.specialFormats[f];
			if (mw.config.get('wgTitle').match(format.title)) {
				headertext = isEven ? format.evenHeader : format.oddHeader;
				generic = false;
				break;
			}
		}

		// no special header matched, use a generic running header
		if (generic) {
			if (isEven)
				headertext = '{{running header|left=|center=}}'; // assume verso, with page number at left
			else
				headertext = '{{running header|center=|right=}}';
		}
		
		$('#wpHeaderTextbox').val(function(i, val) {
			return $.trim(val + '\n' + headertext);
		});

		// if this is unproofed text, then delete the first line of the OCR text, which presumably is raw OCR of the header we've just inserted
		if (!state.page.proofed) {
			var text = editor.get();
			editor.set(text.slice(text.indexOf('\n') + 1));
		}
	};

	/**
	 * Clean up OCR errors in the text, and push <noinclude> content at the top
	 * & bottom of the page into the header & footer boxes respectively.
	 * @param {object} editor The script helpers for the page.
	 */
	var pageCleanup = function(editor) {
		_initialise();
		
		// push <noinclude> content at the top & bottom into the header & footer
		if (editor.get().match(/^<noinclude\>/)) {
			var text = editor.get();
			var e = text.indexOf("</noinclude>");
			$('#wpHeaderTextbox').val(function(i, val) {
				return $.trim(val + "\n" + text.substr(11, e-11).replace(/^\s+|\s+$/g, ''));
			});
			editor.set(text.substr(e+12));
		}
		if (editor.get().match(/<\/noinclude\>$/)) {
			var text = editor.get();
			var s = text.lastIndexOf("<noinclude>");
			$('#wpFooterTextbox').val(function(i, val) {
				return $.trim(text.substr(s+11, text.length-s-11-12).replace(/^\s+|\s+$/g, '') + "\n" + val);
			});
			editor.set(text.substr(0, s));
		}
		
		// clean up text
		editor
			// remove trailing spaces at the end of each line
			.replace(/ +\n/g, '\n')

			// remove trailing whitespace preceding a hard line break
			.replace(/ +<br *\/?>/g, '<br />')

			// remove trailing whitespace and numerals at the end of page text
			// (numerals are nearly always page numbers in the footer)
			.replace(/[\s\d]+$/g, '')

			// remove trailing spaces at the end of refs
			.replace(/ +<\/ref>/g, '</ref>')
	
			// remove trailing spaces at the end of template calls
			.replace(/ +}}/g, '}}')
	
			// convert double-hyphen to mdash (avoiding breaking HTML comment syntax)
			.replace(/([^\!])--([^>])/g, '$1—$2')
	
			// 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:— ",
			// bearing in mind that the space will already be gone if at end of line).
		//	.replace(/ +— +/g, '—')
	
			// join words that are hyphenated across a line break, and weird OCR hyphens (¬)
			// (but leave "|-" table syntax alone)
			.replace(/([^\|])[-¬]\n/g, '$1')
			
			//rajouts maison
			
			
			//remplace les balises </sup> par un formatage poème centré italique petit
			
		//	.replace(/<sup>/g, '<i><small>{{Bloc centré|<poem>')
		//	.replace(new RegExp("</sup>", "g"), '</poem>}}</small></i>')	
		
			//remplace les balises </sup> par un formatage poème italique 
			
			.replace(/<sup>/g, '<i><poem>')
			.replace(new RegExp("</sup>", "g"), '</poem></i>')
		
			
			//remplace les balises </sup> par un formatage poème centré  petit
			
	//		.replace(/<sup>/g, '<small>{{Bloc centré|<poem>')
	//		.replace(new RegExp("</sup>", "g"), '</poem>}}</small>')
			
			//remplace les balises </sub> par un formatage poème centré italique petit
			
			.replace(/<sub>/g, '\'\'<small>{{Centré|')
			.replace(new RegExp("</sub>", "g"), '}}</small>\'\'')
			
			//remplace ++ par un {{iv}}, +++ par un {{iv|5}}
			
			.replace(/\+\+\+/g, '{{iv|5}}')
			.replace(/\+\+/g, '{{iv}}')
			
			//supprime les chiffres
			//.replace(/[0-9]/g, '')
			
			//rajoute des accents circonflexes
			.replace(/naitr/g, 'naîtr')
			.replace(/ nait /g, ' naît ')
			.replace(/maitre/g, 'maître')
			.replace(/ connait/g, ' connaît')
			.replace(/apparait/g, 'apparaît')
			.replace(/plait/g, 'plaît')
			.replace(/entraine/g, 'entraîne')
			.replace(/entraina/g, 'entraîna')
			.replace(/ diner/g, ' dîner')
			.replace(/ renait/g, ' renaît')
			.replace(/paraitre/g, 'paraître')
			.replace(/chaine/g, 'chaîne')
			.replace(/ el /g, ' et ')
			.replace(/ A /g, ' À ')
			
			//replace lettres avec accents non françaises
			.replace(/ć/g, 'c')
			.replace(/ı/g, 'i')
			.replace(/ė/g, 'é')
			.replace(/ċ/g, 'c')
			
			
			
			
			//corrige erreurs d'écriture médiévale
			.replace(/\. le/g, '. Ie')
			.replace(/ ic /g, ' ie ')
			.replace(/cz/g, 'ez')
			.replace(/ ct /g, ' et ')
			.replace(/>>/g, '»')
			.replace(/<</g, '«')
			.replace(/ >/g, ' ')
			.replace(/< /g, ' ')
			.replace(/ AE/g, ' Æ')
			.replace(/ ct /g, ' et ')
			.replace(/ vnc /g, ' vne ')
			
			//Tabourot
			
			//correction f-s
			.replace(/ſ/g, 's')
			.replace(/ fe /g, ' se ')
			.replace(/ufe/g, 'use')
			.replace(/ft/g, 'st')
			.replace(/rfi/g, 'rsi')
			.replace(/fc/g, 'sc')
			.replace(/eft/g, 'est')
			.replace(/ fur /g, ' sur ')
			.replace(/fd/g, 'sd')
			.replace(/affeu/g, 'asseu')
			.replace(/dife/g, 'dise')
			.replace(/cef/g, 'ces')
			.replace(/ fieu/g, ' sieu')
			.replace(/ fes /g, ' ses ')
			.replace(/fur([^e])/g, 'sur$1')
			.replace(/fign/g, 'sign')
			.replace(/fm/g, 'sm')
			.replace(/afio/g, 'asio')
			.replace(/penf/g, 'pens')
			.replace(/fç/g, 'sç')
			.replace(/auffi/g, 'aussi')
			.replace(/paff/g, 'pass')
			.replace(/paf/g, 'pas')
			.replace(/fp/g, 'sp')
			.replace(/fy/g, 'sy')
			.replace(/fiec/g, 'siec')
			.replace(/difo/g, 'diso')
			.replace(/feul/g, 'seul')
			.replace(/pofit/g, 'posit')
			.replace(/perfon/g, 'person')
			.replace(/çoife/g, 'çoise')
			.replace(/ficcle/g, 'siecle')
			.replace(/ fon /g, ' son ')
			.replace(/onfieur/g, 'onsieur')
			.replace(/pluf/g, 'plus')
			.replace(/femb/g, 'semb')
			.replace(/fq/g, 'sq')
			.replace(/ fa /g, ' sa ')
			.replace(/ fi /g, ' si ')
			.replace(/chafq/g, 'chasq')
			.replace(/affez/g, 'assez')
			.replace(/foien/g, 'soien')
			.replace(/folid/g, 'solid')
			.replace(/iufque/g, 'jusque')
			.replace(/fep/g, 'sep')
			.replace(/fuiv/g, 'suiv')
			.replace(/chofe/g, 'chose')
			.replace(/fei/g, 'sei')
			.replace(/deff/g, 'dess')
			.replace(/uifa/g, 'uisa')
			.replace(/aifan/g, 'aisan')
			.replace(/ufsi/g, 'ussi')
			.replace(/effus/g, 'essus')
			.replace(/fecr/g, 'secr')
			.replace(/eufu/g, 'eusv')
			.replace(/ rofe/g, ' rose')
			.replace(/uife/g, 'uise')
			.replace(/leff/g, 'less')
			.replace(/foud/g, 'soud')
			.replace(/ fans /g, ' sans ')
			.replace(/ffade/g, 'ssade')
			.replace(/fonn/g, 'sonn')
			.replace(/fomm/g, 'somm')
			.replace(/treffe/g, 'tresse')
			.replace(/erfer/g, 'erser')
			.replace(/aiffa/g, 'aissa')
			.replace(/eflo/g, 'eslo')
			.replace(/uiffe/g, 'uisse')
			.replace(/laiff/g, 'laiss')
			
			
			
			
			
			
			
			
			
			
			
			//correction u-v
			.replace(/iue/g, 'ive')
			.replace(/aua/g, 'ava')
			.replace(/([ai])u([aeio])/g, '$1v$2')
			.replace(/vn/g, 'un')
			.replace(/uoy/g, 'voy')
			.replace(/qv/g, 'qu')
			.replace(/uua/g, 'uva')
			.replace(/ouu/g, 'ouv')
			.replace(/uui/g, 'uvi')
			.replace(/reuue/g, 'reuve')
			.replace(/duen/g, 'dven')
			.replace(/rouu/g, 'rouv')
			.replace(/riué/g, 'rivé')
			.replace(/auur/g, 'auvr')
			.replace(/deua/g, 'deva')
			.replace(/euite/g, 'evite')
			.replace(/eual/g, 'eval')
			.replace(/Vn/g, 'Un')
			.replace(/ adui/g, ' advi')
			.replace(/heue/g, 'heve')
			.replace(/mouu/g, 'mouv')
			
			
			
			
			
			
			//correction i-j
			.replace(/ i’/g, ' j’')
			.replace(/l’ay/g, 'j’ay')
			.replace(/toufi/g, 'tousj')
			.replace(/ ie/g, ' je')
			.replace(/ io/g, ' jo')
			.replace(/ ia/g, ' ja')
			.replace(/uniour|vniour/g, 'un jour')
			.replace(/Iuge/g, 'Juge')
			.replace(/liure/g, 'livre')
			.replace(/iure/g, 'jure')
			.replace(/iure/g, 'jure')
			.replace(/I’ay/g, 'J’ay')
			.replace(/fiour/g, 'sjour')
			.replace(/iam/g, 'jam')
			.replace(/I’/g, 'J’')
			.replace(/Ia/g, 'Ja')
			
			
			
			//autres
			.replace(/([Cc])omine/g, '$1omme')
			.replace(/ufli/g, 'ussi')
			.replace(/aflez/g, 'assez')
			.replace(/aviou/g, 'aujou')
			.replace(/ in’a/g, ' m’a')
			.replace(/ferue/g, 'serve')
			.replace(/Te fuis/g, 'Je suis')
			.replace(/ani /g, 'ant ')
			.replace(/conferu/g, 'conserv')
			.replace(/defia /g, 'desjà ')
			.replace(/defià /g, 'desjà ')
			.replace(/fleune/g, 'fleuve')
			.replace(/foune/g, 'souve')
			
			
			
			
			
			
			
			
			//titres histoire de l'imagerie populaire:
			.replace(/@/g, '{{il}}\n{{Centré|II}}\n{{il}}\n{{Centré|{{sc|la légende suivant les anciens récits.}}}}\n{{il}}')
			
			
			
			;

		// clean up pages if they don't have <poem>
		if (!editor.contains('<poem>')) {
			editor
				// lines that start with " should probably be new lines,
				// if the previous line ends in punctuation,
				// other than a comma or semicolon
				// and let's get rid of trailing space while we're at it
				.replace(/([^\n\w,;])\n\" */g, '$1\n\n"')
	
				// lines that end with " should probably precede a new line,
				// unless preceded by a comma,
				// or unless the new line starts with a lower-case letter;
				// and let's get rid of preceding space while we're at it
				.replace(/([^,])\ *\"\n([^a-z\n])/g, '$1"\n\n$2')
	
				// remove single line breaks; preserve multiple.
				// but not if there's a tag, template or table syntax either side of the line break
				.replace(/([^>}\|\n])\n([^:#\*<{\|\n])/g, '$1 $2')
	
				// collapse sequences of spaces into a single space
				.replace(/  +/g, ' ');
		}
		
		// more page cleanup
		editor
			// dump spurious hard breaks at the end of paragraphs
			.replace(/<br *\/?>\n\n/g, '\n\n')

			// remove unwanted spaces around punctuation marks
			//.replace(/ ([;:\?!,])/g, '$1')
	
			// unicodify
			.replace(/&mdash;/g, '—')
			.replace(/&ndash;/g, '–')
			.replace(/&quot;/g, '"')
	
			// straighten quotes and apostrophes.
		//	.replace(/[“”]/g, '"')
		//	.replace(/[‘’`]/g, '\'')
	
			//OCR fixes
			// convert i9 to 19, etc.
			.replace(/[il]([0-9])/g, '1$1')
	
			// "the", "them", "their", etcetera
			.replace(/tlie/g, 'the')
	
			// "U" -> "ll" when preceded by a lowercase letter.
			.replace(/([a-z])U/g, '$1ll')
	
			// "would", "could"
			.replace(/woidd/g, 'would')
			.replace(/coidd/g, 'could')
			.replace(/shoidd/g, 'should')
	/*
			// many works have apostrophes missing from OCR
			.replace(/([a-z]) s\b/g, '$1\'s') // it's he's etc
			.replace(/n t\b/g, 'n\'t') //can't isn't didn't etc
			.replace(/([a-zI]) ll\b/g, '$1\'ll') // I'll we'll etc
			.replace(/\bI m\b/g, 'I\'m') // I'm
			.replace(/\b([Yy])ou re\b/g, '$1ou\'re') // you're
			.replace(/\b([Ww])e re\b/g, '$1e\'re') // we're
			.replace(/\b([Tt])hey re\b/g, '$1hey\'re') // they're
			.replace(/([a-zI]) ve\b/g, '$1\'ve') // I've we've etc
	*/
			// expand diacritical templates
			.replace(/{{((ae|oe|\w[:`'~^-]))}}/g, '{{subst'+':$1}}')
	
			// replace "float center" with "block center"; original template name was misleading enough be warrant routinely fixing
			.replace(/\{\{float center/g, '{{block center')
	
			.replace(/<center>\s*([.\n]*?)\s*<\/center>/g, '{{center|$1}}')
			
			
			
		
			;
	};
	
	/**
	 * As you work your way through the page, when you encounter a reference, just mark it with <ref></ref> tags and continue.
	 * Once you've got to the end of the page and proofed the references, simply highlight each reference in turn,
	 * and use this function to move it to its proper position.
	 * @param {object} editor The script helpers for the page.
	 */
	var makeReference = function(editor) {
		_initialise();
		
		var editbox = $('#wpTextbox1').get(0);
		editbox.focus();
		var refStart = editbox.selectionStart;
		var refEnd = editbox.selectionEnd;

		var firstref = editbox.value.indexOf('<ref></ref>');
		if (firstref != -1) {
			editbox.value = editbox.value.slice(0,firstref+5)
			              + editbox.value.slice(refStart, refEnd)
			              + editbox.value.slice(firstref+5, refStart)
			              + editbox.value.slice(refEnd);
		}
	};

	/**
	 * Insert formatted references into the footer box if needed.
	 * @param {object} editor The script helpers for the page.
	 */
	var addPageFooter = function(editor) {
		_initialise();
		
		var editbox = $('#wpTextbox1').get(0);
		var footerbox = $('#wpFooterTextbox').get(0);
		var generic;
		var format;
		var f;
		if (editbox.value.indexOf("<ref>") == -1 && editbox.value.indexOf("{{#tag:ref") == -1) {
			// page contains no refs
			generic = true;
			for (f in state.specialFormats) {
				format = state.specialFormats[f];
				if (mw.config.get('wgTitle').contains(format.title)) {
					footerbox.value = format.footer;
					generic = false;
					break;
				}
			}

			// no special footer matched, use just strip out the references tag
			if (generic)
				footerbox.value = '';
		}
		else {
			generic = true;
			for (f in state.specialFormats) {
				format = state.specialFormats[f];
				if (mw.config.get('wgTitle').contains(format.title)) {
					footerbox.value = format.footerWithReferences;
					generic = false;
					break;
				}
			}

			// no special footer matched, so use a generic ref tag
			if (generic && doGeneric)
				footerbox.value = '{{block center|{{smallrefs}}}}';
		}
	};

	/**
	 * Mark the selected text with {{sc}}. If the text is uppercase, it will be converted to titlecase.
	 * @param {object} editor The script helpers for the page.
	 */
	var smallcaps = function(editor) {
		_initialise();
		
		editor.replaceSelection(function(text) {
			// 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.
			// modifié pour tout changer en lower-case
			if (text == text.toUpperCase())
				text = text.toLowerCase();
			//	text = _titlecase(text);

			
			return '{{sc|' + text + '}}';
		});
	};

	/**
	 * Convert the text to uppercase.
	 * @param {object} editor The script helpers for the page.
	 */
	var upper = function(editor) {
		_initialise();
		
		editor.replaceSelection(function(text) {
			return text.toUpperCase();
		});
	};
});
// </nowiki>