Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Sigil

Notices

Reply
 
Thread Tools Search this Thread
Old Today, 04:23 PM   #1
philja
Evangelist
philja rocks like Gibraltar!philja rocks like Gibraltar!philja rocks like Gibraltar!philja rocks like Gibraltar!philja rocks like Gibraltar!philja rocks like Gibraltar!philja rocks like Gibraltar!philja rocks like Gibraltar!philja rocks like Gibraltar!philja rocks like Gibraltar!philja rocks like Gibraltar!
 
Posts: 423
Karma: 100100
Join Date: Nov 2015
Location: Europe EEC
Device: Kindle Fire HD6 & HD8
help on python replace function needed

Converting an epub2 to epub3, I found that another thing needing fixing was that the <title> in the <head> contained the book title rather than the Chapter title.

I made a regex Find which selected the first few lines :
Code:
(?is)(<title\b[^>]*>).*?(</title>)(.*?<h1\b[^>]*>(?:\s*<[^>]+>\s*)*([^<].+?)\s*</a></h1>)

where: 
\1 = <title>
\2 = </title>
\3 = everything after </title> up to and including </h1>
\4 = the chapter title
This worked fine because the chapter structure was consistent throughout the book. But then it occurred to me that it might be an opportunity to try a python function replace. I'm not a code writer let alone a python coder. So I took inspiration from the examples included in Sigil although I couldn't get the capitalize example function to work (lowercase, titlecase and uppercase were ok). I arrived at the following:
Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    title_open = match[1]
    title_close = match[2]
    between = match[3]
    chapter_title = match[4].strip()

    return title_open + chapter_title + title_close + between
This did not work, which was not really a surprise to me, but it intrigues me not to understand why.

The full regex Find/Replace works fine, but I'm sure it would be helpful to understand more about python function replace. I don't find anything in the latest downloadable user guide and what is in the online version doesn't help me, sadly.

Where did I go wrong, and any recommendations for a useful, simple and easy text on the subject?
philja is offline   Reply With Quote
Old Today, 04:56 PM   #2
BeckyEbook
Wizard
BeckyEbook ought to be getting tired of karma fortunes by now.BeckyEbook ought to be getting tired of karma fortunes by now.BeckyEbook ought to be getting tired of karma fortunes by now.BeckyEbook ought to be getting tired of karma fortunes by now.BeckyEbook ought to be getting tired of karma fortunes by now.BeckyEbook ought to be getting tired of karma fortunes by now.BeckyEbook ought to be getting tired of karma fortunes by now.BeckyEbook ought to be getting tired of karma fortunes by now.BeckyEbook ought to be getting tired of karma fortunes by now.BeckyEbook ought to be getting tired of karma fortunes by now.BeckyEbook ought to be getting tired of karma fortunes by now.
 
BeckyEbook's Avatar
 
Posts: 1,029
Karma: 3761621
Join Date: Jan 2017
Location: Poland
Device: Various
Try this:
Code:
def replace(match, number, file_name, metadata, data):
	if match:
		title_open = match.group(1)
		title_close = match.group(2)
		between = match.group(3)
		chapter_title = match.group(4).strip()

		return title_open + chapter_title + title_close + between
Perhaps the problem is also that you put \s* before the closing </a></h1>, which forces a space to appear before the anchor closes.

To debug functions, it's a good idea to enable the SIGIL_FUNCTION_REPLACE_LOGFILE environment variable; that way, you'll see the errors in the log file.
Attached Thumbnails
Click image for larger version

Name:	sigil-function-debug.png
Views:	7
Size:	18.8 KB
ID:	225958  
BeckyEbook is offline   Reply With Quote
Advert
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to use python function replace jwes Sigil 26 01-13-2026 10:06 AM
Volunteer to write new Chapter on Python Function Replace for Sigil User Guide KevinH Sigil 8 06-03-2025 07:11 PM
Python function terminated stmbtbob Devices 2 05-08-2015 08:21 AM
Python Function Error LoveMissBubbly Calibre 5 09-21-2011 04:52 PM
txt2html function in python benluo Calibre 0 11-15-2010 09:27 AM


All times are GMT -4. The time now is 07:44 PM.


MobileRead.com is a privately owned, operated and funded community.