]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/bib2html.py
c16f21cce28c9b91512bf08cf6393172bb96036a
[lilypond.git] / buildscripts / bib2html.py
1 #!@PYTHON@
2 import os
3 import sys
4 import getopt
5 import tempfile
6
7 # usage:
8 def usage ():
9     print 'usage: %s [-s style] [-o <outfile>] BIBFILES...'
10
11 (options, files) = getopt.getopt (sys.argv[1:], 's:o:', [])
12
13 output = 'bib.html'
14 style = 'long'
15
16 for (o,a) in options:
17     if o == '-h' or o == '--help':
18         usage ()
19         sys.exit (0)
20     elif o == '-s' or o == '--style':
21         style = a
22     elif o == '-o' or o == '--output':
23         output = a
24     else:
25         raise Exception ('unknown option: %s' % o)
26
27
28 if style not in ['alpha','index','long','longp','long-pario','short','short-pario','split']:
29     sys.stderr.write ("Unknown style \`%s'\n" % style)
30
31 tempfile = tempfile.mktemp ('bib2html')
32
33 if not files:
34    usage ()
35    sys.exit (2)
36
37
38 def strip_extension (f, ext):
39     (p, e) = os.path.splitext (f)
40     if e == ext:
41         e = ''
42     return p + e
43
44 nf = []
45 for f in files:
46     nf.append (strip_extension (f, '.bib'))
47
48 files = ','.join (nf)
49
50 open (tempfile + '.aux', 'w').write (r'''
51 \relax 
52 \citation{*}
53 \bibstyle{html-%(style)s}
54 \bibdata{%(files)s}''' % vars ()) 
55
56 cmd = "bibtex %s" % tempfile
57
58 sys.stdout.write ("Invoking `%s'\n" % cmd)
59 stat = os.system (cmd)
60 if stat <> 0:
61     sys.exit(1)
62
63
64 #TODO: do tex -> html on output 
65
66 bbl = open (tempfile + '.bbl').read ()
67
68 open (output, 'w').write  (bbl)
69
70
71 def cleanup (tempfile):
72     for a in ['aux','bbl', 'blg']:
73         os.unlink (tempfile + '.' + a)
74
75 cleanup (tempfile)
76