User:Erutuon/scripts/BC-AD.js

// // Switches the year-numbering system from BCE/CE to BC/AD /* jshint undef: true */ /* globals $, mw */ "use strict";

function yearFormatSwitch { /*	 * This code supports date labels formatted using the following wikitext. * (See and .) BCE can be replaced by CE:  BCE */	var convert = { "BCE": "BC", "B.C.E.": "B.C.", "CE": "AD", "C.E.": "A.D." };	var datetags = document.getElementsByClassName("ce-date"); var articlePath = mw.config.get("wgArticlePath"); var links = { BC: articlePath.replace("$1", "Appendix:Glossary#BC"), AD: articlePath.replace("$1", "Appendix:Glossary#AD") };	for (var i = 0; i < datetags.length; i++) { var datetag = datetags[i], link = datetag && datetag.firstChild, innerSpan = link && link.firstChild, abbreviation = innerSpan && innerSpan.firstChild && innerSpan.firstChild.nodeValue; if (!(link && innerSpan && abbreviation)) continue; if (abbreviation === "CE" || abbreviation === "C.E.") { link.href = links.AD; innerSpan.innerHTML = convert[abbreviation]; // Change position of year number: 10 CE → AD 10. var beforeDatetag = datetag.previousSibling; if (!beforeDatetag) continue; var patt = /((?:\d{1,4} ?(?:-|–|to) ?)?\d{1,4}) ?$/g; // first if hyphen-minus, then en-dash var matches = beforeDatetag.nodeValue.match(patt); if (matches === null) continue; var yearNumber = matches[matches.length - 1]; beforeDatetag.nodeValue = beforeDatetag.nodeValue.substring(0, beforeDatetag.nodeValue.length - yearNumber.length); if(!datetag.nextSibling) datetag.parentElement.appendChild(document.createTextNode(" " + yearNumber)); else datetag.nextSibling.nodeValue = " " + yearNumber + datetag.nextSibling.nodeValue; } else if (abbreviation === "BCE" || abbreviation === "B.C.E.") { datetag.firstChild.href = links.BC; innerSpan.innerHTML = convert[abbreviation]; }	}	/*	 * This code handles dates with the class ce-date2: *  CE */	var datetags2 = document.getElementsByClassName("ce-date2"); for (i = 0; i < datetags2.length; ++i ) { var datetag2 = datetags2[i]; datetag2.innerHTML = convert[datetag2.innerHTML]; } }

$(yearFormatSwitch);

//