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