]> git.lyx.org Git - lyx.git/blob - lib/scripts/include_bib.py
a7b6520a9d3ebd7b974e55d13a0f98c299758666
[lyx.git] / lib / scripts / include_bib.py
1 # file include_bib.py
2 # This file is part of LyX, the document processor.
3 # Licence details can be found in the file COPYING.
4
5 # authors Richard Kimberly Heck and [SchAirport]
6
7 # Full author contact details are available in file CREDITS
8
9 # This script is intended to include a BibTeX-generated biblography
10 # in a LaTeX file, as publishers often want. It can be run manually
11 # on an exported LaTeX file, though it needs to be compiled first,
12 # so the bbl file will exist.
13 #
14 # It should also be possible to create a LyX converter to run this
15 # automatically. To set it up, create a format "ltxbbl"; make sure to
16 # check it as a document format. Then create a LaTeX-->ltxbbl converter,
17 # with the command:
18 #   $${python} $$s/scripts/include_bib.py $$i $$o
19 # and give it the flags:
20 #   needaux,nice
21 # You'll then have it in the export menu.
22 #
23 # We do not activate this converter by default, because there are problems
24 # when one tries to use multiple bibliographies.
25 #
26 # Please report any problems on the devel list.
27
28 import sys, os
29
30 class secbib:
31   def __init__(self, start = -1, end = -1):
32     self.start = start
33     self.end   = end
34
35 class BibError(Exception):
36   def __init__(self, msg):
37     self.msg = msg
38
39   def __str__(self):
40     return self.msg
41
42
43 def InsertBib(fil, out):
44   ''' Inserts the contents of the .bbl file instead of the bibliography in a new .tex file '''
45
46   texlist = open(fil).readlines()
47
48   # multiple bibliographies
49   biblist = []
50   stylist = []
51
52   for i, line in enumerate(texlist):
53     if "\\bibliographystyle" in line:
54       stylist.append(i)
55     elif "\\bibliography" in line:
56       biblist.append(i)
57     elif "\\begin{btSect}" in line:
58       raise BibError("Cannot export sectioned bibliographies")
59
60   if len(biblist) > 1:
61     raise BibError("Cannot export multiple bibliographies.")
62   if not biblist:
63     raise BibError("No biliography found!")
64
65   bibpos = biblist[0]
66   newlist = texlist[0:bibpos]
67   bblfile = fil[:-4] + ".bbl"
68   bbllist = open(bblfile).readlines()
69   newlist += bbllist
70   newlist += texlist[bibpos + 1:]
71
72   outfile = open(out, 'w')
73   outfile.write("".join(newlist))
74   outfile.close()
75   return out
76
77
78 def usage():
79   print (r'''
80 Usage: python include_bib.py file.tex [outfile.tex]
81   Includes the contents of file.bbl, which must exist in the
82   same directory as file.tex, in place of the \bibliography
83   command, and creates the new file outfile.tex. If no name
84   for that file is given, we create: file-bbl.tex.
85 ''')
86
87 if __name__ == "__main__":
88   args = len(sys.argv)
89   if args <= 1 or args > 3:
90     usage()
91     sys.exit(0)
92
93   # we might should make sure this is a tex file....
94   infile = sys.argv[1]
95   if infile[-4:] != ".tex":
96     print ("Error: " + infile + " is not a TeX file")
97     usage()
98     sys.exit(1)
99
100   if args == 3:
101     outfile = sys.argv[2]
102   else:
103     outfile = infile[:-4] + "-bbl.tex"
104
105   newfile = InsertBib(infile, outfile)
106   print ("Wrote " + outfile)