03-15-2011, 11:35 PM | #1 |
Member
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> Code:
<div class="contents_inline"> <h2 class="contents_inline">Table of Contents</h2> COPIED HTML CODE </div> Code:
ul.contents_inline { list-style-type:none !important; } ************************************* 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. |
03-16-2011, 12:51 PM | #2 |
Guru
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.
|
Advert | |
|
03-16-2011, 01:15 PM | #3 |
Enquiring Mind
Posts: 562
Karma: 42350
Join Date: Aug 2010
Location: London, UK
Device: Kindle 3 (WiFi)
|
Nice! Thanks for sharing this, mostlynovels.
|
|
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 |