]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsConverter.C
* src/LaTeX.C
[lyx.git] / src / graphics / GraphicsConverter.C
index c9f6cdd10e99a3406b7201119e83aa9be314ec5d..3a11012ba7ed81d67a7d7c73f8b54e0b652ae236 100644 (file)
@@ -30,6 +30,7 @@
 
 namespace support = lyx::support;
 
+using support::addExtension;
 using support::changeExtension;
 using support::FileName;
 using support::Forkedcall;
@@ -91,7 +92,7 @@ public:
 bool Converter::isReachable(string const & from_format_name,
                            string const & to_format_name)
 {
-       return converters.isReachable(from_format_name, to_format_name);
+       return theConverters().isReachable(from_format_name, to_format_name);
 }
 
 
@@ -280,13 +281,23 @@ static void build_script(FileName const & from_file,
        typedef Converters::EdgePath EdgePath;
 
        script << "#!/usr/bin/env python\n"
-                 "import os, shutil, sys\n\n"
+                 "# -*- coding: utf-8 -*-\n"
+                 "import os, shutil, sys, locale\n\n"
                  "def unlinkNoThrow(file):\n"
                  "  ''' remove a file, do not throw if an error occurs '''\n"
                  "  try:\n"
                  "    os.unlink(file)\n"
                  "  except:\n"
-                 "    pass\n\n";
+                 "    pass\n\n"
+                 "def utf8ToDefaultEncoding(file):\n"
+                 "  ''' if possible, convert to the default encoding '''\n"
+                 "  try:\n"
+                 "    language, output_encoding = locale.getdefaultlocale()\n"
+                 "    if output_encoding == None:\n"
+                 "      output_encoding = 'latin1'\n"
+                 "    return unicode(file, 'utf8').encode(output_encoding)\n"
+                 "  except:\n"
+                 "    return file\n\n";
 
        // we do not use ChangeExtension because this is a basename
        // which may nevertheless contain a '.'
@@ -295,21 +306,23 @@ static void build_script(FileName const & from_file,
 
        EdgePath const edgepath = from_format.empty() ?
                EdgePath() :
-               converters.getPath(from_format, to_format);
+               theConverters().getPath(from_format, to_format);
 
        // Create a temporary base file-name for all intermediate steps.
        // Remember to remove the temp file because we only want the name...
        static int counter = 0;
        string const tmp = "gconvert" + convert<string>(counter++);
-       string const to_base = tempName(string(), tmp);
-       unlink(FileName(to_base));
+       FileName const to_base(tempName(FileName(), tmp));
+       unlink(to_base);
 
        // Create a copy of the file in case the original name contains
        // problematic characters like ' or ". We can work around that problem
        // in python, but the converters might be shell scripts and have more
        // troubles with it.
-       string outfile = changeExtension(to_base, getExtension(from_file.absFilename()));
-       script << "infile = " << quoteName(from_file.absFilename(), quote_python) << "\n"
+       string outfile = addExtension(to_base.absFilename(), getExtension(from_file.absFilename()));
+       script << "infile = utf8ToDefaultEncoding("
+                       << quoteName(from_file.absFilename(), quote_python)
+                       << ")\n"
                  "outfile = " << quoteName(outfile, quote_python) << "\n"
                  "shutil.copy(infile, outfile)\n";
 
@@ -358,12 +371,12 @@ static void build_script(FileName const & from_file,
        EdgePath::const_iterator end = edgepath.end();
 
        for (; it != end; ++it) {
-               lyx::Converter const & conv = converters.get(*it);
+               lyx::Converter const & conv = theConverters().get(*it);
 
                // Build the conversion command
                string const infile      = outfile;
                string const infile_base = changeExtension(infile, string());
-               outfile = changeExtension(to_base, conv.To->extension());
+               outfile = addExtension(to_base.absFilename(), conv.To->extension());
 
                // Store these names in the python script
                script << "infile = "      << quoteName(infile, quote_python) << "\n"