MediaWiki:Common.js: Difference between revisions
Link MII/IPW team codes without combining titles |
Link MII/IPW team codes without combining titles |
||
| Line 1: | Line 1: | ||
/** | /** | ||
* Hide leftover imported rows the infobox no longer shows. | * Hide leftover imported rows the infobox no longer shows. | ||
* Title-case ALL-CAPS soldier names in category lists (native lists use the real page title). | |||
* Put the research-notes disclaimer under that section heading. | * Put the research-notes disclaimer under that section heading. | ||
* Split mashed CSV notes into separate bullets, then link MII/IPW team codes. | * Split mashed CSV notes into separate bullets, then link MII/IPW team codes. | ||
*/ | */ | ||
$(function () { | $(function () { | ||
var NAME_PARTICLES = { | |||
von: 1, van: 1, de: 1, der: 1, den: 1, da: 1, di: 1, du: 1, | |||
la: 1, le: 1, del: 1, della: 1, ter: 1, ten: 1, zu: 1, zum: 1, bin: 1 | |||
}; | |||
var NAME_SUFFIX = { | |||
jr: 'Jr', 'jr.': 'Jr.', sr: 'Sr', 'sr.': 'Sr.', | |||
esq: 'Esq', 'esq.': 'Esq.', ii: 'II', iii: 'III', iv: 'IV' | |||
}; | |||
function lettersOnly(s) { | |||
return String(s || '').replace(/[^A-Za-z]/g, ''); | |||
} | |||
function isAllCaps(s) { | |||
var letters = lettersOnly(s); | |||
return letters.length >= 1 && letters === letters.toUpperCase(); | |||
} | |||
function isAllLower(s) { | |||
var letters = lettersOnly(s); | |||
return letters.length >= 2 && letters === letters.toLowerCase(); | |||
} | |||
function titleCaseWord(s) { | |||
s = String(s || '').toLowerCase(); | |||
return s ? s.charAt(0).toUpperCase() + s.slice(1) : s; | |||
} | |||
function prettyCapsWord(word) { | |||
var trail = ''; | |||
while (word && /[.,]$/.test(word)) { | |||
trail = word.slice(-1) + trail; | |||
word = word.slice(0, -1); | |||
} | |||
var up = lettersOnly(word).toUpperCase(); | |||
if (up.length >= 5 && up.slice(0, 2) === 'MC') { | |||
return 'Mc' + titleCaseWord(up.slice(2)) + trail; | |||
} | |||
if (up.length >= 7 && up.slice(0, 3) === 'MAC') { | |||
return 'Mac' + titleCaseWord(up.slice(3)) + trail; | |||
} | |||
return titleCaseWord(up) + trail; | |||
} | |||
function prettyNameToken(word, leading) { | |||
word = $.trim(word); | |||
if (!word) { | |||
return word; | |||
} | |||
if (word.indexOf('-') !== -1 && word.charAt(0) !== '-' && word.slice(-1) !== '-') { | |||
return $.map(word.split('-'), function (part, i) { | |||
return prettyNameToken(part, leading && i === 0); | |||
}).join('-'); | |||
} | |||
var apos = word.indexOf("'") !== -1 ? "'" : (word.indexOf('\u2019') !== -1 ? '\u2019' : ''); | |||
if (apos) { | |||
var bits = word.split(apos); | |||
return prettyNameToken(bits[0], leading) + apos + prettyNameToken(bits.slice(1).join(apos), false); | |||
} | |||
if (/^(?:[A-Za-z]\.)+$/.test(word)) { | |||
return (isAllCaps(word) || isAllLower(word)) ? word.toUpperCase() : word; | |||
} | |||
var low = word.toLowerCase(); | |||
if (NAME_SUFFIX[low]) { | |||
return NAME_SUFFIX[low]; | |||
} | |||
var bare = lettersOnly(word); | |||
if (bare.length === 1) { | |||
return bare.toUpperCase() + word.replace(/[A-Za-z]/, ''); | |||
} | |||
var lowBare = bare.toLowerCase(); | |||
if (NAME_SUFFIX[lowBare] && /^[A-Za-z]+\.?$/.test(word)) { | |||
var canon = NAME_SUFFIX[lowBare]; | |||
if (word.slice(-1) === '.' && canon.slice(-1) !== '.') { | |||
return canon + '.'; | |||
} | |||
return canon; | |||
} | |||
if (NAME_PARTICLES[lowBare] && /^[A-Za-z]+$/.test(word) && (isAllCaps(word) || isAllLower(word))) { | |||
return leading ? titleCaseWord(lowBare) : lowBare; | |||
} | |||
if (isAllCaps(word) || isAllLower(word)) { | |||
return prettyCapsWord(word); | |||
} | |||
return word; | |||
} | |||
function prettyNamePhrase(text, capLead) { | |||
text = $.trim(text || '').replace(/\s*;\s*/g, ' '); | |||
if (!text) { | |||
return ''; | |||
} | |||
return $.map(text.split(/\s*,\s*/), function (clause, clauseI) { | |||
clause = $.trim(clause); | |||
if (!clause) { | |||
return null; | |||
} | |||
var words = clause.split(/\s+/); | |||
return $.map(words, function (word, i) { | |||
var particle = lettersOnly(word).toLowerCase(); | |||
var leading = (clauseI === 0 && i === 0 && capLead) || words.length === 1; | |||
if (NAME_PARTICLES[particle] && /^[A-Za-z]+$/.test(word) && (isAllCaps(word) || isAllLower(word))) { | |||
return leading ? titleCaseWord(particle) : word.toLowerCase(); | |||
} | |||
return prettyNameToken(word, i === 0); | |||
}).join(' '); | |||
}).join(', '); | |||
} | |||
function prettySoldierLabel(text) { | |||
var m = $.trim(text || '').match(/^(.*) \(([^()]+)\)\s*$/); | |||
if (!m || m[1].indexOf('(') !== -1) { | |||
return prettyNamePhrase(text, true); | |||
} | |||
return prettyNamePhrase(m[1], true) + ' (' + m[2] + ')'; | |||
} | |||
if (mw.config.get('wgNamespaceNumber') === 14) { | |||
$('.mw-category-group li a').each(function () { | |||
var $a = $(this); | |||
var label = $a.text(); | |||
if (!/\(\d/.test(label)) { | |||
return; | |||
} | |||
var pretty = prettySoldierLabel(label); | |||
if (pretty && pretty !== label) { | |||
$a.text(pretty); | |||
$a.attr('title', pretty); | |||
} | |||
}); | |||
} | |||
$('.infobox th, .soldier-infobox th, table.wikitable td').each(function () { | $('.infobox th, .soldier-infobox th, table.wikitable td').each(function () { | ||
var text = $(this).text().replace(/\s+/g, ' ').trim(); | var text = $(this).text().replace(/\s+/g, ' ').trim(); | ||