cldf / pycldf

python package to read and write CLDF datasets
https://cldf.clld.org
Apache License 2.0
15 stars 7 forks source link

Add reference method to Source #142

Closed xrotwang closed 3 years ago

xrotwang commented 3 years ago

Getting an human readable "author (year)" citation for a source might be useful. Something along the lines of

def ref(src):
    persons = src.entry.persons.get('author') or src.entry.persons.get('editor', [])
    s = ' '.join(persons[0].last_names)
    if len(persons) == 2:
        s += ' and {}'.format(' '.join(persons[1].last_names))
    elif len(persons) > 2:
        s += ' et al.'
    return s.replace('{', '').replace('}', '') + ' ({})'.format(src['year'])