From 90f795a29ed8135035aa8aede53255285b98feda Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Thu, 30 Dec 2010 20:29:33 +0000 Subject: [PATCH] Add -roundtrip argument to tex2lyx for tex->lyx->tex roundtrip testing. The reasons for doing this in tex2lyx instead of an external script are: - Correct choice of latex/pdflatex export - Using the correct LyX executable regardless of running inplace or from an installation, or with or without version suffix git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@37049 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/support/Package.cpp | 18 ++++++++++++ src/support/Package.h | 7 ++++- src/tex2lyx/tex2lyx.cpp | 65 ++++++++++++++++++++++++++++++++++------- src/tex2lyx/tex2lyx.h | 2 ++ src/tex2lyx/text.cpp | 6 +++- 5 files changed, 86 insertions(+), 12 deletions(-) diff --git a/src/support/Package.cpp b/src/support/Package.cpp index b65bd1fd23..5e4e7c163a 100644 --- a/src/support/Package.cpp +++ b/src/support/Package.cpp @@ -125,6 +125,24 @@ Package::Package(string const & command_line_arg0, get_system_support_dir(abs_binary, command_line_system_support_dir); + // The LyX executable is one level above binary_dir_ if we are running + // tex2lyx in place. Otherwise it is in binary_dir_. + string abs_lyx_dir; + if (build_support_dir_.empty() || + top_build_dir_location == top_build_dir_is_one_level_up) + abs_lyx_dir = binary_dir_.absFileName(); + else { + FileName fn(addPath(binary_dir_.absFileName(), "../")); + abs_lyx_dir = fn.realPath(); + } + // The LyX executable may have a package suffix if we are not running + // in place. + if (build_support_dir_.empty()) + lyx_binary_ = FileName(addName(abs_lyx_dir, + "lyx" + string(PROGRAM_SUFFIX))); + else + lyx_binary_ = FileName(addName(abs_lyx_dir, "lyx")); + locale_dir_ = get_locale_dir(system_support_dir_); FileName const default_user_support_dir = diff --git a/src/support/Package.h b/src/support/Package.h index a861763218..cb48bb2fe5 100644 --- a/src/support/Package.h +++ b/src/support/Package.h @@ -70,10 +70,14 @@ public: std::string const & command_line_user_support_dir, exe_build_dir_to_top_build_dir); - /** The directory containing the LyX executable. + /** The directory containing the main executable (LyX or tex2lyx). */ FileName const & binary_dir() const { return binary_dir_; } + /** The absolute path to the LyX executable. + */ + FileName const & lyx_binary() const { return lyx_binary_; } + /** The top of the LyX source code tree. */ static FileName const & top_srcdir(); @@ -137,6 +141,7 @@ public: private: FileName binary_dir_; + FileName lyx_binary_; FileName system_support_dir_; FileName build_support_dir_; FileName user_support_dir_; diff --git a/src/tex2lyx/tex2lyx.cpp b/src/tex2lyx/tex2lyx.cpp index 05fe8e31cb..5467b5e015 100644 --- a/src/tex2lyx/tex2lyx.cpp +++ b/src/tex2lyx/tex2lyx.cpp @@ -28,6 +28,7 @@ #include "support/Messages.h" #include "support/os.h" #include "support/Package.h" +#include "support/Systemcall.h" #include #include @@ -168,6 +169,8 @@ void add_known_command(string const & command, string const & o1, bool noweb_mode = false; +bool pdflatex = false; +bool roundtrip = false; namespace { @@ -288,7 +291,8 @@ int parse_help(string const &, string const &) "\t-f Force overwrite of .lyx files.\n" "\t-help Print this message and quit.\n" "\t-n translate a noweb (aka literate programming) file.\n" - "\t-s syntaxfile read additional syntax file.\n" + "\t-roundtrip re-export created .lyx file infile.lyx.lyx to infile.lyx.tex.\n" + "\t-s syntaxfile read additional syntax file.\n" "\t-sysdir dir Set system directory to DIR.\n" "\t-userdir DIR Set user directory to DIR." << endl; @@ -369,6 +373,13 @@ int parse_noweb(string const &, string const &) } +int parse_roundtrip(string const &, string const &) +{ + roundtrip = true; + return 0; +} + + void easyParse(int & argc, char * argv[]) { map cmdmap; @@ -382,6 +393,7 @@ void easyParse(int & argc, char * argv[]) cmdmap["-n"] = parse_noweb; cmdmap["-sysdir"] = parse_sysdir; cmdmap["-userdir"] = parse_userdir; + cmdmap["-roundtrip"] = parse_roundtrip; for (int i = 1; i < argc; ++i) { map::const_iterator it @@ -523,6 +535,29 @@ bool tex2lyx(string const & infilename, FileName const & outfilename, return tex2lyx(FileName(infilename), os, encoding); } + +bool tex2tex(string const & infilename, FileName const & outfilename, + string const & encoding) +{ + if (!tex2lyx(infilename, outfilename, encoding)) + return false; + string command = quoteName(package().lyx_binary().toFilesystemEncoding()); + if (overwrite_files) + command += " -f main"; + else + command += " -f none"; + if (pdflatex) + command += " -e pdflatex "; + else + command += " -e latex "; + command += quoteName(outfilename.toFilesystemEncoding()); + Systemcall one; + if (one.startscript(Systemcall::Wait, command) == 0) + return true; + cerr << "Error: Running '" << command << "' failed." << endl; + return false; +} + } // namespace lyx @@ -549,7 +584,7 @@ int main(int argc, char * argv[]) cerr << to_utf8(message.title_) << ":\n" << to_utf8(message.details_) << endl; if (message.type_ == ErrorException) - exit(1); + return EXIT_FAILURE; } // Now every known option is parsed. Look for input and output @@ -558,7 +593,17 @@ int main(int argc, char * argv[]) infilename = makeAbsPath(infilename).absFileName(); string outfilename; - if (argc > 2) { + if (roundtrip) { + if (argc > 2) { + // Do not allow a user supplied output filename + // (otherwise it could easily happen that LyX would + // overwrite the original .tex file) + cerr << "Error: output filename must not be given in roundtrip mode." + << endl; + return EXIT_FAILURE; + } + outfilename = changeExtension(infilename, ".lyx.lyx"); + } else if (argc > 2) { outfilename = internal_path(os::utf8_argv(2)); if (outfilename != "-") outfilename = makeAbsPath(outfilename).absFileName(); @@ -569,7 +614,7 @@ int main(int argc, char * argv[]) FileName const system_syntaxfile = libFileSearch("", "syntax.default"); if (system_syntaxfile.empty()) { cerr << "Error: Could not find syntax file \"syntax.default\"." << endl; - exit(1); + return EXIT_FAILURE; } read_syntaxfile(system_syntaxfile); if (!syntaxfile.empty()) @@ -580,13 +625,13 @@ int main(int argc, char * argv[]) if (symbols_path.empty()) { cerr << "Error: Could not find file \"unicodesymbols\"." << endl; - exit(1); + return EXIT_FAILURE; } FileName const enc_path = libFileSearch(string(), "encodings"); if (enc_path.empty()) { cerr << "Error: Could not find file \"encodings\"." << endl; - exit(1); + return EXIT_FAILURE; } encodings.read(enc_path, symbols_path); if (!default_encoding.empty() && !encodings.fromLaTeXName(default_encoding)) @@ -598,14 +643,14 @@ int main(int argc, char * argv[]) if (outfilename == "-") { if (tex2lyx(FileName(infilename), cout, default_encoding)) return EXIT_SUCCESS; - else - return EXIT_FAILURE; + } else if (roundtrip) { + if (tex2tex(infilename, FileName(outfilename), default_encoding)) + return EXIT_SUCCESS; } else { if (tex2lyx(infilename, FileName(outfilename), default_encoding)) return EXIT_SUCCESS; - else - return EXIT_FAILURE; } + return EXIT_FAILURE; } // }]) diff --git a/src/tex2lyx/tex2lyx.h b/src/tex2lyx/tex2lyx.h index 63c43fa796..f6fae1e9b4 100644 --- a/src/tex2lyx/tex2lyx.h +++ b/src/tex2lyx/tex2lyx.h @@ -114,6 +114,8 @@ extern CommandMap known_environments; extern CommandMap known_math_environments; /// extern bool noweb_mode; +/// Did we recognize any pdflatex-only construct? +extern bool pdflatex; /// LyX format that is created by tex2lyx int const LYX_FORMAT = 345; diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index 25c4e2e1b0..3e25ef53e1 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -1811,8 +1811,10 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, << endl; } name = dvips_name; - } else if (!pdftex_name.empty()) + } else if (!pdftex_name.empty()) { name = pdftex_name; + pdflatex = true; + } } if (makeAbsPath(name, path).exists()) @@ -2641,6 +2643,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, bool const xfigpdf = abspdfname.exists() && (ext == "pdftex_t" || ext == "pdf_t"); + if (xfigpdf) + pdflatex = true; // Combined PS/PDF/LaTeX: // x_pspdftex.eps, x_pspdftex.pdf, x.pspdftex -- 2.39.2