Copy link to clipboard
Copied
Hello everyone,
I am a beginner of InDesign and I am learning how to use it both for printed books and EPUBs.
I am writing because I was wondering if there was a way, through scripts, to link the endnote text (or just its number) to the endnote reference number in the main text.
The reason is that when exporting to EPUB, InDesign properly makes all the links of the dynamic endnotes, but not the reverse ones, so getting back to the main text after having read the endnote can be a pain when using ereaders that don't have the "go back" function.
My workflow to solve the problem at the moment is to use Peter Kahrel's end2end script to make the dynamic endnotes (and it saves me tons of time!) and then manually do the reverse links either by using the endnote text as the link or by placing a small back arrow at the end of the text, that links to the endnote reference in the main text.
But given that I have hundreds of notes to do, some automatization would be of great help.
All the best
Clemens
1 Correct answer
Hi Clemens,
I'll put the code online so it may be usefull to others (and mayby someone can improve on it or find an indesign-solution)
You'll have to
- create a folder in htdocs
- add a folder named "input" and one named "output".
- Put your html-files in the input folder.
- Put this code in a file in the root of the folder that contains both directories.
- Run it in your browser.
- Done... You'll find your changed files in the output-folder - they will show up as links so you can check them at once
Take care: if
...Copy link to clipboard
Copied
Hi Clemens,
Did you ever get an answer to this question?
I'm struggling with the same problem.
I've solved the EPUB-part of it by doing a grep-search-replace with PHP in the HTML-file of the EPUB-export.
Copy link to clipboard
Copied
Sorry we missed this.
It's certainly doable (or should be).
What exactly do you want the script to do? A more clearly layed out example with screenshots and soforth would be helpful, especially for those of us who don't deal with endnotes very often.
Copy link to clipboard
Copied
Hello,
thank you for the reply.
I'll try to explain the situation more in detail and also add a zip file with a sample chapter (please ignore font and link errors).
- I have got indd files made from original QuarkXpress files (that I don't have and might not exist any more) and with other software.
- They have endnotes that are not dynamic. Simplythere are the little superscript numbers in the main text and at the end of the chapter there is a list of paragraphs, each starting with a relative superscript number and then containing the note text.
- I'd like to export to epub having the notes working in both ways, that is have the little "blue" superscript numbers and if I click on one in the main text, I get to the relative note, and if I click on the note number, I get back to the main text. It doesn't really matter if they are endnotes or footnotes, as long as I can jump back and forth easily.
As of now I have tried the following ways:
1 Manually transforming the endnotes in footnotes (copying the endnote text and pasting it in the footnote, that of course is placed in the main text where the superscrtip number is). The result is an epub with working (i.e. in both directions) notes.
2 Use Kahrel's end2end script to convert the endnotes to dynamic endnotes and then export to epub. The problem is that I get notes working only in one direction, from main text to note, not the other way around, so I have to manually add the reverse links.
Had I just a couple of notes it wouldn't be a problem, but since I have hundreds per book, the process is rather time consuming.
Thank you very much for your interest I hope you can find a nice solution.
All the best
P.s. the example file is here
Copy link to clipboard
Copied
You should make it easier to help you.
As of now I have tried the following ways:
1 Manually transforming the endnotes in footnotes (copying the endnote text and pasting it in the footnote, that of course is placed in the main text where the superscrtip number is). The result is an epub with working (i.e. in both directions) notes.
2 Use Kahrel's end2end script to convert the endnotes to dynamic endnotes and then export to epub. The problem is that I get notes working only in one direction, from main text to note, not the other way around, so I have to manually add the reverse links.
It sounds like modifying Peter's script is the easy approach, but you don't provide a link to it and googling does not find it, so I feel like I've already wasted too much time.
Copy link to clipboard
Copied
Sorry for the lack of detail, somehow I felt that Kahrel's scripts were famous.
Here is the link to the specific script
Copy link to clipboard
Copied
Sorry, I've been busy (and still am).
Sorry for the lack of detail, somehow I felt that Kahrel's scripts were famous.
Here is the link to the specific script
Well, they are, but unfortunately Peter has named his script "end_to_end.jsx" and his web page "dynamic_endnotes.html" and you referred to it as "end2end".
Unfortunately, I try to run it on your file, and your file doesn't seem to meet the requirements. Unless I am mistaken, the document needs a character style applied to endnote references, and a paragraph style applied to the endnotes themselves. Your document doesn't seem to have that.
Perhaps someone else has more time to assist. It's probably not much work to alter Peter's script here...
Copy link to clipboard
Copied
First of all thank you for trying to help me.
Sorry again for the problem. I am little more of a beginner and know almost nothing about scripting and common practice in forums about scripts.
The last time I uploaded the file as I have it (and have tons like this one, all full of "endnotes"), without preparing it for Kahrel's script, so that the original starting situation could be seen.
This time I modified it and reuploaded it at this address
https://rapidshare.com/files/1009414585/examaple_prepared_for_Kahrel_script_end_to_end.zip
The script now can be run flawlessy (at least on my computer, where I have run it).
All the best.
Copy link to clipboard
Copied
Hi,
unfortunately I haven't got an answer yet, so I am trying to learn indesign scripting. By the way, what was your PHP solution?
All the best
P.s. did you get my PM?
Copy link to clipboard
Copied
My php-solution was specific to the project i was working on.
If you send me an Epub-file of a project you are working on I'm happy to adapt the php-code for you.
Essentially what I do is open the epub-file (change the extention to .zip and open it) and run the script on the html-files it contains.
Its a simple regular-expression search replace.
The notenumbers in the text have to have a character-style.
The endnote-texts have to have a paragraph-style.
Preferrably they are not linked. (if they are some filtering will first remove the existing one-way links)
For my project I made an "in" and an "out" folder. The script processes every file in the in-folder and puts the result in the out-folder.
A small piece of the code to show you what it does:
//put anchors next to footnote-numbers and link to footnote-text
$newtekst = preg_replace('/(<span class="Footnote"[^>]*>)(\\d+)/', '<a id="notenummer$2"></a>$1<a href="#notetekst$2">$2</a>', $newtekst);
//put anchors next to footnote-text and link to footnote-numbers
$newtekst = preg_replace('/(<p class="Footnotes"[^>]*><[^>]+><[^>]+>\\()(\\d+)/', '<a id="notetekst$2"></a>$1<a href="#notenummer$2">$2</a>', $newtekst);
PS: if you want to install php you can use an installer like Xampp which makes it very easy -> you can google for it
Copy link to clipboard
Copied
Thank you
I am not so familiar with php (still learning), but playing a little with the code should help me to understand how it works.
Fortunately I have already Xampp installed since I am trying to prepare a website with Joomla.
One last thing before I PM you the file, should the superscript tag of the note number in the main text be the same as the one in the in the endnote section or should they be different, something like "maintext superscript" and "endnote superscript"?
Cheers
Copy link to clipboard
Copied
The character-style and the paragraph style should preferably be different.
There are several ways to do this. The shortest way is to use regular expressions.
To make a regular expression work you have to have a pattern to look for. In this case that will be the style-name (which becomes a class once exported to epub).
Copy link to clipboard
Copied
Thank you very much.
I just sent you a Private Message with the link to the file and the names.
I am really excited about the idea of php to skip the manual copy and paste work!
Copy link to clipboard
Copied
Hi Clemens,
I'll put the code online so it may be usefull to others (and mayby someone can improve on it or find an indesign-solution)
You'll have to
- create a folder in htdocs
- add a folder named "input" and one named "output".
- Put your html-files in the input folder.
- Put this code in a file in the root of the folder that contains both directories.
- Run it in your browser.
- Done... You'll find your changed files in the output-folder - they will show up as links so you can check them at once
Take care: if you recompress the files to zip and change back to epub the mimetype file must be added to the archive first and without compression - the other files can then be added with highest compression.
<?php
$filenames = array();
//loop through input directory and put all files in an array
if ($handle = opendir('./input')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$filenames[]=$entry;
}
}
closedir($handle);
}
//fill variable with contents of each file, one file at a time
$tekst ="";
foreach($filenames as $key => $filename){
$startfile = "./input/".$filename;
if (file_exists($startfile)) {
$fo = fopen($startfile, "r");
while ( $line = fgets($fo) ) {
$tekst .= $line;
}
//link endnote references in 2 directions
//protect class in endnote-text --> not necessary if class of numbers in endnote-text is different from class of numbers in text
$tekst = str_replace('<p class="Testo-note"><span class="Super">','<p class="Testo-note"><span class="##Super">',$tekst);
//put anchors before endnote-numbers and link to endnote-text
$tekst = preg_replace('/(<span class="Super">)(\\d+)/', '<a id="notenumber$2"></a>$1<a href="#notetext$2">$2</a>', $tekst);
//reverse protection --> not necessary if class of numbers in endnote-text is different from class of numbers in text
$tekst = str_replace('<p class="Testo-note"><span class="##Super">','<p class="Testo-note"><span class="Super">',$tekst);
//put anchors before endnote-text and link to endnote-numbers
$tekst = preg_replace('/(<p class="Testo-note"><span class="Super">)(\\d+)/', '<a id="notetext$2"></a>$1<a href="#notenumber$2">$2</a>', $tekst);
//change inputfolder to outputfolder and write changed file to outputfolder
$endfile = str_replace("input","output",$startfile);
$fo = fopen($endfile, "w");
fwrite($fo,$tekst);
$tekst ="";
}
}
//display changed files as link
if ($handle = opendir('./output')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<br /><a href='./output/".$entry."'>".$entry."</a>";
}
}
closedir($handle);
}
?>
Copy link to clipboard
Copied
Thank you very much for the custom script
It works great!!
It will be of great use and will save me innumerable hours of manual work.
I am so happy!!
Thank you again for everything.
Copy link to clipboard
Copied
You're very welcome
Copy link to clipboard
Copied
cramik
This thread has been most valuable and I've managed to use the code and XAMPP software to create bidirectional endnotes in a single XHTML file using your method. However, when my epub comprises multiple XHTML files, and I load all the files into the 'Input' folder and get the corresponding files produced in the 'Output' folder, although all the note refs and endnotes are highlighted as links, they do not link to their corresponding detstinations in the other XHTML files.
Am I missing something - if the epub is multiple XHTML files, should I be doing something different?
Thanks
Steve
Copy link to clipboard
Copied
I wrote this code to work for a book with multiple chapters. Each chapter had it's own footnotes and was in a seperate file.
So, this script should work if (and only if) each seperate html file has it's own footnotes.
If this does not work for you send me a link and I'll take a look at your code.
Copy link to clipboard
Copied
Ah, I see.
In this situation they are endnotes and are all located at the back of the book - and therefore end up in a separate html file from the other html files containing the note refs.
Since my post I have found the best way to do it was export from InDesign to epub without breaking into multiple html files (i.e. do not use the 'split epub' function in ID) - then your script works brilliantly. A single html file obviously then does not give me the page breaks I need in the ebook - I then get round that by putting 'page-break-before;always' in the relevant heading paragraph styles in the CSS file.
So I think I'm sorted - many thanks for coming back to me so quickly. And thanks again for your great script - it's a real time-saver.
Steve
Copy link to clipboard
Copied
Hi cramik
Unfortunately I think I was a little premature with my reply earlier this morning.
Although a single html file allows the script to run okay, the problem is that my endnotes are renumbered for each chapter:
Chapter 1
1. ...
2. ...
Chapter 2
1. ...
2. ...
which of course means that there are duplicate anchors and hyperlinks created.
I could show you the epub/html file (before and after) - just not sure how I get them to you/onto this forum?
Many thanks
Steve
Copy link to clipboard
Copied
Hello,
I am getting a bit desperate
I really wished there was a script that could transform static endnotes into dynamic bi-directional endnotes or footnotes.
Could anyone at suggest me some good resources to learn how to modify scripts?
It will take me less to learn a new programming language than to manually do all the notes.
Plus it's a skill that might be useful for other things as well in the future.
Al the best.

