pubs/pubs/pretty.py
Fabien C. Y. Benureau dc4e118c3c make utf8 citekeys possible in python 2.7. closes #28
This involved many changes, some side effects of the change include:
- remove of all `u"abc"` forms, in favor of
  `from __future__ import unicode_literals`. Their usage was
  inconsistent anyway, leading to problems when mixing with
  unicode content.
- improve the tests, to allow printing for usecase even when
  crashing. Should make future test easier. This is done with a
  rather hacky `StdIO` class in `p3`, but it works.
- for some reason, the skipped test for Python 2 seems to work
  now. While the previous point might seem related, it is not clear
  that this is actually the case.
2018-04-10 14:45:54 +09:00

72 lines
2.0 KiB
Python

from __future__ import unicode_literals
import re
from . import color
from .bibstruct import TYPE_KEY
CHARS = re.compile('[{}\n\t\r]')
def sanitize(s):
return CHARS.sub('', s)
# should be adaptated to bibtexparser dicts
def person_repr(p):
raise NotImplementedError
return ' '.join(s for s in [
' '.join(p.first(abbr=True)),
' '.join(p.last(abbr=False)),
' '.join(p.lineage(abbr=True))] if s)
def short_authors(bibdata):
try:
authors = [p for p in bibdata['author']]
if len(authors) < 3:
return ' and '.join(authors)
else:
return authors[0] + (' et al.' if len(authors) > 1 else '')
except KeyError: # When no author is defined
return ''
def bib_oneliner(bibdata):
authors = short_authors(bibdata)
journal = ''
if 'journal' in bibdata:
journal = ' ' + bibdata['journal']
elif bibdata[TYPE_KEY] == 'inproceedings':
journal = ' ' + bibdata.get('booktitle', '')
return sanitize('{authors} \"{title}\"{journal}{year}'.format(
authors=color.dye_out(authors, 'author'),
title=color.dye_out(bibdata.get('title', ''), 'title'),
journal=color.dye_out(journal, 'publisher'),
year=' ({})'.format(color.dye_out(bibdata['year'], 'year'))
if 'year' in bibdata else ''
))
def bib_desc(bib_data):
article = bib_data[list(bib_data.keys())[0]]
s = '\n'.join('author: {}'.format(p)
for p in article['author'])
s += '\n'
s += '\n'.join('{}: {}'.format(k, v) for k, v in article.items())
return s
def paper_oneliner(p, citekey_only=False):
if citekey_only:
return p.citekey
else:
bibdesc = bib_oneliner(p.bibdata)
tags = '' if len(p.tags) == 0 else '| {}'.format(
','.join(color.dye_out(t, 'tag') for t in sorted(p.tags)))
return '[{citekey}] {descr} {tags}'.format(
citekey=color.dye_out(p.citekey, 'citekey'),
descr=bibdesc, tags=tags)