2 # -*- coding: utf-8 -*-
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
8 # authors Richard Heck and [SchAirport]
10 # Full author contact details are available in file CREDITS
15 def __init__(self, start = -1, end = -1):
19 class BibError(Exception):
20 def __init__(self, msg):
27 def InsertBib(fil, out):
28 ''' Inserts the contents of the .bbl file instead of the bibliography in a new .tex file '''
30 texlist = open(fil, 'r').readlines()
32 # multiple bibliographies
36 for i, line in enumerate(texlist):
37 if "\\bibliographystyle" in line:
39 elif "\\bibliography" in line:
41 elif "\\begin{btSect}" in line:
42 raise BibError("Cannot export sectioned bibliographies")
45 raise BibError("Cannot export multiple bibliographies.")
47 raise BibError("No biliography found!")
50 newlist = texlist[0:bibpos]
51 bblfile = fil[:-4] + ".bbl"
52 bbllist = open(bblfile, 'r').readlines()
54 newlist += texlist[bibpos + 1:]
56 outfile = open(out, 'w')
57 outfile.write("".join(newlist))
64 Usage: python include_bib.py file.tex [outfile.tex]
65 Includes the contents of file.bbl, which must exist in the
66 same directory as file.tex, in place of the \bibliography
67 command, and creates the new file outfile.tex. If no name
68 for that file is given, we create: file-bbl.tex.
71 if __name__ == "__main__":
73 if args <= 1 or args > 3:
77 # we might should make sure this is a tex file....
79 if infile[-4:] != ".tex":
80 print "Error: " + infile + " is not a TeX file"
87 outfile = infile[:-4] + "-bbl.tex"
89 newfile = InsertBib(infile, outfile)
90 print "Wrote " + outfile