updated on Thu Jan 26 00:18:00 UTC 2012
[aur-mirror.git] / latex-pubmed / gorefs.py
blobd9b8f4d5773c703724e22fb58aec3337b2518438
1 from pyP2B.pyP2Bclass import pyP2B
2 import os,re,optparse
3 from accents import latexAccents
4 import commands
6 def utf_to_latex(text):
7 '''
8 Converts utf to latex
9 '''
10 for search, replace in latexAccents:
11 text = text.replace(search, replace)
12 return text
15 def main():
16 '''
17 Main
18 '''
19 bibSuccess = False
21 parser = optparse.OptionParser(usage='%prog [options] inputfile[.tex] <outputfile[.bib]>', version='%prog version 0.1')
22 parser.add_option('-m', '--makepdf', action="store_true", default=False, dest='makepdf', help="Execute commands to create an output pdf")
23 parser.add_option('-q', '--quiet', action="store_true", default=False, dest='quiet', help="Do not use verbose output")
24 (args, opts) = parser.parse_args()
27 (options, args) = parser.parse_args()
28 verbose = not options.quiet
30 if args:
32 try:
33 bibname = '%s.bib' % os.path.splitext(args[1])[0]
34 except:
35 bibname = '%s.bib' % os.path.splitext(args[0])[0]
37 try:
39 basefile = os.path.splitext(args[0])[0]
40 texname = '%s.tex' % basefile
41 tex = open(texname).read()
43 except IOError :
44 print >>os.sys.stderr, 'Cannot open file %s!' % args[0]
45 os.sys.exit(2)
47 bib = open(bibname, "w")
49 if verbose: print >>os.sys.stdout, 'Generating bibliography file %s...' % bibname
51 for m in re.findall("cite\{pmid(\d+)\}", tex):
52 myref = pyP2B()
54 utf_text = myref.getPubmedReference(m)
55 print >>bib, utf_to_latex(utf_text)
57 bib.close()
58 bibSuccess = True
60 else:
61 parser.print_help()
63 if bibSuccess and options.makepdf:
65 output = commands.getoutput('rm %s.aux' % basefile)
66 output += commands.getoutput('pdflatex %s' % basefile)
67 output += commands.getoutput('bibtex %s' %basefile)
68 output += commands.getoutput('pdflatex %s' % basefile)
69 output += commands.getoutput('pdflatex %s' % basefile)
71 if verbose:
72 print >>os.sys.stdout, 'Generating pdf file %s.pdf...' % basefile
73 print output
77 if __name__ == "__main__":
78 main()