Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Formats > ePub

Notices

Reply
 
Thread Tools Search this Thread
Old 03-15-2011, 11:35 PM   #1
mostlynovels
Member
mostlynovels doesn't littermostlynovels doesn't littermostlynovels doesn't litter
 
Posts: 10
Karma: 228
Join Date: Jan 2011
Location: USA
Device: past: Bookeen (2yrs); present: laptop(WinXP), 3.5"phone(WinMob6.5)
Use Regex to Code an Inline TOC, from an External TOC's .ncx File

If, for whatever reason, you would like to include an inline TOC in an EPUB, and if the EPUB already has an acceptable external TOC, then you can easily generate most of the html code by using the .ncx file.

I was inspired to find out if this could be done using regex+replace, after reading how to do it with the perl script shared by St_Albert in Post #3 of https://www.mobileread.com/forums/sho...d.php?t=121607 .

The below regex+replace seems to successfully produce html code of <ul><li> for an inline TOC. Multilevels seem to be indented appropriately. If you prefer another element instead of <li> , e.g. <p> , see the note at the end of this post.

If you come up with improvements, please post!


*************************************

1. Make a copy of the .ncx file

2. Use these 3 sets of regex+replace on the copy of the .ncx file, in sequence (adjust for your flavor of regex).
Code:
 #1 search     <navPoint[\w\W]*?>
     replace    <ul class="contents_inline">
 #2 search     </navpoint>
     replace    </ul>
 #3 search     <navLabel>\s*?<text>([\w\W]*?)</text>\s*?</navLabel>\s*?<content src="([\w\W]*?)"/>
     replace    <a href="$2"><li class="contents_inline">$1</li></a>
3. Copy out the relevant html code between the first <ul class="contents_inline"> , and the last </ul> ,and then sandwich it in something like:
Code:
 <div class="contents_inline">
   <h2 class="contents_inline">Table of Contents</h2>
   COPIED HTML CODE
 </div>
4. To NOT display <ul> with bullets, add to the CSS file:
Code:
 ul.contents_inline {
 list-style-type:none !important;
 }
5. The TOC can be further styled in the CSS file as desired, with selectors based on the class contents_inline .

*************************************

NOTE: In the html if you prefer another element, e.g. <p> , instead of <li> , just substitute p in the regex+replace for li and delete the ul tags (i.e. replace with nothing). However then any multilevels won't be automatically indented. To maintain indentation maybe could alternatively substitute in the regex+replace div for ul , and then indent the divs via CSS.
mostlynovels is offline   Reply With Quote
Old 03-16-2011, 12:51 PM   #2
st_albert
Guru
st_albert gives new meaning to the word 'superlative.'st_albert gives new meaning to the word 'superlative.'st_albert gives new meaning to the word 'superlative.'st_albert gives new meaning to the word 'superlative.'st_albert gives new meaning to the word 'superlative.'st_albert gives new meaning to the word 'superlative.'st_albert gives new meaning to the word 'superlative.'st_albert gives new meaning to the word 'superlative.'st_albert gives new meaning to the word 'superlative.'st_albert gives new meaning to the word 'superlative.'st_albert gives new meaning to the word 'superlative.'
 
Posts: 697
Karma: 150000
Join Date: Feb 2010
Device: none
That's pretty slick. I like how it automatically preserves the nested TOC structure (if any) by making nested <ul>'s.
st_albert is offline   Reply With Quote
Advert
Old 03-16-2011, 01:15 PM   #3
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)
Nice! Thanks for sharing this, mostlynovels.
DMSmillie is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inline TOC from toc.ncx elmago79 Kindle Formats 38 03-25-2011 01:56 PM
Saving with old toc.ncx file Haderlump Sigil 1 12-28-2010 01:11 PM
adding a toc.ncx file to the .prc package cyberbaffled Amazon Kindle 4 09-14-2010 03:21 PM
Trying to make an Inline TOC crutledge Sigil 2 05-09-2010 06:46 AM
adjusting toc.ncx file to restore missing chapters viewed in Adobe Digital Editions cyberbaffled ePub 5 12-06-2009 10:44 PM


All times are GMT -4. The time now is 03:23 PM.


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