From 445f38b4ee84cce7e1523d4650314161b9bf3aa0 Mon Sep 17 00:00:00 2001 From: Jean Date: Fri, 21 Feb 2025 21:20:27 -0800 Subject: [PATCH] Adding two-term exceptions, fixes #25 --- src/replace-words.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/replace-words.js b/src/replace-words.js index 65b6847..b9b6990 100644 --- a/src/replace-words.js +++ b/src/replace-words.js @@ -14,6 +14,7 @@ function replaceAll() { const isPronounsSet = !(options.preset === undefined || options.preset == ""); const isPovSet = !(options.pov === undefined || options.pov == ""); const isPovLegal = isPovSet && (options.pov != "third" || (isNameSet && isPronounsSet)); + const isAllSet = isNameSet && isPronounsSet && isPovLegal; if (isPovLegal) { replaceVerbs(options); @@ -34,7 +35,10 @@ function replaceAll() { replaceAlso(options); } - replaceCuts(); + if (isAllSet) { + replaceExceptions(options); + replaceCuts(); + } }); } @@ -231,6 +235,13 @@ function replaceAlso(options) { }); } +/* Replaces exception statements by POV */ +function replaceExceptions(options) { + const searchTerm = /[Ee]xc\/([^\/]*)\/([^\/]*)\//; + walk(document.body, searchTerm, options, exceptionMethod); +} + + /* Replaces cut statements with cuts */ function replaceCuts() { const searchTerm = /[Cc]ut\/([^\/]+)\/(-?[\d]+)\//; @@ -425,6 +436,22 @@ function cutMethod(node, searchTerm, unused) { cutMethod(node, searchTerm, unused); } +/* Allows for custom words to be cut off */ +function exceptionMethod(node, searchTerm, options) { + let 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); + cutMethod(node, searchTerm, options); +} + /* Returns the input word, capitalized */ function capitalize(word) { return word.charAt(0).toUpperCase() + word.slice(1);