Quote:
Originally Posted by theducks
Maybe a fork of the Language cleaner plugin
That replaces $#+% words with others
|
I don't know this plugin, and I don't know witch solution is the easiest, but anyway, since I was writing my function while you were writing your message, here it is (it still is simple):
Quote:
Originally Posted by moldy
Thank you Lokiri - that would be very helpful if you did that and it will probably save me a lot of time tomorrow.
|
Put your json file in the config-folder of calibre.
If you make a "replace all", it will load the file only once, during the first passage.
Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
"""
Replace John|Paul|George|Ringo by Mick, Kieth, Ronnie or Charlie,
using a dict {"John": "Mick", ...}
This dict is loaded from a json file, plaaced in the config folder of calibre.
"""
from calibre.utils.config import JSONConfig
m = match[0]
# first passage
# Load the dict from a json file and store it in the persistent dict "data"
# (see the help of calibre for more details about the dict "data")
if number == 1:
fname = 'beastones.json'
data['equiv'] = JSONConfig(fname)
if not data['equiv']:
print(f'Problem loading {fname}, no treatment will be done')
# normal treatment
return data['equiv'].get(m, m) # if the json file was not found, no changes are made