def get_inbook_template(self, e): # Required fields: author/editor, title, chapter/pages, publisher, year # Optional fields: volume, series, address, edition, month, note, key return toplevel[ sentence(sep=' ')[ self.format_names('author'), join["(", date, ")"] ], self.format_title(e, 'title'), sentence(sep=' ')[ optional[ "In ", editor_names(), "," ], self.format_btitle(e, 'booktitle', as_sentence=False), optional[ join[ "(", sentence(add_period=False)[ optional[field('edition'), ' ed.'], self.format_volume(e), pages, ], ")" ] ] ], sentence(sep=': ')[ optional_field('address'), field('publisher'), ], sentence[optional_field('note')], ]
def get_incollection_template(self, e): # Required fields: author, title, booktitle, year # Optional fields: editor, pages, organization, publisher, address, # month, note, key return toplevel[self.format_author_or_editor_and_date(e), self.format_title(e, 'title'), sentence(sep=' ')[self.format_btitle(e, 'booktitle'), optional["(", pages, ")"]], sentence(sep=': ')[optional_field('address'), field('publisher'), ]]
def format_author_or_editor_and_date(self, e): if 'author' in e.persons and 'editor' in e.persons: return sentence(sep=' ')[self.format_names('author'), together["(", date, ")."], self.format_editor(e, as_sentence=False)] elif 'author' in e.persons: return sentence(sep=' ')[self.format_names('author'), together["(", date, ")"]] else: return sentence(sep=' ')[self.format_editor(e, as_sentence=False), together["(", date, ")"]]
def format_patent(self, e): template = toplevel [ self.format_title(e, 'title'), sentence(capfirst=False) [ date, ], href [ field('howpublished'), sentence(capfirst=False) [ field('note') ], ], self.format_web_refs(e), ] return template.format_data(e)
def format_inproceedings(self, e): template = toplevel[ sentence[self.format_names('author')], self.format_title(e, 'title'), words['In', sentence(capfirst=False)[ optional[self.format_editor(e, as_sentence=False)], self.format_btitle(e, 'booktitle', as_sentence=False), self.format_volume_and_series(e, as_sentence=False), optional[pages], ], self.format_address_organization_publisher_date(e), ], sentence(capfirst=False)[optional_field('note')], self.format_web_refs(e), ] return template.format_data(e)
def format_article(self, e): volume_and_pages = first_of[ # volume and pages, with optional issue number optional[join[tag('bold')[field('volume')], optional['(', field('number'), ')'], ':', pages], ], # pages only words['pages', pages], ] template = toplevel[self.format_names('author'), self.format_title(e, 'title'), sentence( capfirst=False)[tag('emph')[field('journal')], optional[volume_and_pages], date], sentence(capfirst=False)[optional_field('note')], ] return template.format_data(e)
def get_article_template(self, e): template = toplevel[ first_name('author'), field('title'), '/', names('author', sep=', '), '//', sentence(sep='. – ')[ tag('em')[field('journal')], date, optional[join[words['V.', field('volume')], optional[words[', Is.', field('number')]], optional[words['. – P.', pages]]]]], sentence(sep=' ')[optional[self.format_doi(e)], optional[join['(', field('note'), ')']]] ] return template
def format_proceedings(self, e): template = toplevel [ first_of [ # there are editors optional [ join(' ')[ self.format_editor(e), sentence [ self.format_btitle(e, 'title', as_sentence=False), self.format_volume_and_series(e, as_sentence=False), self.format_address_organization_publisher_date(e), ], ], ], # there is no editor optional_field('organization'), sentence [ self.format_btitle(e, 'title', as_sentence=False), self.format_volume_and_series(e, as_sentence=False), self.format_address_organization_publisher_date( e, include_organization=False), ], ], sentence(capfirst=False) [ optional_field('note') ], ] return template.format_data(e)
def format_volume_and_series(self, e, as_sentence=True): volume_and_series = optional [ words [ 'volume', field('volume'), optional [ words ['of', field('series')] ] ] ] number_and_series = optional [ words [ join(sep=Symbol('nbsp')) ['number', field('number')], optional [ words ['in', field('series')] ] ] ] series = optional_field('series') result = first_of [ volume_and_series, number_and_series, series, ] if as_sentence: return sentence(capfirst=False) [result] else: return result
def format_unpublished(self, e): template = toplevel[sentence[self.format_names('author')], self.format_title(e, 'title'), sentence(capfirst=False)[field('note'), optional[date]], self.format_web_refs(e), ] return template.format_data(e)
def format_title(self, e, which_field, as_sentence=True): def protected_capitalize(x): """Capitalize string, but protect {...} parts.""" result = Text() level = 0 for pos, c in enumerate(x): if c == '{': level += 1 c = '' elif c == '}': level -= 1 c = '' elif pos == 0: c = c.upper() elif level <= 0: c = c.lower() result += c return result formatted_title = field( which_field, apply_func=protected_capitalize) if as_sentence: return sentence(capfirst=False) [ formatted_title ] else: return formatted_title
def format_volume_and_series(self, e, as_sentence=True): # FIXME: evaluate the field 'language' and translate volume_and_series = optional [ words [ 'volume', field('volume'), optional [ words ['of', field('series')] ] ] ] number_and_series = optional [ words [ join(sep=Symbol('nbsp')) ['number', field('number')], optional [ words ['in', field('series')] ] ] ] series = optional_field('series') result = first_of [ volume_and_series, number_and_series, series, ] if as_sentence: return sentence(capfirst=True) [result] else: return result
def format_inbook(self, e): template = toplevel [ tag('pubtitle') [tag('b') [field('title')]], Symbol('br'), sentence [self.format_names('author')], self.format_volume_and_series(e), Symbol('br'), words [ sentence(capfirst=True) [ self.format_chapter_and_pages(e), #optional[ self.format_editor(e, as_sentence=False) ], self.format_btitle(e, 'booktitle', as_sentence=False), #self.format_volume_and_series(e, as_sentence=False), #self.format_chapter_and_pages(e), ], Symbol('br'), sentence [ field('publisher'), optional_field('address'), optional [ words [field('edition'), 'edition'] ], #date, #Symbol('br'), #optional_field('note'), ], ], Symbol('br'), sentence [ optional_field('note'), ], Symbol('br'), #self.format_web_refs(e), ] return template.format_data(e)
def format_title(self, e, which_field, as_sentence=False): def protected_capitalize(x): """Capitalize string, but protect {...} parts.""" result = "" level = 0 for pos, c in enumerate(x): if c == '{': level += 1 elif c == '}': level -= 1 elif pos == 0: c = c.upper() elif level <= 0: c = c.lower() result += c return result # formatted_title = field(which_field) formatted_title = tag('pubtitle') [ tag('b') [field(which_field)]] # formatted_title = field( ABC ERROR # which_field, apply_func=protected_capitalize) if as_sentence: return sentence(capfirst=False) [tag('b') [ formatted_title ]] else: return formatted_title
def format_title(self, e, which_field, as_sentence=True): formatted_title = field(which_field) if as_sentence: return sentence(capfirst=False)[formatted_title] else: return formatted_title
def format_inproceedings(self, e): template = toplevel [ sentence [self.format_names('author')], self.format_title(e, 'title'), words [ 'In', sentence(capfirst=False) [ optional[ self.format_editor(e, as_sentence=False) ], self.format_btitle(e, 'booktitle', as_sentence=False), self.format_volume_and_series(e, as_sentence=False), optional[ pages ], ], self.format_address_organization_publisher_date(e), ], sentence(capfirst=False) [ optional_field('note') ], ] return template.format_data(e)
def format_web_refs(self, e): # Based on urlbst output.web.refs return sentence(add_period=False)[ optional[self.format_url(e)], optional[self.format_eprint(e)], optional[self.format_pubmed(e)], optional[self.format_doi(e)], ]
def format_misc(self, e): template = toplevel[optional[sentence[self.format_names('author')]], optional[self.format_title(e, 'title')], sentence[optional[field('howpublished')], optional[date], ], sentence(capitalize=False)[optional_field('note')], self.format_web_refs(e), ] return template.format_data(e)
def format_web_refs(self, e): # based on urlbst output.web.refs return sentence(capfirst=False) [ optional [ self.format_url(e) ], optional [ self.format_eprint(e) ], optional [ self.format_pubmed(e) ], optional [ self.format_doi(e) ], ]
def format_names(self, role, as_sentence=True): formatted_names = names(role, sep=', ', sep2=' and ', last_sep=', and ') if as_sentence: return sentence(capfirst=False)[formatted_names] else: return formatted_names
def format_title(self, e, which_field, as_sentence=True): from pybtex.bibtex.utils import change_case formatted_title = field(which_field, apply_func=lambda text: change_case(text, 't')) if as_sentence: return sentence(capitalize=False)[formatted_title] else: return formatted_title
def format_title(self, e, which_field, as_sentence=True): from pybtex.bibtex.utils import change_case formatted_title = field( which_field, apply_func=lambda text: change_case(text, 't')) if as_sentence: return sentence(capitalize=False) [ formatted_title ] else: return formatted_title
def format_phdthesis(self, e): template = toplevel[sentence[self.format_names('author')], self.format_btitle(e, 'title'), sentence['PhD thesis', field('school'), optional_field('address'), date, ], sentence(capfirst=False)[optional_field('note')], self.format_web_refs(e), ] return template.format_data(e)
def format_mastersthesis(self, e): template = toplevel[sentence[self.format_names('author')], self.format_title(e, 'title'), sentence["Master's thesis", field('school'), optional_field('address'), date, ], sentence(capitalize=False)[optional_field('note')], self.format_web_refs(e), ] return template.format_data(e)
def get_phdthesis_template(self, e): # Required fields: author, title, school, year # Optional fields: address, month, note, key return toplevel[ sentence(sep=' ')[ self.format_names('author'), join["(", date, ")"] ], sentence(sep=' ')[ self.format_btitle(e, 'title', as_sentence=False), "(Doctoral dissertation)" ], sentence[ field('school'), optional_field('address'), ], sentence[optional_field('note')], ]
def format_book(self, e): template = toplevel[self.format_author_or_editor(e), self.format_btitle(e, 'title'), self.format_volume_and_series(e), sentence[field('publisher'), optional_field('address'), self.format_edition(e), date], sentence(capfirst=False)[optional_field('note')], self.format_web_refs(e), ] return template.format_data(e)
def format_web_refs(self, e): # based on urlbst output.web.refs # Adds adsurl with link return sentence(capfirst=False) [ optional [ self.format_url(e) ], optional [ self.format_adsurl(e) ], optional [ self.format_eprint(e) ], optional [ self.format_pubmed(e) ], optional [ self.format_doi(e) ], ]
def format_dsa(self, e): # based on urlbst format.dsa return words['DSA:', href[join['https://datasheet.datasheetarchive.com/', field('dsa'), '.pdf'], field('dsa', apply_func=lambda x: x.split('/')[-1])], join[sentence(capfirst=False, add_period=False)[ '(PDF', optional_field('dsasize'), optional_field('dsadate'), ], ')']]
def format_unpublished(self, e): template = toplevel [ sentence [self.format_names('author')], self.format_title(e, 'title'), sentence(capfirst=False) [ field('note'), optional[ date ] ], ] return template.format_data(e)
def format_manual(self, e): # TODO this only corresponds to the bst style if author is non-empty # for empty author we should put the organization first template = toplevel[optional[sentence[self.format_names('author')]], self.format_btitle(e, 'title'), sentence[optional_field('organization'), optional_field('address'), self.format_edition(e), optional[date], ], sentence(capfirst=False)[optional_field('note')], self.format_web_refs(e), ] return template.format_data(e)
def format_misc(self, e): template = toplevel [ optional[ sentence [self.format_names('author')] ], optional[ self.format_title(e, 'title') ], sentence[ optional[ field('howpublished') ], optional[ date ], ], sentence(capfirst=False) [ optional_field('note') ], ] return template.format_data(e)
def get_inproceedings_template(self, e): # Required fields: author, title, booktitle, year # Optional fields: editor, pages, organization, publisher, address, # month, note, key return toplevel[ self.format_authors(e), self.format_title(e, 'title'), sentence(sep=' ')[ "In", self.format_btitle(e, 'booktitle', as_sentence=False), ], sentence[optional_field('note')], ]
def get_proceedings_template(self, e): # Required fields: title, year # Optional fields: editor, publisher, organization, address, month, # note, key return toplevel[ sentence(sep=' ')[ first_of[ optional[self.format_editor(e, as_sentence=False)], optional_field('organization'), "None to claim their bones" ], join["(", first_of[optional[date], "n.d."], ")"] ], self.format_btitle(e, 'title'), sentence(sep=': ')[ optional_field('address'), optional_field('publisher'), ], sentence[optional_field('note')], ]
def format_techreport(self, e): template = toplevel[sentence[self.format_names('author')], self.format_title(e, 'title'), sentence[words[first_of[optional_field('type'), 'Technical Report', ], optional_field('number'), ], field('institution'), optional_field('address'), date, ], sentence(capfirst=False)[optional_field('note')], self.format_web_refs(e), ] return template.format_data(e)
def format_article(self, e): template = toplevel [ self.format_names('author'), self.format_title(e, 'title'), join [tag('emph') [field('journal')], ' ', tag('strong')[field('volume')], ', ', unsrt.pages, ' (', field('year'), ')'], sentence(capfirst=False) [ optional_field('note') ], self.format_web_refs(e), ] return template.format_data(e)
def format_ghd(self, e): # based on urlbst format.ghd return words['GDR-HC Download:', href[ join['https://hc-ddr.hucki.net/wiki/lib/exe/fetch.php/', field('ghd')], field('ghd', apply_func=lambda x: x.split('/')[-1].split('.', 1)[0])], join['(', sentence(capfirst=False, add_period=False)[ optional_field('ghdtype'), optional_field('ghdsize'), optional_field('ghddate')], ')']]
def format_lpns(self, e): # based on urlbst format.dsa return words['Li-Pro.Net Store:', href[ join['http://store.li-pro.net/', field('lpns')], field('lpns', apply_func=lambda x: x.split('/')[-1].split('.', 1)[0])], join['(', sentence(capfirst=False, add_period=False)[ optional_field('lpnstype'), optional_field('lpnssize'), optional_field('lpnsdate')], ')']]
def get_unpublished_template(self, e): # Required fields: author, title, note # Optional fields: month, year, key template = toplevel[ sentence(sep=' ')[ self.format_names('author', as_sentence=False), join["(", first_of[optional[date], "n.d."], ")"] ], self.format_btitle(e, 'title'), sentence[field('note')], ] return template
def format_volume_and_series(self, e): volume_and_series = optional [ sentence(capfirst=False, sep=' ') [ 'Volume', field('volume'), optional [ words ['of', field('series')] ] ] ] number_and_series = optional [ sentence(capfirst=False, sep=' ') [ join(sep=Symbol('nbsp')) ['Number', field('number')], optional [ words ['in', field('series')] ] ] ] series = optional [ sentence(capfirst=False) [field('series')] ] return first_of [ volume_and_series, number_and_series, series, ]
def format_phdthesis(self, e): template = toplevel [ sentence [self.format_names('author')], self.format_btitle(e, 'title'), sentence[ 'PhD thesis', field('school'), optional_field('address'), date, ], sentence(capfirst=False) [ optional_field('note') ], ] return template.format_data(e)
def format_book(self, e): template = toplevel [ self.format_author_or_editor(e), self.format_btitle(e, 'title'), self.format_volume_and_series(e), sentence [ field('publisher'), optional_field('address'), self.format_edition(e), date ], sentence(capfirst=False) [ optional_field('note') ], ] return template.format_data(e)
def format_mastersthesis(self, e): template = toplevel [ sentence [self.format_names('author')], self.format_title(e, 'title'), sentence[ "Master's thesis", field('school'), optional_field('address'), date, ], sentence(capfirst=False) [ optional_field('note') ], self.format_web_refs(e), ] return template.format_data(e)
def format_article(self, e): volume_and_pages = first_of [ # volume and pages, with optional issue number optional [ join [ field('volume'), optional['(', field('number'),')'], ':', pages ], ], # pages only words ['pages', pages], ] template = toplevel [ self.format_names('author'), self.format_title(e, 'title'), sentence(capfirst=False) [ tag('emph') [field('journal')], optional[ volume_and_pages ], date], sentence(capfirst=False) [ optional_field('note') ], ] return template.format_data(e)
def format_manual(self, e): # TODO this only corresponds to the bst style if author is non-empty # for empty author we should put the organization first template = toplevel [ optional [ sentence [ self.format_names('author') ] ], self.format_btitle(e, 'title'), sentence [ optional_field('organization'), optional_field('address'), self.format_edition(e), optional[ date ], ], sentence(capfirst=False) [ optional_field('note') ], ] return template.format_data(e)
def format_editor(self, e, as_sentence=True): editors = self.format_names('editor', as_sentence=False) if 'editor' not in e.persons: # when parsing the template, a FieldIsMissing exception # will be thrown anyway; no need to do anything now, # just return the template that will throw the exception return editors if len(e.persons['editor']) > 1: word = 'editors' else: word = 'editor' result = join(sep=', ') [editors, word] if as_sentence: return sentence(capfirst=False) [result] else: return result
def format_techreport(self, e): template = toplevel [ sentence [self.format_names('author')], self.format_title(e, 'title'), sentence [ words[ first_of [ optional_field('type'), 'Technical Report', ], optional_field('number'), ], field('institution'), optional_field('address'), date, ], sentence(capfirst=False) [ optional_field('note') ], ] return template.format_data(e)
def format_incollection(self, e): template = toplevel [ sentence [self.format_names('author')], self.format_title(e, 'title'), words [ 'In', sentence(capfirst=False) [ optional[ self.format_editor(e, as_sentence=False) ], self.format_btitle(e, 'booktitle', as_sentence=False), self.format_volume_and_series(e, as_sentence=False), self.format_chapter_and_pages(e), ], ], sentence [ optional_field('publisher'), optional_field('address'), self.format_edition(e), date, ], ] return template.format_data(e)
def format_names(role): return words()[names(role, sep=', ', sep2=' and ', last_sep=', and ')] formats = { 'article': words(sep='')[ '\n - | ', words(sep=' ')[ format_names('author'), brackets(field('year'))], ',', '\n | ', bold(field('title')), ',', '\n | ', sentence(sep=', ')[ italic(field('journal')), optional[words(sep=' ')[ field('volume'), optional[brackets(field('number'))]]], optional[field('pages')], ], optional['\n | ', field('url')] ], 'book': words(sep='')[ '\n - | ', words(sep=' ')[ format_names('author'), brackets(field('year'))], ',', '\n | ', bold(field('title')), ',', '\n | ', sentence(sep=', ')[ optional[field('edition')], optional[field('series')], optional[field('edition')],
def format_names(self, role): return sentence(capfirst=False) [names(role, sep=', ', sep2 = ' and ', last_sep=', and ')]
def format_names(self, role, as_sentence=True): formatted_names = names(role, sep=', ', sep2 = ' and ', last_sep=', and ') if as_sentence: return sentence(capfirst=False) [formatted_names] else: return formatted_names