Adding capitalization feature, resolves #30

This commit is contained in:
Jean Viscogliosi-Pate 2025-06-26 18:55:35 -07:00
parent 0f716a4332
commit 94b04337be
1 changed files with 37 additions and 7 deletions

View File

@ -40,6 +40,7 @@ function replaceAll() {
if (isAllSet) {
replaceExceptions(options);
replaceCuts();
replaceCapitals();
}
});
}
@ -226,13 +227,18 @@ function replaceExceptions(options) {
walk(document.body, searchTerm, options, exceptionMethod);
}
/* Replaces cut statements with cuts */
function replaceCuts() {
const searchTerm = /cut\/([^\/]+)\/(-?[\d]+)\//i;
walk(document.body, searchTerm, null, cutMethod);
}
/* Replaces cut statements with cuts */
function replaceCapitals() {
const searchTerm = /(cap|Cap|CAP)\/([^\/]+)\//i;
walk(document.body, searchTerm, null, capitalMethod);
}
/* Turns a term into regexp format and uses that to replace it */
function escapeAndReplace(searchTerm, replaceValue, caseSensitive) {
let searchTermEscaped = searchTerm.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
@ -331,6 +337,22 @@ function verbMethod(node, searchTerm, options) {
verbMethod(node, searchTerm, options);
}
/* Allows for first, second, and third-person POV to read differently */
function exceptionMethod(node, searchTerm, options) {
const match = node.nodeValue.match(searchTerm);
if (match == null) { return; }
let replaceValue;
if (options.pov == "third") {
replaceValue = match[2];
} else {
replaceValue = match[1];
}
node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue);
exceptionMethod(node, searchTerm, options);
}
/* Allows for custom words to be cut off */
function cutMethod(node, searchTerm, unused) {
const match = node.nodeValue.match(searchTerm);
@ -355,20 +377,28 @@ function cutMethod(node, searchTerm, unused) {
cutMethod(node, searchTerm, unused);
}
/* Allows for custom words to be cut off */
function exceptionMethod(node, searchTerm, options) {
/* Allows for custom words to be capitalized */
function capitalMethod(node, searchTerm, unused) {
const match = node.nodeValue.match(searchTerm);
if (match == null) { return; }
const style = match[1];
const target = match[2];
const isLowerCase = /c/.test(style.slice(0,1)); // cap
let replaceValue;
if (options.pov == "third") {
replaceValue = match[2];
if (isLowerCase) {
replaceValue = target.toLowerCase();
} else {
replaceValue = match[1];
const isStartCase = /a/.test(style.slice(1,2)); // Cap
if (isStartCase) {
replaceValue = capitalize(target);
} else { // CAP
replaceValue = target.toUpperCase();
}
}
node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue);
exceptionMethod(node, searchTerm, options);
capitalMethod(node, searchTerm, unused);
}
/* Returns the input word, capitalized */