Reputation: 333
I'm writing an add-on to help visually highlight certain text in court opinions, legal memos, and law review articles. As an example:
The question is whether section 404(c) of the FSA bars Jackson's 2021 Motion. The answer is yes, because Jackson's 2018 Motion received a “complete review…on the merits.” Section 404(c) states that “[n]o court shall entertain a motion made under [Section 404] to reduce a sentence…if a previous motion made under this section to reduce the sentence was…denied after a complete review of the motion on the merits.” Section 404(c) thus unambiguously “establishes that a defendant can file only one motion for resentencing.” United States v. Smith, 982 F.3d 106, 112 (2d Cir. 2020) (quoting United States v. Jackson, 945 F.3d 315, 321 (5th Cir. 2019)); see also United States v. Mannie, 971 F.3d 1145, 1152 (10th Cir. 2020) (stating that section 404(c) “essentially ensure[s] that offenders only get one bite of the apple”); United States v. Maxwell, 800 Fed. Appx. 373, 376 (6th Cir. 2020) (“The First Step Act gives defendants only one bite at the apple”).
In this text, at the most basic level, I want to highlight quotes in one color:
And parentheticals in another color:
I also want to highlight all start and end parentheses and all semicolons, provided that they're not nested within a parenthetical, as in, e.g., "(quoting United States v. Jackson, 945 F.3d 315, 321 (5th Cir. 2019))."
To me, the most natural solution is, for each paragraph, to get the start and end index of a section that I want to highlight, then to create a Range object based off of that start/end indices, and then to modify the range accordingly. E.g., consider the following text: "Word is a great program. See Word v. Word Perfect, 55 F.3d 55 (19th Cir. 2054) (concluding "Word is superior")." For this text, one set of start/end indices of interest would be [54, 84], which corresponds to "(concluding "Word is superior")."
Because some of the text I'd like to highlight is likely to repeat—words, phrases, punctuation marks—it won't do to just search for the word, because that will pick out a number of false positives. E.g., searching for ")" will pick out the closing parentheses on "(c)," which I don't want to highlight, and the inner closing parentheses on "(quoting United States v. Jackson, 945 F.3d 315, 321 (5th Cir. 2019))," which I also don't want to highlight.
Unfortunately, there seems to be no way to get a Range object by start and end index. I've searched around the Word JS documentation and on Stack Overflow, but to no avail. All similar questions are from several years ago and conclude that it's not possible to select ranges by start and end index, but because several years have passed, it seems possible that things have changed.
That is, my question is: is it possible—and if so, how—to get a Range object by the start and end index of text in the paragraph, as described above?
Some questions I've looked at:
The code I have in place right now, which works for searching for words rather than by indices, is as follows:
async function run() {
Word.run(async (context) => {
const color = Office.context.document.settings.get("parentheticalColor");
const paragraphs = context.document.body.paragraphs;
paragraphs.load("items");
await context.sync();
for (let i = 0; i < paragraphs.items.length; i++) {
const paragraph = paragraphs.items[i];
const text = paragraph.text;
const response = await textAPI(text);
let matches = response.parentheticals;
await context.sync();
// matches is currently, e.g, ['of the FSA', '"establishes that a', '.']
// ideally it would be, e.g., [[5, 14], [19, 37], [56, 56]]
// then you could do something like paragraph.getRange(start, end)
for (let match of matches) {
const ranges = paragraph.search(match, { matchCase: true });
ranges.load("items");
await context.sync();
for (let k = 0; k < ranges.items.length; k++) {
ranges.items[k].font.color = color;
}
}
}
await context.sync();
}).catch(function (error) {
console.log("Error: " + JSON.stringify(error));
});
}
Upvotes: 1
Views: 62