lyx_mirror/lib/scripts/include_bib.py

91 lines
2.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# file include_bib.py
# This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING.
# authors Richard Heck and [SchAirport]
# Full author contact details are available in file CREDITS
import sys, os
class secbib:
def __init__(self, start = -1, end = -1):
self.start = start
self.end = end
class BibError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
def InsertBib(fil, out):
''' Inserts the contents of the .bbl file instead of the bibliography in a new .tex file '''
texlist = open(fil, 'r').readlines()
# multiple bibliographies
biblist = []
stylist = []
for i, line in enumerate(texlist):
if "\\bibliographystyle" in line:
stylist.append(i)
elif "\\bibliography" in line:
biblist.append(i)
elif "\\begin{btSect}" in line:
raise BibError("Cannot export sectioned bibliographies")
if len(biblist) > 1:
raise BibError("Cannot export multiple bibliographies.")
if not biblist:
raise BibError("No biliography found!")
bibpos = biblist[0]
newlist = texlist[0:bibpos]
bblfile = fil[:-4] + ".bbl"
bbllist = open(bblfile, 'r').readlines()
newlist += bbllist
newlist += texlist[bibpos + 1:]
outfile = open(out, 'w')
outfile.write("".join(newlist))
outfile.close()
2012-03-26 16:06:04 +00:00
return out
2012-03-26 16:06:04 +00:00
def usage():
print r'''
Usage: python include_bib.py file.tex [outfile.tex]
Includes the contents of file.bbl, which must exist in the
same directory as file.tex, in place of the \bibliography
command, and creates the new file outfile.tex. If no name
for that file is given, we create: file-bbl.tex.
'''
if __name__ == "__main__":
2012-03-26 16:06:04 +00:00
args = len(sys.argv)
if args <= 1 or args > 3:
usage()
sys.exit(0)
# we might should make sure this is a tex file....
infile = sys.argv[1]
if infile[-4:] != ".tex":
print "Error: " + infile + " is not a TeX file"
usage()
sys.exit(1)
if args == 3:
outfile = sys.argv[2]
else:
outfile = infile[:-4] + "-bbl.tex"
newfile = InsertBib(infile, outfile)
print "Wrote " + outfile