Fix shift-endnotes to get --decrement working and handle non-contiguous note ids #819
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In adding tests for shift-endnotes I discovered
--decrement
didn't work. And upon investigating that, I found--increment
didn't either if the note ids weren't contiguous.The way that the class shift_endnotes function worked, it create a range() for the notes to be modified, but 1) that range was wrong if the ids weren't perfectly contiguous, and 2) was, as far as I can tell, wrong for decrements all the time.
For both ranges, it assumed the count of endnotes equaled the highest endnote#, and that's definitely not true once a shift has happened, and it might not be true before a shift has happened, if the reason for doing a shift is to reduce a gap caused by the deletion of an endnote in the middle. It gets worse if a set of notes are shifted, then a subset of those same notes are shifted again. This can easily happen if someone needs to add a note at note-10 and another at note-27, e.g.
Conceptually, what needs to happen with shift is that starting at whatever endnote# is specified, all the remaining notes, whatever their numbers, need to be shifted by the amount, either up or down. So that's what I did—I made a list of all the integer endnote numbers1, removed the numbers that are less than the starting (target) endnote number, and then bump the remaining ids by the appropriate amount.
Something that the command does not take into account was whether a shift can produce duplicate endnote ids. I did not change it to do so, either.
1I believe we have one or two books with non-integer endnotes; this command won't work on them, before or after these changes, since the starting # is specified as an integer and the code looks specifically for note-{number}.
EDIT:
I tested on Don Quixote and Pepys: the former took just under 8 seconds, the latter took 1:49. I didn't take the time to try Gibbon. :)
EDIT 2:
Sorry, forgot—shift-illustrations appears to have the same problem. I'll fix it once this is resolved and merged.