Author: baum
Date: Thu Dec 30 21:29:33 2010
New Revision: 37049
URL: http://www.lyx.org/trac/changeset/37049

Log:
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

Modified:
   lyx-devel/trunk/src/support/Package.cpp
   lyx-devel/trunk/src/support/Package.h
   lyx-devel/trunk/src/tex2lyx/tex2lyx.cpp
   lyx-devel/trunk/src/tex2lyx/tex2lyx.h
   lyx-devel/trunk/src/tex2lyx/text.cpp

Modified: lyx-devel/trunk/src/support/Package.cpp
==============================================================================
--- lyx-devel/trunk/src/support/Package.cpp     Thu Dec 30 18:59:59 2010        
(r37048)
+++ lyx-devel/trunk/src/support/Package.cpp     Thu Dec 30 21:29:33 2010        
(r37049)
@@ -125,6 +125,24 @@
                        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 =

Modified: lyx-devel/trunk/src/support/Package.h
==============================================================================
--- lyx-devel/trunk/src/support/Package.h       Thu Dec 30 18:59:59 2010        
(r37048)
+++ lyx-devel/trunk/src/support/Package.h       Thu Dec 30 21:29:33 2010        
(r37049)
@@ -70,10 +70,14 @@
                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 @@
 
 private:
        FileName binary_dir_;
+       FileName lyx_binary_;
        FileName system_support_dir_;
        FileName build_support_dir_;
        FileName user_support_dir_;

Modified: lyx-devel/trunk/src/tex2lyx/tex2lyx.cpp
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/tex2lyx.cpp     Thu Dec 30 18:59:59 2010        
(r37048)
+++ lyx-devel/trunk/src/tex2lyx/tex2lyx.cpp     Thu Dec 30 21:29:33 2010        
(r37049)
@@ -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 @@
 
 
 bool noweb_mode = false;
+bool pdflatex = false;
+bool roundtrip = false;
 
 
 namespace {
@@ -288,7 +291,8 @@
                "\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_roundtrip(string const &, string const &)
+{
+       roundtrip = true;
+       return 0;
+}
+
+
 void easyParse(int & argc, char * argv[])
 {
        map<string, cmd_helper> cmdmap;
@@ -382,6 +393,7 @@
        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 @@
        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 @@
                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 @@
        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 @@
        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 @@
        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 @@
        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;
 }
 
 // }])

Modified: lyx-devel/trunk/src/tex2lyx/tex2lyx.h
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/tex2lyx.h       Thu Dec 30 18:59:59 2010        
(r37048)
+++ lyx-devel/trunk/src/tex2lyx/tex2lyx.h       Thu Dec 30 21:29:33 2010        
(r37049)
@@ -114,6 +114,8 @@
 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;
 

Modified: lyx-devel/trunk/src/tex2lyx/text.cpp
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/text.cpp        Thu Dec 30 18:59:59 2010        
(r37048)
+++ lyx-devel/trunk/src/tex2lyx/text.cpp        Thu Dec 30 21:29:33 2010        
(r37049)
@@ -1811,8 +1811,10 @@
                                                     << 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 @@
                                        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

Reply via email to