Update citekey generation: "short_title"

main
Jonas Kulhanek 3 years ago
parent bc802e2bab
commit 9856193952

@ -1,7 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
try: try:
import __builtin__ import __builtin__
except: except Exception:
# Python 3.x # Python 3.x
import builtins import builtins
if 'unicode' not in builtins.__dict__.keys(): if 'unicode' not in builtins.__dict__.keys():
@ -63,7 +63,8 @@ def author_last(author_str):
def valid_citekey(citekey): def valid_citekey(citekey):
"""Return if a citekey is a valid filename or not""" """Return if a citekey is a valid filename or not"""
# FIXME: a bit crude, but efficient for now (and allows unicode citekeys) # FIXME: a bit crude, but efficient for now (and allows unicode citekeys)
return not '/' in citekey return '/' not in citekey
class CitekeyFormatter(Formatter): class CitekeyFormatter(Formatter):
def __init__(self): def __init__(self):
@ -80,19 +81,18 @@ class CitekeyFormatter(Formatter):
s = val s = val
return str2citekey(s.__format__(fmt)) return str2citekey(s.__format__(fmt))
def get_value(self, key, args, entry): def get_value(self, key, args, entry):
if isinstance(key, (str, unicode)): if isinstance(key, (str, unicode)):
okey = key okey = key
if key == 'author' and not 'author' in entry: if key == 'author' and 'author' not in entry:
key = 'editor' key = 'editor'
elif key == 'editor' and not 'editor' in entry: elif key == 'editor' and 'editor' not in entry:
key = 'author' key = 'author'
if key == 'first_word' and 'title' in entry:
return entry['title'].split(' ')[0]
if key == 'author_last_name' and 'author' in entry: if key == 'author_last_name' and 'author' in entry:
return author_last(entry['author'][0]) return author_last(entry['author'][0])
if key == 'short_title' and 'title' in entry:
return get_first_word(entry['title'])
else: else:
if key in entry: if key in entry:
return entry[key] return entry[key]
@ -102,7 +102,18 @@ class CitekeyFormatter(Formatter):
else: else:
raise ValueError('Key must be a str instance') raise ValueError('Key must be a str instance')
def generate_citekey(bibdata, format_string='{author_last_name}{year}'):
def get_first_word(title):
"""
Returns the first word of the title as used in Google Scholar or Arxiv citekeys
"""
title = re.split(r'[^a-zA-Z0-9]', title)
word_blacklist = {'and', 'on', 'in', 'of', 'the', 'a', 'an', 'at'}
word = next((x for x in title if x and x.lower() not in word_blacklist), None)
return word
def generate_citekey(bibdata, format_string='{author_last_name}{year}{short_title}'):
""" Generate a citekey from bib_data. """ Generate a citekey from bib_data.
:raise ValueError: if no author nor editor is defined. :raise ValueError: if no author nor editor is defined.

@ -40,15 +40,16 @@ normalize_citekey = boolean(default=False)
# substitutions are used: # substitutions are used:
# author_last_name: last name of the first author # author_last_name: last name of the first author
# year: year of publication # year: year of publication
# first_word: first word of the title # short_title: first word of the title (excluding words such as "the", "an", ...)
# modifiers: # modifiers:
# l: converts the text to lowercase # l: converts the text to lowercase
# u: converts the text to uppercase # u: converts the text to uppercase
# examples: # examples:
# {{author_last_name:l}}{{year}} generates 'yang2020' # {{author_last_name:l}}{{year}} generates 'yang2020'
# {{author_last_name}}{{year}}{{first_word}} generates 'Yang2020Towards' # {{author_last_name}}{{year}}{{short_title}} generates 'Yang2020Towards'
# {{author_last_name:l}}{{year}}{{short_title:l}} generates 'yang2020towards'
# {{author_last_name:u}}{{year}} generates 'YANG2020' # {{author_last_name:u}}{{year}} generates 'YANG2020'
citekey_format = string(default='{{author_last_name:l}}{{year}}') citekey_format = string(default='{{author_last_name:l}}{{year}}{{short_title:l}}')
[formating] [formating]

Loading…
Cancel
Save