]> git.lyx.org Git - lyx.git/blob - lib/scripts/include_bib.py
* RELEASE-NOTES
[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 Heck and [SchAirport]
6
7 # Full author contact details are available in file CREDITS
8
9 import sys, os
10
11 class secbib:
12   def __init__(self, start = -1, end = -1):
13     self.start = start
14     self.end   = end
15
16 class BibError(Exception):
17   def __init__(self, msg):
18     self.msg = msg
19
20   def __str__(self):
21     return self.msg
22
23
24 def InsertBib(fil, out):   
25   ''' Inserts the contents of the .bbl file instead of the bibliography in a new .tex file '''
26
27   texlist = open(fil, 'r').readlines()
28
29   # multiple bibliographies
30   biblist = []
31   stylist = []
32   
33   for i, line in enumerate(texlist):
34     if "\\bibliographystyle" in line:
35       stylist.append(i)
36     elif "\\bibliography" in line:
37       biblist.append(i)
38     elif "\\begin{btSect}" in line:
39       raise BibError("Cannot export sectioned bibliographies")
40   
41   filenew = fil[:-4] + "-bibinc.tex" #The new .tex file
42
43   if len(biblist) > 1:
44     raise BibError("Cannot export multiple bibliographies.")
45   if not biblist:
46     raise BibError("No biliography found!")
47
48   bibpos = biblist[0]
49   newlist = texlist[0:bibpos]
50   bblfile = fil[:-4] + ".bbl"
51   bbllist = open(bblfile, 'r').readlines()
52   newlist += bbllist
53   newlist += texlist[bibpos + 1:]
54     
55   outfile = open(out, 'w')
56   outfile.write("".join(newlist))
57   outfile.close()
58   return filenew
59     
60
61 if __name__ == "__main__":
62   newfile = InsertBib(sys.argv[1], sys.argv[2])
63   print "Wrote " + newfile