Il 11/09/2011 19:24, Tommaso Cucinotta ha scritto:
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.

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.

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;
 	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;
 	///
 	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(' ');
+	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());
 	}
 
 	if (status == CANCEL) {
@@ -3596,15 +3621,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 39668)
+++ 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();
 	}
@@ -693,8 +698,8 @@
 			os << '\\' << from_ascii(params().getCmdName())
 			   << '{' << from_utf8(incfile) << '}';
 		} else {
-		incfile = changeExtension(incfile, ".tex");
-		incfile = latex_path(incfile);
+			incfile = changeExtension(incfile, ".tex");
+			incfile = latex_path(incfile);
 			// FIXME UNICODE
 			os << '\\' << from_ascii(params().getCmdName())
 			   << '{' << from_utf8(incfile) <<  '}';
@@ -702,6 +707,8 @@
 		break;
 	} 
 	case LISTINGS: {
+		runparams.exportdata->addExternalFile(tex_format, writefile,
+						      exportfile);
 		os << '\\' << from_ascii(params().getCmdName());
 		string const opt = to_utf8(params()["lstparams"]);
 		// opt is set in QInclude dialog and should have passed validation.
@@ -832,8 +839,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();
Index: src/insets/InsetGraphics.cpp
===================================================================
--- src/insets/InsetGraphics.cpp	(revisione 39668)
+++ src/insets/InsetGraphics.cpp	(copia locale)
@@ -789,7 +789,14 @@
 	// Convert the file if necessary.
 	// Remove the extension so LaTeX will use whatever is appropriate
 	// (when there are several versions in different formats)
-	latex_str += prepareFile(runparams);
+	string file_path = prepareFile(runparams);
+	if (!runparams.export_folder.empty()) {
+		/// Relative pathnames starting with ../ will be sanitized
+		/// if exporting to a different folder
+		while (file_path.substr(0, 17) == "\\lyxdot \\lyxdot /")
+			file_path = file_path.substr(17, file_path.length() - 17);
+	}
+	latex_str += file_path;
 	latex_str += '}' + after;
 	// FIXME UNICODE
 	os << from_utf8(latex_str);
Index: src/Exporter.h
===================================================================
--- src/Exporter.h	(revisione 39668)
+++ src/Exporter.h	(copia locale)
@@ -30,7 +30,9 @@
 
 
 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
- *  will be asked before existing files are overwritten.
+ *  will be asked before existing files are overwritten. If \p prefix_chk
+ *  is false, then \p destFile is not checked to be in the same path as
+ *  \p sourceFile.
  *  \return
  *  - SUCCESS if this file got copied
  *  - FORCE   if subsequent calls should not ask for confirmation before
@@ -39,7 +41,7 @@
  */
 CopyStatus copyFile(std::string const & format,
 	support::FileName const & sourceFile, support::FileName const & destFile,
-	std::string const & latexFile, bool force);
+	std::string const & latexFile, bool force, bool prefix_chk = true);
 
 
 class ExportedFile {
Index: src/Exporter.cpp
===================================================================
--- src/Exporter.cpp	(revisione 39668)
+++ src/Exporter.cpp	(copia locale)
@@ -62,7 +62,7 @@
  */
 CopyStatus copyFile(string const & format,
 		    FileName const & sourceFile, FileName const & destFile,
-		    string const & latexFile, bool force)
+		    string const & latexFile, bool force, bool prefix_chk)
 {
 	CopyStatus ret = force ? FORCE : SUCCESS;
 
@@ -72,7 +72,7 @@
 	// other directories than the document directory is desired.
 	// Also don't overwrite files that already exist and are identical
 	// to the source files.
-	if (!prefixIs(onlyPath(sourceFile.absFileName()), package().temp_dir().absFileName())
+	if ((prefix_chk && !prefixIs(onlyPath(sourceFile.absFileName()), package().temp_dir().absFileName()))
 	    || sourceFile.checksum() == destFile.checksum())
 		return ret;
 
Index: src/OutputParams.h
===================================================================
--- src/OutputParams.h	(revisione 39668)
+++ src/OutputParams.h	(copia locale)
@@ -251,6 +251,9 @@
 	
 	/// Include all children notwithstanding the use of \includeonly
 	bool includeall;
+
+	/// Explicit output folder, if any is desired
+	std::string export_folder;
 };
 
 

Reply via email to