Il 09/09/2011 23:55, Tommaso Cucinotta ha scritto:
Thinking aloud:
-) a destination folder might be inferred from the destination file-name, i.e., its immediately containing folder.

And this is done in the attached patch.

Example of usage:
I have master.lyx including c/child.lyx (by relative path).
I issue:

  ./src/lyx -E latex /tmp/dest.tex master.lyx

and I get created:

  /tmp/dest.tex
  /tmp/c
  /tmp/c/child.tex

How does it look like ?

That's done by adding to OutputParams a further parameter, namely export_folder. If it is set to non-empty, then the pathname of the final exported file is set relative to such export_folder location, plus the relative path encoded within the inset.

However, graphics files are not copied, so the exported latex cannot compile unless you copy the additionally needed external files.

    T.

Index: src/LyX.cpp
===================================================================
--- src/LyX.cpp	(revisione 39648)
+++ 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 39648)
+++ 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 39648)
+++ 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") {
@@ -3596,15 +3614,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 39648)
+++ 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