User:Ohconfucius/script/EngvarB.js: Difference between revisions

Content deleted Content added
U.S.A. to USA
Undid revision 1299042620 by Ohconfucius (talk)
 
(13 intermediate revisions by the same user not shown)
Line 31:
},
{
category: 'EngvarB', name: 'COMMONWEALTHBRITISH', isMinorEdit: true, script: function (editor) {
ohc_ENGVAR_set_variation(editor, "B");
}
Line 137:
protect_string(/((?:analy|iri|peni)ses)/gi);
protect_string(/(World Health Organization)/gi);
protect_string(/(Royal Space Force: The Wings of Honnêamise)/g);
 
// protect from hyphenation
Line 188 ⟶ 189:
 
// old-fashioned
// editor.replace(/(whil)st /gi, '$1e ');
// editor.replace(/(among)st /gi, '$1 ');
 
// hyphenation
Line 228 ⟶ 229:
'maneuver(ab|ed|ing)': '$1manoeuvr$2',
'maneuver': '$1manoeuvre',
'meager': '$1meagre',
'louve(red|ring)': '$1louv$2',
'(louv|somb)er': '$1$2re',
Line 288 ⟶ 290:
// '~l' words where the 'l' doubles as past and present participle
'((?:\\w{3,9}[-–—]|)(?:fu|pan))el(e[dr]|ist|ing)': '$1$2ell$3',
'(bev|canc|couns|fu|jew|lab|lev|marv|mod|revpan|prop|quarr|r[ei]v|sign|trav)el(e[dr]|ing)': '$1$2ell$3',
'([a-zA-Z]{2,8}[^eglpstz ])el(e[dr]|est|ing)(\\b)': '$1$2ell$3$4', // added 'p' to avoid changing of "misspelling"
'(app|di|enthr|equ|initi|journ|riv|sign|tot)al(e[dr]|ing)': '$1$2all$3' // appalled/appalling is also American
Line 345 ⟶ 347:
// remove ambiguous pound sign; rem incidental intervening space
editor.replace(/₤ ?(\d)/g, '£$1');
// reconvert special cases
 
editor.replace(/\b(vice) (president|chairman|chancellor|minister)/gi, '$1-$2');
 
}
 
function ohc_ENGVAR_simple_c(editor) {
var table = {
'ageing': '$1aging',
'aluminum': '$1aluminium',
'adrenalin(\\b)': '$1adrenaline$2',
'annex(\\b)': '$1annexe$2',
'(ana|breatha|cata|hydro|para)lyz(e|ing)': '$1$2lys$3',
'bau(lk)': '$1ba$2',
'(de|of)fense': '$1$2fence',
'percent(\\b)': '$1per cent$2',
'license': '$1licence',
'(licen)c(ing|ed)': '$1$2s$3',
'percent(\\b)': '$1per cent$2',
'(practi)s(ing|ed)': '$1$2c$3',
'furor(\\b)': '$1furore$2',
Line 399 ⟶ 402:
 
// 'f' words
//'(S|s)ulfur': '$1$2ulphur', //disabled per WP:ARTCONARTCOM
 
'anymore': '$1any more',
Line 426 ⟶ 429:
// '~l' words where the 'l' doubles as past and present participle
'((?:\\w{3,9}[-–—]|)(?:fu|pan))el(e[dr]|ist|ing)': '$1$2ell$3',
'(bev|canc|couns|fu|jew|lab|lev|marv|mod|revpan|prop|quarr|r[ei]v|sign|trav)el(e[dr]|ing)': '$1$2ell$3',
'([a-zA-Z]{2,8}[^eglpstz ])el(e[dr]|est|ing)(\\b)': '$1$2ell$3$4', // added 'p' to avoid changing of "misspelling"
'(app|di|enthr|equ|initi|journ|riv|sign|tot)al(e[dr]|ing)': '$1$2all$3' // appalled/appalling is also American
Line 495 ⟶ 498:
'diarrhoea': '$1diarrhea',
'(de|of)fence': '$1$2fense',
'per cent(\\b)': '$1percent$2',
'licence': '$1license',
'(licen)c(ing|ed)': '$1$2s$3',
'per cent(\\b)': '$1percent$2',
'(practi)s(ing|ed)': '$1$2c$3',
'furore(\\b)': '$1furor$2',
Line 534 ⟶ 537:
 
// 'f' words
//'(S|s)ulfur': '$1$2ulphur', //disabled per WP:ARTCONARTCOM
 
'any more': '$1anymore',
Line 551 ⟶ 554:
'honour(abl[ey]|ed|ing|s|\\b)': '$1honor$2',
'(endeav|lab)our(e[dr]|ing|s|\\W)': '$1$2or$3',
'moul(dy?d|t)(ed|ing|s|\\W)': '$1mol$2$3',
 
// '~re~' words
Line 557 ⟶ 560:
'(centi|kilo|micro|milli|nano|-|hundred\\s|\\d\\s)metre': '$1$2meter',
'(centi|milli|deci|pico|hecto|\\b)litre(s?\\b)': '$1$2liter$3',
'(peda|mono|syna)gogue(s?\\W)': '$1$2gog$3', //rem "demagog"; rem "synagog" and "pedagog" June 2025
// '(\\W[a-zA-Z]{3,12}[^s\\b])iz(e[drs]?|ation(s|al|)|ing)': '$1$2is$3',
 
// '~l' words where the 'l' doubles as past and present participle
'((?:\\w{3,9}[-–—]|)(?:fu|pan))ell(e[dr]|ist|ing)': '$1$2el$3',
'(bev|canc|couns|fu|jew|lab|lev|marv|mod|pan|prop|quarr|rev|sign|trav)ell?(e[dr]|ing)': '$1$2el$3',
'([a-zA-Z]{2,8}[^eglpstz ])ell?(e[dr]|est|ing)(\\b)': '$1$2el$3$4', // added 'p' to avoid changing of "misspelling"
// '(\\bexceldi|enthr|equ|initi|journ|riv|sign|tot)((all?:(e[dr]|est|ing)\\b)': '$1l1$2al$23', // "excellingappalled/excelled"appalling is USalso EnglishAmerican
// '\\b(appal|excel)((?:e[dr]|est|ing)\\b)': '$1l$2', // "excelling/excelled" is US English
 
'(di|enthr|equ|initi|journ|riv|sign|tot)all(e[dr]|ing)': '$1$2al$3' // appalled/appalling is also American
};
 
Line 591 ⟶ 594:
 
// '~logu~' words
'([^\\w\\d\\-\\/])(ana|cata|epi|homo|mono|pro)logu(ed|ing)': '$1$2log$3', // removing "dialogue" per user:coffeeandcrumbs (21/8/2018) and epilog per User:DemonDays64
'([^\\w\\d\\-\\/])(ana|cata|epi|homo|mono|pro)logue(s?\\b)': '$1$2log$3', // removing "dialogue" per user:coffeeandcrumbs
 
// '~l' words where the 'l' doubles as past and present participle
Line 600 ⟶ 603:
 
'([ \\|\\[\\*])aeroplane': '$1airplane',
// '([ \\|\\[\\*])\\b[Mm](um|om(my|))\\b': '$1mother', // disabled 10 June – frequent false positives
 
};
 
Line 615 ⟶ 617:
 
// remove US spelling from {{convert}} template output
// editor.replace(/({{convert\|[^}]*)\|(?:sp|spell)[ ]*=[ ]*us/gi, '$1');
// editor.replace(/({{(?:in|ft|yd|[mck]?m)[23]?[ ]+to[ ]+[^}]*)\|(?:sp|spell)[ ]*=[ ]*(?:American|us)/gi, '$1');
// editor.replace(/({{(?:pd|pop[ ]+density[ ])+(?:km|mi)2[^}]*)\|(?:sp|spell)[ ]*=[ ]*(?:American|us)/gi, '$1');
 
// remove ambiguous dollar sign; rem incidental intervening space
editor.replace(/$ ?(\d)/g, '$$1');
 
// reconvert special cases
}
Line 655 ⟶ 656:
 
ohc_ENGVAR_insert_template(editor, variation);
ohc_Reorder_template(editor)
ohc_ENGVAR_edit_summary(editor, variation);
 
Line 735 ⟶ 737:
/** ------------------------------------------------------------------------ **/
 
function ohc_ENGVAR_insert_templatemonth_year(editor, variation) {
const monthMap = [
// Add a template to the article identifying English variant
'January', 'February', 'March', 'April', 'May', 'June',
// Build a string with "Month YYYY"
'July', 'August', 'September', 'October', 'November', 'December'
var date_object = new Date();
];
var month_map = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const now = new Date();
var date_string = month_map[date_object.getMonth()] + ' ' + date_object.getFullYear();
return `${monthMap[now.getMonth()]} ${now.getFullYear()}`;
}
 
function remove_deprecated(editor) {
var template_string;
const deprecatedMatcher = /\{\{\s*Engvar(?:A|C)[^}]*\}\}\s*/gi; // Only remove EngvarA and EngvarC
switch (variation) {
editor.replace(deprecatedMatcher, '');
case 'B':
return editor;
template_string = '{{EngvarB|date=' + date_string + '}}';
}
break;
case 'O':
template_string = '{{Use Oxford spelling|date=' + date_string + '}}';
break;
case 'C':
template_string = '{{Use Canadian English|date=' + date_string + '}}';
break;
case 'U':
template_string = '{{Use American English|date=' + date_string + '}}';
break;
default:
return;
}
 
function ohc_ENGVAR_insert_template(editor, variant) {
// Matcher for every English variation template
editor = remove_deprecated(editor);
var template_matcher = new RegExp('{{(?:[_ ]*Template[_ ]*:)?[_ ]*(?:Use[_ ]+(?:British(?:[_ ]+\\(?(?:Oxford|IUPAC)\\)?)?|X|Hong Kong|European|Commonweal?th|Scottish|American|Canadian|Indian|Irish|Hiberno|Jamaican|Nigerian|Bangladeshi|Pakistani|Singapore(?:an)?|South African|Trinidadian|New Zealand|US|U.S.|Oxford|IUPAC)(?:(?:[_ ]+|-)English)?(?:[_ ]+Oxford)?(?:[_ ]+spelling)?|Engvar(?:Au?|Ox?|[BC])|U?BE|EB|Use BrE|En-(?:US|GB(?:-oxendict)?|CA|NZ))(?:/sandbox)?[_ ]*(?:|\\|[ ]*date[ ]*=[^{}|]*)[ ]*}}', 'gi'); //removed Australian 24/9/19
 
const variantClass = {
'U': [
'Use American English', 'Use Liberian English', 'Use Philippine English'
],
'C': ['Use Canadian English'],
'O': ['Use Oxford spelling'],
'B': [
'Use Commonwealth English spelling', 'Use British English', 'Use Antiguan and Barbudan English',
'Use Australian English', 'Use Bangladeshi English', 'Use Ghanaian English', 'Use Hiberno-English',
'Use Hong Kong English', 'Use Indian English', 'Use Jamaican English', 'Use Kenyan English',
'Use Malaysian English', 'Use New Zealand English', 'Use Nigerian English',
'Use Pakistani English', 'Use Singapore English', 'Use Sri Lankan English', 'Use South African English',
'Use Trinidad and Tobago English', 'Use Ugandan English'
]
};
 
const allTemplates = new Set(Object.values(variantClass).flat());
 
const templatePattern = Array.from(allTemplates)
.map(name => name.replace(/[\s\-]/g, '[\\s\\-]+').replace(/([()])/g, '\\$1'))
.join('|');
 
const allVarTemplatesMatcher = new RegExp(
`\{\{\s*(?:Template\s*:)?\s*(${templatePattern})(\s*\|[^}]*)?\}\}`,
'gi'
);
 
let foundVariantTemplate = false;
let engvarBFound = false;
const currentDate = month_year();
const allowedTemplates = variantClass[variant] || [];
 
// Process existing templates
editor.replace(allVarTemplatesMatcher, function (match, templateName, paramStr = '') {
const currentTemplate = templateName.trim();
 
if (!allowedTemplates.includes(currentTemplate)) {
console.log(`Removing non-matching variant template: {{${currentTemplate}}}`);
return '';
}
 
foundVariantTemplate = true;
 
let params = paramStr.split('|').map(p => p.trim()).filter(Boolean);
let paramMap = {};
 
for (const p of params) {
const [k, v] = p.split('=').map(s => s.trim());
paramMap[k] = v || true;
}
 
paramMap['date'] = currentDate;
 
const paramString = Object.entries(paramMap)
.map(([k, v]) => v === true ? k : `${k}=${v}`)
.join('|');
 
return `{{${templateName}|${paramString}}}`;
});
 
// Handle EngvarB specifically
const engvarBMatcher = /\{\{\s*EngvarB\s*(\|[^}]*)?\}\}/gi;
 
if (variant === 'B') {
editor.replace(engvarBMatcher, function (match, paramStr = '') {
engvarBFound = true;
 
let params = paramStr.split('|').map(p => p.trim()).filter(Boolean);
let paramMap = {};
 
for (const p of params) {
const [k, v] = p.split('=').map(s => s.trim());
paramMap[k] = v || true;
}
 
paramMap['date'] = currentDate;
 
const paramString = Object.entries(paramMap)
.map(([k, v]) => v === true ? k : `${k}=${v}`)
.join('|');
 
return `{{EngvarB|${paramString}}}`;
});
} else {
// Remove EngvarB when switching to non-Commonwealth variants
editor.replace(engvarBMatcher, '');
}
 
// Insert new template if needed
if (!foundVariantTemplate && !(variant === 'B' && engvarBFound)) {
let newTemplate = '';
 
if (variant === 'B') {
newTemplate = `{{Use British English|date=${currentDate}}}`;
} else if (allowedTemplates.length > 0) {
newTemplate = `{{${allowedTemplates[0]}|date=${currentDate}}}`;
}
 
if (newTemplate) {
const shortdescMatcher = /\{\{\s*short\s+description\s*\|[^}]+\}\}\s*(\r?\n)?/i;
 
if (editor.contains(shortdescMatcher)) {
editor.replace(shortdescMatcher, function (m) {
return m + newTemplate + '\n';
});
} else {
editor.prepend(newTemplate + '\n');
}
}
}
}
 
function ohc_Reorder_template(editor) {
// Ensure Short description appears before EngvarB if both exist
editor.replace(/(\{\{Use \w+ (?:spelling|English) ?\|[^}]+\}\}\n?)((?:\{\{[\S\s]+\}\}\n?)+|)(\{\{[Ss]hort description ?\|[^}]+\}\}(<!--[^>{}]+-->|)\n?)/g, '$3$2$1');
 
// If not already tagged
if (!editor.contains(template_matcher)) {
// Prepend new template
editor.prepend(template_string + '\r\n');
} else {
// Replace old template
editor.replace(template_matcher, template_string);
}
}
 
Line 778 ⟶ 878:
switch (variation) {
case 'B':
variation_string = 'EngvarB by [[User:Ohconfucius/script/EngvarB|script]]';
break;
case 'O':
variation_string = 'Oxford spellingby [[User:Ohconfucius/script/EngvarB|script]]';
break;
case 'C':
variation_string = 'Canadian Englishby [[User:Ohconfucius/script/EngvarB|script]]';
break;
case 'U':
variation_string = 'American Englishby [[User:Ohconfucius/script/EngvarB|script]]';
break;
default:
Line 793 ⟶ 893:
}
 
doaction('diff');
editor.appendEditSummary(variation_string);
editor.appendEditSummary(variation_string);
}
// </syntaxhighlight>