On 13/09/2011 1:52 AM, Tommaso Cucinotta wrote:
The attached improved patch correctly exports EmbeddedObjects.lyx:

$ ./src/lyx -E latex /tmp/out/objects.tex lib/doc/EmbeddedObjects.lyx
$ find /tmp/out/
/tmp/out/
/tmp/out/DummyDocument2.tex
/tmp/out/clipart
/tmp/out/clipart/CommentNoteImageQt4.eps
/tmp/out/clipart/ExternalMaterialQt4.eps
/tmp/out/clipart/footnoteQt4.eps
....
/tmp/out/EmbeddedObjects.lyx
....

$ cd /tmp/out
$ latex objects.tex
....
Output written on objects.dvi (99 pages, 365384 bytes).

Anyone willing to review it ?

Bye,

     T.


lyx-export-filename_v4.patch


Index: src/LyX.cpp
===================================================================
--- src/LyX.cpp (revisione 39668)
+++ src/LyX.cpp (copia locale)
@@ -112,6 +112,9 @@

  string geometryArg;

+/// Output filename to be used when issuing an export request (-e).
+string output_file;
+
  LyX * singleton_ = 0;

  void showFileError(string const&  error)
@@ -1054,6 +1057,9 @@
                  "                  Look on Tools->Preferences->File 
formats->Format\n"
                  "                  to get an idea which parameters should be 
passed.\n"
                  "                  Note that the order of -e and -x switches 
matters.\n"
+                 "\t-E [--export-to] fmt filename\n"
+                 "                  where fmt is the export format of choice (see 
--export),\n"
+                 "                  and filename is the destination 
filename.\n"
                  "\t-i [--import] fmt file.xxx\n"
                  "                  where fmt is the import format of choice\n"
                  "                  and file.xxx is the file to be imported.\n"
@@ -1123,6 +1129,24 @@
  }


+int parse_export_to(string const&  type, string const&  output_file, string&  
batch)
+{
+       if (type.empty()) {
+               lyxerr<<  to_utf8(_("Missing file type [eg latex, ps...] after "
+                                        "--export-to switch"))<<  endl;
+               exit(1);
+       }
+       if (output_file.empty()) {
+               lyxerr<<  to_utf8(_("Missing destination filename after "
+                                        "--export-to switch"))<<  endl;
+               exit(1);
+       }
+       batch = "buffer-export " + type + " " + output_file;
+       use_gui = false;
+       return 2;
+}
+
+
  int parse_export(string const&  type, string const&, string&  batch)
  {
        if (type.empty()) {
@@ -1216,8 +1240,10 @@
        cmdmap["-userdir"] = parse_userdir;
        cmdmap["-x"] = parse_execute;
        cmdmap["--execute"] = parse_execute;
-       cmdmap["-e"] = parse_export;
+       cmdmap["-e"] = parse_export;

whitespace error

        cmdmap["--export"] = parse_export;
+       cmdmap["-E"] = parse_export_to;
+       cmdmap["--export-to"] = parse_export_to;
        cmdmap["-i"] = parse_import;
        cmdmap["--import"] = parse_import;
        cmdmap["-geometry"] = parse_geometry;
Index: src/Buffer.h
===================================================================
--- src/Buffer.h        (revisione 39668)
+++ src/Buffer.h        (copia locale)
@@ -602,11 +602,12 @@

        

-       ///
-       bool doExport(std::string const&  format, bool put_in_tempdir,
+       /// target is a format name optionally followed by a space
+       /// and a destination file-name
+       bool doExport(std::string const&  target, bool put_in_tempdir,
                bool includeall, std::string&  result_file) const;
        ///
-       bool doExport(std::string const&  format, bool put_in_tempdir,
+       bool doExport(std::string const&  target, bool put_in_tempdir,
                      bool includeall = false) const;

Can we keep format ("latex") and target ("thisfile.tex") separated? And in fact would it not make it easier to do so, since the command-line parser has already done the work?

        ///
        bool preview(std::string const&  format, bool includeall = false) const;
Index: src/Buffer.cpp
===================================================================
--- src/Buffer.cpp      (revisione 39668)
+++ src/Buffer.cpp      (copia locale)
@@ -1928,7 +1928,15 @@

                case LFUN_BUFFER_EXPORT: {
                        docstring const arg = cmd.argument();
-                       enable = arg == "custom" || 
params().isExportable(to_utf8(arg));
+                       if (arg == "custom") {
+                               enable = true;
+                               break;
+                       }
+                       string format = to_utf8(arg);
+                       size_t pos = format.find(' ');
+                       if (pos != string::npos)
+                               format = format.substr(0, pos);
+                       enable = params().isExportable(format);
                        if (!enable)
                                flag.message(bformat(
                                        _("Don't know how to export to format: 
%1$s"), arg));
@@ -3391,12 +3399,20 @@
  }


-bool Buffer::doExport(string const&  format, bool put_in_tempdir,
-       bool includeall, string&  result_file) const
+bool Buffer::doExport(string const&  target, bool put_in_tempdir,
+                     bool includeall, string&  result_file) const
  {
+       OutputParams runparams(&params().encoding());
+       string format = target;
+       string filename;
+       size_t pos = target.find(' ');

This is the second time already that you need to split this string, which confirms my point about keeping format and target separated. You can have two versions of doExport, and the compiler will pick the right one based on the arguments you call it with.

+       if (pos != string::npos) {
+               filename = target.substr(pos + 1, target.length() - pos - 1);
+               format = target.substr(0, pos);
+               runparams.export_folder = 
FileName(filename).onlyPath().realPath();
+       }
        MarkAsExporting exporting(this);
        string backend_format;
-       OutputParams runparams(&params().encoding());
        runparams.flavor = OutputParams::LATEX;
        runparams.linelen = lyxrc.plaintext_linelen;
        runparams.includeall = includeall;
@@ -3439,10 +3455,12 @@
                        runparams.flavor = OutputParams::XETEX;
        }

-       string filename = latexName(false);
-       filename = addName(temppath(), filename);
-       filename = changeExtension(filename,
-                                  formats.extension(backend_format));
+       if (filename.empty()) {
+               filename = latexName(false);
+               filename = addName(temppath(), filename);
+               filename = changeExtension(filename,
+                                          formats.extension(backend_format));
+       }

        // Plain text backend
        if (backend_format == "text") {
@@ -3557,7 +3575,8 @@
        // if format == "dvi") to the result dir.
        vector<ExportedFile>  const files =
                runparams.exportdata->externalFiles(format);
-       string const dest = onlyPath(result_file);
+       string const dest = runparams.export_folder.empty() ?
+               onlyPath(result_file) : runparams.export_folder;
        bool use_force = use_gui ? lyxrc.export_overwrite == ALL_FILES
                                 : force_overwrite == ALL_FILES;
        CopyStatus status = use_force ? FORCE : SUCCESS;
@@ -3566,9 +3585,15 @@
        vector<ExportedFile>::const_iterator const en = files.end();
        for (; it != en&&  status != CANCEL; ++it) {
                string const fmt = formats.getFormatFromFile(it->sourceName);
+               string fixedName = it->exportName;
+               while (fixedName.substr(0, 3) == "../")
+                       fixedName = fixedName.substr(3, fixedName.length() - 3);
+               FileName fixedFileName = makeAbsPath(fixedName, dest);
+               fixedFileName.onlyPath().createPath();
                status = copyFile(fmt, it->sourceName,
-                       makeAbsPath(it->exportName, dest),
-                       it->exportName, status == FORCE);
+                       fixedFileName,
+                       it->exportName, status == FORCE,
+                       runparams.export_folder.empty());
        }

I guess we have to live with this. What happens with the current code, actually? Do we output e.g. .eps files to parent directories?

--
Julien

Reply via email to