Quote:
Originally Posted by rogue_ronin
My first take is that I think the problem comes down to the fact that the (?<author>) function has to include the comma because you have to find the beginning and the end of the name -- if there were separate functions for First and Last you could exclude the comma.
|
I agree - there is no way to
not find the comma as part of the author's name.
Quote:
BTW, your original regex found the Author as "Last, First" not "First Last," in the test window, so I cannot comment to its effectiveness.
|
The reason for the difference is that he has the "Swap author firstname and lastname" option checked.
I think it's an error to keep the comma in the lastname. There's no way to get rid of it that I can see short of fixing the code, so I searched the code:
lines 135 to 144 of meta.py have:
Code:
if prefs['swap_author_names'] and mi.authors:
def swap(a):
parts = a.split()
if len(parts) > 1:
t = parts[-1]
parts = parts[:-1]
parts.insert(0, t)
return ' '.join(parts)
mi.authors = [swap(x) for x in mi.authors]
I'm a rank beginner in python, but I can read this, and if the swap option is checked it makes an array of character strings from the author's name using the split() function, then finds the last element in that array (variable "t") and sticks it at the beginning of that array. Presumably, split() leaves the comma at the end of the next to last element in that array (which becomes the lastname and the last element after swap() runs).
Dropping the last char of the parts[:-1] string, if it is a comma, will work for simple cases. However, this area of the code could probably be improved even more. For example, the code above will change "Tolkien, J R R" to "R Tolkien, J R"
I have the bare minimum of skill to improve this code, but I suspect someone who is more familiar with python, ebooks and the philosophy of calibre could do better. For example, do you want to split the name at the comma, instead of at the last character string, or are commas in author names common? Do you want to do something special with "John T Smith, Jr." or "Smith, John T, Jr." or add more checkbox options or what? Anyone who wants to compile a list of various author name formats, single and multiple that might be encountered and comments on what the code should do in each case could help whoever wants to improve this code.
I'd suggest anyone who wants this improved should add a ticket and get back here to post the ticket number and their comments on
exactly how the improvement should work.
BTW, If anyone wants a simple fix to their own code, adding the two lines below will do it:
Code:
if prefs['swap_author_names'] and mi.authors:
def swap(a):
parts = a.split()
if len(parts) > 1:
t = parts[-1]
parts = parts[:-1]
if parts[-1].endswith(','):
parts[-1]=parts[-1][:-1]
parts.insert(0, t)
return ' '.join(parts)
mi.authors = [swap(x) for x in mi.authors]
For those who haven't ever played with source code or programming, it's not really that hard. Kovid and python have made it easy. In Windows, you just need to get one program (Bazaar) and run it once to retrieve the source code, then set an environment variable to tell calibre to use it. I do simple fixes like this for special cases.
edit:
An even simpler fix is to change the split() in the original code to split(','). This splits on the comma (assumes that the firstname and lastname are separated by a comma - as mine all are). This correctly swaps a name like "Tolkien, J R R."