Il 10/09/2011 01:39, Tommaso Cucinotta ha scritto:
However, graphics files are not copied, so the exported latex cannot compile unless you copy the additionally needed external files.

Independently on whether the output folder is explicitly provider or automatically inferred from an output filename, copying the files to the export folder seems not too difficult as well, like in the attached patch.

One note: I guess it's impossible for me to understand the whole logic behind these complex file name manipulations. I would largely prefer to leave this part to some expert of this area of the code.

    T.

Index: src/LyX.cpp
===================================================================
--- src/LyX.cpp	(revisione 39654)
+++ 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;
 	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 39654)
+++ 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;
 	///
 	bool preview(std::string const & format, bool includeall = false) const;
Index: src/Buffer.cpp
===================================================================
--- src/Buffer.cpp	(revisione 39654)
+++ src/Buffer.cpp	(copia locale)
@@ -1928,10 +1928,18 @@
 
 		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));
+					_("Don't know how to FUCK export to format: %1$s"), arg));
 			break;
 		}
 
@@ -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(' ');
+	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,9 @@
 	// 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;
+	LYXERR(Debug::GRAPHICS, "dest=" << dest);
 	bool use_force = use_gui ? lyxrc.export_overwrite == ALL_FILES
 				 : force_overwrite == ALL_FILES;
 	CopyStatus status = use_force ? FORCE : SUCCESS;
@@ -3566,8 +3586,14 @@
 	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);
+		LYXERR(Debug::FIND, "destfname=" << makeAbsPath(fixedName, dest).realPath() << ", exportName=" << it->exportName << ", fixedName=" << fixedName << ", dest=" << dest);
+		FileName fixedFileName = makeAbsPath(fixedName, dest);
+		fixedFileName.onlyPath().createPath();
 		status = copyFile(fmt, it->sourceName,
-			makeAbsPath(it->exportName, dest),
+			fixedFileName,
 			it->exportName, status == FORCE);
 	}
 
@@ -3596,15 +3622,15 @@
 }
 
 
-bool Buffer::doExport(string const & format, bool put_in_tempdir,
+bool Buffer::doExport(string const & target, bool put_in_tempdir,
 		      bool includeall) const
 {
 	string result_file;
 	// (1) export with all included children (omit \includeonly)
-	if (includeall && !doExport(format, put_in_tempdir, true, result_file))
+	if (includeall && !doExport(target, put_in_tempdir, true, result_file))
 		return false;
 	// (2) export with included children only
-	return doExport(format, put_in_tempdir, false, result_file);
+	return doExport(target, put_in_tempdir, false, result_file);
 }
 
 
Index: src/insets/InsetInclude.cpp
===================================================================
--- src/insets/InsetInclude.cpp	(revisione 39654)
+++ src/insets/InsetInclude.cpp	(copia locale)
@@ -513,15 +513,20 @@
 					      from_utf8(masterBuffer->filePath())));
 	}
 
+	string exppath = incfile;
+	if (!runparams.export_folder.empty())
+		exppath = makeAbsPath(exppath, runparams.export_folder).realPath();
+	FileName(exppath).onlyPath().createPath();
+
 	// write it to a file (so far the complete file)
 	string exportfile;
 	string mangled;
 	// bug 5681
 	if (type(params()) == LISTINGS) {
-		exportfile = incfile;
+		exportfile = exppath;
 		mangled = DocFileName(included_file).mangledFileName();
 	} else {
-		exportfile = changeExtension(incfile, ".tex");
+		exportfile = changeExtension(exppath, ".tex");
 		mangled = DocFileName(changeExtension(included_file.absFileName(), ".tex")).
 			mangledFileName();
 	}
@@ -832,8 +837,13 @@
 		return 0;
 	}
 
+	string exppath = incfile;
+	if (!runparams.export_folder.empty())
+		exppath = makeAbsPath(exppath, runparams.export_folder).realPath();
+	FileName(exppath).onlyPath().createPath();
+
 	// write it to a file (so far the complete file)
-	string const exportfile = changeExtension(incfile, ".sgml");
+	string const exportfile = changeExtension(exppath, ".sgml");
 	DocFileName writefile(changeExtension(included_file, ".sgml"));
 
 	Buffer * tmp = loadIfNeeded();

Reply via email to