Doc: Contributing -- add instructions on git-cl issue`
[lilypond/mpolesky.git] / scripts / build / bib2html.py
blob8434cbfb54d04c4423406cfeb83fc6810efdf289
1 #!@PYTHON@
2 import os
3 import sys
4 import getopt
5 import tempfile
7 # usage:
8 def usage ():
9 print 'usage: %s [-s style] [-o <outfile>] BIBFILES...'
11 (options, files) = getopt.getopt (sys.argv[1:], 's:o:', [])
13 output = 'bib.html'
14 style = 'long'
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)
28 if style not in ['alpha','index','long','longp','long-pario','short','short-pario','split']:
29 sys.stderr.write ("Unknown style \`%s'\n" % style)
31 if not files:
32 usage ()
33 sys.exit (2)
36 def strip_extension (f, ext):
37 (p, e) = os.path.splitext (f)
38 if e == ext:
39 e = ''
40 return p + e
42 nf = []
43 for f in files:
44 nf.append (strip_extension (f, '.bib'))
46 files = ','.join (nf)
48 tmpfile = tempfile.mkstemp ('bib2html')[1]
50 open (tmpfile + '.aux', 'w').write (r'''
51 \relax
52 \citation{*}
53 \bibstyle{html-%(style)s}
54 \bibdata{%(files)s}''' % vars ())
56 tmpdir = tempfile.gettempdir ()
58 cmd = "TEXMFOUTPUT=%s bibtex %s" % (tmpdir, tmpfile)
60 sys.stdout.write ("Invoking `%s'\n" % cmd)
61 stat = os.system (cmd)
62 if stat <> 0:
63 sys.exit(1)
66 #TODO: do tex -> html on output
68 bbl = open (tmpfile + '.bbl').read ()
70 open (output, 'w').write (bbl)
73 def cleanup (tmpfile):
74 for a in ['aux','bbl', 'blg']:
75 os.unlink (tmpfile + '.' + a)
77 cleanup (tmpfile)