File size: 3,223 Bytes
f65fe85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# bib2texi.py
#
# This file is part of LilyPond, the GNU music typesetter.
#
# Copyright (C) 2001--2020  Han-Wen Nienhuys <[email protected]>
#
# LilyPond is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LilyPond is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.

import codecs
import os
import sys
import getopt
import tempfile

# usage:


def usage():
    print('usage: bib2texi.py [-s style] [-o <outfile>] [-q] BIBFILES...')
    print('-q suppresses most output')


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

output = 'bib.itexi'
style = 'long'
show_output = True

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
    elif o == '-q':
        show_output = False
    else:
        raise Exception('unknown option: %s' % o)

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

marker = """@c This file was autogenerated
@c     from: %s
@c     by:   %s

""" % (", ".join(files), sys.argv[0])


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)

tmpfile = tempfile.mkstemp('bib2texi')[1]

# This writes a .aux file to the temporary directory.
# The .aux file contains the commands for bibtex
# PEH changed the bibstyle to allow a single template file in the parent directory
# The template filename is texi-*.bst, where * defaults to 'long' but can be a parameter
open(tmpfile + '.aux', 'w', encoding='utf8').write(r'''
\relax
\citation{*}
\bibstyle{%(style)s}
\bibdata{%(files)s}''' % vars())

tmpdir = tempfile.gettempdir()

if show_output:
    quiet_flag = ''
else:
    quiet_flag = ' -terse '

# The command line to invoke bibtex
cmd = "TEXMFOUTPUT=%s bibtex %s %s" % (tmpdir, quiet_flag, tmpfile)

if show_output:
    sys.stdout.write("Running bibtex on %s\n" % files)
    sys.stdout.write(cmd)
# And invoke it
stat = os.system(cmd)
if stat != 0:
    sys.stderr.write("Bibtex exited with nonzero exit status!")
    sys.exit(1)

# TODO: do tex -> itexi on output
# Following lines copy tmpfile.bbl to the desired output file
bbl = codecs.open(tmpfile + '.bbl', 'r', 'utf-8').read()

if bbl.strip() == '':
    sys.stderr.write("Bibtex generated an empty file!")
    sys.exit(1)

fout = codecs.open(output, 'w', 'utf-8')
fout.write(marker)
fout.write(bbl)
fout.close()


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


cleanup(tmpfile)
# Following line added by PEH - script was leaving a dangling temporary file with no extension
os.unlink(tmpfile)