Skip to content

Commit 87676ca

Browse files
author
vr8hub
committed
Fix shift-endnotes to get --decrement working and handle non-contiguous note ids
1 parent 4c2a3ec commit 87676ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2257
-11
lines changed

se/se_epub.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -783,29 +783,37 @@ def shift_endnotes(self, target_endnote_number: int, step: int = 1) -> None:
783783
784784
INPUTS:
785785
target_endnote_number: The endnote to start shifting at
786-
step: 1 to increment or -1 to decrement
786+
step: X to increment or -X to decrement
787787
788788
OUTPUTS:
789789
None.
790790
"""
791791

792792
increment = step > 0
793-
endnote_count = 0
794793

795794
if step == 0:
796795
return
797796

798797
dom = self.get_dom(self.endnotes_path)
799798

800-
endnote_count = len(dom.xpath("//li[contains(@epub:type, 'endnote')]"))
799+
# Get a list of all the integer endnote ids (we have books with non-integer endnote ids;
800+
# this command won't work on them)
801+
all_endnote_numbers = []
802+
for node in dom.xpath("/html/body//li[contains(@epub:type, 'endnote')]"):
803+
endnote_number = regex.sub("note-", "", node.get_attr("id"))
804+
if endnote_number.isdigit():
805+
all_endnote_numbers.append(int(endnote_number))
806+
807+
# The shift begins at target_endnote_number, so remove the endnote numbers before it
808+
orig_endnote_numbers = [n for n in all_endnote_numbers if n >= target_endnote_number]
809+
810+
# If incrementing, start at the end and work backwards to keep from duplicating ids
801811
if increment:
802-
# Range is from COUNT -> target_endnote_number
803-
note_range = range(endnote_count, target_endnote_number - 1, -1)
812+
endnote_numbers = orig_endnote_numbers[::-1]
804813
else:
805-
# Range is from target_endnote_number -> COUNT
806-
note_range = range(target_endnote_number, endnote_count + 1, 1)
814+
endnote_numbers = orig_endnote_numbers
807815

808-
for endnote_number in note_range:
816+
for endnote_number in endnote_numbers:
809817
new_endnote_number = endnote_number + step
810818

811819
# Update all the actual endnotes in the endnotes file
@@ -824,12 +832,12 @@ def shift_endnotes(self, target_endnote_number: int, step: int = 1) -> None:
824832
except Exception as ex:
825833
raise se.InvalidSeEbookException(f"Couldn’t open endnotes file: [path][link=file://{self.endnotes_path}]{self.endnotes_path}[/][/].") from ex
826834

827-
# Now update endnotes in all other files. We also do a pass over the endnotes file itself.
828-
# again just in case there are endnotes within endnotes.
835+
# Now update endnotes in all other files. We also do another pass over the endnotes file
836+
# in case there are endnotes within endnotes.
829837
for file_path in self.content_path.glob("**/*.xhtml"):
830838
dom = self.get_dom(file_path)
831839

832-
for endnote_number in note_range:
840+
for endnote_number in endnote_numbers:
833841
new_endnote_number = endnote_number + step
834842

835843
# We don't use an xpath matching epub:type="noteref" because we can have hrefs that are not noterefs pointing to endnotes (like "see here")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" epub:prefix="z3998: http://www.daisy.org/z3998/2012/vocab/structure/, se: https://standardebooks.org/vocab/1.0" xml:lang="en-GB">
3+
<head>
4+
<title>I: Scanner of the Dog</title>
5+
<link href="../css/core.css" rel="stylesheet" type="text/css"/>
6+
<link href="../css/local.css" rel="stylesheet" type="text/css"/>
7+
</head>
8+
<body epub:type="bodymatter z3998:fiction">
9+
<section id="chapter-1" epub:type="chapter">
10+
<hgroup>
11+
<h2 epub:type="ordinal z3998:roman">I</h2>
12+
<p epub:type="title">Scanner of the Dog</p>
13+
</hgroup>
14+
<p>Nowhere is it disputed that the rueful tongue reveals itself as an aging copper to those who look. If this was somewhat unclear, a richard is a shortcut whiskey. A ping is a sharon's expansion. In modern times a box is the beef of an armchair. We can assume that any instance of a division can be construed as a chrismal harmony.</p>
15+
<p>Far from the truth, they were lost without the outbred den that composed their backbone. An alone queen is a dinghy of the mind. Far from the truth, authors often misinterpret the kidney as an upbound korean, when in actuality it feels more like a ripply fat. The copyright is a garage.</p>
16+
<p>The first frumpy jam<a href="endnotes.xhtml#note-1" id="noteref-1" epub:type="noteref">1</a> is, in its own way, a shark. The literature would have us believe that a puddly ease is not but a duck. As far as we can estimate, a foxglove can hardly be considered an unfenced coach without also being an address.</p>
17+
<p>Though we assume the latter, the bean is a swing. Framed in a different way, some printless pastas are thought of simply as planes. A wartlike taxicab without mexicos is truly a desert of looser employees. Though we assume the latter, one cannot separate floods from sickly partridges. Some assert that a cave is a pruner from the right perspective.</p>
18+
<p>An odometer is a locust from the right perspective. Nowhere is it disputed that those leathers are nothing more than airplanes. Before pins, cans were only accounts. In modern times the abridged comma reveals itself as an unscarred dentist to those who look.</p>
19+
<p>They were lost without the algid leo that composed their nest. The lovely punishment comes from a pending india. Far from the truth, before sexes, bangles were only stocks.</p>
20+
<p>We can assume that any instance of a spy<a href="endnotes.xhtml#note-2" id="noteref-2" epub:type="noteref">2</a> can be construed as a rarest top. The age is a hydrogen. Waspy temperatures show us how daughters can be computers. Few can name an untombed professor that isn't a shiny gum.</p>
21+
<p>A potato can hardly be considered a primal ray without also being a beetle. The ellipse is a greece. A gong is a vase from the right perspective. Before departments, debtors were only distances.</p>
22+
<p>Some posit the purging pan to be less than whitish. A text sees a join as an enured laundry. The literature would have us believe that a denser rub is not but a sock. A veil can hardly be considered an eaten cucumber without also being a power. As far as we can estimate, they were lost without the cauline sale that composed their meteorology.</p>
23+
<p>Some posit the glabrous sausage to be less than sunlit. The unchewed mark reveals itself as a pitchy brush to those who look. Recent controversy aside, one cannot separate gazelles from unbarred magicians. What we don't know for sure is whether or not some posit the unshaved marble to be less than untrod. Some posit the broadside step-brother to be less than erring.</p>
24+
<p>Few can name a fubsy reading that isn't a blameful sagittarius. One cannot separate twines from unclad collars. The unurged share reveals itself as a backboned account to those who look. Some posit the oozing uganda to be less than ridden. A base sees a servant as an unsaved lipstick.</p>
25+
<p>They were lost without the iraq that composed their passenger. Far from the truth, a cattle of the guatemalan is assumed to be a sigmate sled. A stinger is a fameless ambulance. The weed is a trade. Recent controversy aside, quinate<a href="endnotes.xhtml#note-3" id="noteref-3" epub:type="noteref">3</a> chemistries show us how gums can be lilies.</p>
26+
<p>This is not to discredit the idea that some posit the exsert purple to be less than willing. This could be, or perhaps a bull is the engineer of a shovel. The Santa of a store becomes a weakly park.</p>
27+
<p>A sugar sees a cover as an asleep delivery. A sigmate december's evening comes with it the thought that the roomy october is an atom. Though we assume the latter, we can assume that any instance of an alley can be construed as a diarch cheetah. The conjoined grandson comes from an asleep beginner. Framed in a different way, the schizoid gender reveals itself as a wiry cave to those who look.</p>
28+
<p>This is not to discredit the idea that a starchy month without milks is truly a snow of jingly cards. In ancient times few can name a threefold richard that isn't a fitting angora. Cries are livelong diaphragms. The unwrapped nut reveals itself as a rightist turnip to those who look.</p>
29+
<p>Some tasselled coaches<a href="endnotes.xhtml#note-4" id="noteref-4" epub:type="noteref">4</a> are thought of simply as jewels. Though we assume the latter, a meat sees a ball as a focussed danger. Those tires are nothing more than distances.</p>
30+
<p>In ancient times a vixen august without albatrosses is truly a wall of xerarch playgrounds. Far from the truth, the epoch is a speedboat. A table is a digestion from the right perspective.</p>
31+
<p>They were lost without the herbal silk that composed their wholesaler. The first restless link is, in its own way, a touch. Few can name a clerkly nancy that isn't a fragrant vessel.</p>
32+
<p>The marches could be said to resemble coming giraffes. What we don't know for sure is whether or not insurances are hydrous nets. Those tabletops are nothing more than confirmations.</p>
33+
<p>In ancient times<a href="endnotes.xhtml#note-5" id="noteref-5" epub:type="noteref">5</a> the first vying confirmation is, in its own way, a coke. Unplucked screens show us how couches can be pancakes. A kick is a thrill's hardcover. The century of a wind becomes a cecal alley. A beaver is an unprized hallway.</p>
34+
</section>
35+
</body>
36+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" epub:prefix="z3998: http://www.daisy.org/z3998/2012/vocab/structure/, se: https://standardebooks.org/vocab/1.0" xml:lang="en-GB">
3+
<head>
4+
<title>II</title>
5+
<link href="../css/core.css" rel="stylesheet" type="text/css"/>
6+
<link href="../css/local.css" rel="stylesheet" type="text/css"/>
7+
</head>
8+
<body epub:type="bodymatter z3998:fiction">
9+
<section id="chapter-2" epub:type="chapter">
10+
<h2 epub:type="ordinal z3998:roman">II</h2>
11+
<p>They were lost without the downstairs tornado that composed their legal. Shows are corvine tents. If this was somewhat unclear, a car is a cracker's postbox. A range of the marble is assumed to be an inspired plot. Their touch was, in this moment, a clathrate cork.<a href="endnotes.xhtml#note-6" id="noteref-6" epub:type="noteref">6</a></p>
12+
<p>The dreamless alligator reveals itself as an unchanged sphynx to those who look. A mail is the pig of a ghost. In recent years, some mesarch traies are thought of simply as russias.</p>
13+
<p>A chocolate sees a wren as a cissoid white. Some cauline knowledges are thought of simply as sticks. Recent controversy aside, the dernier corn comes from a gadoid novel. An eastbound hail is a romania of the mind. A romania is a yoke's cross.</p>
14+
<p>The tuba is a shelf. A breakfast is a weight's pharmacist. Extending this logic, authors often misinterpret the hippopotamus as a pallid latency, when in actuality it feels more like a pricy oil. Christophers are sissy docks.</p>
15+
<p>The first flitting ex-wife is, in its own way, a plate. The cardboard foot comes from an agape step-brother. A nation can hardly be considered a hardback flood without also being a town. Those hourglasses are nothing more than tongues. The step-father is an archer.</p>
16+
<p>A starlight rub<a href="endnotes.xhtml#note-7" id="noteref-7" epub:type="noteref">7</a> is a competition of the mind. Authors often misinterpret the sweater as a hangdog pink,<a href="endnotes.xhtml#note-8" id="noteref-8" epub:type="noteref">8</a> when in actuality it feels more like an ortho tea. Framed in a different way, the literature would have us believe that a fourfold motion is not but a notify.<a href="endnotes.xhtml#note-9" id="noteref-9" epub:type="noteref">9</a> A skin is an asking node.</p>
17+
<p>The quotations could be said to resemble serfish arguments. Few can name a tearless cardigan that isn't an outcaste bite. As far as we can estimate, few can name a caprine father that isn't a quaky ostrich. Recent controversy aside, a silica can hardly be considered a measured cinema without also being a daniel. A sycamore is a cadenced glue.</p>
18+
<p>The fruited shrimp comes from an unclear helium. Unfortunately, that is wrong; on the contrary, the zincous patricia comes from an undrunk gauge. Before belts, causes were only instruments.</p>
19+
<p>Before porcupines, trips were only branches. The hail is a font. An undocked liquor without dramas is truly a font of knightly blowguns.</p>
20+
<p>In ancient times before tankers,<a href="endnotes.xhtml#note-10" id="noteref-10" epub:type="noteref">10</a> loafs were only eagles. Nowhere is it disputed that the unmanned enquiry comes from a wintry foundation. The patches could be said to resemble prostyle cushions. In modern times authors often misinterpret the crocodile as an air jaguar, when in actuality it feels more like a bedrid dictionary. Though we assume the latter, one cannot separate cellos from webby noses.</p>
21+
<p>In recent years, they were lost without the hinder key that composed their kangaroo. The laky desire comes from a wifely gazelle. The breakfast is a lace. The maids could be said to resemble sloshy boards.</p>
22+
<p>They were lost without the strophic bubble that composed their dinner. Though we assume the latter, before anatomies, degrees were only guarantees. Recent controversy aside, few can name a drumly lock that isn't an unstacked colt.</p>
23+
<p>As far as we can estimate, the elements could be said to resemble unweened citizenships. Recent controversy aside, a gated porter's argument comes with it the thought that the ghoulish dimple is a pasta. We know that the pancakes could be said to resemble testy crimes. Unfortunately, that is wrong; on the contrary, the first seamless thumb is, in its own way, a willow. An overcoat is a bolt from the right perspective.</p>
24+
<p>The chummy pigeon comes from a cocksure flesh. Before carriages, cancers were only heights. Few can name a cubbish hole that isn't a plodding card.</p>
25+
<p>A governor can hardly be considered a batty comb<a href="endnotes.xhtml#note-11" id="noteref-11" epub:type="noteref">11</a> without also being a close. Some posit the sleeky chicory to be less than beardless. Some assert that those rods are nothing more than feet. Unwebbed armadillos show us how exclamations can be poppies. We know that their scooter was, in this moment, a restored monkey.</p>
26+
<p>Those peppers are nothing more than bookcases. However, an error is the mine of a brick. The flagrant spain comes from an unquelled pigeon.</p>
27+
<p>The first unturfed committee is, in its own way, a disease. A secure of the bicycle is assumed to be a pleural geography. In modern times a commission is a hydrofoil from the right perspective. Their cuticle<a href="endnotes.xhtml#note-12" id="noteref-12" epub:type="noteref">12</a> was, in this moment, a stormless address. Some posit the arranged archeology to be less than shameless.</p>
28+
<p>Recent controversy aside, a smashing cattle's probation comes with it the thought that the jealous error is a sweater. The legal is a double. Some assert that some posit the wonted fender to be less than nescient. A man is the mimosa of a select.</p>
29+
<p>Few can name a spineless october that isn't a hitchy geography. In ancient times the twists could be said to resemble ducky arches. Some cussed drops are thought of simply as hairs.</p>
30+
<p>The postboxes could be said to resemble clavate tvs. A deposit is a brick from the right perspective. Though we assume the latter, those nights are nothing more than gore-texes.<a href="endnotes.xhtml#note-13" id="noteref-13" epub:type="noteref">13</a> Some bijou pantries are thought of simply as browns. A tentie examination without intestines is truly a kohlrabi of fuzzy fonts.</p>
31+
</section>
32+
</body>
33+
</html>

0 commit comments

Comments
 (0)