|
|
#1 |
|
Connoisseur
![]() Posts: 92
Karma: 10
Join Date: Jul 2023
Device: none
|
Trying to use python function replace
Trying to use Python function replace to change strings from all caps to title case. This is the regular expression that I use and it appears to find what I want, but when I click on replace, nothing changes. This is the search that I use:
1\Name=title case 1\Find="(?<=>)([\\w —’]*)(?=</a)" 1\Replace=\\F<titlecase> 1\Controls=RX DN CF Also, where are the Python functions stored on Windows 11? |
|
|
|
|
|
#2 |
|
Sigil Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
|
They are stored in your Sigil Preferences folder.
I will run this test on my 2.7.0 build to see if I can replicate what you are seeing. |
|
|
|
|
|
#3 |
|
Sigil Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
|
Yes a recently introduced bug of some sort. The titlecase python function is somehow getting lost which breaks the apply_func_to_match_groups routine.
I will get you a fix or a workaround. |
|
|
|
|
|
#4 |
|
Connoisseur
![]() Posts: 92
Karma: 10
Join Date: Jul 2023
Device: none
|
|
|
|
|
|
|
#5 |
|
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 29,016
Karma: 210162574
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Yes. The user defined Python functions are stored in the json file in the user's preference directory. They are not kept in a Python script. The builtins like titlecase are part of the Sigil program directory. Namely titlecase.py, fr_utils.py and functionrep.py in the python3lib directory
|
|
|
|
|
|
#6 |
|
Connoisseur
![]() Posts: 92
Karma: 10
Join Date: Jul 2023
Device: none
|
I put some logging code in titlecase.py and it's never called.
|
|
|
|
|
|
#7 |
|
Sigil Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
|
Yes because the routine that invokes it is broken. Somehow the SigilMatch object end() access method is being confused with the end attribute of the match object.
I do not know why that is showing up now and not earlier. Either way, make the following change in functionsearch.py inside the SigilMatch object changing its "beg" and "end" attributes to mtchbeg and mtchend and updating the accessor methods which should prevent the name collision with the end() method and all should work. So after modifications it should look like the following: Code:
class SigilMatch(object):
def __init__(self, text, groupslist):
self.string = text
self.groupslist = groupslist
self.mtchbeg, self.mtchend = groupslist[0]
def start(self):
return self.mtchbeg
def end(self):
return self.mtchend
def span(self, i):
if i < 0 or i >= len(self.groupslist):
raise IndexError("match group index not valid");
return self.groupslist[i][0], self.groupslist[i][1]
def group(self, i=0):
if i < 0 or i >= len(self.groupslist):
raise IndexError("match group index not valid");
return self.string[self.groupslist[i][0] : self.groupslist[i][1]]
def groups(self):
results = []
for i, v in enumerate(self.groupslist):
if i > 0:
results.append(self.string[v[0]: v[1]])
return results
|
|
|
|
|
|
#8 |
|
Sigil Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
|
Please give that a try and let me know how it goes.
|
|
|
|
|
|
#9 |
|
Connoisseur
![]() Posts: 92
Karma: 10
Join Date: Jul 2023
Device: none
|
Still nothing. Here is the changed function:
Code:
class SigilMatch(object):
def __init__(self, text, groupslist):
self.string = text
self.groupslist = groupslist
self.mtchbeg, self.mtchend = groupslist[0]
def start(self):
return self.mtchbeg
def end(self):
return self.mtchend
def span(self, i):
if i < 0 or i >= len(self.groupslist):
raise IndexError("match group index not valid");
return self.groupslist[i][0], self.groupslist[i][1]
def group(self, i=0):
if i < 0 or i >= len(self.groupslist):
raise IndexError("match group index not valid");
return self.string[self.groupslist[i][0] : self.groupslist[i][1]]
def groups(self):
results = []
for i, v in enumerate(self.groupslist):
if i > 0:
results.append(self.string[v[0]: v[1]])
return results
|
|
|
|
|
|
#10 |
|
Sigil Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
|
That is strange, as this change allows all my tests with sub match groups in python function replace for titlecase to now work. Are you sure you are using this new functionsearch.py file?
Please try the simple example shown in the following images. The goal is to capture the text inside the h1 tag and run titlecase on it. Clicking the find button results in the following: See before.png Then click on the replace button and the fixed version of Sigil nicely titlecases the contents of the h1 tag. See the after.png This simple test did not work without the change I posted earlier. No idea why that attribute name and accessor function name collision did not happen in earlier testing but it did now until this fix. I am going to push this change to master so it appears in the next release. So please try a trivial test case like this one with your modified functionsearch.py put in the right place in the python3lib inside Sigil's program. It should all now work. Last edited by KevinH; Yesterday at 08:40 PM. |
|
|
|
|
|
#11 |
|
Sigil Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
|
The above is with the builtin titlecase function unmodified.
|
|
|
|
|
|
#12 |
|
Connoisseur
![]() Posts: 92
Karma: 10
Join Date: Jul 2023
Device: none
|
It works now. I had to close and reopen sigil. Many thanks.
|
|
|
|
|
|
#13 |
|
Sigil Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 9,199
Karma: 6565382
Join Date: Nov 2009
Device: many
|
Glad to hear it works! Thanks for your bug report!
|
|
|
|
![]() |
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Volunteer to write new Chapter on Python Function Replace for Sigil User Guide | KevinH | Sigil | 8 | 06-03-2025 08:11 PM |
| Python function terminated unexpectedly | woodr2011 | Library Management | 12 | 11-04-2019 06:26 PM |
| Python function terminated | stmbtbob | Devices | 2 | 05-08-2015 09:21 AM |
| Python Function Error | LoveMissBubbly | Calibre | 5 | 09-21-2011 05:52 PM |
| txt2html function in python | benluo | Calibre | 0 | 11-15-2010 10:27 AM |