User:Þjarkur/Reference formatter.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:Þjarkur/Reference formatter. |
/*
This code is [[en:Wikipedia:AutoEd]] with a few additions.
*/
var AutoEd_baseurl = 'https://wiki.riteme.site/w/index.php?action=raw&ctype=text/javascript&title=Wikipedia:AutoEd/';
//Import individual modules for use
if ( typeof autoEdUnicodify !== 'undefined') {
mw.loader.load(AutoEd_baseurl + 'unicodify.js'); // autoEdUnicodify() converts HTML entities to WikiText
mw.loader.load(AutoEd_baseurl + 'htmltowikitext.js'); // autoEdHTMLtoWikitext() converts HTML to wikitext
// mw.loader.load(AutoEd_baseurl + 'headlines.js'); // autoEdHeadlines() fixes common headline errors and renames some headers
mw.loader.load(AutoEd_baseurl + 'unicodecontrolchars.js'); // autoEdUnicodeControlChars() converts HTML to wikitext
mw.loader.load(AutoEd_baseurl + 'unicodehex.js'); // autoEdUnicodeHex() converts hex encoded characters to unicode
mw.loader.load(AutoEd_baseurl + 'templates.js'); // autoEdTemplates() cleans up templates
mw.loader.load(AutoEd_baseurl + 'tablestowikitext.js'); // autoEdTablestoWikitext() replaces HTML tables with wikitables
mw.loader.load(AutoEd_baseurl + 'extrabreaks.js'); // autoEdExtraBreaks() removes extra BR tags
mw.loader.load(AutoEd_baseurl + 'links.js'); // autoEdLinks() cleans up common link errors
}
var lang = mw.config.get('wgContentLanguage')
function fiffFunctions() { //Activates individual modules when "auto ed" tab is clicked
var txt = document.editform && document.editform.wpTextbox1
var value = txt.value
value = value.replace(/\u00AD/g, '') //Soft hyphen
if (lang === 'is') {
value = ReflistVantar(value);
}
value = autoEdUnicodify(value);
value = autoEdUnicodeHex(value);
value = autoEdWikilinks(value);
value = autoEdHTMLtoWikitext(value);
value = autoEdUnicodeControlChars(value);
value = autoEdTemplates(value);
value = autoEdTablestoWikitext(value);
value = autoEdExtraBreaks(value);
value = autoEdLinks(value);
if (lang === 'is') {
value = fjarlægjaDagsetningahlekki(value);
value = Íslenskun(value);
value = FeitletrunVantar(value);
value = ÚtlenskirTenglar(value);
// value = Ártalabil(value);
}
value = SkrýtnirHausar(value);
value = LagfæraStaðsetninguTilvísana(value);
txt.value = value
}
//Initiates AutoEd
function fiffExecute() {
if (!document.getElementById('wpTextbox1')) return;
// copy wikEd ([[User:Cacycle/wikEd.js]]) frame to wpTextbox1 textarea
// for compatibility with WikiEd
if (typeof wikEdUseWikEd !== 'undefined') {
if (wikEdUseWikEd === true) {
WikEdUpdateTextarea();
}
}
//alert/return if fiffFunctions is not defined
if (typeof fiffFunctions === 'undefined') {
alert('AutoEd/core.js: fiffFunctions is undefined');
return;
}
if (typeof autoEdUnicodify !== 'undefined' && typeof autoEdHTMLtoWikitext !== 'undefined') {
fiffFunctions();
fiffEditSummary();
}
// copy wpTextbox1 textarea back to wikEd frame
// for compatibility with WikiEd
if (typeof wikEdUseWikEd !== 'undefined') {
if (wikEdUseWikEd === true) {
WikEdUpdateFrame();
}
}
}
//Adds Tag to edit summary textbox
function fiffEditSummary() {
var txt = document.forms.editform.wpSummary;
// Click 'Show changes'
if (typeof autoEdClick === 'undefined' || autoEdClick) {
document.forms.editform.wpDiff.click();
}
}
// Add "auto ed" tab and associate with actions
// Make sure the document is ready and our dependencies are loaded
$.when(
$.ready,
mw.loader.using(['mediawiki.util'])
).done(function () {
var $fiffLink;
//Execute AutoEd after call from "view mode"
if (mw.util.getParamValue('AutoEd')) {
setTimeout(fiffExecute, 300)
}
// Set default values for any unset variables
if (typeof fiffLinkHover === 'undefined') {
fiffLinkHover = "Keyra fiffara";
}
if (typeof fiffLinkName === 'undefined') {
fiffLinkName = "Fiffa";
}
if (typeof fiffLinkLocation === 'undefined') {
fiffLinkLocation = "p-cactions";
}
// Add the "auto ed" tab
if (document.getElementById('ca-edit')) {
var url = mw.util.getUrl(mw.config.get('wgPageName'), { action: 'edit', AutoEd: 'true' });
$fiffLink = $(mw.util.addPortletLink(
fiffLinkLocation,
url,
fiffLinkName,
'ca-AutoEd',
fiffLinkHover,
'',
document.getElementById('ca-move')
));
if (typeof document.forms.editform !== 'undefined') {
$fiffLink.on('click', function (e) {
e.preventDefault();
fiffExecute();
});
}
}
});
function fjarlægjaDagsetningahlekki(value) {
return value
.replace(/\[\[(\d{3,4}|\d{1,2}\. (janúar|febrúar|mars|apríl|maí|júní|júlí|ágúst|september|október|nóvember|desember))\]\]/g, '$1')
.replace(/\[\[(?:\d{3,4}|\d{1,2}\. (?:janúar|febrúar|mars|apríl|maí|júní|júlí|ágúst|september|október|nóvember|desember))\|([^\]]+?)\]\]/g, '$1')
}
function Íslenskun(value) {
return value
.replace(/== ?References ?==/g, '== Tilvísanir ==')
.replace(/== ?External links ?==/g, '== Tenglar ==')
.replace(/(\[\[Category:[^\]]+?\]\])/g, '')
}
function ReflistVantar(value) {
if (!/<ref/.test(value)) return value;
if (/(<references|{{reflist|{{notelist)/i.test(value)) return value;
var split = value.split(/((?:{{stubbur|\[\[Flokkur:)[\s\S]*)/i)
return (split[0]) + '\n== Tilvísanir ==\n{{reflist}}\n' + (split[1] || '')
}
function FeitletrunVantar(value) {
if (/'''/.test(value)) return value;
var titill = mw.config.get('wgTitle') || ''
var re = new RegExp('(.+?(' + titill + ').+?)', 'i');
if (!re.test(value)) return value;
if (/({{|=\/)/.test(value.match(re)[1])) return value; // Match is in an infobox
return value.replace(re, "'''$2'''")
}
function LagfæraStaðsetninguTilvísana(value) {
// return cleanUpFootnotes(value)
var regex = new RegExp('(?: +?)?((?:<ref name=[^>]+/>|<ref.+?</ref>)+)([.,]+?)?', 'gi');
return value.replace(regex, '$2$1')
}
function ÚtlenskirTenglar(value) {
return value.replace(/\[\[:(..):([^\]]+)\|(([^\]]+))\]\]/g, '{{ill|$3|$1|$2}}')
}
function Ártalabil(value) {
return value.replace(/([^\d]\d{4})-(d{4}[^\d])/g, '$1–$2')
}
function SkrýtnirHausar(value) {
return value
.replace(/\n=([^=]+)\= ?\n/g, '\n== $1 ==\n')
.replace(/<\/? ?big ?>/g, '')
.replace(/<\/? ?u ?>/g, '')
.replace(/= ?'''([^=]+?)''' ?=/g, '= $1 =')
.replace(/\n'''(.+?)''' ?\n/g, '\n== $1 ==\n')
}
function autoEdWhitespace(str) { //MAIN FUNCTION describes list of fixes
str = str.replace(/\t/g, " ");
str = str.replace(/^ ? ? \n/gm, "\n");
// Extra newlines
if (str.search(/\uE000/g) < 0) { // see [[Private Use Area]]
// Mark spacing before stub templates
str = str.replace(/(\n)[ \t]*(\n)[ \t]*(\n\{\{[^{}]*\-stub\}\})/gm, "$1$2$3");
str = str.replace(/(\n)[ \t]*(\n\{\{[^{}]*\-stub\}\})/gm, "$1$2");
// Remove extra newlines
str = str.replace(/(?:[\t ]*\n)+[\t ]*(\n\uE000\n\uE000\n)/gm, "$1");
str = str.replace(/(\n\n)\n+/gm, "$1");
str = str.replace(/(\n[\t ]*\n)(?:[\t ]*\n)+/g, "$1");
// Unmark
str = str.replace(/\uE000/g, '');
}
str = str.replace(/(=\n[\t ]*\n[\t ]*\n)(?:[\t ]*\n)+/g, "$1");
return str;
}
fiffExecute()
// From : https://wiki.riteme.site/wiki/Wikipedia:AutoEd/wikilinks.js
// Credits: A modification of [[Wikipedia:WikiProject User scripts/Scripts/Formatter]]
function autoEdWikilinks(str) { //MAIN FUNCTION describes list of fixes
//Get the list of all wikilinks with underscores
var m = str.match(/\[\[[^\[\]]*_[^\[\]]*\]\]/g);
if (m) {
//For each wikilink in the list
for (var i = 0; i < m.length; i++) {
var x = m[i].toString() // Contains the entire wikilink
// Exclude URLs and leading underscores
if( !x.match(/^\[\[[\t ]*(?:http|ftp|https):/i) && !x.match(/^\[\[_[^\[\]]*\]\]/) ) {
var x_arr = x.match(/^(\[\[[^\[\]\|]*)(\|?[^\[\]]*?\]\])$/);
var a = x_arr[1]; // Everything before the pipe (or everything minus ]])
var b = x_arr[2]; // Everything after the pipe (or ]])
// Replace underscores with spaces
a = a.replace(/_/g, ' ');
// Do the replacement
str = str.replace(x, a+b);
}
}
}
//Fix links which have no target
str = str.replace(/\[\[[\t ]*\|/gi, '[[');
//Leading and trailing space before the pipe inside wikilinks
str=str.replace(/(\[\[)[\t ]+([^\[\]\|]*?)(\|)/g, '$1$2$3');
str=str.replace(/(\[\[)([^\[\]\|]*?)[\t ]+(\|)/g, '$1$2$3');
//Leading space after the pipe (or in an unpiped) wikilink
str=str.replace(/^(\[\[[^\[\]\|]*?\||\[\[)[\t ]+([^\[\]\|\t ][^\[\]\|]*?)(\]\])/g, '$1$2$3');
str=str.replace(/(\[\[[^\[\]\|]*?\||\[\[)[\t ]+([^\[\]\|\t ][^\[\]\|]*?)(\]\])/g, ' $1$2$3');
//Trailing space after the pipe (or in an unpiped) wikilink
str=str.replace(/(\[\[[^\[\]\|]*?\||\[\[)([^\[\]\|\t ][^\[\]\|]*?)[\t ]+(\]\])([^A-Za-z])/gi, '$1$2$3 $4');
str=str.replace(/(\[\[[^\[\]\|]*?\||\[\[)([^\[\]\|]*?)[\t ]+(\]\])$/gi, '$1$2$3');
//Get the list of all piped wikilinks
var m = str.match(/\[\[[^\[]*?\|[^\[]*?\]\]/g);
if (m) {
//For each piped wikilink in the list
for (var i = 0; i < m.length; i++) {
var n_arr = m[i].toString().match(/\[\[[ ]*([^\[]*?)\|[ ]*([^\[]*?)\]\]/);
var n = n_arr[0]; // Contains the entire piped link
var a = n_arr[1]; // Contains everything before pipe
var b = n_arr[2]; // Contains everything after pipe
var c = b.replace(/[\.,:; ]*$/); // Same as b, but without trailing punctuation
// //Is the display name a leading substring of the wikilink?
// if (b.indexOf(a) == 0 || b.indexOf(autoEd_first2lower(a)) == 0) {
// //Create a simplified replacement string
// var k = n.replace(/\[\[([^\]\|]*?)\|(\1)([\w]*?)\]\]/i, "[[$2]]$3");
// //And do the replacement
// str = str.replace(n, k);
// }
if (c.indexOf(a) == 0 || c.indexOf(autoEd_first2lower(a)) == 0) {
// Create a simplified replacement string
var k = n.replace(/\[\[([^\]\|]*?)\|(\1)([\w\.,:;]*?)\]\]/i, "[[$2]]$3");
// And do the replacement
str = str.replace(n, k);
}
}
}
//Push trailing characters into display string of piped wikilinks
//str = str.replace(/\[\[([^\[\]\|]+)\|([^\[\]\|]+)\]\]([a-z]+)/g, "[[$1|$2$3]]");
//Removes links to current article
var p1 = mw.config.get('wgPageName'); // PAGENAME including underscores
var p2 = mw.config.get('wgPageName').replace(/_/g, ' '); // PAGENAME without underscores
var p3 = autoEd_first2lower(p1); // First character lowercase PAGENAME including underscores
var p4 = autoEd_first2lower(p2); // First character lowercase PAGENAME without underscores
// Standard wikilinks
str = str.replace(new RegExp('\\[\\[(' + p1 + '|' + p2 + '|' + p3 + '|' + p4 + ')\\]\\]', 'g'), '$1');
// Piped wikilinks
str = str.replace(new RegExp('\\[\\[(?:' + p1 + '|' + p2 + '|' + p3 + '|' + p4 + ')\\|([^\\]\\|]*)\\]\\]', 'g'), '$1');
//Shorten interwiki links
str = str.replace(/\[\[WIKTIONARY:/gi, '[[wikt:');
str = str.replace(/\[\[WIKINEWS:/gi, '[[n:');
str = str.replace(/\[\[WIKIBOOKS:/gi, '[[b:');
str = str.replace(/\[\[WIKIQUOTE:/gi, '[[q:');
str = str.replace(/\[\[WIKISOURCE:/gi, '[[s:');
str = str.replace(/\[\[WIKISPECIES:/gi, '[[species:');
str = str.replace(/\[\[WIKIVERSITY:/gi, '[[v:');
str = str.replace(/\[\[(?:WIKIMEDIA|FOUNDATION):/gi, '[[wmf:');
str = str.replace(/\[\[METAWIKIPEDIA:/gi, '[[m:');
//Replace [[Foo #bar]] -> [[Foo#bar]]
str = str.replace(/\[\[([^\]]*?)( |_)+#([^\]]*?)\]\]/g, '[[$1#$3]]');
//Replace [[Foo|Foo]] -> [[Foo| ]]
str = str.replace(/\|\]\]/g, '| ]]');
return str;
}
// Converts the first character in a string to lower case
// Notes: Used by autoEdWikilinks
function autoEd_first2lower(str) {
if (str != "") {
var letter = str.substr(0, 1);
return letter.toLowerCase() + str.substr(1, str.length);
} else {
return "";
}
}