|
|
#1 |
|
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 125
Karma: 38500
Join Date: Sep 2016
Location: San Jose, CA
Device: Kindle moving to Kobo or Boox
|
Using a TOC to create Chapter headings
I have an EPUB file that just uses images as the chapters names. I'm trying to condense the file since it's so large because of all the graphics. The TOC is correct and usable.
I've written some Regex-Func functions for simpler stuff but this is harder for me to wrap my brain around. Here's a sample in the TOC <p class="toc1"><a href="part0013.html#CCNA1-f66f6b0d51c44ea49012bf2fe61db1ae" class="toc_text"><strong class="calibre1">9. </strong> The Chickens Draw First Blood</a></p> <p class="toc1"><a href="part0014.html#DB7S1-f66f6b0d51c44ea49012bf2fe61db1ae" class="toc_text"><strong class="calibre1">10. </strong> My Singing Makes Things Worse, and Everyone Is Totally Shocked</a></p> #part0013.html: <body class="calibre"> <div class="fullimage" id="DB7S1-f66f6b0d51c44ea49012bf2fe61db1ae"><img alt="" src="../images/00021.jpeg" class="calibre3"/></div> I can see how to do this by brute-force: unzip the EPUB and using Python directly on the HTML files (I'm capable of that). But surely there are possible other tools available. Thank you for suggestions or pointers. ![]() [EDIT] Here's what I have so far (which is missing most of it I know): [EDIT2] I'm pretty my code start below is not going in the right direction. ![]() Code:
from bs4 import BeautifulSoup
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
soup = BeautifulSoup(html, "html.parser")
string = soup.a.text.split('.')
chap_num = string[0].strip()
chap_title = string[1].strip()
id = soup.a['href'].split('#')
# not sure how to go to/check the next item in the TOC
# not sure how to place the output onto the right page
return f'<h2>{chap_num} {chap_title}</h2>'
Last edited by meghane_e; Today at 03:38 PM. Reason: Adding to function as I go |
|
|
|
|
|
#2 |
|
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 125
Karma: 38500
Join Date: Sep 2016
Location: San Jose, CA
Device: Kindle moving to Kobo or Boox
|
So... I found this pre-written function change_title_of_page_to_chapter_name.
Is this a step in the right direction? Code:
# Use expression: <(h[123]) [^<>]* id=['"]([^'"]+)['"][^<>]*>([^<>]+)
from calibre import replace_entities
from calibre.ebooks.oeb.polish.toc import TOC, toc_to_html
from calibre.gui2.tweak_book import current_container
from calibre.ebooks.oeb.base import xml2str
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
if match is None:
# All matches found, output the resulting Table of Contents.
# The argument metadata is the metadata of the book being edited
if 'toc' in data:
toc = data['toc']
root = TOC()
for (file_name, tag_name, anchor, text) in toc:
parent = root.children[-1] if tag_name == 'h2' and root.children else root
parent.add(text, file_name, anchor)
toc = toc_to_html(root, current_container(), 'toc.html', 'Table of Contents for ' + metadata.title, metadata.language)
print (xml2str(toc))
else:
print ('No headings to build ToC from found')
else:
# Add an entry corresponding to this match to the Table of Contents
if 'toc' not in data:
# The entries are stored in the data object, which will persist
# for all invocations of this function during a 'Replace All' operation
data['toc'] = []
tag_name, anchor, text = match.group(1), replace_entities(match.group(2)), replace_entities(match.group(3))
data['toc'].append((file_name, tag_name, anchor, text))
return match.group() # We don't want to make any actual changes, so return the original matched text
# Ensure that we are called once after the last match is found so we can
# output the ToC
replace.call_after_last_match = True
# Ensure that when running over multiple files, this function is called,
# the files are processed in the order in which they appear in the book
replace.file_order = 'spine'
|
|
|
|
|
|
#3 |
|
Resident Curmudgeon
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 81,083
Karma: 150263703
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
You can then do a search.replace regex to replace the graphic chapter headers with text.
|
|
|
|
|
|
#4 |
|
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 125
Karma: 38500
Join Date: Sep 2016
Location: San Jose, CA
Device: Kindle moving to Kobo or Boox
|
I wish I could understand what you mean lol. So the problem is on the chapter pages there is no text only a html code for a graphic
<div class="fullimage" id="DB7S1-f66f6b0d51c44ea49012bf2fe61db1ae"><img alt="" src="../images/00021.jpeg" class="calibre3"/></div> The only place in the book that has the text is the TOC (page0003.html) with html like this: Code:
<p class="toc1"><a href="part0013.html#CCNA1-f66f6b0d51c44ea49012bf2fe61db1ae" class="toc_text"><strong class="calibre1">9. </strong> The Chickens Draw First Blood</a></p> <p class="toc1"><a href="part0014.html#DB7S1-f66f6b0d51c44ea49012bf2fe61db1ae" class="toc_text"><strong class="calibre1">10. </strong> My Singing Makes Things Worse, and Everyone Is Totally Shocked</a></p> I can do a search on the following to get the ID Code:
<div class="fullimage" id="([^"]+)"><img alt="" src="../images/000\d+.jpeg" class="calibre3"/></div> I definitely want the function to return Code:
return f'<h2 class="chapter-heading">{new_chapter_title}</h2>'
UPDATE: It seems like this line needs to be adjusted since I'm not using (<h[1|2|3]) in the Search? Code:
tag_name, anchor, text = match.group(1), replace_entities(match.group(2)), replace_entities(match.group(3)) Last edited by meghane_e; Today at 04:51 PM. |
|
|
|
![]() |
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to use command line to automatically create ToC from all headings? | Whip | Editor | 23 | 09-05-2024 04:11 PM |
| create a useable TOC when book uses images for chapter headings | stumped | Sigil | 23 | 06-09-2019 04:12 PM |
| Help with Chapter seperators (lines underneath chapter headings) | indieauthor83 | Sigil | 9 | 06-23-2017 07:01 AM |
| Issue With Chapter Headings and TOC | yoss15 | Kindle Formats | 5 | 02-07-2012 02:54 PM |
| Managing HTML Link Behavior, From TOC to Chapter Headings | FlooseMan Dave | Calibre | 1 | 04-01-2010 12:55 AM |