Implement configuration for default citekey formating

main
Jonas Kulhanek 5 years ago
parent c991e5875c
commit ffeeb4311b

@ -57,7 +57,7 @@ def valid_citekey(citekey):
return not '/' in citekey return not '/' in citekey
def generate_citekey(bibdata): def generate_citekey(bibdata, format_string='%A%Y'):
""" 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.
@ -72,8 +72,16 @@ def generate_citekey(bibdata):
try: try:
year = entry['year'] year = entry['year']
except KeyError: except KeyError:
year = '' raise ValueError(
citekey = '{}{}'.format(''.join(author_last(first_author)), year) "No author or editor defined: cannot generate a citekey.")
try:
first_word = entry['title'].split(' ')[0]
except KeyError:
first_word = ''
author_last_name = author_last(first_author)
citekey = format_string.replace('%Y', year).replace('%y', year[-2:]) \
.replace('%A', author_last_name).replace('%a', author_last_name.lower()) \
.replace('%W', first_word).replace('%w', first_word.lower())
return str2citekey(citekey) return str2citekey(citekey)

@ -119,7 +119,10 @@ def command(conf, args):
citekey = args.citekey citekey = args.citekey
if citekey is None: if citekey is None:
base_key = bibstruct.generate_citekey(bibentry) if conf['main']['normalize_citekey']:
base_key = bibstruct.generate_citekey(bibentry, conf['main']['citekey_format'])
else:
base_key = bibstruct.extract_citekey(bibentry)
citekey = rp.unique_citekey(base_key, bibentry) citekey = rp.unique_citekey(base_key, bibentry)
elif citekey in rp: elif citekey in rp:
ui.error('citekey already exist {}.'.format(citekey)) ui.error('citekey already exist {}.'.format(citekey))

@ -31,6 +31,17 @@ note_extension = string(default='txt')
# the full python stack is printed. # the full python stack is printed.
debug = boolean(default=False) debug = boolean(default=False)
# If true the citekey is normalized using the 'citekey_format' on adding new publications.
normalize_citekey = boolean(default=False)
# String specifying how to format the citekey. The following
# substitutions are used:
# %a: last name of the first author in lowercase
# %A: last name of the first author in PascalCase
# %Y: four letter year of release (e.g. 2019)
# %y: two last letters of release year (e.g. 19)
citekey_format = string(default='%a%Y')
[formating] [formating]
# Enable bold formatting, if the terminal supports it. # Enable bold formatting, if the terminal supports it.

@ -1 +1 @@
__version__ = '0.8.3' __version__ = '0.8.4'

Loading…
Cancel
Save