Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Editor

Notices

Reply
 
Thread Tools Search this Thread
Old 11-24-2021, 08:33 PM   #1
chouette
Junior Member
chouette began at the beginning.
 
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!
chouette is offline   Reply With Quote
Old 11-24-2021, 09:22 PM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
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
kovidgoyal is online now   Reply With Quote
Old 11-25-2021, 10:01 AM   #3
chouette
Junior Member
chouette began at the beginning.
 
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.
chouette is offline   Reply With Quote
Old 11-25-2021, 01:59 PM   #4
lomkiri
Groupie
lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.
 
lomkiri's Avatar
 
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>
and you want to insert the note [80] (i.e. the old n. 80 (and over) will be renumbered)

— 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+)(\])
— Set the search as regex-function and create the function with the code:
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)
Of course, you must change the var. "start_at" to feet your needs.
— 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.
lomkiri is offline   Reply With Quote
Old 11-27-2021, 10:42 AM   #5
chouette
Junior Member
chouette began at the beginning.
 
Posts: 3
Karma: 10
Join Date: Nov 2021
Device: Calibre on PC
Quote:
Originally Posted by lomkiri View Post
If, e.g., your note-anchor is something like this :
Code:
<a class="noteAnchor" href="../Text/footnotes.xhtml#footnote-0003">[3]</a>
and you want to insert the note [80] (i.e. the old n. 80 (and over) will be renumbered)

— 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+)(\])
— Set the search as regex-function and create the function with the code:
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)
Of course, you must change the var. "start_at" to feet your needs.
— 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.
Thank you so much! My note link is as follows:
Code:
<a id="back_note_3" href="notes.html#note_3" title="3" class="noteref">3</a>
And the note itself is like this:
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>
chouette is offline   Reply With Quote
Old 11-27-2021, 12:15 PM   #6
lomkiri
Groupie
lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.
 
lomkiri's Avatar
 
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>\])
The function is still the same for both form.

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
lomkiri is offline   Reply With Quote
Old 01-02-2022, 02:31 PM   #7
ugosan
Junior Member
ugosan began at the beginning.
 
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.
ugosan is offline   Reply With Quote
Old 01-02-2022, 03:23 PM   #8
lomkiri
Groupie
lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.
 
lomkiri's Avatar
 
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)
lomkiri is offline   Reply With Quote
Old 01-02-2022, 04:56 PM   #9
ugosan
Junior Member
ugosan began at the beginning.
 
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)
Thank you very much for your support.
ugosan is offline   Reply With Quote
Old 01-03-2022, 01:27 PM   #10
lomkiri
Groupie
lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.
 
lomkiri's Avatar
 
Posts: 167
Karma: 1497966
Join Date: Jul 2021
Device: N/A
Quote:
Originally Posted by ugosan View Post
I have modified the script because it was giving me "IndexError: no such group", but now it works fine
It depends of the search string you've used. In the one I wrote for Chouette:
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.

Quote:
Originally Posted by ugosan View Post
Thank you very much for your support.
Most welcome :-).
lomkiri is offline   Reply With Quote
Old 01-04-2022, 12:45 PM   #11
ugosan
Junior Member
ugosan began at the beginning.
 
Posts: 3
Karma: 10
Join Date: Jan 2022
Device: Kobo
Quote:
Originally Posted by lomkiri View Post
It depends of the search string you've used. In the one I wrote for Chouette:
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.
is exactly how you write, I had guessed that this was the reason, but now you have explained it, I understand better.
Thanks.
ugosan
ugosan is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
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


All times are GMT -4. The time now is 11:00 PM.


MobileRead.com is a privately owned, operated and funded community.