MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Kindle Formats (https://www.mobileread.com/forums/forumdisplay.php?f=168)
-   -   Inline TOC from toc.ncx (https://www.mobileread.com/forums/showthread.php?t=121607)

elmago79 02-15-2011 08:51 PM

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.

DMSmillie 02-16-2011 09:05 PM

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.

st_albert 02-27-2011 08:45 PM

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";

Yes, it's really crude. It doesn't preserve the structure of multi-level TOC's, though that could be easily added. So could I/O parameters for input and output filenames. That is left for an exercise for the student. :D

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. :rofl:)

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.

DMSmillie 02-27-2011 09:23 PM

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! :thumbsup:

st_albert 02-27-2011 09:53 PM

You are very kind m'lady! Hope it is useful to you.

JSWolf 02-27-2011 10:04 PM

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?

st_albert 02-27-2011 10:23 PM

@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.)

JSWolf 02-27-2011 10:27 PM

Quote:

Originally Posted by st_albert (Post 1419779)
@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.)

I'm not saying the script is not what the OP asked for. But, I'm thinking the OP may not know about converting ePub to Mobipocket using Calibre. I've done it and it works very well.

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.

st_albert 02-27-2011 11:10 PM

I agree. :) No argument from me!

DMSmillie 02-28-2011 02:10 AM

Quote:

Originally Posted by JSWolf (Post 1419750)
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?

Quote:

Originally Posted by st_albert (Post 1419779)
@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.

Exactly. That, and the fact that the HTML TOC generated by Calibre is tacked on at the end of the book regardless of where you might want it to appear. And the fact that you might already have spent time hand-crafting exactly what you want included in the TOC when creating the NCX file, but Calibre won't use that to create an inline TOC. Nor will anything else. And the fact that, unless something has changed, a Calibre-created MOBI file won't work on Kindle for PC if you upload it to Amazon and specify that DRM should be added to the ebook.

Quote:

Originally Posted by st_albert (Post 1419779)
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.)

No, they won't. If the inline TOC isn't already present in the EPUB, there won't be one in the MOBI.

DMSmillie 02-28-2011 02:42 AM

Quote:

Originally Posted by JSWolf (Post 1419786)
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.

Not entirely clear how the OPF file would help with the creation of an inline TOC...?

JSWolf 02-28-2011 05:40 PM

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.

JSWolf 02-28-2011 05:43 PM

Quote:

Originally Posted by DMSmillie (Post 1420002)
Exactly. That, and the fact that the HTML TOC generated by Calibre is tacked on at the end of the book regardless of where you might want it to appear. And the fact that you might already have spent time hand-crafting exactly what you want included in the TOC when creating the NCX file, but Calibre won't use that to create an inline TOC. Nor will anything else. And the fact that, unless something has changed, a Calibre-created MOBI file won't work on Kindle for PC if you upload it to Amazon and specify that DRM should be added to the ebook.

But if you have an internal ToC, you end up with two ToC instead of just one.

DMSmillie 02-28-2011 05:54 PM

Quote:

Originally Posted by JSWolf (Post 1421102)
But if you have an internal ToC, you end up with two ToC instead of just one.

In an EPUB, yes. But only because EPUB readers use the NCX file to generate a table of contents to present to the user.

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.

JSWolf 02-28-2011 06:14 PM

Try converting an ePub with just an external ToC and see what you think.


All times are GMT -4. The time now is 06:51 PM.

Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.