]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsConverter.cpp
ctests: regex to revert two instant_preview tests
[lyx.git] / src / graphics / GraphicsConverter.cpp
index 917681256e64f5f8e545ee3befe7ac9d8905f1de..4930f0d7a0a98888726df8f706d7652ff751b036 100644 (file)
@@ -142,6 +142,7 @@ Converter::Impl::Impl(FileName const & from_file, string const & to_file_base,
                << "\n--------------------------------------\n");
 
        // Output the script to file.
+       // FIXME THREAD
        static int counter = 0;
        script_file_ = FileName(onlyPath(to_file_base) + "lyxconvert" +
                convert<string>(counter++) + ".py");
@@ -250,7 +251,15 @@ static void build_conversion_command(string const & command, ostream & script)
                  "    sys.exit(1)\n\n";
 
        // Delete the infile
-       script << "unlinkNoThrow(infile)\n\n";
+       script << "if infile != outfile:\n"
+                 "  unlinkNoThrow(infile)\n\n";
+}
+
+
+static string const strip_digit(string const & format)
+{
+       // Strip trailing digits from format names e.g. "pdf6" -> "pdf"
+       return format.substr(0, format.find_last_not_of("0123456789") + 1);
 }
 
 
@@ -260,7 +269,7 @@ static void build_script(string const & from_file,
                  string const & to_format,
                  ostream & script)
 {
-       LASSERT(from_format != to_format, /**/);
+       LASSERT(from_format != to_format, return);
        LYXERR(Debug::GRAPHICS, "build_script ... ");
        typedef Graph::EdgePath EdgePath;
 
@@ -280,6 +289,7 @@ static void build_script(string const & from_file,
 
        // Create a temporary base file-name for all intermediate steps.
        // Remember to remove the temp file because we only want the name...
+       // FIXME THREAD
        static int counter = 0;
        string const tmp = "gconvert" + convert<string>(counter++);
        string const to_base = FileName::tempName(tmp).toFilesystemEncoding();
@@ -317,12 +327,12 @@ static void build_script(string const & from_file,
                   << libScriptSearch("$$s/scripts/convertDefault.py",
                                      quote_python) << ' ';
                if (!from_format.empty())
-                       os << from_format << ':';
+                       os << strip_digit(from_format) << ':';
                // The extra " quotes around infile and outfile are needed
                // because the filename may contain spaces and it is used
                // as argument of os.system().
                os << "' + '\"' + infile + '\"' + ' "
-                  << to_format << ":' + '\"' + outfile + '\"' + '";
+                  << strip_digit(to_format) << ":' + '\"' + outfile + '\"' + '";
                string const command = os.str();
 
                LYXERR(Debug::GRAPHICS,
@@ -348,7 +358,18 @@ static void build_script(string const & from_file,
                // Build the conversion command
                string const infile      = outfile;
                string const infile_base = changeExtension(infile, string());
-               outfile = addExtension(to_base, conv.To->extension());
+               outfile = conv.result_file.empty()
+                       ? addExtension(to_base, conv.To->extension())
+                       : addName(subst(conv.result_dir,
+                                       token_base, infile_base),
+                                 subst(conv.result_file,
+                                       token_base, onlyFileName(infile_base)));
+
+               // If two formats share the same extension we may get identical names
+               if (outfile == infile && conv.result_file.empty()) {
+                       string const new_base = FileName::tempName(tmp).toFilesystemEncoding();
+                       outfile = addExtension(new_base, conv.To->extension());
+               }
 
                // Store these names in the python script
                script << "infile = "