Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Calibre > Editor

Notices

Reply
 
Thread Tools Search this Thread
Old 11-22-2014, 08:14 AM   #1
phossler
Wizard
phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.
 
Posts: 1,071
Karma: 412718
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>
phossler is offline   Reply With Quote
Old 11-22-2014, 08:40 AM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,771
Karma: 22666666
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:]
kovidgoyal is offline   Reply With Quote
Advert
Old 11-22-2014, 11:00 AM   #3
phossler
Wizard
phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.
 
Posts: 1,071
Karma: 412718
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?
Attached Thumbnails
Click image for larger version

Name:	Capture.JPG
Views:	199
Size:	112.8 KB
ID:	131445  
phossler is offline   Reply With Quote
Old 11-22-2014, 11:06 AM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,771
Karma: 22666666
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))
kovidgoyal is offline   Reply With Quote
Old 11-22-2014, 11:38 AM   #5
phossler
Wizard
phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.
 
Posts: 1,071
Karma: 412718
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)
Attached Thumbnails
Click image for larger version

Name:	Capture.JPG
Views:	245
Size:	118.1 KB
ID:	131447  
Attached Files
File Type: epub blah.epub (2.3 KB, 180 views)
phossler is offline   Reply With Quote
Advert
Old 11-22-2014, 11:44 AM   #6
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,771
Karma: 22666666
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.
kovidgoyal is offline   Reply With Quote
Old 11-22-2014, 01:24 PM   #7
phossler
Wizard
phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.
 
Posts: 1,071
Karma: 412718
Join Date: Jan 2009
Location: Valley Forge, PA, USA
Device: Kindle Paperwhite


Yep, I was missing something

Thanks
phossler is offline   Reply With Quote
Old 11-22-2014, 10:28 PM   #8
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Quote:
Originally Posted by kovidgoyal View Post
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.
That seems like a somewhat confusing way of doing things. I assume there must be some good reason behind the scenes why you are doing it, but at least there should maybe be a warning in the manual in the Function Mode section.

Or maybe this was simply the easiest way of doing it before function mode when I guess it really didn't matter?
eschwartz is offline   Reply With Quote
Old 11-22-2014, 10:38 PM   #9
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,771
Karma: 22666666
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.
kovidgoyal is offline   Reply With Quote
Old 11-22-2014, 10:55 PM   #10
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Quote:
Originally Posted by kovidgoyal View Post
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.
Well, I see the logic in starting form the cursor. But in an ebook where there is a particular order to the files, I can see a certain logic in going to the next file in the spine instead of the next tab.

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.
eschwartz is offline   Reply With Quote
Old 11-22-2014, 10:58 PM   #11
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,771
Karma: 22666666
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.
kovidgoyal is offline   Reply With Quote
Old 11-23-2014, 09:51 AM   #12
phossler
Wizard
phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.
 
Posts: 1,071
Karma: 412718
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)
Attached Thumbnails
Click image for larger version

Name:	Capture.JPG
Views:	211
Size:	32.4 KB
ID:	131473  

Last edited by phossler; 11-23-2014 at 10:09 AM.
phossler is offline   Reply With Quote
Old 11-23-2014, 05:35 PM   #13
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Thanks for the change!

https://github.com/kovidgoyal/calibr...e5dc6b5f2708bd
eschwartz is offline   Reply With Quote
Old 11-24-2014, 08:12 AM   #14
phossler
Wizard
phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.
 
Posts: 1,071
Karma: 412718
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))
Great stuff -- thanks
phossler is offline   Reply With Quote
Old 11-24-2014, 08:26 AM   #15
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,771
Karma: 22666666
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'
kovidgoyal is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

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


All times are GMT -4. The time now is 08:21 AM.


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