View Single Post
Old 10-18-2023, 07:12 PM   #4
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,103
Karma: 1954138
Join Date: Aug 2015
Device: Kindle
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>)

Last edited by capink; 10-18-2023 at 07:17 PM.
capink is offline   Reply With Quote