]> git.lyx.org Git - lyx.git/blob - lib/scripts/include_bib.py
Add tableaux to outliner
[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 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 -tt $$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 import sys, os
31
32 class secbib:
33   def __init__(self, start = -1, end = -1):
34     self.start = start
35     self.end   = end
36
37 class BibError(Exception):
38   def __init__(self, msg):
39     self.msg = msg
40
41   def __str__(self):
42     return self.msg
43
44
45 def InsertBib(fil, out):   
46   ''' Inserts the contents of the .bbl file instead of the bibliography in a new .tex file '''
47
48   texlist = open(fil, 'r').readlines()
49
50   # multiple bibliographies
51   biblist = []
52   stylist = []
53   
54   for i, line in enumerate(texlist):
55     if "\\bibliographystyle" in line:
56       stylist.append(i)
57     elif "\\bibliography" in line:
58       biblist.append(i)
59     elif "\\begin{btSect}" in line:
60       raise BibError("Cannot export sectioned bibliographies")
61   
62   if len(biblist) > 1:
63     raise BibError("Cannot export multiple bibliographies.")
64   if not biblist:
65     raise BibError("No biliography found!")
66
67   bibpos = biblist[0]
68   newlist = texlist[0:bibpos]
69   bblfile = fil[:-4] + ".bbl"
70   bbllist = open(bblfile, 'r').readlines()
71   newlist += bbllist
72   newlist += texlist[bibpos + 1:]
73     
74   outfile = open(out, 'w')
75   outfile.write("".join(newlist))
76   outfile.close()
77   return out
78     
79
80 def usage():
81   print r'''
82 Usage: python include_bib.py file.tex [outfile.tex]
83   Includes the contents of file.bbl, which must exist in the
84   same directory as file.tex, in place of the \bibliography
85   command, and creates the new file outfile.tex. If no name
86   for that file is given, we create: file-bbl.tex.
87 '''  
88
89 if __name__ == "__main__":
90   args = len(sys.argv)
91   if args <= 1 or args > 3:
92     usage()
93     sys.exit(0)
94
95   # we might should make sure this is a tex file....
96   infile = sys.argv[1]
97   if infile[-4:] != ".tex":
98     print "Error: " + infile + " is not a TeX file"
99     usage()
100     sys.exit(1)
101
102   if args == 3:
103     outfile = sys.argv[2]
104   else:
105     outfile = infile[:-4] + "-bbl.tex"
106
107   newfile = InsertBib(infile, outfile)
108   print "Wrote " + outfile