]> git.lyx.org Git - features.git/commitdiff
Add -roundtrip argument to tex2lyx for tex->lyx->tex roundtrip testing.
authorGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Thu, 30 Dec 2010 20:29:33 +0000 (20:29 +0000)
committerGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Thu, 30 Dec 2010 20:29:33 +0000 (20:29 +0000)
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
src/support/Package.h
src/tex2lyx/tex2lyx.cpp
src/tex2lyx/tex2lyx.h
src/tex2lyx/text.cpp

index b65bd1fd235226e023221ade58dae7647b4b7a67..5e4e7c163ae6e27e5df4c5aadb41cc2306add981 100644 (file)
@@ -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 =
index a8617632182174e1e3ae25111150e21246aad26e..cb48bb2fe59db78f47cca74ede1ba0ceef6fde64 100644 (file)
@@ -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_;
index 05fe8e31cbba6583cee74a2e22037cdb6e26fddf..5467b5e015d2ce83f2d27889199ba5659401da42 100644 (file)
@@ -28,6 +28,7 @@
 #include "support/Messages.h"
 #include "support/os.h"
 #include "support/Package.h"
+#include "support/Systemcall.h"
 
 #include <cstdlib>
 #include <iostream>
@@ -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<string, cmd_helper> 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<string, cmd_helper>::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;
 }
 
 // }])
index 63c43fa796e78d76abe0f16fd354fa75f5cb1801..f6fae1e9b4cb40ee0d9e5e1305fb1b4559badc0a 100644 (file)
@@ -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;
 
index 25c4e2e1b08c4246e7c2b6d5cdf18a52cbe890dd..3e25ef53e148828d9379055f8a5c5e685a808ec9 100644 (file)
@@ -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