def test_bibtex2source(self): from clld.scripts.util import bibtex2source bibtex2source(Record('book', 'id', author='M, R and G, H and Z, U')) bibtex2source(Record('book', 'id', editor='M, R and G, H')) bibtex2source( Record('book', 'id', title='tb', customfield='cf', year="1920}"))
def test_ContextObject(): from clld.lib.coins import ContextObject c = ContextObject('sid', 'journal', ('jtitle', '\xe2')) assert '%C3%A2' in c.span_attrs()['title'] c = ContextObject('sid', 'journal', ('jtitle', binary_type('ä'.encode('utf8')))) assert '%C3%A4' in c.span_attrs()['title'] bib = Record('book', '1', title='The Title', author='L, F') ContextObject('sid', 'book', ('btitle', 'the title')) ContextObject.from_bibtex('sid', bib) bib = Record('article', '1', title='The Title', author='The One and The Other', journal='J') ContextObject.from_bibtex('sid', bib) bib = Record('phdthesis', '1', title='The Title') ContextObject.from_bibtex('sid', bib) bib = Record('conference', '1', title='The Title', booktitle='something') co = ContextObject.from_bibtex('sid', bib) assert isinstance(co.span_attrs(), dict) assert ContextObject('äöü', 'äöü').span_attrs() assert ContextObject('äöü'.encode('latin1'), None).span_attrs()
def test_Record(self): from clld.lib.bibtex import Record, EntryType rec = Record('article', '1', author=['a', 'b'], editor='a and b') self.assertEqual(rec['author'], 'a and b') self.assertEqual(rec.get('author'), rec.getall('author')) self.assertEqual(rec['editor'], rec.get('editor')) self.assertEqual(rec.getall('editor'), ['a', 'b']) rec = Record('book', '1', title='The Title', author='author', editor='ed', booktitle='bt', school='s', issue='i', pages='1-4', publisher='M', note="Revised edition") self.assertIn('@book', rec.__unicode__()) self.assertIn('@book', rec.__str__()) self.assertIn('bt', rec.text()) for fmt in ['txt', 'en', 'ris', 'mods']: rec.format(fmt) Record.from_string(rec.__unicode__(), lowercase=True) Record.from_object(Mock()) rec = Record('incollection', '1', title='The Title', editor='ed', booktitle='bt', school='s', issue='i', pages='1-4', publisher='M', note="Revised edition") self.assertIn('In ', rec.text()) rec = Record('article', '1', title='The Title', journal='The Journal', volume="The volume", issue='issue') self.assertTrue('The Journal' in rec.text()) rec = Record('xmisc', '1', note='Something') self.assertEqual(rec.genre, EntryType.misc) self.assertIn('Something', rec.text())
def test_ContextObject(self): from clld.lib.coins import ContextObject bib = Record('book', '1', title='The Title', author='L, F') co = ContextObject('sid', 'book', ('btitle', 'the title')) co = ContextObject.from_bibtex('sid', bib) bib = Record('article', '1', title='The Title', author='The One and The Other', journal='J') co = ContextObject.from_bibtex('sid', bib) bib = Record('phdthesis', '1', title='The Title') co = ContextObject.from_bibtex('sid', bib) bib = Record('conference', '1', title='The Title', booktitle='something') co = ContextObject.from_bibtex('sid', bib) self.assertTrue(isinstance(co.span_attrs(), dict))
def test_Database(self): from clld.lib.bibtex import Record, Database db = Database([]) self.assertEqual(len(db), 0) db = Database([Record('book', 'id')]) self.assertEqual(db[0], db['id']) assert unicode(db) db = Database.from_file('notexisting.bib') self.assertEqual(len(db), 0) db = Database.from_file(TESTS_DIR.joinpath('test.bib')) self.assertEqual(len(db), 1)
def _get_bibtex(refs): for ref in refs: genre = 'misc' id = ref['id'] attrs = dict(all=ref['text']) t = ref['text'] match = YEAR.search(t) if match: authors = 'editor' if match.group('ed') else 'author' attrs['key'], attrs[authors] = normalized_author(t[:match.start()].strip()) attrs['title'], rem = [s.strip() for s in re.split('\.|\?', t[match.end():], 1)] attrs['year'] = match.group('year') attrs['key'] = '%(key)s %(year)s' % attrs m = EDS.match(rem) if m: assert 'editor' not in attrs attrs['editor'] = normalized_author(m.group('eds').strip())[1] genre = 'incollection' rem = rem[m.end():].strip() mm = BTITLE_PAGES.match(rem) if mm: attrs['booktitle'] = mm.group('btitle').strip() attrs['pages'] = mm.group('pages').strip() rem = rem[mm.end():].strip() else: mm = JOURNAL.match(rem) if mm: genre = 'article' attrs['journal'] = mm.group('journal').strip() attrs['volume'] = mm.group('volume').strip() if mm.group('number'): attrs['number'] = mm.group('number').strip() attrs['pages'] = mm.group('pages').strip() rem = rem[mm.end():].strip() m = PUBLISHER.match(rem) if m: if genre == 'misc': genre = 'book' attrs['place'] = m.group('place').strip() attrs['publisher'] = m.group('publisher').strip() rem = rem[m.end():].strip() _rem = [] for piece in [p.strip() for p in re.split('\.(?:\s+|$)', rem) if p.strip()]: if piece.startswith('http') and not re.search('\s+', piece): attrs['url'] = piece elif piece.startswith('(') and piece.endswith(')'): attrs['note'] = piece[1:-1].strip() else: _rem.append(piece) rem = '. '.join(_rem) if not slug(unicode(rem)): del attrs['all'] yield Record(genre, id, **attrs)
def test_Database(self): from clld.lib.bibtex import Record, Database db = Database([]) self.assertEqual(len(db), 0) db = Database([Record('book', 'id')]) self.assertEqual(db[0], db['id']) assert text_type(db) db = Database.from_file('notexisting.bib') self.assertEqual(len(db), 0) db = Database.from_file(TESTS_DIR.joinpath('test.bib')) self.assertEqual(len(db), 1) assert '@' in db[0]['title'] assert [r for r in db] self.assertRaises(NotImplementedError, db.format, 'txt')
def test_Record(self): from clld.lib.bibtex import Record rec = Record('book', '1', title='The Title', editor='ed', booktitle='bt', school='s', issue='i', pages='1-4', publisher='M') self.assertTrue('@book' in rec.__unicode__()) self.assertTrue('@book' in rec.__str__()) self.assertTrue('The Title' in rec.text()) for fmt in ['txt', 'en', 'ris', 'mods']: rec.format(fmt) rec = Record.from_string(rec.__unicode__()) rec = Record.from_object(Mock())
def test_bibtex2source(self): from clld.scripts.util import bibtex2source bibtex2source(Record('book', 'id', title='tb', customfield='cf', year="1920}"))
# -*- coding: utf-8 -*- from clld.lib.bibtex import Record from clldutils.misc import slug CFG = { 'EXPORTS': [], 'PUBLICATIONS': [ Record( 'ARTICLE', 'HammarstroemEtAl2011Oslo', ('author', u'Harald Hammarström and Sebastian Nordhoff'), ('year', '2011'), ('title', 'LangDoc: Bibliographic Infrastructure for Linguistic Typology'), ('journal', 'Oslo Studies in Language'), ('volume', '3'), ('number', '2'), ('pages', '31-43'), ('url', 'https://www.journals.uio.no/index.php/osla/article/view/75/199'), ), Record( 'UNPUBLISHED', 'HammarstroemEtAl2011Howmany', ('author', u"Hammarström, Harald and Nordhoff, Sebastian"), ('year', u"2011"), ('title', u"How many languages have so far been described?"), ('howpublished', u"Paper presented at NWO Endangered Languages Programme Conference, Leiden, April 2011" ), ),