Renumbering navpoints in BBEdit the not-so-hard way

AddoMeter by arbyreed
Because it’s been more than a week since I last opened myself up to scorn about my lack of scripting skillz, here’s a how-to for another semiautomated e-production task in BBEdit (or TextWrangler): renumbering navpoints in an NCX.

So, let’s say you have an NCX to which you need to add another navpoint, or from which you need to remove one. If the navMap has only four or five points, as in the screenshot below, renumbering by hand is no big deal.
Navpoints to be renumbered
If you’re looking at a book with, say, twenty points, or two hundred, though, forget it. How can we of the scriptless masses automate this vile task? Well, I’ve got it down to about half a minute.

  1. With your NCX open, select Text > Remove Line Breaks.
    NCX with line breaks removed
  2. Under the window’s Text Options menu, turn off Soft Wrap Text, if it’s on. This makes it easier to see what you’re doing.
    Flattened NCX with text-wrap off
  3. Replace all <navPoint with \r<navPoint
    Find dialog for breaking navpoints onto new lines
    Now each navpoint is on its own line.
    Each navpoint on a new line
  4. Select all lines except the first one, and click Text > Add/Remove Line Numbers…
    Add/remove Line Numbers dialog
    Fill out the dialog as shown and click “Number.” Now you have sequential numbers at the start of each line.
    Navpoints with numbers next to them
    Hmm . . . See where this is going?
  5. Use a RegEx pattern to replace the playOrder values with the numbers you’ve just added. Depending on the source, some NCXs will use different patterns, but this is the pattern I have saved in my Find dialog:
    Find dialog for renumbering navpoints
    Replace all.
  6. Viola! Your points are now renumbered.
    Renumbered navpoints, still flat
    You can either leave the list flat or reflow the XML to nest it again. If you’re using BBEdit 11, you may want to install Keith Bolland’s Reflow XML script and assign a shortcut to it; in pre-11 versions, Markup > Tidy > Reflow Document… will do it.
    Navpoints renumbered and reflowed

Go forth and use your powers for good.

Leave a Reply

%d bloggers like this: