|  11-22-2014, 08:14 AM | #1 | 
| Wizard            Posts: 1,090 Karma: 447222 Join Date: Jan 2009 Location: Valley Forge, PA, USA Device: Kindle Paperwhite | 
				
				Function Mode - Not a feature request
			 
			
			Don't know any python so a small request Cook booking the example for auto numbering (changed to H1's) and if I re-run it then it adds another number (I was even able to figure out how to add the word 'Chapter' all by myself   ) Code: def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    section_number = '%d. ' % number
    return match.group(1) + 'Chapter ' +  section_number + match.group(2)Q - Can someone tell me the tweak to have the function to delete any leading text up to and including the space after the '.' first? If files are rearranged or added or deleted then numbers will be off and re-running the RegEx-Function just adds more. I could do a 'regular' regular expression, but adding the logic to the function would be much handier Code: <?xml version='1.0' encoding='utf-8'?> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <title>blah</title> </head> <body> <h1>Chapter 4. Chapter 4. Chapter 4. blah</h1> </body> </html> | 
|   |   | 
|  11-22-2014, 08:40 AM | #2 | 
| creator of calibre            Posts: 45,604 Karma: 28548974 Join Date: Oct 2006 Location: Mumbai, India Device: Various | Code: def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    section_number = '%d. ' % number
    return match.group(1) + 'Chapter ' +  section_number + match.group(2).partition('.')[2][1:] | 
|   |   | 
| Advert | |
|  | 
|  11-22-2014, 11:00 AM | #3 | 
| Wizard            Posts: 1,090 Karma: 447222 Join Date: Jan 2009 Location: Valley Forge, PA, USA Device: Kindle Paperwhite | 
			
			Thanks, but it didn't seem to keep the original text Did I do it right? | 
|   |   | 
|  11-22-2014, 11:06 AM | #4 | 
| creator of calibre            Posts: 45,604 Karma: 28548974 Join Date: Oct 2006 Location: Mumbai, India Device: Various | 
			
			That assumes that there is already a Chapter x. prefix. If you want to handle both the case where there is a prefix and one where there is not, use this: Code: import re
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    section_number = '%d. ' % number
    return match.group(1) + 'Chapter ' +  section_number + re.sub(r'^Chapter \d+\. ', '', match.group(2)) | 
|   |   | 
|  11-22-2014, 11:38 AM | #5 | 
| Wizard            Posts: 1,090 Karma: 447222 Join Date: Jan 2009 Location: Valley Forge, PA, USA Device: Kindle Paperwhite | 
			
			thanks Another hopefully not too dumb question I had the 4 files in 'normal' order (start.xhtml, two.xhtml, three.xhtml, four.xhtml) and ran the code from post#4 several times and it did not keep adding so that works just fine If I re-arrange the files (four.xhtml, two.xhtml, three.xhtml, start.xhtml) and rerun the F&R again, the first file in logical order (four.xhtml) keeps the Chapter 4, but the sequence number resets to 1 with the second file in logical order (two.xhtml) and goes 2 and 3 Do I need to set something to 1 to get the counter's working 1,2,3,4? Thanks (and sorry to be a pest) | 
|   |   | 
| Advert | |
|  | 
|  11-22-2014, 11:44 AM | #6 | 
| creator of calibre            Posts: 45,604 Karma: 28548974 Join Date: Oct 2006 Location: Mumbai, India Device: Various | 
			
			If you want a consistent numbering across files, your best bet is to close all the open file tabs, otherwise the order will be somewhat random, because find and replace starts at the current cursor position and moves on to the end fo the file, then the next file and so on, until it wraps around to the start of the first file and goes from the top of the file to the cursor position. IIRC, if none of the files are open, all text files are processed in 'spine' order, that is, the order they appear in in the Files Browser. | 
|   |   | 
|  11-22-2014, 01:24 PM | #7 | 
| Wizard            Posts: 1,090 Karma: 447222 Join Date: Jan 2009 Location: Valley Forge, PA, USA Device: Kindle Paperwhite |  Yep, I was missing something Thanks | 
|   |   | 
|  11-22-2014, 10:28 PM | #8 | |
| Ex-Helpdesk Junkie            Posts: 19,421 Karma: 85400180 Join Date: Nov 2012 Location: The Beaten Path, USA, Roundworld, This Side of Infinity Device: Kindle Touch fw5.3.7 (Wifi only) | Quote: 
 Or maybe this was simply the easiest way of doing it before function mode when I guess it really didn't matter? | |
|   |   | 
|  11-22-2014, 10:38 PM | #9 | 
| creator of calibre            Posts: 45,604 Karma: 28548974 Join Date: Oct 2006 Location: Mumbai, India Device: Various | 
			
			Search and replace always goes from the cursor onwards, in pretty much every implementation of S&R in an editor. It allows for much more control.
		 | 
|   |   | 
|  11-22-2014, 10:55 PM | #10 | |
| Ex-Helpdesk Junkie            Posts: 19,421 Karma: 85400180 Join Date: Nov 2012 Location: The Beaten Path, USA, Roundworld, This Side of Infinity Device: Kindle Touch fw5.3.7 (Wifi only) | Quote: 
 I don't particularly think it must be changed though, because I haven't really thought about the pros and cons, and I bow to your superior knowledge.  I do think if that is the way it works, it should be noted in the documentation for Function Mode, as a potentially common pitfall.   | |
|   |   | 
|  11-22-2014, 10:58 PM | #11 | 
| creator of calibre            Posts: 45,604 Karma: 28548974 Join Date: Oct 2006 Location: Mumbai, India Device: Various | 
			
			The proper solution is to add a function annotation, like call_after_last_match that tells the editor to run the search in spine order or reverse spine order, when searching on multiple files.
		 | 
|   |   | 
|  11-23-2014, 09:51 AM | #12 | 
| Wizard            Posts: 1,090 Karma: 447222 Join Date: Jan 2009 Location: Valley Forge, PA, USA Device: Kindle Paperwhite | 
			
			I was thinking maybe a new F&R checkbox [X] Spine Order that just started the F&R at file#1 instead of the current file Up/Down and Wrap would be unchanged (probably) Last edited by phossler; 11-23-2014 at 10:09 AM. | 
|   |   | 
|  11-23-2014, 05:35 PM | #13 | 
| Ex-Helpdesk Junkie            Posts: 19,421 Karma: 85400180 Join Date: Nov 2012 Location: The Beaten Path, USA, Roundworld, This Side of Infinity Device: Kindle Touch fw5.3.7 (Wifi only) | |
|   |   | 
|  11-24-2014, 08:12 AM | #14 | 
| Wizard            Posts: 1,090 Karma: 447222 Join Date: Jan 2009 Location: Valley Forge, PA, USA Device: Kindle Paperwhite | 
			
			So would it look like this when the capability is in the next version? Code: mport re
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    section_number = '%d. ' % number
    replace.file_order = 'spine'
    return match.group(1) + 'Chapter ' +  section_number + re.sub(r'^Chapter \d+\. ', '', match.group(2))  | 
|   |   | 
|  11-24-2014, 08:26 AM | #15 | 
| creator of calibre            Posts: 45,604 Karma: 28548974 Join Date: Oct 2006 Location: Mumbai, India Device: Various | 
			
			Not quite Code: import re
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    section_number = '%d. ' % number    
    return match.group(1) + 'Chapter ' +  section_number + re.sub(r'^Chapter \d+\. ', '', match.group(2))
replace.file_order = 'spine' | 
|   |   | 
|  | 
| 
 | 
|  Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post | 
| Function mode: Feature Request | jbacelar | Editor | 2 | 11-22-2014 02:36 AM | 
| Function mode in editor S&R -- coming soon | eschwartz | Editor | 12 | 11-21-2014 08:26 AM | 
| Error in function mode in editor S&R | jbacelar | Editor | 3 | 11-21-2014 05:34 AM | 
| Feature Request at the import-function | ralphffm44 | Calibre | 4 | 04-02-2010 01:14 PM | 
| Feature Request - cover browse in the "Fetch Metadata from server" function | nathander13 | Calibre | 1 | 01-30-2010 02:11 PM |