def pybtex_to_pieberry(key, ent): '''Transform pybtex Entry class to a PieObject''' obj = PieObject() modfields = {} for bkey, val in ent.fields.items(): if bibtexmap.has_key(bkey): modfields[bibtexmap[bkey]] = ublc(val) obj.add_aspect_bibdata(**modfields) obj.BibData_Key = key if not ent.fields.has_key('title'): if ent.fields.has_key('booktitle'): obj.title = ublc(ent.items['booktitle']) else: raise ValueError, 'No title in this entry' for persons in ent.persons.itervalues(): for person in persons: person.text = unicode(person) formatter = Formatter() formatted_names = formatter.format_people(ent) # formatted_names = formatter.format_author_or_editor(ent) rendered_names = formatted_names.render(plaintext.Backend()).rstrip('.') corpnamehere = re_corpname.match(rendered_names) if not ent.fields.has_key('year'): raise Exception, "No valid date for this item" if not ent.fields.has_key('month'): ent.fields['month'] = 'January' datestr = '01 %s %s' % (ent.fields['month'], ent.fields['year']) obj.BibData_DatePublished = datetime.datetime.strptime(datestr, '%d %B %Y') if corpnamehere: rendered_names = '%s%s' % (corpnamehere.group(1), corpnamehere.group(2)) obj.corpauthor = rendered_names else: obj.author = rendered_names obj.BibData_Type = ent.type return obj
def get_formatted_citation(e, key='k', format='html'): '''Return a string containing a formatted citation for a given _PybTeX_ entry (not a PieObject)''' formatter = Formatter() # ents = ((key, e),) ents = (e,) formatted_data = formatter.format_entries(ents) for d in formatted_data: if format == 'html': rendered_data = d.text.render(html.Backend()) else: rendered_data = d.text.render(plaintext.Backend()) # get hrefs in order href_search = re_href.search(rendered_data) output_data = None if format == 'html': if e.fields.has_key('url') and e.fields.has_key('howpublished'): output_data = rendered_data.replace( e.fields['howpublished'], '<a href="%s">%s</a>' % ( ublc(e.fields['url']), e.fields['howpublished'] ) ) elif format == 'plaintext': pass #nothing to be done in this case elif format == 'urlplaintext': if e.fields.has_key('url') and e.fields.has_key('howpublished'): output_data = '%s\n%s' % ( rendered_data, e.fields['url'] ) elif format == 'org': if e.fields.has_key('url') and e.fields.has_key('howpublished'): output_data = rendered_data.replace( e.fields['howpublished'], '[[%s][%s]]' % ( ublc(e.fields['url']), e.fields['howpublished'] ) ) else: raise Exception, 'Invalid format request' if not output_data: output_data = rendered_data #obliterate curly brackets transtbl = {ord('{'): None, ord('}'): None} output_data = output_data.translate(transtbl) return output_data