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