Skip to content

Instantly share code, notes, and snippets.

@LucHermitte
Last active May 6, 2017 12:55
Show Gist options
  • Save LucHermitte/debb2ba869e42c35adca204127d97ede to your computer and use it in GitHub Desktop.
Save LucHermitte/debb2ba869e42c35adca204127d97ede to your computer and use it in GitHub Desktop.
My way of shortening serie names for Calibre
# Python function that generates a shortened name for a serie managed by Calibre
# (c) Luc Hermitte 2017
# GPLv3
#
# The following has been inspired by https://manual.calibre-ebook.com/template_lang.html#using-general-program-mode
# but defined as a Python function as I found nowhere where GPM programs go.
# Copy-paste this function in the Function template (advanced options), name: "initials", number of parameters: 1
# and then add a custom colunm named "initials" defined as "{series:initials()}"
#
# The function strips French and English leading articles, and then keep the stripped serie name if it is
# made of only one word, or keep initial otherwise
# The reason I had to start over from scratch, is to support title with non-ascii characters like for instance "Le chateau de ma mère".
# I also make sure to ignore English "'s".
#
def evaluate(self, formatter, kwargs, mi, locals, title):
import re
stripped = re.sub(r'^(The|A|An|L|Le|Un|Une|Les|La|)\s+', '', title, flags=re.I)
nb_words = (len([v for v in re.findall(r'\w+', stripped, flags=re.UNICODE) if v]))
if nb_words == 1:
return stripped
return re.sub(r"(\w)\w*('s)?\W*", r'\1', stripped, flags=re.UNICODE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment