## this script is made by Alex Travesset ## Date: July 2012 import re import argparse parser = argparse.ArgumentParser(description='Converts a bibtex file to html, which should be later formatted by css.',epilog='The current script only works for the article item, other entries will be added in the future. The output is intentionally ugly. The way this script is intended to work is to cut and paste the output file into your html page and format it using css styles by defining the classes for the div and span tags, see output file for the names of the different classes. If you have questions or comments contact the author: Alex Travesset. Publications in his web page: http://www.public.iastate.edu/~trvsst/Publications/publications.html where done using this script.') parser.add_argument('file_input',metavar='file.bib',help='bibtex file to be converted to html') parser.add_argument('-f',default='Biblio_css.html',metavar='file.html',dest='file_out',help='html output file (default = Biblio_css.html)') parser.add_argument('-O',metavar='n',dest='format_list',type=int,nargs=6,default=range(0,6),help='Order of fields in html file, where 0=title 1=author 2=journal 3=year 4=volume 5=pages (default: 0 1 2 3 4 5)') parser.add_argument('-S',default=3,metavar='n',dest='n_sort',type=int,choices=range(6),help='sorting criteria, where 0=title 1=author 2=journal 3=year 4=volume 5=pages (default: 3)') parser.add_argument('-noreverse',action='store_const',default=True,const=False,metavar='',dest='rev',help='Sorting is done in reverse by defaut, -noreverse overrides the default') args = parser.parse_args() #define the article fields and the numpages field article_fields=['title','author','journal','year','volume','pages','url','doi'] art_numpages='numpages' #sort according to the specified criteria art_field=list(article_fields) #note that the last two fields 6,7 (url and doi remain intact) for n in range(6): art_field[n]=article_fields[args.format_list[n]] #define the sorting criteria sort_field=article_fields[args.n_sort] #temporal variables art_trans=list(art_field) add_pages=art_numpages #define an empty list that will contain all articles List_art=[]; #open the bibtex file f=open(args.file_input,'r') #read the entire list biblio=f.read() f.close() #tag to identify a reference p_article=re.compile('[@]ARTICLE[{]') #split by references refs=p_article.split(biblio) #define p_white=re.compile('[\n]') for ref in refs: for strn in art_field: ini_field=strn+'\s[=]'+'\s[{]' p_prev_stuff=re.compile(ini_field) if(p_prev_stuff.search(ref)): p_elim=re.compile('(?<='+ini_field+').+?(?=[}])',re.DOTALL) m=p_elim.search(ref) #eliminate return characters line=p_white.split(m.group()) f_line='' for li in line: f_line+=li art_trans[art_field.index(strn)]=m.group() #some journals have the annoying habit of using number of pages, not final page ini_field=art_numpages+'\s[=]'+'\s[{]' p_prev_stuff=re.compile(ini_field) if(p_prev_stuff.search(ref)): p_elim=re.compile('(?<='+ini_field+').+?(?=[}])',re.DOTALL) m=p_elim.search(ref) add_pages=m.group() if(add_pages!=art_numpages): #correct the number of pages initial page, final page n_str=art_trans[art_field.index('pages')] art_trans[art_field.index('pages')]=n_str+'-'+str(int(n_str)+int(add_pages)-1) add_pages=art_numpages #add the new bibliography to the entry List_art.append(list(art_trans)) #now we will fix the authors with specified format, which is Lastname, Initials;.. and Lastname, def Sort_author(z): auths=z.split('and') auth_tem='' p_coma=re.compile('[,]') for auth in auths: #if there is a coma (,) Lastname appears first, find if there is a coma if(p_coma.search(auth)): if(auths.index(auth)==0): name=p_coma.split(auth) auth_tem=name[0]+', '+re.search('\w',name[1]).group() elif(auths.index(auth)==len(auths)-1): name=p_coma.split(auth) auth_tem+=' and '+re.search('\w',name[1]).group()+'. '+name[0]+',' else: name=p_coma.split(auth) auth_tem+='; '+re.search('\w',name[1]).group()+'. '+name[0] #if there is no coma then First Name appears first else: name=auth.split() if(auths.index(auth)==0): auth_tem=name[len(name)-1]+', '+re.search('\w',name[0]).group() elif(auths.index(auth)==len(auths)-1): name=auth.split() auth_tem+=' and '+re.search('\w',name[0]).group()+'. '+name[len(name)-1]+',' else: name=auth.split() auth_tem+='; '+re.search('\w',name[0]).group()+'. '+name[len(name)-1] return auth_tem for jj in List_art: jj[art_field.index(article_fields[1])]=Sort_author(jj[art_field.index(article_fields[1])]) #now we will sort according to specified key def SortMy(z): return z[art_field.index(sort_field)] List_art.sort(key=SortMy,reverse=args.rev) #create html file f=open(args.file_out,'w') str_div_op='
' str_div_cl='
' span_list=list(art_field) for strn in art_field: span_list[art_field.index(strn)]='' str_spn_cl='' str_ref='(link)'+str_ref_end f.write(str_new) #add the end tag for article f.write('\n') f.write(str_div_cl) f.write('\n') ##program ends here