Changing conditional check to enable also replacements again

This commit is contained in:
Jean Viscogliosi-Pate 2025-02-22 02:11:03 -08:00
parent b815eb9da7
commit 7147c8e53b
1 changed files with 54 additions and 45 deletions

View File

@ -30,7 +30,7 @@ function replaceAll() {
replacePronouns(options);
}
const isAlsoSet = !options.also === undefined;
const isAlsoSet = !(options.also === undefined);
if (isAlsoSet) {
replaceAlso(options);
}
@ -58,20 +58,20 @@ function replaceVerbs(options) {
/* This could be made faster if I give premade regexp expressions for each, but I don't want to do that! */
function replacePronouns(options) {
let pronouns = getPronouns(options.preset, options.other);
replacePronounSet("Prn/s", "prn/s", pronouns["subjective"]);
replacePronounSet("Prn/o", "prn/o", pronouns["objective"]);
replacePronounSet("Prn/p", "prn/p", pronouns["possessive"]);
replacePronounSet("Prn/a", "prn/a", pronouns["adjective"]);
replacePronounSet("Prn/r", "prn/r", pronouns["reflexive"]);
replacePronounSet("Prn/H", "prn/H", pronouns["honorific-abbr"]);
replacePronounSet("Prn/h", "prn/h", pronouns["honorific"]);
replacePronounSet("Prn/N", "prn/N", pronouns["adult"]);
replacePronounSet("Prn/n", "prn/n", pronouns["youth"]);
replacePronounSet("Prn/F", "prn/f", pronouns["parent"]);
replacePronounSet("Prn/f", "prn/f", pronouns["child"]);
replacePronounSet("Prn/k", "prn/k", pronouns["sibling"]);
replacePronounSet("Prn/m", "prn/m", pronouns["married"]);
replacePronounSet("Prn/d", "prn/d", pronouns["dating"]);
walk(document.body, /([Pp])rn\/s/, pronouns["subjective"], pronounMethod);
walk(document.body, /([Pp])rn\/o/, pronouns["objective"], pronounMethod);
walk(document.body, /([Pp])rn\/p/, pronouns["possessive"], pronounMethod);
walk(document.body, /([Pp])rn\/a/, pronouns["adjective"], pronounMethod);
walk(document.body, /([Pp])rn\/r/, pronouns["reflexive"], pronounMethod);
walk(document.body, /([Pp])rn\/H/, pronouns["honorific-abbr"], pronounMethod);
walk(document.body, /([Pp])rn\/h/, pronouns["honorific"], pronounMethod);
walk(document.body, /([Pp])rn\/N/, pronouns["adult"], pronounMethod);
walk(document.body, /([Pp])rn\/n/, pronouns["youth"], pronounMethod);
walk(document.body, /([Pp])rn\/F/, pronouns["parent"], pronounMethod);
walk(document.body, /([Pp])rn\/f/, pronouns["child"], pronounMethod);
walk(document.body, /([Pp])rn\/k/, pronouns["sibling"], pronounMethod);
walk(document.body, /([Pp])rn\/m/, pronouns["married"], pronounMethod);
walk(document.body, /([Pp])rn\/d/, pronouns["dating"], pronounMethod);
}
/* Gets pronouns based on the provided key (or user input) */
@ -176,45 +176,39 @@ function getPronouns(key, other) {
}
}
/* Replaces pronoun keys in capital and lowercase with their equivalent pronouns */
function replacePronounSet(keyCapital, key, pronoun) {
escapeAndReplace(keyCapital, capitalize(pronoun), true);
escapeAndReplace(key, pronoun, true);
}
/* Replaces all point-of-view keys */
function replacePov(options) {
let pronouns;
if (options.pov == "third") {
pronouns = getPronouns(options.preset, options.other);
replacePronounSet("Pov/S", "pov/S", options.name);
replacePronounSet("Pov/O", "pov/O", options.name);
replacePronounSet("Pov/P", "pov/P", options.name + "'s");
replacePronounSet("Pov/A", "pov/A", options.name + "'s");
replacePronounSet("Pov/R", "pov/R", options.name + "'s self");
walk(document.body, /([Pp])ov\/S/, options.name, pronounMethod);
walk(document.body, /([Pp])ov\/O/, options.name, pronounMethod);
walk(document.body, /([Pp])ov\/P/, options.name + "s", pronounMethod);
walk(document.body, /([Pp])ov\/A/, options.name + "s", pronounMethod);
walk(document.body, /([Pp])ov\/R/, options.name + "s self", pronounMethod);
} else {
pronouns = getPronouns(options.pov, null);
replacePronounSet("Pov/S", "pov/S", pronouns["subjective"]);
replacePronounSet("Pov/O", "pov/O", pronouns["objective"]);
replacePronounSet("Pov/P", "pov/P", pronouns["possessive"]);
replacePronounSet("Pov/A", "pov/A", pronouns["adjective"]);
replacePronounSet("Pov/R", "pov/R", pronouns["reflexive"]);
walk(document.body, /([Pp])ov\/S/, pronouns["subjective"], pronounMethod);
walk(document.body, /([Pp])ov\/O/, pronouns["objective"], pronounMethod);
walk(document.body, /([Pp])ov\/P/, pronouns["possessive"], pronounMethod);
walk(document.body, /([Pp])ov\/A/, pronouns["adjective"], pronounMethod);
walk(document.body, /([Pp])ov\/R/, pronouns["reflexive"], pronounMethod);
}
replacePronounSet("Pov/s", "pov/s", pronouns["subjective"]);
replacePronounSet("Pov/o", "pov/o", pronouns["objective"]);
replacePronounSet("Pov/p", "pov/p", pronouns["possessive"]);
replacePronounSet("Pov/a", "pov/a", pronouns["adjective"]);
replacePronounSet("Pov/r", "pov/r", pronouns["reflexive"]);
walk(document.body, /([Pp])ov\/s/, pronouns["subjective"], pronounMethod);
walk(document.body, /([Pp])ov\/o/, pronouns["objective"], pronounMethod);
walk(document.body, /([Pp])ov\/p/, pronouns["possessive"], pronounMethod);
walk(document.body, /([Pp])ov\/a/, pronouns["adjective"], pronounMethod);
walk(document.body, /([Pp])ov\/r/, pronouns["reflexive"], pronounMethod);
}
/* Replaces plural point-of-view keys */
function replacePlv(options) {
let pronouns = getPronouns(options.pov + "-plural", null);
replacePronounSet("Plv/s", "plv/s", pronouns["subjective"]);
replacePronounSet("Plv/o", "plv/o", pronouns["objective"]);
replacePronounSet("Plv/p", "plv/p", pronouns["possessive"]);
replacePronounSet("Plv/a", "plv/a", pronouns["adjective"]);
replacePronounSet("Plv/r", "plv/r", pronouns["reflexive"]);
walk(document.body, /([Pp])lv\/s/, pronouns["subjective"], pronounMethod);
walk(document.body, /([Pp])lv\/o/, pronouns["objective"], pronounMethod);
walk(document.body, /([Pp])lv\/p/, pronouns["possessive"], pronounMethod);
walk(document.body, /([Pp])lv\/a/, pronouns["adjective"], pronounMethod);
walk(document.body, /([Pp])lv\/r/, pronouns["reflexive"], pronounMethod);
}
/* Replaces additional terms specified by the user */
@ -226,14 +220,14 @@ function replaceAlso(options) {
/* Replaces exception statements by POV */
function replaceExceptions(options) {
const searchTerm = /exc\/([^\/]*)\/([^\/]*)\/ig/;
const searchTerm = /exc\/([^\/]*)\/([^\/]*)\//i;
walk(document.body, searchTerm, options, exceptionMethod);
}
/* Replaces cut statements with cuts */
function replaceCuts() {
const searchTerm = /cut\/([^\/]+)\/(-?[\d]+)\/ig/;
const searchTerm = /cut\/([^\/]+)\/(-?[\d]+)\//i;
walk(document.body, searchTerm, null, cutMethod);
}
@ -271,11 +265,26 @@ function walk(node, searchTerm, replaceValue, replaceMethod) {
}
}
/* Replaces all of the provided terms in a node */
/* Replaces every instance of the provided terms */
function directMethod(node, searchTerm, replaceValue) {
node.nodeValue = node.nodeValue.replaceAll(searchTerm, replaceValue);
}
/* Replaces all pronouns */
function pronounMethod(node, searchTerm, pronoun) {
const match = node.nodeValue.match(searchTerm);
if (match == null) { return; }
const isCapital = /P/.test(match[1]);
let replaceValue = pronoun;
if (isCapital) {
replaceValue = capitalize(replaceValue);
}
node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue);
pronounMethod(node, searchTerm, pronoun);
}
/* Replaces all verbs, not used for third-person plural */
function verbMethod(node, searchTerm, options) {
const match = node.nodeValue.match(searchTerm);
@ -354,7 +363,7 @@ function exceptionMethod(node, searchTerm, options) {
}
node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue);
cutMethod(node, searchTerm, options);
exceptionMethod(node, searchTerm, options);
}
/* Returns the input word, capitalized */