]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsConverter.C
hopefully fix tex2lyx linking.
[lyx.git] / src / graphics / GraphicsConverter.C
index 4986fc643c0126136b5de2637a96e21d99d67fbf..b9e522bca5f792faf9561d9a12d919b352b969c0 100644 (file)
@@ -39,6 +39,7 @@ using support::libScriptSearch;
 using support::onlyPath;
 using support::onlyFilename;
 using support::quoteName;
+using support::quote_python;
 using support::subst;
 using support::tempName;
 using support::unlink;
@@ -123,28 +124,13 @@ string const & Converter::convertedFile() const
        return pimpl_->finished_ ? pimpl_->to_file_ : empty;
 }
 
-} // namespace graphics
-} // namespace lyx
-
-
-//------------------------------
-// Implementation details follow
-//------------------------------
-
-namespace {
-
 /** Build the conversion script.
  *  The script is output to the stream \p script.
  */
-void build_script(string const & from_file, string const & to_file_base,
+static void build_script(string const & from_file, string const & to_file_base,
                  string const & from_format, string const & to_format,
                  ostream & script);
 
-} // namespace anon
-
-
-namespace lyx {
-namespace graphics {
 
 Converter::Impl::Impl(string const & from_file,   string const & to_file_base,
                      string const & from_format, string const & to_format)
@@ -232,12 +218,8 @@ void Converter::Impl::converted(pid_t /* pid */, int retval)
        }
 }
 
-} // namespace graphics
-} // namespace lyx
-
-namespace {
 
-string const move_file(string const & from_file, string const & to_file)
+static string const move_file(string const & from_file, string const & to_file)
 {
        if (from_file == to_file)
                return string();
@@ -258,7 +240,7 @@ string const move_file(string const & from_file, string const & to_file)
 }
 
 
-void build_conversion_command(string const & command, ostream & script)
+static void build_conversion_command(string const & command, ostream & script)
 {
        // Store in the python script
        script << "\nif os.system(r'" << command << "') != 0:\n";
@@ -287,7 +269,7 @@ void build_conversion_command(string const & command, ostream & script)
 }
 
 
-void build_script(string const & from_file,
+static void build_script(string const & from_file,
                  string const & to_file_base,
                  string const & from_format,
                  string const & to_format,
@@ -327,23 +309,34 @@ void build_script(string const & from_file,
        // in python, but the converters might be shell scripts and have more
        // troubles with it.
        string outfile = changeExtension(to_base, getExtension(from_file));
-       script << "infile = '"
-              << subst(subst(from_file, "\\", "\\\\"), "'", "\\'") << "'\n"
-                 "outfile = " << quoteName(outfile) << "\n"
+       script << "infile = " << quoteName(from_file, quote_python) << "\n"
+                 "outfile = " << quoteName(outfile, quote_python) << "\n"
                  "shutil.copy(infile, outfile)\n";
 
+       // Some converters (e.g. lilypond) can only output files to the
+       // current directory, so we need to change the current directory.
+       // This has the added benefit that all other files that may be
+       // generated by the converter are deleted when LyX closes and do not
+       // clutter the real working directory.
+       script << "os.chdir(" << quoteName(onlyPath(outfile)) << ")\n";
+
        if (edgepath.empty()) {
                // Either from_format is unknown or we don't have a
                // converter path from from_format to to_format, so we use
                // the default converter.
                script << "infile = outfile\n"
-                      << "outfile = " << quoteName(to_file) << '\n';
+                      << "outfile = " << quoteName(to_file, quote_python)
+                      << '\n';
 
                ostringstream os;
-               os << support::os::python() << " \""
-                  << libFileSearch("scripts", "convertDefault.py") << "\" ";
+               os << support::os::python() << ' '
+                  << libScriptSearch("$$s/scripts/convertDefault.py",
+                                     quote_python) << ' ';
                if (!from_format.empty())
                        os << 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 + '\"' + '";
                string const command = os.str();
@@ -365,7 +358,7 @@ void build_script(string const & from_file,
        EdgePath::const_iterator end = edgepath.end();
 
        for (; it != end; ++it) {
-               ::Converter const & conv = converters.get(*it);
+               lyx::Converter const & conv = converters.get(*it);
 
                // Build the conversion command
                string const infile      = outfile;
@@ -373,22 +366,26 @@ void build_script(string const & from_file,
                outfile = changeExtension(to_base, conv.To->extension());
 
                // Store these names in the python script
-               script << "infile = "      << quoteName(infile) << '\n'
-                      << "infile_base = " << quoteName(infile_base) << '\n'
-                      << "outfile = "     << quoteName(outfile) << '\n';
+               script << "infile = "      << quoteName(infile, quote_python) << "\n"
+                         "infile_base = " << quoteName(infile_base, quote_python) << "\n"
+                         "outfile = "     << quoteName(outfile, quote_python) << '\n';
 
+               // See comment about extra " quotes above (although that
+               // applies only for the first loop run here).
                string command = conv.command;
                command = subst(command, token_from, "' + '\"' + infile + '\"' + '");
                command = subst(command, token_base, "' + '\"' + infile_base + '\"' + '");
                command = subst(command, token_to,   "' + '\"' + outfile + '\"' + '");
-               command = libScriptSearch(command);
+               command = libScriptSearch(command, quote_python);
 
                build_conversion_command(command, script);
        }
 
        // Move the final outfile to to_file
-       script << move_file("outfile", quoteName(to_file));
+       script << move_file("outfile", quoteName(to_file, quote_python));
        lyxerr[Debug::GRAPHICS] << "ready!" << endl;
 }
 
-} // namespace anon
+} // namespace graphics
+
+} // namespace lyx