View Single Post
Old 10-22-2023, 08:35 AM   #6
Phssthpok
Age improves with wine.
Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.Phssthpok knows how to set a laser printer to stun.
 
Posts: 584
Karma: 95229
Join Date: Nov 2014
Device: Kindle Oasis, Kobo Libra II
Quote:
Originally Posted by capink View Post
Depending on where you want to insert the text, you can utilize the replace function mode with the function below
Code:
import regex

def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    if not file_name.lower().startswith('chapter'):
        return match.group()
    name = regex.sub(r'(.+?)(\d+)\.x?html?', r'\1 \2', file_name).title()
    header = f'<h1>{name}</h1>'
    return regex.sub('(<body[^>]*>)', r'\1' + '\n' + header, match.group(), regex.MULTILINE)
The search string should be: .+
Then press replace all.

This will insert the chapter heading as a h1 header at the very top (just below <body>)
Ah, excellent, many thanks!
Phssthpok is offline   Reply With Quote