-
Notifications
You must be signed in to change notification settings - Fork 1
/
Titlecaseconverter.js
221 lines (197 loc) · 7.32 KB
/
Titlecaseconverter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Forked from https://en.wikipedia.org/wiki/User:ZKang123/Titlecaseconverter.js
/* eslint-disable no-alert, no-console */
$( () => {
/**
* Convert titles to title case
*/
function toTitleCase( title ) {
const isAllCaps = title.toUpperCase() === title;
if ( isAllCaps ) {
title = title.toLowerCase();
}
title = title.split( ' ' ).map( ( word, index, array ) => {
// Retain words that are already in uppercase or are special cases
if ( word.toUpperCase() === word || isSpecialCase( word ) ) {
return word;
}
// Retain capitalization for words following certain punctuation marks
if ( index > 0 && /[/;\-,]/.test( array[ index - 1 ] ) ) {
return word.charAt( 0 ).toUpperCase() + word.slice( 1 );
}
// if there's already a capital letter in the word, we probably don't want to change it
const hasUpperCaseLetter = word.toLowerCase() !== word;
if ( hasUpperCaseLetter ) {
return word;
} else if ( shouldCapitalize( word, index, array ) ) {
return word.charAt( 0 ).toUpperCase() + word.slice( 1 ).toLowerCase();
} else {
return word.toLowerCase();
}
} ).join( ' ' );
// Capitalize first letters that occur after punctuation
title = title.replace( / [^A-Za-z][a-z]/g, ( match ) => ' ' + match.slice( 1, 2 ) + match.slice( 2 ).toUpperCase() );
// Capitalize anything after a semicolon
title = title.replace( /;[a-z]/g, ( match ) => ';' + match.slice( 1 ).toUpperCase() );
// Capitalize letters mid-word that occur after hyphens or slashes
title = title.replace( /-[a-z]/g, ( match ) => '-' + match.slice( 1 ).toUpperCase() );
title = title.replace( /\/[a-z]/g, ( match ) => '/' + match.slice( 1 ).toUpperCase() );
return title;
}
/**
* Check if a word is an abbreviation or an exception
*/
function isSpecialCase( word ) {
// Define custom exceptions for abbreviations and specific titles
const exceptions = [ 'MRT', 'LTA', 'S$', 'US$', 'NASA', 'FBI', 'MP3' ]; // Add more exceptions as needed
return exceptions.includes( word ) || /^[A-Z0-9]+$/.test( word );
}
function shouldCapitalize( word, index, array ) {
const alwaysCapitalize = [ 'Me', 'It', 'His', 'If', 'Be', 'Am', 'Is', 'Are', 'Being', 'Was', 'Were', 'Been', 'During', 'Through', 'About', 'Until', 'Below', 'Under' ];
const doNotCapitalize = [ 'a', 'an', 'the', 'and', 'but', 'or', 'nor', 'for', 'yet', 'so', 'as', 'in', 'of', 'on', 'to', 'from', 'into', 'like', 'over', 'with', 'upon' ];
const punctuationMarks = [ '.', ',', ';', ':', '?', '!' ];
const isAbbr = isSpecialCase( word );
const isProperNoun = alwaysCapitalize.includes( word );
const isShortWord = doNotCapitalize.includes( word );
const isFirstOrLastWord = index === 0 || index === array.length - 1;
const isLongPreposition = word.length >= 5;
const isVerb = [ 'be', 'am', 'is', 'are', 'being', 'was', 'were', 'been' ].includes( word.toLowerCase() );
// Preserve capitalization after punctuation marks
if ( index > 0 ) {
const prevWord = array[ index - 1 ];
const lastChar = prevWord.charAt( prevWord.length - 1 );
if ( punctuationMarks.includes( lastChar ) ) {
return true;
}
}
return isAbbr || isFirstOrLastWord || isProperNoun || isLongPreposition || !isShortWord || isVerb;
}
/**
* Convert reference titles in the HTML content
*/
function convertReferenceTitles( htmlString ) {
const citationRegex = /<ref[^>]*>.*?<\/ref>/gi;
const titleRegex = /(\|title=)([^|]+)(\|)/i;
return htmlString.replace( citationRegex, ( match ) => match.replace( titleRegex, ( titleMatch, p1, p2, p3 ) => {
const originalTitle = p2.trim();
const titleCaseTitle = toTitleCase( originalTitle );
return `${ p1 }${ titleCaseTitle }${ p3 }`;
} ) );
}
/**
* Load the script and add the sidebar link
*/
function loadTitleCaseConverter() {
// Create the sidebar link
const sidebarLink = document.createElement( 'li' );
const link = document.createElement( 'a' );
link.innerText = 'Convert Ref Titles to Title Case';
link.href = '#';
link.style.cssText = 'cursor: pointer; color: #0645ad;';
// Add click event listener to the link
link.addEventListener( 'click', ( event ) => {
event.preventDefault();
const textArea = document.querySelector( '#wpTextbox1' );
if ( textArea ) {
const summary = 'Converted reference titles to title case per [[MOS:CT]]';
textArea.value = convertReferenceTitles( textArea.value );
// Set default editing summary
const summaryInput = document.querySelector( '#wpSummary' );
if ( summaryInput && !summaryInput.value.trim() ) {
summaryInput.value = summary;
}
} else {
alert( 'Error: Editing area not found!' );
}
} );
sidebarLink.appendChild( link );
// Add the link to the sidebar (p-tb section)
const sidebar = document.getElementById( 'p-tb' );
const ul = sidebar ? sidebar.querySelector( 'ul' ) : null;
if ( ul ) {
ul.appendChild( sidebarLink );
} else {
alert( 'Error: Sidebar section not found!' );
}
}
function runUnitTests() {
const tests = [
// normal
{
old: 'The South and West lines',
new: 'The South and West Lines'
},
{
old: 'Work on second phase of MRT system ahead of schedule',
new: 'Work on Second Phase of MRT System Ahead of Schedule'
},
{
old: 'Earlier target date for Phase II MRT',
new: 'Earlier Target Date for Phase II MRT'
},
{
old: 'MRT System to be Implemented in Eight Stages',
new: 'MRT System to Be Implemented in Eight Stages'
},
{
old: 'MRT to Bt Batok, Bt Gombak and Choa Chu Kang on Mar 10',
new: 'MRT to Bt Batok, Bt Gombak and Choa Chu Kang on Mar 10'
},
// mid-word hyphens and slashes
{
old: 'Revived, re-opened, newly appreciated',
new: 'Revived, Re-Opened, Newly Appreciated'
},
{
old: "Streetscapes/eldridge street Synagogue;a prayer-filled time capsule from the 1880's",
new: "Streetscapes/Eldridge Street Synagogue;A Prayer-Filled Time Capsule from the 1880's"
},
{
old: 'Phase 2 gets go-ahead to ensure continuity',
new: 'Phase 2 Gets Go-Ahead To Ensure Continuity'
},
// weird mid-word capitalization
{
old: 'Phase 2 gets go-ahead to build iPad',
new: 'Phase 2 Gets Go-Ahead To Build iPad'
},
{
old: 'Phase 2 gets go-ahead to build DataMall',
new: 'Phase 2 Gets Go-Ahead To Build DataMall'
},
// all caps
{
old: 'PHASE 2 GETS GO-AHEAD TO ENSURE CONTINUITY',
new: 'Phase 2 Gets Go-Ahead To Ensure Continuity'
},
// punctuation at beginning of word
{
old: 'She was "amazingly spectacular"',
new: 'She Was "Amazingly Spectacular"'
}
];
let i = 1;
let failures = 0;
for ( const test of tests ) {
const actual = toTitleCase( test.old );
if ( actual !== test.new ) {
console.log( `[Titlecaseconverter.js] Failed unit test ${ i }. Received "${ actual }" instead of "${ test.new }".` );
failures++;
}
i++;
}
if ( !failures ) {
console.log( '[Titlecaseconverter.js] All unit tests passed. Yay.' );
}
}
// Load the script when the page is ready
if ( document.readyState !== 'loading' ) {
loadTitleCaseConverter();
} else {
document.addEventListener( 'DOMContentLoaded', loadTitleCaseConverter );
}
// Put this at the top of your common.js file to run unit tests in the browser devtools console:
// window.TitleCaseConverterUnitTests = true;
if ( window.TitleCaseConverterUnitTests ) {
runUnitTests();
}
} );