Quantcast
Channel: MobileRead Forums - Reading and Management
Viewing all articles
Browse latest Browse all 24022

:help: Auto Numbering With Starting Number Inserted?

$
0
0
I found this would help me reducing time instead editing one by one,

i have to replace h2 with Chapter section with auto numbering,
i dont understand how to implement the function to my desire,

I want to start with number 501 then if matching each of search will generate itself <h2>Chapter 502</h2> to each html files match inside..

any helP?

or can i know how to use Regex Function better?
maybe a reusable collection of it? Please point me to the link ..

Thanks



Code:

From Calibre Function Mode
Auto numbering sections
Now we will see something a little different. Suppose your HTML file has many sections, each with a heading in an <h2> tag that looks like <h2>Some text</h2>. You can create a custom function that will automatically number these headings with consecutive section numbers, so that they look like <h2>1. Some text</h2>.

def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    section_number = '%d. ' % number
    return match.group(1) + section_number + match.group(2)

# Ensure that when running over multiple files, the files are processed
# in the order in which they appear in the book
replace.file_order = 'spine'
Use it with the find expression:

(?s)(<h2[^<>]*>)(.+?</h2>)
Place the cursor at the top of the file and click Replace all.

This function uses another of the useful extra arguments to replace(): the number argument. When doing a Replace All number is automatically incremented for every successive match.

Another new feature is the use of replace.file_order – setting that to 'spine' means that if this search is run on multiple HTML files, the files are processed in the order in which they appear in the book. See Choose file order when running on multiple HTML files for details.


Viewing all articles
Browse latest Browse all 24022

Trending Articles