From 13b1277cd2f22bca7b4dec40eb6ca762b7d6fafe Mon Sep 17 00:00:00 2001 From: Julien Rioux Date: Mon, 3 Oct 2011 16:35:27 +0000 Subject: [PATCH] lyxpreview: Handle bibtex-generated references and bibliography. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@39795 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/scripts/legacy_lyxpreview2ppm.py | 15 ++------ lib/scripts/lyxpreview2bitmap.py | 28 ++++++++------- lib/scripts/lyxpreview_tools.py | 51 ++++++++++++++++++++++++++-- src/graphics/PreviewLoader.cpp | 6 ++++ 4 files changed, 73 insertions(+), 27 deletions(-) diff --git a/lib/scripts/legacy_lyxpreview2ppm.py b/lib/scripts/legacy_lyxpreview2ppm.py index 58f02144b1..af01505398 100644 --- a/lib/scripts/legacy_lyxpreview2ppm.py +++ b/lib/scripts/legacy_lyxpreview2ppm.py @@ -81,7 +81,7 @@ import glob, os, pipes, re, string, sys from lyxpreview_tools import copyfileobj, error, filter_pages, find_exe, \ find_exe_or_terminate, join_metrics_and_rename, latex_commands, \ latex_file_re, make_texcolor, mkstemp, pdflatex_commands, progress, \ - run_command, warning, write_metrics_info + run_command, run_latex, warning, write_metrics_info def usage(prog_name): @@ -290,12 +290,7 @@ def legacy_conversion_step1(latex_file, dpi, output_format, fg_color, bg_color, error("Unable to move color info into the latex file") # Compile the latex file. - latex_call = '%s "%s"' % (latex, latex_file) - - latex_status, latex_stdout = run_command(latex_call) - if latex_status: - warning("%s had problems compiling %s" \ - % (os.path.basename(latex), latex_file)) + latex_status, latex_stdout = run_latex(latex, latex_file) if pdf_output: return legacy_conversion_step3(latex_file, dpi, output_format, True, skipMetrics) @@ -318,11 +313,7 @@ def legacy_conversion_pdflatex(latex_file, failed_pages, legacy_metrics, gs, filter_pages(latex_file, pdf_latex_file, failed_pages) # pdflatex call - pdflatex_call = '%s "%s"' % (pdflatex, pdf_latex_file) - pdflatex_status, pdflatex_stdout = run_command(pdflatex_call) - if pdflatex_status: - warning("%s had problems compiling %s" \ - % (os.path.basename(pdflatex), pdf_latex_file)) + pdflatex_status, pdflatex_stdout = run_latex(pdflatex, pdf_latex_file) pdf_file = latex_file_re.sub(".pdf", pdf_latex_file) diff --git a/lib/scripts/lyxpreview2bitmap.py b/lib/scripts/lyxpreview2bitmap.py index e7887b6a4a..7b19e48244 100755 --- a/lib/scripts/lyxpreview2bitmap.py +++ b/lib/scripts/lyxpreview2bitmap.py @@ -44,6 +44,7 @@ # --fg=: The foreground color as a hexadecimal string, eg '000000'. # --bg=: The background color as a hexadecimal string, eg 'faf0e6'. # --latex=: The converter for latex files. Default is latex. +# --bibtex=: The converter for bibtex files. Default is bibtex. # --lilypond: Preprocess through lilypond-book. Default is false. # --lilypond-book=: # The converter for lytex files. Default is lilypond-book. @@ -79,10 +80,10 @@ import getopt, glob, os, re, shutil, string, sys from legacy_lyxpreview2ppm import legacy_conversion_step1 -from lyxpreview_tools import copyfileobj, error, filter_pages, find_exe, \ - find_exe_or_terminate, join_metrics_and_rename, latex_commands, \ - latex_file_re, make_texcolor, mkstemp, pdflatex_commands, progress, \ - run_command, warning, write_metrics_info +from lyxpreview_tools import bibtex_commands, copyfileobj, error, \ + filter_pages, find_exe, find_exe_or_terminate, join_metrics_and_rename, \ + latex_commands, latex_file_re, make_texcolor, mkstemp, pdflatex_commands, \ + progress, run_command, run_latex, run_tex, warning, write_metrics_info def usage(prog_name): @@ -95,6 +96,7 @@ Options: --fg=: Foreground color (default: black, ie '000000') --bg=: Background color (default: white, ie 'ffffff') --latex=: Specify the executable for latex (default: latex) + --bibtex=: Specify the executable for bibtex (default: bibtex) --lilypond: Preprocess through lilypond-book (default: false) --lilypond-book=: The executable for lilypond-book (default: lilypond-book) @@ -312,6 +314,7 @@ def main(argv): dpi = 128 fg_color = "000000" bg_color = "ffffff" + bibtex = None latex = None lilypond = False lilypond_book = None @@ -320,9 +323,9 @@ def main(argv): # Parse and manipulate the command line arguments. try: - (opts, args) = getopt.gnu_getopt(argv[1:], "dhv", ["bg=", "debug", - "dpi=", "fg=", "help", "latex=", "lilypond", "lilypond-book=", - "png", "ppm", "verbose"]) + (opts, args) = getopt.gnu_getopt(argv[1:], "dhv", ["bibtex=", "bg=", + "debug", "dpi=", "fg=", "help", "latex=", "lilypond", + "lilypond-book=", "png", "ppm", "verbose"]) except getopt.GetoptError, err: error("%s\n%s" % (err, usage(script_name))) @@ -331,6 +334,8 @@ def main(argv): if opt in ("-h", "--help"): print usage(script_name) sys.exit(0) + elif opt == "--bibtex": + bibtex = [val] elif opt == "--bg": bg_color = val elif opt in ("-d", "--debug"): @@ -385,6 +390,7 @@ def main(argv): # External programs used by the script. latex = find_exe_or_terminate(latex or latex_commands) + bibtex = find_exe(bibtex or bibtex_commands) if lilypond: lilypond_book = find_exe_or_terminate(lilypond_book or ["lilypond-book"]) @@ -393,6 +399,7 @@ def main(argv): progress("Latex command: %s" % latex) progress("Latex produces pdf output: %s" % pdf_output) + progress("Bibtex command: %s" % bibtex) progress("Lilypond-book command: %s" % lilypond_book) progress("Preprocess through lilypond-book: %s" % lilypond) progress("Altering the latex file for font size and colors") @@ -449,12 +456,7 @@ def main(argv): warning("Unable to move color info into the latex file") # Compile the latex file. - latex_call = '%s "%s"' % (latex, latex_file) - - latex_status, latex_stdout = run_command(latex_call) - if latex_status: - warning("%s had problems compiling %s" \ - % (os.path.basename(latex), latex_file)) + latex_status, latex_stdout = run_latex(latex, latex_file, bibtex) # The dvi output file name dvi_file = latex_file_re.sub(".dvi", latex_file) diff --git a/lib/scripts/lyxpreview_tools.py b/lib/scripts/lyxpreview_tools.py index 767689f98c..546ed24248 100644 --- a/lib/scripts/lyxpreview_tools.py +++ b/lib/scripts/lyxpreview_tools.py @@ -12,7 +12,7 @@ # A repository of the following functions, used by the lyxpreview2xyz scripts. # copyfileobj, error, find_exe, find_exe_or_terminate, make_texcolor, mkstemp, -# progress, run_command, warning +# progress, run_command, run_latex, warning # Requires python 2.4 or later (subprocess module). @@ -23,7 +23,8 @@ import os, re, string, subprocess, sys, tempfile debug = False verbose = False -# Known flavors of latex +# Known flavors of latex and bibtex +bibtex_commands = ("bibtex", "bibtex8", "biber") latex_commands = ("latex", "pplatex", "platex", "latex2e") pdflatex_commands = ("pdflatex", "xelatex", "lualatex") @@ -306,3 +307,49 @@ def join_metrics_and_rename(original_metrics, new_metrics, new_page_indexes, ori original_metrics[legacy_index] = (index, metric) else: original_metrics.insert(legacy_index, (index, metric)) + + +def run_latex(latex, latex_file, bibtex = None): + # Run latex + latex_status, latex_stdout = run_tex(latex, latex_file) + + if bibtex is None: + return latex_status, latex_stdout + + # The aux and log output file names + aux_file = latex_file_re.sub(".aux", latex_file) + log_file = latex_file_re.sub(".log", latex_file) + + # Run bibtex/latex if necessary + progress("Checking if a bibtex run is necessary") + if string_in_file(r"\bibdata", aux_file): + bibtex_status, bibtex_stdout = run_tex(bibtex, aux_file) + latex_status, latex_stdout = run_tex(latex, latex_file) + # Rerun latex if necessary + progress("Checking if a latex rerun is necessary") + if string_in_file("Warning: Citation", log_file): + latex_status, latex_stdout = run_tex(latex, latex_file) + + return latex_status, latex_stdout + + +def run_tex(tex, tex_file): + tex_call = '%s "%s"' % (tex, tex_file) + + tex_status, tex_stdout = run_command(tex_call) + if tex_status: + warning("%s had problems compiling %s" \ + % (os.path.basename(tex), tex_file)) + return tex_status, tex_stdout + + +def string_in_file(string, infile): + if not os.path.isfile(infile): + return False + f = open(infile, 'r') + for line in f.readlines(): + if string in line: + f.close() + return True + f.close() + return False diff --git a/src/graphics/PreviewLoader.cpp b/src/graphics/PreviewLoader.cpp index eadcd93f23..a71fcd84d3 100644 --- a/src/graphics/PreviewLoader.cpp +++ b/src/graphics/PreviewLoader.cpp @@ -602,6 +602,12 @@ void PreviewLoader::Impl::startLoading(bool wait) cs << " --latex=xelatex"; if (buffer_.params().encoding().package() == Encoding::japanese) cs << " --latex=platex"; + if (buffer_.params().bibtex_command != "default") + cs << " --bibtex=" << quoteName(buffer_.params().bibtex_command); + else if (buffer_.params().encoding().package() == Encoding::japanese) + cs << " --bibtex=" << quoteName(lyxrc.jbibtex_command); + else + cs << " --bibtex=" << quoteName(lyxrc.bibtex_command); if (buffer_.params().bufferFormat() == "lilypond-book") cs << " --lilypond"; -- 2.39.2