summaryrefslogtreecommitdiff
path: root/scripts/build/bib2html.py
blob: c16f21cce28c9b91512bf08cf6393172bb96036a (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!@PYTHON@
import os
import sys
import getopt
import tempfile

# usage:
def usage ():
    print 'usage: %s [-s style] [-o <outfile>] BIBFILES...'

(options, files) = getopt.getopt (sys.argv[1:], 's:o:', [])

output = 'bib.html'
style = 'long'

for (o,a) in options:
    if o == '-h' or o == '--help':
        usage ()
        sys.exit (0)
    elif o == '-s' or o == '--style':
        style = a
    elif o == '-o' or o == '--output':
        output = a
    else:
        raise Exception ('unknown option: %s' % o)


if style not in ['alpha','index','long','longp','long-pario','short','short-pario','split']:
    sys.stderr.write ("Unknown style \`%s'\n" % style)

tempfile = tempfile.mktemp ('bib2html')

if not files:
   usage ()
   sys.exit (2)


def strip_extension (f, ext):
    (p, e) = os.path.splitext (f)
    if e == ext:
        e = ''
    return p + e

nf = []
for f in files:
    nf.append (strip_extension (f, '.bib'))

files = ','.join (nf)

open (tempfile + '.aux', 'w').write (r'''
\relax 
\citation{*}
\bibstyle{html-%(style)s}
\bibdata{%(files)s}''' % vars ()) 

cmd = "bibtex %s" % tempfile

sys.stdout.write ("Invoking `%s'\n" % cmd)
stat = os.system (cmd)
if stat <> 0:
    sys.exit(1)


#TODO: do tex -> html on output 

bbl = open (tempfile + '.bbl').read ()

open (output, 'w').write  (bbl)


def cleanup (tempfile):
    for a in ['aux','bbl', 'blg']:
        os.unlink (tempfile + '.' + a)

cleanup (tempfile)