X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Fscripts%2Finclude_bib.py;h=7e14d13579472dfe239d88066ab9e11d38e4ea91;hb=e5fc7327e398c6b243113f6a8f624ed917287ee4;hp=6c92ad449e357ad4c62b981be2f35759aa3c4e52;hpb=76ffbd136ae935861e8b98a9e47b3ad0957924be;p=lyx.git diff --git a/lib/scripts/include_bib.py b/lib/scripts/include_bib.py index 6c92ad449e..7e14d13579 100644 --- a/lib/scripts/include_bib.py +++ b/lib/scripts/include_bib.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + # file include_bib.py # This file is part of LyX, the document processor. # Licence details can be found in the file COPYING. @@ -6,6 +8,26 @@ # Full author contact details are available in file CREDITS +# This script is intended to include a BibTeX-generated biblography +# in a LaTeX file, as publishers often want. It can be run manually +# on an exported LaTeX file, though it needs to be compiled first, +# so the bbl file will exist. +# +# It should also be possible to create a LyX converter to run this +# automatically. To set it up, create a format "ltxbbl"; make sure to +# check it as a document format. Then create a LaTeX-->ltxbbl converter, +# with the command: +# python -tt $$s/scripts/include_bib.py $$i $$o +# and give it the flags: +# needaux,nice +# You'll then have it in the export menu. +# +# We do not activate this converter by default, because there are problems +# when one tries to use multiple bibliographies. +# +# Please report any problems on the devel list. + +from __future__ import print_function import sys, os class secbib: @@ -21,7 +43,7 @@ class BibError(Exception): return self.msg -def InsertBib(fil, out): +def InsertBib(fil, out): ''' Inserts the contents of the .bbl file instead of the bibliography in a new .tex file ''' texlist = open(fil, 'r').readlines() @@ -29,7 +51,7 @@ def InsertBib(fil, out): # multiple bibliographies biblist = [] stylist = [] - + for i, line in enumerate(texlist): if "\\bibliographystyle" in line: stylist.append(i) @@ -37,8 +59,6 @@ def InsertBib(fil, out): biblist.append(i) elif "\\begin{btSect}" in line: raise BibError("Cannot export sectioned bibliographies") - - filenew = fil[:-4] + "-bibinc.tex" #The new .tex file if len(biblist) > 1: raise BibError("Cannot export multiple bibliographies.") @@ -51,13 +71,39 @@ def InsertBib(fil, out): bbllist = open(bblfile, 'r').readlines() newlist += bbllist newlist += texlist[bibpos + 1:] - + outfile = open(out, 'w') outfile.write("".join(newlist)) outfile.close() - return filenew - + return out + + +def usage(): + print (r''' +Usage: python include_bib.py file.tex [outfile.tex] + Includes the contents of file.bbl, which must exist in the + same directory as file.tex, in place of the \bibliography + command, and creates the new file outfile.tex. If no name + for that file is given, we create: file-bbl.tex. +''') if __name__ == "__main__": - newfile = InsertBib(sys.argv[1], sys.argv[2]) - print "Wrote " + newfile + args = len(sys.argv) + if args <= 1 or args > 3: + usage() + sys.exit(0) + + # we might should make sure this is a tex file.... + infile = sys.argv[1] + if infile[-4:] != ".tex": + print ("Error: " + infile + " is not a TeX file") + usage() + sys.exit(1) + + if args == 3: + outfile = sys.argv[2] + else: + outfile = infile[:-4] + "-bbl.tex" + + newfile = InsertBib(infile, outfile) + print ("Wrote " + outfile)