Jump to content

User:Omegatron/monobook.js/dashfixer.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Omegatron (talk | contribs) at 01:15, 13 January 2006 (self contained). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/*

== Dash fixer ==

This script adds a tab to fix some dash typography in the article.  When you press the tab, it converts HTML entities for dashes into actual Unicode characters, and changes hyphen pseudo-dashes into actual em or en dashes, depending on the placement of the hyphens.  ( -- → —, for instance.)

Then it adds an edit summary and presses ''Show changes'' for you so you can check it for mistakes.

By [[User:Omegatron]]

<pre><nowiki> */

function replacedash() {
    var txt = document.editform.wpTextbox1;

    // Convert all html entities into actual dash characters 
    txt.value = txt.value.replace(/&mdash;/g, '—');
    txt.value = txt.value.replace(/&ndash;/g, '–');
    txt.value = txt.value.replace(/&minus;/g, '−');

    // Convert -- and em dashes *with or without* spaces --> em dash character surrounded by spaces
    txt.value = txt.value.replace(/([a-zA-Z\'\"”\]\}\)])\s? ?(--|—|&mdash;)\s? ?([a-zA-Z\'\"“\[\{\(])/g, '$1 — $3');

    // Convert - or en dashes *with* spaces --> em dash character surrounded by spaces
    txt.value = txt.value.replace(/([a-zA-Z\'\"”\]\}])( |&nbsp;) ?(-|–|&ndash;)  ?([a-zA-Z\'\"“\[\{])/g, '$1$2— $4');

    // Convert hyphen next to lone number into a minus sign character
//    txt.value = txt.value.replace(/([a-zA-Z\'\"”\]\>])(\s)-(\d)/g, '$1$2−$3');
//    Should not touch anything inside math tags...

    // Em dashes for dates?
    txt.value = txt.value.replace(/([\s\(][12]\d\d\d)\s?(--?|—|&mdash;)\s?([12]\d\d\d|\d\d)([\s\),.;])/g, '$1–$3$4');

    // Add a tag to the summary box
    var txt = document.editform.wpSummary;
    var summary = "[[User:Omegatron#Regular expressions|Regex dash fixer]]";
	if (txt.value.indexOf(summary) == -1) {
		if (txt.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
			txt.value += " | ";
		}
		txt.value += summary;
	}

    // Press the diff button to check it
    document.editform.wpDiff.click()
}

addOnloadHook(function () {
    if(document.forms.editform) {
        addLink('p-cactions', 'javascript:replacedash()', '—', 'ca-dashfixer', 'Fixes dash formatting', '', '');
    }
});

// </nowiki></pre>