def json2bib(jsonstring, key, type='article'): """Convert a json string into a Bibentry object.""" if not json: return data = json.loads(jsonstring) # need to remove authors field from data authors = None if 'author' in data: authors = data['author'] del data['author'] if 'issued' in data: data['year'] = str(data['issued']['date-parts'][0][0]) del data['issued'] # delete other problematic fields if 'editor' in data: del data['editor'] entry = Entry(type, fields=data) if authors: for author in authors: entry.add_person(Person(first=author['given'], last=author['family']), 'author') return Bibentry(key, entry).as_string()
def json2bib(jsonstring, key, type='article'): """Convert a json string into a Bibentry object.""" if not json: return data = json.loads(jsonstring) # need to remove authors field from data authors = None if 'author' in data: authors = data['author'] del data['author'] if 'issued' in data: data['year'] = str(data['issued']['date-parts'][0][0]) del data['issued'] # delete other problematic fields if 'editor' in data: del data['editor'] entry = Entry(type, fields=data) if authors: for author in authors: entry.add_person(Person(first=author['given'], last=author['family']), 'author') return Bibentry(key, entry)
def process_entry(self, entry): def process_person(person_entry, role): persons = person_entry.findall(bibtexns + 'person') if persons: for person in persons: process_person(person, role) else: text = person_entry.text.strip() if text: e.add_person(Person(text), role) else: names = {} for name in person_entry.getchildren(): names[remove_ns(name.tag)] = name.text e.add_person(Person(**names), role) id_ = entry.get('id') item = entry.getchildren()[0] type = remove_ns(item.tag) e = Entry(type) for field in item.getchildren(): field_name = remove_ns(field.tag) if field_name in Person.valid_roles: process_person(field, field_name) else: e.fields[field_name] = field.text.strip() return id_, e
def process_entry(self, entry): e = Entry(entry['type']) for (k, v) in entry.iteritems(): if k in Person.valid_roles: for names in v: e.add_person(Person(**names), k) elif k == 'type': pass else: e.fields[k] = unicode(v) return e
def data2bib(data, key, type='article'): """Convert a python dict into a Bibentry object.""" if not data: return # need to remove authors field from data authors = None if 'authors' in data: authors = data['authors'] if isinstance(authors, str): authors = split_name_list(authors) if len(authors) == 1: authors = authors[0].split(',') del data['authors'] entry = Entry(type, fields=data) if authors: for p in authors: entry.add_person(Person(p), 'author') return Bibentry(key, entry)
def data2bib(data, key, type='article'): """Convert a python dict into a Bibentry object.""" if not data: return # need to remove authors field from data authors = None if 'authors' in data: authors = data['authors'] if isinstance(authors, str): authors = split_name_list(authors) if len(authors) == 1: authors = authors[0].split(',') del data['authors'] entry = Entry(type, fields=data) if authors: for p in authors: entry.add_person(Person(p), 'author') return Bibentry(key, entry).as_string()
def get_pybtex_object(obj, texify=True): '''convert from PieObject to a pybtex Entry''' def f_(text, protectcaps=False): if type(text) in (str, unicode): if protectcaps: text = protect_caps( text, PIE_CONFIG.getboolean( 'Format', 'protect_all_caps_in_citations') ) if texify: text = eblc(text) return text else: #this ain't text, don't touch it return text if not obj.BibData_Type: raise KeyError, 'Necessary fields missing - BibTeX type' pybtex_entry = Entry(obj.BibData_Type.lower()) key_set = False for btkey, objfield in bibtexmap.items(): # if btkey == 'publisher': # print 'yes publisher:', getattr(obj, objfield) if btkey == 'author': if obj.AuthorIsCorporate(): pybtex_entry.add_person(Person('{%s}' % obj.Author()), btkey) else: for name in split_name_list(obj.Author()): pybtex_entry.add_person(Person(name), btkey) continue elif btkey == 'editor': if not getattr(obj, objfield): continue for name in split_name_list(getattr(obj, objfield)): pybtex_entry.add_person(Person(name), btkey) continue elif btkey == 'title': pybtex_entry.fields[btkey] = f_(obj.Title(texstuff=True), protectcaps=False) continue elif type(getattr(obj, objfield)) not in (str, unicode): continue elif len(getattr(obj, objfield)) == 0: continue elif btkey == 'url': pybtex_entry.fields[btkey] = f_(obj.Url()) continue elif btkey == 'pie_bibdatakey': pybtex_entry.key = getattr(obj, objfield) key_set = True elif btkey in ('bttype', 'pie_corpauthor', 'pie_datepublished'): continue # elif type(getattr(obj, objfield) == datetime.datetime): # continue else: # if not getattr(obj, objfield): continue pybtex_entry.fields[btkey] = f_(getattr(obj, bibtexmap[btkey])) # if btkey == 'publisher': print 'PUBLISHER SET' if not (obj.ReferDate().day == 1 and obj.ReferDate().month == 1): # hacky hack - if publication date is supposedly 1 January, # then we disbelieve it and assume that only the year has been # set. pybtex_entry.fields['month'] = obj.ReferDate().strftime('%B') if not key_set: pybtex_entry.key = 'nominal_key' pybtex_entry.fields['year'] = obj.ReferDate().strftime('%Y') # pprint(pybtex_entry.fields) return pybtex_entry
def get_pybtex_object(obj): '''convert from PieObject to a pybtex Entry''' if not obj.BibData_Type: raise KeyError, 'Necessary fields missing - BibTeX type' pybtex_entry = Entry(obj.BibData.Type.lower()) for btkey, objfield in bibtexmap: if btkey == 'author': if obj.AuthorIsCorporate(): pybtex_entry.add_person(Person('{%s}' % obj.Author()), btkey) else: for name in split_name_list(obj.Author()): pybtex_entry.add_person(Person(name), btkey) elif btkey == 'editor': for name in split_name_list(obj.Author()): pybtex_entry.add_person(Person(name), btkey) elif btkey == 'title': pybtex_entry.fields[btkey] = obj.Title() else: pybtex_entry.fields[btkey] = getattr(obj, bibtexmap[btkey]) pybtex_entry.fields['month'] = obj.ReferDate().strftime('%B') pybtex_entry.fields['year'] = obj.ReferDate.strftime('%Y') return pybtex_entry