View Single Post
Old 08-29-2022, 04:25 PM   #3
Tex2002ans
Wizard
Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.
 
Posts: 2,306
Karma: 13057279
Join Date: Jul 2012
Device: Kobo Forma, Nook
Quote:
Originally Posted by Turtle91 View Post
Welcome to MR!
Same. Welcome to MR, kawaberitan!

Quote:
Originally Posted by kawaberitan View Post
I don't want to do it manually Is there a macro or shortcut to link the endnotes?
No.

But to speed things up, there are a few tricks you can do with:
  • Regular Expressions
  • + Merging, then splitting files

If the code in your EPUB is clean, it's relatively simple.

- - -

Let's say your book has footnotes in the form of:

Chapter1.xhtml:

Code:
<p>This is an example.<sup>1</sup></p>
Endnotes.xhtml:

Code:
<p>1. This is an endnote.</p>
See how all little note numbers have:
  • <sup>
  • + a number
  • </sup>

and all endnotes have:
  • <p>
  • + a number
  • + a period

Your book will probably have slightly different code/patterns...

But these will help when you create a regular expression!

What you ultimately want is something like this:

Code:
<p>This is an example.<a href="#fn1" id="ft1">[1]</a></p>

<hr />

<p class="footnote"><a href="#ft1" id="fn1">[1]</a> An example footnote.</p>
  • The first one says:
    • "go visit fn1" + "my id is ft1"
  • The second one says the opposite:
    • "go visit ft1" + "my id is fn1"

These are just 2 simple HTML links pointing back/forth!

So... How do we get there?

Step 1: Regular Expressions

- - -

Note: In Sigil/Calibre, make sure you put your search into "Regex" Mode.

After you are done, make sure you set it back to how it was.

- - -

Step 1.1: Change Superscript into Clickable Link

We want to change <sup>1</sup> into an actual link:

Search: <sup>(\d+)</sup>
Replace: <a href="#fn\1" id="ft\1">[\1]</a>

How does this search work?
  • <sup> = look for a superscript
  • \d+ = look for ONE OR MORE numbers
    • This has () wrapped around it.
    • Parentheses are special symbols in regular expressions they stand for a "Capture Group".
    • It says "Take whatever you found between these parentheses, and shove it into Group 1".
  • </sup> = look for a closing superscript

Replace says:
  • <a = Create a new link
  • href="#fn\1" = Point to "fn" + whatever we captured in Group 1
  • id="ft\1"> = Make my id "ft" + whatever we captured in Group 1
  • [\1] = Put a bracket, then whatever we captured, then a closing bracket.
  • </a> = Close the link.

- - -

Now, we just do the opposite.

Step 1.2: Change Footnote/Endnote into Clickable Link

Search: <p>(\d+)\.
Replace: <p class="footnote"><a href="#ft\1" id="fn\1">[\1]</a>

Again, this is very similar. The Search is doing:
  • <p> = Look for a paragraph.
  • (\d+) = Look for a ONE OR MORE numbers + toss it into "Group 1".
  • \. = Look for a period.
    • Note: The '.' is a very special symbol in Regular Expressions. Because we want to look for an actual PERIOD character, we need the slash.

Replace is very similar.

- - -

Step 1.3: Test Links Back/Forth

Open up the book in Preview, then click the links and see if they work!

If they send you to the proper location, great! You're all done.

- - -

Step 2 (Optional): Merge + Split Files

Honestly, I recommend footnotes at the end of each file... this:
  • makes the <a> code MUCH MUCH easier.
  • keeps each chapter as a standalone file.

If you want to have a separate endnotes file... you can follow the trick I wrote about in:

This combines all files into one "merged.xhtml" file, then splits them all back up.

This lets Sigil/Calibre take care of all the renaming of chapter links.

- - - - - - -

Side Note: If you want to learn even more about footnotes, I've written about this extensively over the years.

Type this in your favorite search engine:

Code:
footnotes tex2002ans site:mobileread.com
and there are over 500 topics which I've discussed this. Most recently:

And remember:

Get the notes into clean links.

And KISS (Keep It Simple, Stupid)!

The less complicated your make your code, the less likely things are going to go wrong.

Last edited by Tex2002ans; 08-29-2022 at 04:41 PM.
Tex2002ans is offline   Reply With Quote