You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.0 KiB

from __future__ import unicode_literals
11 years ago
import unicodedata
import re
from .p3 import ustr, uchr
# Citekey stuff
11 years ago
TYPE_KEY = 'type'
CONTROL_CHARS = ''.join(map(uchr, list(range(0, 32)) + list(range(127, 160))))
CITEKEY_FORBIDDEN_CHARS = '@\'\\,#}{~%/ ' # '/' is OK for bibtex but forbidden
11 years ago
# here since we transform citekeys into filenames
CITEKEY_EXCLUDE_RE = re.compile('[%s]'
% re.escape(CONTROL_CHARS + CITEKEY_FORBIDDEN_CHARS))
def str2citekey(s):
key = unicodedata.normalize('NFKD', ustr(s)).encode('ascii', 'ignore').decode()
11 years ago
key = CITEKEY_EXCLUDE_RE.sub('', key)
# Normalize chars and remove non-ascii
return key
def check_citekey(citekey):
# TODO This is not the right way to test that (17/12/2012)
if ustr(citekey) != str2citekey(citekey):
raise ValueError(u"Invalid `{}` citekey; ".format(citekey) +
u"utf-8 citekeys are not supported yet.\n"
u"See https://github.com/pubs/pubs/issues/28 for details.")
11 years ago
def verify_bibdata(bibdata):
if bibdata is None or len(bibdata) == 0:
raise ValueError(u"no valid bibdata")
if len(bibdata) > 1:
raise ValueError(u"ambiguous: multiple entries in the bibdata.")
11 years ago
def get_entry(bibdata):
verify_bibdata(bibdata)
for e in bibdata.items():
return e
11 years ago
def extract_citekey(bibdata):
citekey, entry = get_entry(bibdata)
return citekey
def author_last(author_str):
""" Return the last name of the author """
return author_str.split(',')[0]
11 years ago
def generate_citekey(bibdata):
""" Generate a citekey from bib_data.
:param generate: if False, return the citekey defined in the file,
11 years ago
does not generate a new one.
:raise ValueError: if no author nor editor is defined.
"""
citekey, entry = get_entry(bibdata)
author_key = 'author' if 'author' in entry else 'editor'
11 years ago
try:
first_author = entry[author_key][0]
11 years ago
except KeyError:
raise ValueError(
u"No author or editor defined: cannot generate a citekey.")
11 years ago
try:
year = entry['year']
11 years ago
except KeyError:
year = ''
citekey = u'{}{}'.format(u''.join(author_last(first_author)), year)
11 years ago
return str2citekey(citekey)
11 years ago
def extract_docfile(bibdata, remove=False):
""" Try extracting document file from bib data.
Returns None if not found.
:param remove: remove field after extracting information (default: False)
"""
try:
if 'file' in bibdata:
field = bibdata['file']
# Check if this is mendeley specific
for f in field.split(':'):
if len(f) > 0:
break
if remove:
bibdata.pop('file')
# This is a hck for Mendeley. Make clean
if f[0] != '/':
f = '/' + f
return f
if 'attachments' in bibdata:
return bibdata['attachments']
if 'pdf' in bibdata:
return bibdata['pdf']
11 years ago
except (KeyError, IndexError):
return None