Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Formats > Kindle Formats

Notices

Reply
 
Thread Tools Search this Thread
Old 02-15-2011, 08:51 PM   #1
elmago79
Member
elmago79 began at the beginning.
 
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.
elmago79 is offline   Reply With Quote
Old 02-16-2011, 09:05 PM   #2
DMSmillie
Enquiring Mind
DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'
 
DMSmillie's Avatar
 
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.
DMSmillie is offline   Reply With Quote
 
Advertisement
Old 02-27-2011, 08:45 PM   #3
st_albert
Fanatic
st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.
 
Posts: 552
Karma: 94420
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";
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.

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.
st_albert is offline   Reply With Quote
Old 02-27-2011, 09:23 PM   #4
DMSmillie
Enquiring Mind
DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'
 
DMSmillie's Avatar
 
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!
DMSmillie is offline   Reply With Quote
Old 02-27-2011, 09:53 PM   #5
st_albert
Fanatic
st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.
 
Posts: 552
Karma: 94420
Join Date: Feb 2010
Device: none
You are very kind m'lady! Hope it is useful to you.
st_albert is offline   Reply With Quote
Old 02-27-2011, 10:04 PM   #6
JSWolf
Resident Curmudgeon
JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.
 
JSWolf's Avatar
 
Posts: 38,637
Karma: 19637653
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Aura H2O, Sony PRS-650, Sony PRS-T1, nook STR, iPad 1, iPhone 5
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?
JSWolf is offline   Reply With Quote
Old 02-27-2011, 10:23 PM   #7
st_albert
Fanatic
st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.
 
Posts: 552
Karma: 94420
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.)
st_albert is offline   Reply With Quote
Old 02-27-2011, 10:27 PM   #8
JSWolf
Resident Curmudgeon
JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.
 
JSWolf's Avatar
 
Posts: 38,637
Karma: 19637653
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Aura H2O, Sony PRS-650, Sony PRS-T1, nook STR, iPad 1, iPhone 5
Quote:
Originally Posted by st_albert View Post
@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.

Last edited by JSWolf; 02-27-2011 at 10:33 PM.
JSWolf is offline   Reply With Quote
Old 02-27-2011, 11:10 PM   #9
st_albert
Fanatic
st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.st_albert has top level security clearance to Area 51.
 
Posts: 552
Karma: 94420
Join Date: Feb 2010
Device: none
I agree. No argument from me!
st_albert is offline   Reply With Quote
Old 02-28-2011, 02:10 AM   #10
DMSmillie
Enquiring Mind
DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'
 
DMSmillie's Avatar
 
Posts: 562
Karma: 42350
Join Date: Aug 2010
Location: London, UK
Device: Kindle 3 (WiFi)
Quote:
Originally Posted by JSWolf View Post
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 View Post
@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 View Post
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 is offline   Reply With Quote
Old 02-28-2011, 02:42 AM   #11
DMSmillie
Enquiring Mind
DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'
 
DMSmillie's Avatar
 
Posts: 562
Karma: 42350
Join Date: Aug 2010
Location: London, UK
Device: Kindle 3 (WiFi)
Quote:
Originally Posted by JSWolf View Post
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...?
DMSmillie is offline   Reply With Quote
Old 02-28-2011, 05:40 PM   #12
JSWolf
Resident Curmudgeon
JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.
 
JSWolf's Avatar
 
Posts: 38,637
Karma: 19637653
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Aura H2O, Sony PRS-650, Sony PRS-T1, nook STR, iPad 1, iPhone 5
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 is offline   Reply With Quote
Old 02-28-2011, 05:43 PM   #13
JSWolf
Resident Curmudgeon
JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.
 
JSWolf's Avatar
 
Posts: 38,637
Karma: 19637653
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Aura H2O, Sony PRS-650, Sony PRS-T1, nook STR, iPad 1, iPhone 5
Quote:
Originally Posted by DMSmillie View Post
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.
JSWolf is offline   Reply With Quote
Old 02-28-2011, 05:54 PM   #14
DMSmillie
Enquiring Mind
DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'DMSmillie understands when you whisper 'The dog barks at midnight.'
 
DMSmillie's Avatar
 
Posts: 562
Karma: 42350
Join Date: Aug 2010
Location: London, UK
Device: Kindle 3 (WiFi)
Quote:
Originally Posted by JSWolf View Post
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.
DMSmillie is offline   Reply With Quote
Old 02-28-2011, 06:14 PM   #15
JSWolf
Resident Curmudgeon
JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.
 
JSWolf's Avatar
 
Posts: 38,637
Karma: 19637653
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Aura H2O, Sony PRS-650, Sony PRS-T1, nook STR, iPad 1, iPhone 5
Try converting an ePub with just an external ToC and see what you think.
JSWolf is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
NCX From Html TOC Unno Kindle Formats 20 09-16-2011 10:31 AM
Tags around inline TOC links jhempel24 Sigil 0 01-08-2011 07:21 AM
Kindle inline TOC removed from news in 0.7.20 as a bugfix? buckcs Calibre 4 09-25-2010 03:05 PM
toc.ncx playOrder question aarcane ePub 8 05-25-2010 06:00 AM
Trying to make an Inline TOC crutledge Sigil 2 05-09-2010 06:46 AM


All times are GMT -4. The time now is 04:40 AM.


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