View Single Post
Old 06-19-2024, 04:49 PM   #3
lomkiri
Groupie
lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.
 
lomkiri's Avatar
 
Posts: 170
Karma: 1497966
Join Date: Jul 2021
Device: N/A
You may have a look on this thread or this message to see how to use a regex-function to numerate by increment

In your case, the function could be something like :
Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    data['counter'] = data.get('counter', 0) + 1
    return f'<h2 class="class_sez">Chapter {data["counter"]}</h2>'
Note : Works only "replacing all"
For a correct numeration, be sure to be on the first page of the epub when starting the "replace all"

If you want to reset the counter at each "Part nn", capture the expression of the title of the part in a group (group 1 in my exemple) with an expression like :
(<h1[^<>]*>Part \d</h1>)|(?:<h2[^<>]*>.+?</h2>)
(up to you to adapt it to your epub)

and use a function like :
Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    
    # Found a new part:
    if match.group(1):
        data['counter'] = 0
        return match.group(0)

    # else, found a chapter:
    data['counter'] = data.get('counter', 0) + 1
    return f'<h2 class="class_sez">Chapter {data["counter"]}</h2>'

Last edited by lomkiri; 06-20-2024 at 01:59 PM.
lomkiri is offline   Reply With Quote