#!/usr/bin/python # Program mref.py converts typical .tex file into BibTex format # with MR links by reference to MR database using MREF # Written by Bartlomiej Siudeja # Based on script mrefh.py by Jim Pitman pitman@stat.berkeley.edu 10/10/03. # Uses standard input and standard output. # e.g. on unix system, save program in a file mrefh.py then do # chmod +x mref.py # then starting from a file refs.bbl to create refs.html usage is # cat refs.tex | mrefh.py > refs.bib import urllib,string,sys,re flag = r'\bibitem' def mref(texstring): m=re.search("\{([\w\-]*)\}",texstring) texstring=re.sub("(\W*)bibitem(\s*)\{([\w\-]*)\}","",texstring) front = r'http://www.ams.org/mathscinet-mref?ref=' back = r'&mref-submit=Search&dataType=bibtex' address = '%s%s%s' % (front,urllib.quote_plus(texstring),back) infile = urllib.urlopen(address) while 1: line = infile.readline() if string.find(line,'No Unique Match Found') >=0 : if m: print '\n@article {'+m.group(1)+',\n TITLE = {'+texstring+'}\n}' sys.stderr.write('\n!! No Match for: '+m.group(1)+' !!\n') else: print '\n@article {!!!,\n TITLE = {'+texstring+'}\n}' sys.stderr.write('\n!! No Match for: '+texstring+' !!\n') break if not line : break if string.find(line,'Matched') >= 0: break while 1: if string.find(line,'No Unique Match Found') >=0 : break line = infile.readline() if string.find(line,'@')>=0: m2=re.search("\@(\w*)\s*\{(\w*)",line) if m: if m2: if string.find(m2.group(1),'preamble')<0: line='@'+m2.group(1)+'{'+m.group(1)+',\n' else: line='@article{'+m.group(1)+',\n' else: sys.stderr.write('!! Error in: '+texstring+' !!\n') line=re.sub("(.*)
","",line)
		if not line : break
		if string.find(line,'}
') >= 0: if m2: if string.find(m2.group(1),'preamble')<0: print ' URL = {http://www.ams.org/mathscinet-getitem?mr='+m2.group(2)+'}\n}', else: print '}', else: print ' URL = {}\n}', if m: sys.stderr.write('!! Error in: '+m.group(1)+' !!\n') else: sys.stderr.write('!! Error in: '+texstring+' !!\n') break print line, f = sys.stdin currentstring = '' addline = 0 while 1: line = f.readline() if not line : mref(currentstring) break if string.find(line,flag) >= 0: addline = 1 mref(currentstring) currentstring = line elif addline == 1: currentstring = currentstring + line