Update index

The script can be used to apply page-number spans (1, 2, 3 > 1–3), to undo those spans (1–3 > 1, 2, 3), to re-sort page numbers after you edited a topic, and to combine identical topics in a single entry. In the latter case the page references are combined and ranged as well. But first a word about topic separators.

Topic separators

It's not always easy to determine where the topic ends and where the page references start. Therefore you'll have to tell the script what the topic separator in your index is. For example, if the topic separator in your index is an en-space, then you set this in the script. Open the script in a text editor and go to this line (it's at the top of the script):

var topic_separator = '\u2002';

Here \u2002 is the code for the en-space. Naturally, the en-space mustn't be used for anything else. (Instead of entering \u2002 you can copy and paste the en-space as a character between the single quotes.) After making the change, save the script, making sure that it remains a plain text file.

A popular separator is comma+space, but commas can occur inside a topic, and comma+space is also the standard page-number separator. So comma+space is not a safe separator. Two spaces, too, are unsafe. So if you use a separator that occurs elsewhere as well, you'll have to change it temporarily.

If you update just a small number of entries there's no problem changing the separator manually. But if you use the script on a whole index, or a large part of it, you don't want to replace the separator manually. Instead, use the script index-update-insert-topic-separators.jsx to insert the code %% where it thinks the page references start. The codes are highlighted so that it's easy to see where they are. Check whether the codes are placed correctly, and if they are, make sure the code is set in the script (%% is the script's preset, but do check), then select a frame and run the script.

When you're done editing the index, simply replace the separator code string with the original separator.

Some examples

The script can be used to apply page-number spans. Before:

Leech, G.,%%ii, iii, iv, 1, 2, 3, 7, 10, 21, 22, 23

After:

Leech, G.,%%ii–iv, 1–3, 7, 10, 21–23

Conversely, the script can be used to undo number-spanning (see Page ranges, below).

Another scenario, which occurs often after some robust editing of the index, is that you end up with two identical topics with different page numbers. The page numbers at both topics are correct so you want to merge the two entries. You start with e.g. the following two entries:

Leech, G.,%%ii, iv, 1, 2, 3, 7, 10, 21, 22, 23
Leech, G.,%%iii, vi, 14, 6, 8, 9, 10, 15, 11, 22, 23

Select the two paragraphs (no need to select the full paragraph: you can select from the centre of the first to the centre of the second paragraph), then run the script. The result:

Leech, G.,%%ii–iv, vi, 1–3, 6–11, 14–15, 21–3

Use

The script runs either on just the selected paragraph (no need to select the whole paragraph, just place the cursor somewhere in it), on a number of selected paragraphs that have the same topic (see the example, above), or on the whole index. To process the whole index, select one of the index's text frames and run the script.

Ranging pages

In the example above, pages are ranged. Whether this should happen and, if yes, in what way, is determined by the setting of the page_span variable at the beginning of the script. "0" means 'no ranging at all'; "1" is the standard ranging of consecutive numbers; "2" allows for gaps of one page. By increasing the value of page_span, the spanner becomes more tolerant. Look for this line at the beginning of the script:

page_span = 1

and set the value you want (0 to disable spanning). Some examples:

page_span = 0: '1, 2, 3, 4, 6, 8' (no change)
page_span = 1: '1, 2, 3, 4, 6, 8' > '1-4, 6, 8'
page_span = 2: '1, 2, 3, 4, 6, 8' > '1-6, 8'
page_span = 3: '1, 2, 3, 4, 6, 8' > '1-8'
etc.

Note that you can undo ranging by setting the page span to 0.

Dropping digits (abbreviating ranges)

The script is set to drop digits (also known as 'abbreviating page ranges') so that 123-129 is changed to 123-9. Ranges are abbreviated maximally (as is customary in Britain), e.g. 2006-9. To disable digit dropping, look for this line:

drop_digits = true;
and change it to
drop_digits = false;

The script has no interface.


Version history

24 Aug. 2023: Rewrote part of the code to make the script more efficient. Added functionality so that you can select any number of paragraphs to merge entries.

October 2015: yet another fix needed for paragraph styles.

June 2010: fixed issue with paragraph styles.


Useful script? Saved you lots of time?

Consider making a donation. To make a donation, please press the button below. This is Paypal's payment system; you don't need a Paypal account to use it: you can use several types/brands of credit and debit card.

Peter Kahrel's paypal account

index-update.jsx

index-update-insert-topic-separators.jsx

Back to the main page on indexing

Back to the main script page

Installing and running scripts

Editing a script

Questions, comments? Get in touch