![]() |
#1 |
Junior Member
![]() Posts: 3
Karma: 10
Join Date: Nov 2021
Device: Calibre on PC
|
Insert and renumber endnotes
Hello all, I'm new to the forum and have been using Calibre for e-book conversion and editing. My HTML skills are rudimentary, but I can usually manage to polish an auto-generated EPUB.
Is there a way for me insert a totally new endnote (from a revision) and its corresponding back link into an e-book without having to manually renumber all the (hundreds of) endnotes that follow? Thanks! ![]() |
![]() |
![]() |
![]() |
#2 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,351
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
There's no builtin tool for it, but you can use the function mode of search and replace to create something, if you are willing to write a small function in python. See https://manual.calibre-ebook.com/function_mode.html
|
![]() |
![]() |
![]() |
#3 |
Junior Member
![]() Posts: 3
Karma: 10
Join Date: Nov 2021
Device: Calibre on PC
|
Thank you so much for your quick response! I've never written a function before. Perhaps someone would be willing to give me a quick tutorial using this particular example? Thanks again.
|
![]() |
![]() |
![]() |
#4 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 167
Karma: 1497966
Join Date: Jul 2021
Device: N/A
|
If, e.g., your note-anchor is something like this :
Code:
<a class="noteAnchor" href="../Text/footnotes.xhtml#footnote-0003">[3]</a> — Make a backup of you book — Put as a search string (you'll have to adapt it to the form of the note-anchors of your ebook) : Code:
<a[^>]+>\[\K(\d+)(\]) Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): start_at = 80 if (n := int(match.group(1))) >= start_at: return str(n+1) + match.group(2) return match.group(0) — Replace all — Insert your own note as n. 80 Same process for the notes themselves, just adapt the regex to their form.. If you don't know how to adapt the regexes to your case, give an example of the note-anchor and of the body of the note. Last edited by lomkiri; 11-25-2021 at 06:45 PM. |
![]() |
![]() |
![]() |
#5 | |
Junior Member
![]() Posts: 3
Karma: 10
Join Date: Nov 2021
Device: Calibre on PC
|
Quote:
Code:
<a id="back_note_3" href="notes.html#note_3" title="3" class="noteref">3</a> Code:
<dl id="note_3" class="footnote"><dt class="calibre42">[<a href="ch3.html#back_note_3" title="3" class="calibre43">←3</a>]</dt><dd class="calibre44"><p class="block_91"> Note text goes here.</p></dd></dl> |
|
![]() |
![]() |
![]() |
#6 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 167
Karma: 1497966
Join Date: Jul 2021
Device: N/A
|
Well, if all your note-anchors and note-bodies are on the same form, those two regexes will do the job:
Code:
Note-anchors: class="noteref">\K(\d+)(</a>) note bodies: \[<a href=[^#]+#back_note_\d+" title="\d+" class="calibre\d+">←\K(\d+)(</a>\]) Although it's possible (and not difficult) to adapt the numbers inside the tags (back_note_3, note_3), for the sake of simplicity I didn't touch to them, only to the displayed numbers. When you'll add your own note, e.g. 80, put 80a as numbers in the tags, to avoid duplicated links: <a id="back_note_80a" href="notes.html#note_80a" title="80a" class="noteref">80</a> Don't forget to: 1) back-up you book first 2) change the value of "start_at" in the function 3) See the differences after a replace all, to check if it went as you were expecting 4) Check the book with the bug-icon or with F7 5) Check some notes liink to see if they are still working properly |
![]() |
![]() |
![]() |
#7 |
Junior Member
![]() Posts: 3
Karma: 10
Join Date: Jan 2022
Device: Kobo
|
Hello. I tried this function, but I can't set it: when I insert it copy&paste in "create function" it returns me: "The code you created is not valid Python code, with error: invalid sintax (name, line 3)".
I'm using Calibre 3.48 on macOS 10.13.8. Thanks. |
![]() |
![]() |
![]() |
#8 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 167
Karma: 1497966
Join Date: Jul 2021
Device: N/A
|
Calibre prior to 5.00 is working with python 2, after version 5.00 it's python 3, that's why it doesn't works. If you upgrade Calibre, it will work. There is plenty of new features, you should do it.
If you want to keep your old version try this code: Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): start_at = 80 n = int(match.group(1)) if n >= start_at: return str(n+1) + match.group(2) return match.group(0) |
![]() |
![]() |
![]() |
#9 |
Junior Member
![]() Posts: 3
Karma: 10
Join Date: Jan 2022
Device: Kobo
|
Hi lomkiri, you are fast as light
![]() I'd like to install the new version of Calibre, unfortunately my OS doesn't support it. I have modified the script because it was giving me "IndexError: no such group", but now it works fine: Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): start_at = 19 n = int(match.group(1)) if n >= start_at: return str(n-1) return match.group(0) |
![]() |
![]() |
![]() |
#10 | |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 167
Karma: 1497966
Join Date: Jul 2021
Device: N/A
|
Quote:
class="noteref">\K(\d+)(</a>) there were 2 capturing groups: group(1) -> (\d+) and group(2) -> (</a>) You probably put a different string with only one capturing group, so you correctly adapted the code of the function. Most welcome :-). |
|
![]() |
![]() |
![]() |
#11 | |
Junior Member
![]() Posts: 3
Karma: 10
Join Date: Jan 2022
Device: Kobo
|
Quote:
Thanks. ugosan |
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
The command "Renumber TOC Entries" seems not to work | RbnJrg | Sigil | 2 | 06-28-2021 09:58 AM |
Endnotes within Endnotes | Alcuin7 | Editor | 22 | 11-30-2018 01:41 PM |
Calibre to EndNotes X3 is it possible? | fms95032 | Calibre | 6 | 10-19-2016 11:09 AM |
Endnotes | Siam | Sigil | 14 | 03-17-2013 02:05 PM |
Number items renumber in Kindle | sliming | Conversion | 6 | 08-27-2011 10:43 PM |