02-15-2011, 07:51 PM | #1 |
Member
Posts: 12
Karma: 10
Join Date: Jun 2010
Device: PRS-300
|
Inline TOC from toc.ncx
Hi. I have several EPUB files that I want to turn into mobi files, and I want to use an inline (HTML) TOC in them. Since I already have a working toc.ncx files, is there a way to use the ncx file to generate the correspondent HTML code?
I tried to search for an answer in the forums, but I had no luck. |
02-16-2011, 08:05 PM | #2 |
Enquiring Mind
Posts: 562
Karma: 42350
Join Date: Aug 2010
Location: London, UK
Device: Kindle 3 (WiFi)
|
Hi elmago79 - I'm not aware of any scripts out there to do this, I'm afraid, though it would certainly be feasible, and a nice tool if anyone were to create a decent script to convert NCX files into HTML TOCs.
|
02-27-2011, 07:45 PM | #3 |
Guru
Posts: 697
Karma: 150000
Join Date: Feb 2010
Device: none
|
OK, I'm almost embarrassed to post this, but here is a very simple perl script that will read the epub's toc.ncx and create an html block (not a complete html file -- there is no <head> section) that provides a bare-bones inline TOC when you include it in your epub.
Code:
#!/usr/bin/perl # parse a toc.ncx file and output xhtml statements for an in-line TOC file # # usage: parse_ncx.pl < toc.ncx > toc.txt # ### sample of data we are interested in: ### #<?xml version="1.0" encoding="UTF-8"?> #<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd"> #<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" xml:lang="en" version="2005-1"> #<head> #<meta name="dtb:uid" content="awp2010-06-03T15:07:06Z"/> #<meta name="dtb:depth" content="2"/> #<meta name="dtb:totalPageCount" content="0"/> #<meta name="dtb:maxPageNumber" content="0"/> #</head> #<docTitle> #<text>The Cresperian Alliance</text> #</docTitle> #<navMap> #<navPoint id="navpoint-1" playOrder="1"> #<navLabel> #<text>Title page</text> => use for $item_text #</navLabel> #<content src="1.html"/> => user for $target #</navPoint> # etc for all <navPoint> ... </navPoint> entries. #</navMap> #</ncx> $item_text = "X"; $target = "X"; $| =1; #don't buffer writes to stdout. print "<h2>Table of Contents</h2>\n" ; while (<STDIN>) { # read a line and look for info if (/^#/) {next;} #skip blank lines if (m\<text>(.+?)</text>\) { $item_text=$1; } elsif (m\<content src="(.+?)"/>\) { $target = $1; print ('<div><a href="../', $target, '">', $item_text, "</a></div>\n" ) ; # } elsif (m\</navPoint>\) { # print ("<p><a href='../", $target, "'>", $item_text, "</a></p>\n" ) ; } } ; # end while print "\n<!-- Done -->\n"; For now, just copy the code section and paste it as "parse_ncx.pl" As it is, it is used as a "filter" like so : first, unpack the epub, or at least extract the toc.ncx file into your working directory. Then, from a command prompt, do parse_ncx.pl < toc.ncx > toc.txt This will read from the toc.ncx file and write to a new file called "toc.txt" the html equivalents of the ncx table of contents. Then, you edit the epub (say, with sigil) and create a new blank toc.xhtml file, paste in the contents of toc.txt into the <body> of that file, assign the "table of contents" semantic to your new toc.xhtml file, and save. Now you have an inline xhtml toc file. If you then use kindlegen to convert it to mobi format, you will at least have the inline TOC that you need. Of course there are other issues of the epub -> mobi conversion that will need to be dealt with as well. (another exercise for the student. ) This assumes you have perl on your operating system. If you are on Windows, you may need to install perl in order for this to work. Do a Google search for "Active Perl" and go for it. It's free. And as always, if you don't understand what's going on here at all, this post isn't for you. If you have specific questions, and comments / criticisms of the code, then have at it! I only posted this schlock program because nobody else seemed to want to jump in with a better solution. This is what works for me. |
02-27-2011, 08:23 PM | #4 |
Enquiring Mind
Posts: 562
Karma: 42350
Join Date: Aug 2010
Location: London, UK
Device: Kindle 3 (WiFi)
|
Yay! Thank you for posting this, st_albert! Nothing to be embarrassed about - it doesn't need to be all-singing and dancing - just create an HTML TOC, and that's exactly what this does.
Much appreciated! |
02-27-2011, 08:53 PM | #5 |
Guru
Posts: 697
Karma: 150000
Join Date: Feb 2010
Device: none
|
You are very kind m'lady! Hope it is useful to you.
|
02-27-2011, 09:04 PM | #6 |
Resident Curmudgeon
Posts: 76,309
Karma: 136006010
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
It's just so much easier to use Calibre to convert the ePub into Mobi and you will get a ToC as well. So why fiddle with scripts that don't do the full job?
|
02-27-2011, 09:23 PM | #7 |
Guru
Posts: 697
Karma: 150000
Join Date: Feb 2010
Device: none
|
@JSWolf you are absolutely correct, if you are doing the conversion for your own use.
If it is for production, you may want more fine-grained control, however. The script does what I wanted it to do, not what anyone else wanted it to do. The OP asked for it, who knows whether he needed it? If you upload an epub to Amazon, and they (amazon) convert it to a kindle file, will they generate the inline TOC from the toc.ncx? (I don't know -- I'm asking.) |
02-27-2011, 09:27 PM | #8 | |
Resident Curmudgeon
Posts: 76,309
Karma: 136006010
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
Quote:
I think you can upload and already created Mobipocket file and with the OPF file you have plenty of control over how the Mobipocket file comes out. Last edited by JSWolf; 02-27-2011 at 09:33 PM. |
|
02-27-2011, 10:10 PM | #9 |
Guru
Posts: 697
Karma: 150000
Join Date: Feb 2010
Device: none
|
I agree. No argument from me!
|
02-28-2011, 01:10 AM | #10 | ||
Enquiring Mind
Posts: 562
Karma: 42350
Join Date: Aug 2010
Location: London, UK
Device: Kindle 3 (WiFi)
|
Quote:
Quote:
No, they won't. If the inline TOC isn't already present in the EPUB, there won't be one in the MOBI. |
||
02-28-2011, 01:42 AM | #11 |
Enquiring Mind
Posts: 562
Karma: 42350
Join Date: Aug 2010
Location: London, UK
Device: Kindle 3 (WiFi)
|
|
02-28-2011, 04:40 PM | #12 |
Resident Curmudgeon
Posts: 76,309
Karma: 136006010
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
One other thing you can do is load the ePub into Mobipocket Creator for Windows and it will generate a Mobipocket eBook. No idea if that will be a better job with the ToC then Calibre.
|
02-28-2011, 04:43 PM | #13 | |
Resident Curmudgeon
Posts: 76,309
Karma: 136006010
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
Quote:
|
|
02-28-2011, 04:54 PM | #14 | |
Enquiring Mind
Posts: 562
Karma: 42350
Join Date: Aug 2010
Location: London, UK
Device: Kindle 3 (WiFi)
|
Quote:
With a MOBI file, though, and on the Kindle, the NCX file isn't used to present a table of contents to the user - it simply makes it possible to jump to the next or previous navigation point using the 5-way control button. The only kind of TOC presented to the user in a MOBI file is an inline TOC, created as part of the book content. The MOBI reader then needs a guide item (placed in the OPF file when creating the files used to build the MOBI file, then inserted into the HEAD section of the HTML inside the resulting MOBI file) which points to the location of the inline TOC, so that the reader device or app's "Go To... Table of Contents" menu item will link to the correct location in the book content. |
|
02-28-2011, 05:14 PM | #15 |
Resident Curmudgeon
Posts: 76,309
Karma: 136006010
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
Try converting an ePub with just an external ToC and see what you think.
|
Thread Tools | Search this Thread |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
NCX From Html TOC | Unno | Kindle Formats | 20 | 09-16-2011 09:31 AM |
Tags around inline TOC links | jhempel24 | Sigil | 0 | 01-08-2011 06:21 AM |
Kindle inline TOC removed from news in 0.7.20 as a bugfix? | buckcs | Calibre | 4 | 09-25-2010 02:05 PM |
toc.ncx playOrder question | aarcane | ePub | 8 | 05-25-2010 05:00 AM |
Trying to make an Inline TOC | crutledge | Sigil | 2 | 05-09-2010 05:46 AM |