parent
7c65aa4eda
commit
509001f004
|
@ -38,20 +38,25 @@ function replaceAll() {
|
||||||
|
|
||||||
/* Replaces all instances of "Y/n" with the user's name */
|
/* Replaces all instances of "Y/n" with the user's name */
|
||||||
function replaceName(options) {
|
function replaceName(options) {
|
||||||
const search_term = /\by\/n\b|\(y\/n\)|\[y\/n\]/ig;
|
const searchTerm = /\by\/n\b|\(y\/n\)|\[y\/n\]/ig;
|
||||||
walk(document.body, search_term, options["name"], directMethod);
|
walk(document.body, searchTerm, options["name"], directMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Replaces all verb keys */
|
/* Replaces all verb keys */
|
||||||
function replaceVerbs(options) {
|
function replaceVerbs(options) {
|
||||||
const isPluralThird = options.pov == "third" && getPronouns(options.preset, options.other).plurality == "plural";
|
const isPlural = getPronouns(options.preset, options.other).plurality == "plural";
|
||||||
if (isPluralThird) {
|
if (isPlural) {
|
||||||
const search_term = /([Pp])(?:rn|ov)\/S [Vv]rb\/([\w\s]+)\/([\w\s]+)\/|[Vv]rb\/([\w\s]+)\/([\w\s]+)\/ ([Pp])(?:rn|ov)\/S/;
|
walk(document.body, /([Vv])r([Nn])\/([\w\s]+)\/([\w\s]+)\//, options, pluralPrnVerbMethod);
|
||||||
walk(document.body, search_term, options, pluralThirdVerbMethod);
|
} else {
|
||||||
|
walk(document.body, /([Vv])r[Nn]\/([\w\s]+)\/([\w\s]+)\//, options, prnVerbMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
const search_term = /vrb\/([\w\s]+)\/([\w\s]+)\//;
|
const isPluralThird = isPlural && options.pov == "third";
|
||||||
walk(document.body, search_term, options, verbMethod);
|
if (isPluralThird) {
|
||||||
|
walk(document.body, /([Vv])r([Bb])\/([\w\s]+)\/([\w\s]+)\//, options, pluralThirdVerbMethod);
|
||||||
|
} else {
|
||||||
|
walk(document.body, /([Vv])r[Bb]\/([\w\s]+)\/([\w\s]+)\//, options, verbMethod);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Replaces all pronoun keys */
|
/* Replaces all pronoun keys */
|
||||||
|
@ -225,21 +230,21 @@ function replaceAlso(options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Turns a term into regexp format and uses that to replace it */
|
/* Turns a term into regexp format and uses that to replace it */
|
||||||
function escapeAndReplace(search_term, replace_value, case_sensitive) {
|
function escapeAndReplace(searchTerm, replaceValue, caseSensitive) {
|
||||||
let search_term_escaped = search_term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
let searchTermEscaped = searchTerm.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
||||||
const flags = case_sensitive ? "g" : "ig"
|
const flags = caseSensitive ? "g" : "ig"
|
||||||
if (search_term_escaped[0].match(/[a-z]/i)) {
|
if (searchTermEscaped[0].match(/[a-z]/i)) {
|
||||||
search_term_escaped = `\\b${search_term_escaped}`
|
searchTermEscaped = `\\b${searchTermEscaped}`
|
||||||
}
|
}
|
||||||
if (search_term_escaped[search_term_escaped.length - 1].match((/[a-z]/i))) {
|
if (searchTermEscaped[searchTermEscaped.length - 1].match((/[a-z]/i))) {
|
||||||
search_term_escaped = `${search_term_escaped}\\b`
|
searchTermEscaped = `${searchTermEscaped}\\b`
|
||||||
}
|
}
|
||||||
const search_term_regexp = new RegExp(search_term_escaped, flags)
|
const searchTermRegexp = new RegExp(searchTermEscaped, flags)
|
||||||
walk(document.body, search_term_regexp, replace_value, directMethod);
|
walk(document.body, searchTermRegexp, replaceValue, directMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Traverses a node and its children to run the provided replacement method */
|
/* Traverses a node and its children to run the provided replacement method */
|
||||||
function walk(node, search_term, replace_value, replaceMethod) {
|
function walk(node, searchTerm, replaceValue, replaceMethod) {
|
||||||
if (node.contentEditable == "true" || node.type == "textarea" || node.type == "input") { return; }
|
if (node.contentEditable == "true" || node.type == "textarea" || node.type == "input") { return; }
|
||||||
let child, next;
|
let child, next;
|
||||||
switch (node.nodeType) {
|
switch (node.nodeType) {
|
||||||
|
@ -249,58 +254,143 @@ function walk(node, search_term, replace_value, replaceMethod) {
|
||||||
child = node.firstChild;
|
child = node.firstChild;
|
||||||
while (child) {
|
while (child) {
|
||||||
next = child.nextSibling;
|
next = child.nextSibling;
|
||||||
walk(child, search_term, replace_value, replaceMethod);
|
walk(child, searchTerm, replaceValue, replaceMethod);
|
||||||
child = next;
|
child = next;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 3: /* TEXT_NODE */
|
case 3: /* TEXT_NODE */
|
||||||
replaceMethod(node, search_term, replace_value);
|
replaceMethod(node, searchTerm, replaceValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Replaces all of the provided terms in a node */
|
/* Replaces all of the provided terms in a node */
|
||||||
function directMethod(node, search_term, replace_value) {
|
function directMethod(node, searchTerm, replaceValue) {
|
||||||
node.nodeValue = node.nodeValue.replaceAll(search_term, replace_value);
|
node.nodeValue = node.nodeValue.replaceAll(searchTerm, replaceValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Replaces all verbs, not used for third-person plural */
|
/* Replaces all verbs, not used for third-person plural */
|
||||||
/* TODO Is the "options" variable here readable? It's not replace_value and that seems bad */
|
/* TODO Is the "options" variable here readable? It's not replaceValue and that seems bad */
|
||||||
function verbMethod(node, search_term, options) {
|
function verbMethod(node, searchTerm, options) {
|
||||||
let match = node.nodeValue.match(search_term);
|
let match = node.nodeValue.match(searchTerm);
|
||||||
if (match == null) { return; }
|
if (match == null) { return; }
|
||||||
const verb = match[1];
|
const isCapital = /V/.test(match[1]);
|
||||||
const tense = match[2].toUpperCase().replaceAll(" ", "_");
|
const verb = match[2];
|
||||||
const replace_value = verbsHelper.getConjugation(verbsData, verb, tense, getPovIndex(options));
|
let tense = getTense(match[3]);
|
||||||
node.nodeValue = node.nodeValue.replace(search_term, replace_value);
|
const isTenseProvided = tense != null;
|
||||||
verbMethod(node, search_term, options);
|
if (!isTenseProvided) {
|
||||||
|
tense = "PAST";
|
||||||
|
}
|
||||||
|
|
||||||
|
let replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, getPovIndex(options));
|
||||||
|
|
||||||
|
if (isCapital) {
|
||||||
|
replaceValue = capitalize(replaceValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isTenseProvided) {
|
||||||
|
node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue);
|
||||||
|
} else {
|
||||||
|
const searchTermShortVerb = /([Vv])r[Bb]\/([\w\s]+)\//
|
||||||
|
node.nodeValue = node.nodeValue.replace(searchTermShortVerb, replaceValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
verbMethod(node, searchTerm, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Replaces all verbs, used only for third-person plural */
|
/* Replaces all verbs, used only for third-person plural */
|
||||||
/* TODO This is meant to be able to replace in both subject-object and object-subject order, but it's only the first for now. */
|
function pluralThirdVerbMethod(node, searchTerm, options) {
|
||||||
function pluralThirdVerbMethod(node, search_term, options) {
|
let match = node.nodeValue.match(searchTerm);
|
||||||
let match = node.nodeValue.match(search_term);
|
|
||||||
if (match == null) { return; }
|
if (match == null) { return; }
|
||||||
const before = match[1];
|
const isCapital = /V/.test(match[1]);
|
||||||
const after = match[4];
|
const isNamedActor = /B/.test(match[2]);
|
||||||
const wasBefore = !(before === undefined);
|
const verb = match[3];
|
||||||
let verb, tense;
|
let tense = getTense(match[4]);
|
||||||
if (wasBefore) {
|
const isTenseProvided = tense != null;
|
||||||
verb = match[2];
|
if (!isTenseProvided) {
|
||||||
tense = match[3];
|
tense = "PAST";
|
||||||
} else {
|
|
||||||
verb = match[5];
|
|
||||||
tense = match[6];
|
|
||||||
}
|
}
|
||||||
tense = tense.toUpperCase().replaceAll(" ", "_");
|
|
||||||
const replace_verb = verbsHelper.getConjugation(verbsData, verb, tense, 2);
|
let replaceValue;
|
||||||
let replace_value;
|
if (isNamedActor) {
|
||||||
if (wasBefore) {
|
replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, 2);
|
||||||
replace_value = options["name"] + " " + replace_verb;
|
|
||||||
} else {
|
} else {
|
||||||
replace_value = replace_verb + " " + options["name"];
|
replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, getPovIndex(options));
|
||||||
}
|
}
|
||||||
node.nodeValue = node.nodeValue.replace(search_term, replace_value);
|
|
||||||
pluralThirdVerbMethod(node, search_term, options);
|
if (isCapital) {
|
||||||
|
replaceValue = capitalize(replaceValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isTenseProvided) {
|
||||||
|
node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue);
|
||||||
|
} else {
|
||||||
|
const searchTermShortVerb = /([Vv])r([Bb])\/([\w\s]+)\//
|
||||||
|
node.nodeValue = node.nodeValue.replace(searchTermShortVerb, replaceValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
pluralThirdVerbMethod(node, searchTerm, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Replaces verbs by pronoun rather than POV, not used for plural */
|
||||||
|
function prnVerbMethod(node, searchTerm, options) {
|
||||||
|
let match = node.nodeValue.match(searchTerm);
|
||||||
|
if (match == null) { return; }
|
||||||
|
const isCapital = /V/.test(match[1]);
|
||||||
|
const verb = match[2];
|
||||||
|
let tense = getTense(match[3]);
|
||||||
|
const isTenseProvided = tense != null;
|
||||||
|
if (!isTenseProvided) {
|
||||||
|
tense = "PAST";
|
||||||
|
}
|
||||||
|
|
||||||
|
let replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, 2);
|
||||||
|
|
||||||
|
if (isCapital) {
|
||||||
|
replaceValue = capitalize(replaceValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isTenseProvided) {
|
||||||
|
node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue);
|
||||||
|
} else {
|
||||||
|
const searchTermShortVerb = /([Vv])r[Bb]\/([\w\s]+)\//
|
||||||
|
node.nodeValue = node.nodeValue.replace(searchTermShortVerb, replaceValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
prnVerbMethod(node, searchTerm, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Replaces verbs by pronoun rather than POV, used only for plural */
|
||||||
|
function pluralPrnVerbMethod(node, searchTerm, options) {
|
||||||
|
let match = node.nodeValue.match(searchTerm);
|
||||||
|
if (match == null) { return; }
|
||||||
|
const isCapital = /V/.test(match[1]);
|
||||||
|
const isNamedActor = /N/.test(match[2]);
|
||||||
|
const verb = match[3];
|
||||||
|
let tense = getTense(match[4]);
|
||||||
|
const isTenseProvided = tense != null;
|
||||||
|
if (!isTenseProvided) {
|
||||||
|
tense = "PAST";
|
||||||
|
}
|
||||||
|
|
||||||
|
let replaceValue;
|
||||||
|
if (isNamedActor) {
|
||||||
|
replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, 2);
|
||||||
|
} else {
|
||||||
|
replaceValue = verbsHelper.getConjugation(verbsData, verb, tense, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isCapital) {
|
||||||
|
replaceValue = capitalize(replaceValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isTenseProvided) {
|
||||||
|
node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue);
|
||||||
|
} else {
|
||||||
|
const searchTermShortVerb = /([Vv])r([Bb])\/([\w\s]+)\//
|
||||||
|
node.nodeValue = node.nodeValue.replace(searchTermShortVerb, replaceValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
pluralPrnVerbMethod(node, searchTerm, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns the input word, capitalized */
|
/* Returns the input word, capitalized */
|
||||||
|
@ -309,7 +399,6 @@ function capitalize(word) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Determines the index for point-of-view to be used when conjugating verbs */
|
/* Determines the index for point-of-view to be used when conjugating verbs */
|
||||||
/* TODO Did I ever implement third-person plural? */
|
|
||||||
function getPovIndex(options) {
|
function getPovIndex(options) {
|
||||||
switch (options.pov) {
|
switch (options.pov) {
|
||||||
case "first":
|
case "first":
|
||||||
|
@ -325,4 +414,39 @@ function getPovIndex(options) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Gets the verb tense from a regexp match */
|
||||||
|
function getTense(match) {
|
||||||
|
const tense = match.toUpperCase().replaceAll(" ", "_");
|
||||||
|
const tenses = [
|
||||||
|
// SIMPLE
|
||||||
|
'SIMPLE_PAST',
|
||||||
|
'PAST',
|
||||||
|
'SIMPLE_PRESENT',
|
||||||
|
'PRESENT',
|
||||||
|
'SIMPLE_FUTURE',
|
||||||
|
'FUTURE',
|
||||||
|
// PROGRESSIVE
|
||||||
|
'PROGRESSIVE_PAST',
|
||||||
|
'PROGRESSIVE_PRESENT',
|
||||||
|
'PROGRESSIVE_FUTURE',
|
||||||
|
// PERFECT
|
||||||
|
'PERFECT_PAST',
|
||||||
|
'PERFECT_PRESENT',
|
||||||
|
'PERFECT_FUTURE',
|
||||||
|
// PERFECT PROGRESSIVE
|
||||||
|
'PERFECT_PROGRESSIVE_PAST',
|
||||||
|
'PERFECT_PROGRESSIVE_PRESENT',
|
||||||
|
'PERFECT_PROGRESSIVE_FUTURE',
|
||||||
|
// PARTICIPLE
|
||||||
|
'PARTICIPLE_PRESENT',
|
||||||
|
'PARTICIPLE_PAST',
|
||||||
|
];
|
||||||
|
|
||||||
|
if (tenses.indexOf(tense) == -1) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return tense;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
replaceAll();
|
replaceAll();
|
||||||
|
|
Loading…
Reference in New Issue