diff --git a/pubs/bibstruct.py b/pubs/bibstruct.py index a8312b0..66f6ae6 100644 --- a/pubs/bibstruct.py +++ b/pubs/bibstruct.py @@ -57,7 +57,7 @@ def valid_citekey(citekey): return not '/' in citekey -def generate_citekey(bibdata): +def generate_citekey(bibdata, format_string='%A%Y'): """ Generate a citekey from bib_data. :raise ValueError: if no author nor editor is defined. @@ -72,8 +72,16 @@ def generate_citekey(bibdata): try: year = entry['year'] except KeyError: - year = '' - citekey = '{}{}'.format(''.join(author_last(first_author)), year) + raise ValueError( + "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) diff --git a/pubs/commands/add_cmd.py b/pubs/commands/add_cmd.py index b932a08..cf67fa9 100644 --- a/pubs/commands/add_cmd.py +++ b/pubs/commands/add_cmd.py @@ -119,7 +119,10 @@ def command(conf, args): citekey = args.citekey 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) elif citekey in rp: ui.error('citekey already exist {}.'.format(citekey)) diff --git a/pubs/config/spec.py b/pubs/config/spec.py index 4c50f4c..a067ed4 100644 --- a/pubs/config/spec.py +++ b/pubs/config/spec.py @@ -31,6 +31,17 @@ note_extension = string(default='txt') # the full python stack is printed. 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] # Enable bold formatting, if the terminal supports it. diff --git a/pubs/version.py b/pubs/version.py index b4e3540..21320a8 100644 --- a/pubs/version.py +++ b/pubs/version.py @@ -1 +1 @@ -__version__ = '0.8.3' +__version__ = '0.8.4'