User:Omegatron/monobook.js/dashfixer.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Omegatron/monobook.js/dashfixer. |
function replacedash() {
var txt = document.editform.wpTextbox1;
// Convert all html entities into actual dash characters
txt.value = txt.value.replace(/(—|—|—)/g, '—');
txt.value = txt.value.replace(/(–|–|–)/g, '–');
txt.value = txt.value.replace(/(−|−|−)/g, '−');
// Convert -- and em dashes *with or without* spaces --> em dash character surrounded by spaces
txt.value = txt.value.replace(/([a-zA-Z\'\"”\]\}\)])\s? ?(--|—|—)\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\'\"”\]\}])( | ) ?(-|–|–) ?([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?(--?|—|—)\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()
}
$(function () {
if(document.forms.editform) {
mw.util.addPortletLink('p-cactions', 'javascript:replacedash()', '—', 'ca-dashfixer', 'Fixes dash formatting', '', '');
}
});