Jump to content

User:Omegatron/monobook.js/headingformattingfixer.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Cacycle (talk | contribs) at 10:40, 19 January 2006. 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.
/* taken from: http://en.wikipedia.org/w/index.php?title=User:Omegatron/monobook.js&oldid=34206452 */
/* <pre><nowiki> */


// ================================================================
// [[User:Lupin]]'s crazy navigation popups
// (http://en.wikipedia.org/wiki/Wikipedia:Tools#Navigation_popups)

// [[User:Lupin/popups.js]] - please include this line

document.write('<script type="text/javascript" src="'
             + 'http://en.wikipedia.org/w/index.php?title=User:Lupin/popups.js'
             + '&action=raw&ctype=text/javascript&dontcountme=s"></script>');

popupAdminLinks = true;
popupFixRedirs = true;
popupFixDabs = true;
popupStructure = 'original'



// ================================================================
// [[User:Trilobite]]'s tools
// (http://en.wikipedia.org/wiki/User:Trilobite/Tools):

function addlilink(tabs, url, name, id) {
    var na = document.createElement('a');
    na.href = url;
    na.id = id;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    li.appendChild(na);
    tabs.appendChild(li);
    return li;
}

function morelinks() {
    var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
    if(document.title.indexOf("Editing ") == 0) addlilink(tabs, 'javascript:replace()', 'replace', '');
    if(document.title.indexOf("Editing ") == 0) addlilink(tabs, 'javascript:replacedash()', '—', '');
    if(document.title.indexOf("Editing ") == 0) addlilink(tabs, 'javascript:formatunits()', 'µ', '');
    if(document.title.indexOf("Editing ") == 0) addlilink(tabs, 'javascript:mathfixer()', '±', '');
    if(document.title.indexOf("Editing ") == 0) addlilink(tabs, 'javascript:headingfixer()', '==', '');
    if(document.title.indexOf("Editing Image:") == 0) addlilink(tabs, 'javascript:unverified()', 'tag', '');
}

function replace() {
    var s = prompt("Search regexp?");
    if(s) {
        var r = prompt("Replace regexp?");
        if(!r && r != '') return;
        var txt = document.editform.wpTextbox1;
        txt.value = txt.value.replace(new RegExp(s, "g"), r);
    }
}

function unverified() {
    document.editform.wpSummary.value = 'needs a source and copyright tag - see [[Wikipedia:Image copyright tags]]';
    var txt = document.editform.wpTextbox1;
    if(txt.value.length > 0) txt.value += '\n';
    txt.value += '{{unverified}}';
    txt.focus();
}



// ================================================================
// Dash fixer - adds a tab that fixes several obvious en/em dash, minus sign, and such special characters.
// by User:Omegatron

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()
}



// ================================================================
// Unit formatter - new tab adds spaces between number and units, makes units consistent
// by User:Omegatron

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

    // Convert all &deg; into actual ° symbol
    txt.value = txt.value.replace(/&deg;/g, '°');

    // Convert the word ohm(s) or the html entity into the actual O symbol (Omega, not the actual ohm symbol &#8486;) and make sure it's spaced
    txt.value = txt.value.replace(/(\d)\s?(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|µ|µ|µ|n|p|f|a|z|y)?\s?(&Omega;|ohm|Ohm)s?([\s,.])/g, '$1 $2O$4');

    // Convert various micro symbols into the actual micro symbol, make sure it's spaced
    txt.value = txt.value.replace(/(\d)\s?(&mu;|µ|&micro;)(g|s|m|A|K|mol|cd|rad|sr|Hz|N|J|W|Pa|lm|lx|C|V|O|F|Wb|T|H|S|Bq|Gy|Sv|kat|°C|M)([\s,.])/g, '$1 µ$3$4');

    // Convert capital K to lowercase k in units
    txt.value = txt.value.replace(/(\d)\s?K(g|s|m|A|K|mol|cd|rad|sr|Hz|N|J|W|Pa|lm|lx|C|V|O|F|Wb|T|H|S|Bq|Gy|Sv|kat|°C|M)([\s,.])/g, '$1 k$2$3');

    // Capitalize units correctly
    txt.value = txt.value.replace(/(\d)\s?(khz)([\s,.])/gi, '$1 kHz$3');
    txt.value = txt.value.replace(/(\d)\s?(mhz)([\s,.])/gi, '$1 MHz$3');
    txt.value = txt.value.replace(/(\d)\s?(ghz)([\s,.])/gi, '$1 GHz$3');
    txt.value = txt.value.replace(/(\d)\s?(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|µ|µ|µ|n|p|f|a|z|y)?(hz|HZ)([\s,.])/g, '$1 $2Hz$4');
    txt.value = txt.value.replace(/(\d)\s?(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|µ|µ|µ|n|p|f|a|z|y)?(pa|PA)([\s,.])/g, '$1 $2Pa$4');

    // Add a space before dB or B
    txt.value = txt.value.replace(/(\d)\s?(dB|B)\b/g, '$1 $2');

    // Add a space before any units that were missed before
    txt.value = txt.value.replace(/(\d)\s?(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|µ|n|p|f|a|z|y)?(g|m|A|K|mol|cd|rad|sr|Hz|N|J|W|Pa|lm|lx|C|V|O|F|Wb|T|H|S|Bq|Gy|Sv|kat|°C|M)([\s,.])/g, '$1 $2$3$4');

    // Separate one for seconds since they give a lot of false positives like "1970s".  Only difference is mandatory prefix.
    txt.value = txt.value.replace(/(\d)\s?(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|µ|n|p|f|a|z|y)(s)([\s,.])/g, '$1 $2$3$4');

    // bps or b/s or bits/s --> bit/s
    txt.value = txt.value.replace(/([KkMmGgTtPpEeYyZz])(bps|bits?\/s|b\/s)/g, '$1bit/s');

    // Bps or byte/s or bytes/s --> B/s
    txt.value = txt.value.replace(/([KkMmGgTtPpEeYyZz])(Bps|bytes?\/s)/g, '$1B/s');

    // After that, make capitalization correct
    txt.value = txt.value.replace(/K(bit|B)\/s/g, 'k$1/s');
    txt.value = txt.value.replace(/m(bit|B)\/s/g, 'M$1/s');
    txt.value = txt.value.replace(/g(bit|B)\/s/g, 'G$1/s');
    txt.value = txt.value.replace(/t(bit|B)\/s/g, 'T$1/s');
    txt.value = txt.value.replace(/e(bit|B)\/s/g, 'E$1/s');
    txt.value = txt.value.replace(/y(bit|B)\/s/g, 'Y$1/s');
    txt.value = txt.value.replace(/z(bit|B)\/s/g, 'Z$1/s');

    // Common error
    txt.value = txt.value.replace(/mibi(bit|byte)/g, 'mebi$1');

    // Add a tag to the summary box
    var txt = document.editform.wpSummary;
    var summary = "[[User:Omegatron#Regular expressions|Regex unit formatter]]";
	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()
}



// ================================================================
// Math character fixer
// by User:Omegatron

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

    // Convert hyphen next to a number into a minus sign character
    txt.value = txt.value.replace(/([^a-zA-Z0-9\,\_\{])-(\d)/g, '$1-$2');

    // Changes 2x3 to 2×3
    txt.value = txt.value.replace(/(\d\s?)x(\s?\d)/g, '$1×$2');

    // Changes 10^3 to 10<sup>3</sup>
//    txt.value = txt.value.replace(/(\d+)\^(\d+)/g, '$1<sup>$2</sup>');

    // Changes x^3 to x<sup>3</sup>
    txt.value = txt.value.replace(/([0-9a-zA-Z])\^(\d+)/g, '$1<sup>$2</sup>');

    // Changes 2 +/- 3 to 2±3
    txt.value = txt.value.replace(/(\s|\d)\+\/(-|-|-)(\s|\d)/g, '$1±$3');

    // Add a tag to the summary box
    var txt = document.editform.wpSummary;
    var summary = "[[User:Omegatron#Regular expressions|Regex math character 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()
}



// ================================================================
// Heading formatting fixer
// by User:Omegatron

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

    // Format heading markup the way it is generated with the + tab
    txt.value = txt.value.replace(/((^|\n)={2,})( |\t)*(.*?)( |\t)*(={2,})/g, '$1 $4 $6\n\n');

    // 1 blank line before the headings
    // (this is super-kludgy because of the newlines conflicting when one heading is right after another)
    // (seems to work, though, for now.  some other day...)
    txt.value = txt.value.replace(/(\n+={2,}) (.*?) (={2,})/g, '\n\n$1 $2 $3');

    // Remove all extra newlines?
    txt.value = txt.value.replace(/(\n[ \t\v\r\f]*){2,}/g, '\n\n');

    // Format External links and See also according to the Manual of Style
    txt.value = txt.value.replace(/\=\= external links? \=\=/ig, '== External links ==');
    txt.value = txt.value.replace(/\=\= see also \=\=/ig, '== See also ==');

    // Add a tag to the summary box
    var txt = document.editform.wpSummary;
    var summary = "[[User:Omegatron#Regular expressions|Regex heading 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()
}



// ================================================================
// Add editing tabs to the bottom of articles, too (http://meta.wikimedia.org/wiki/User_Styles/bottom_tabs)

function morelinks() {
  var tabs = document.getElementById('p-cactions').cloneNode(true);
  tabs.id = 'mytabs';
  var listitems = tabs.getElementsByTagName('LI');
  for (i=0;i<listitems.length;i++) {
    if(listitems[i].id) listitems[i].id = 'mytabs-' + listitems[i].id;
  }
  document.getElementById('column-content').appendChild(tabs);
}
if (window.addEventListener) window.addEventListener("load",morelinks,false);
else if (window.attachEvent) window.attachEvent("onload",morelinks);


/*
function bottomtabs() {
  var tabs = document.getElementById('p-cactions').cloneNode(true);
  tabs.id = 'mytabs';
  var listitems = tabs.getElementsByTagName('LI');
  for (i=0;i<listitems.length;i++) {
    if(listitems[i].id) listitems[i].id = 'mytabs-' + listitems[i].id;
  }
  document.getElementById('column-content').appendChild(tabs);
}
if (window.addEventListener) window.addEventListener("load",bottomtabs,false);
else if (window.attachEvent) window.attachEvent("onload",bottomtabs);
*/


// ================================================================
// Adds an unwatch link after each entry on the regular watchlist
// (instead of requiring you to navigate to a separate page)
// by User:Omegatron
// Workaround for Bug 424 http://bugzilla.wikipedia.org/show_bug.cgi?id=424

function unwatchlinks(){
if(document.title.indexOf("My watchlist") == 0) {
    var x,i;
    x=document.links;                                           // Gets all the links on the page
        for(i=0;i<x.length;++i) {
            if (/action=history/.test(x[i].href)) {             // Find all the ones that link to history pages
                newUnwatchLink = document.createElement('a');
                newSeparator = document.createTextNode("; ");
                newUnwatchLink.href = x[i].href.match(/\/w\/.+?&/) + "action=unwatch";    // Replace the action=history with action=unwatch
                newUnwatchLink.innerHTML = "un";
                x[i].parentNode.insertBefore(newUnwatchLink, x[i].nextSibling);
                x[i].parentNode.insertBefore(newSeparator, x[i].nextSibling);             // Insert after the hist link with a semicolon
            }
        }
    }
}



//************************************ Edit lead section *****************************
//From http://en.wikipedia.org/w/index.php?title=User:ABCD/monobook.js&oldid=19452182
function addEditSection0(){
    ta['ca-edit-0'] = ['', 'Edit the zeroth section of this page'];
    if(!document.getElementById) return;
    var x = document.getElementById('ca-edit');
    if(!x) return;
    var y = document.createElement('LI');
    y.id = 'ca-edit-0';
    if(x.className == 'selected'){
      if(/&action=edit&section=0$/.test(window.location.href)){
        x.className = 'istalk';
        y.className = 'selected';
      } else {
        x.className = 'selected istalk';
      }
    } else if(x.className == 'selected istalk'){
      if(/&action=edit&section=0$/.test(window.location.href)){
        x.className = 'istalk';
        y.className = 'selected istalk';
      } else {
        y.className = 'istalk';
      }
    } else {
      y.className = x.className;
      x.className = 'istalk';
    }
    var z = document.createElement('A');
    if(x.children){
        z.href = x.children[0].href + '&section=0';
        z.appendChild(document.createTextNode('0'));
        y.appendChild(z);
        document.getElementById('p-cactions').children[1].insertBefore(y,x.nextSibling);
    }else{
        z.href = x.childNodes[0].href + '&section=0';
        z.appendChild(document.createTextNode('0'));
        y.appendChild(z);
        document.getElementById('p-cactions').childNodes[3].insertBefore(y,x.nextSibling);
    }
}

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

    // Format heading markup the way it is generated with the + tab
    txt.value = txt.value.replace(/((^|\n)={2,})( |\t)*(.*?)( |\t)*(={2,})/g, '$1 $4 $6\n\n');

    // 1 blank line before the headings
    // (this is super-kludgy because of the newlines conflicting when one heading is right after another)
    // (seems to work, though, for now.  some other day...)
    txt.value = txt.value.replace(/(\n+={2,}) (.*?) (={2,})/g, '\n\n$1 $2 $3');

    // Remove all extra newlines?
    txt.value = txt.value.replace(/(\n[ \t\v\r\f]*){2,}/g, '\n\n');

    // Spaces after * and # lists
    txt.value = txt.value.replace(/\n(\*|\#)\s*(\w|\[)/g, '\n$1 $2');

    // Format External links and See also according to the Manual of Style
    txt.value = txt.value.replace(/\=\= external links? \=\=/ig, '== External links ==');
    txt.value = txt.value.replace(/\=\= see also \=\=/ig, '== See also ==');

    // Add a tag to the summary box
    var txt = document.editform.wpSummary;
    var summary = "[[User:Omegatron#Regular expressions|Regex heading 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:headingfixer()', '==', 'ca-headingfixer', 'Fixes heading whitespace and deletes excess newlines', '', '');
    }
});



// ================================================================
// Loads each tool after loading the page

window.onload = Main;
function Main() {
    morelinks();
    unwatchlinks();
//    addEditSection0();
}

/* </nowiki></pre> */