Added -E option to specify export format and destination file-name (and export folder).

Addressing #4501.
See also http://comments.gmane.org/gmane.editors.lyx.devel/138329


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@39678 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Tommaso Cucinotta 2011-09-15 01:07:49 +00:00
parent 200b8428b8
commit eff76bd3ab
10 changed files with 119 additions and 31 deletions

View File

@ -1928,7 +1928,15 @@ bool Buffer::getStatus(FuncRequest const & cmd, FuncStatus & flag)
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,21 @@ bool Buffer::isExporting() const
}
bool Buffer::doExport(string const & format, bool put_in_tempdir,
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();
FileName(filename).onlyPath().createPath();
}
MarkAsExporting exporting(this);
string backend_format;
OutputParams runparams(&params().encoding());
runparams.flavor = OutputParams::LATEX;
runparams.linelen = lyxrc.plaintext_linelen;
runparams.includeall = includeall;
@ -3439,10 +3456,12 @@ bool Buffer::doExport(string const & format, bool put_in_tempdir,
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 +3576,8 @@ bool Buffer::doExport(string const & format, bool put_in_tempdir,
// 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 +3586,19 @@ bool Buffer::doExport(string const & format, bool put_in_tempdir,
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;
if (!runparams.export_folder.empty()) {
// Relative pathnames starting with ../ will be sanitized
// if exporting to a different folder
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 +3626,15 @@ bool Buffer::doExport(string const & format, bool put_in_tempdir,
}
bool Buffer::doExport(string const & format, bool put_in_tempdir,
bool includeall) const
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);
}

View File

@ -602,11 +602,12 @@ public:
///
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;

View File

@ -53,7 +53,9 @@ static int checkOverwrite(FileName const & filename)
/** 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 only_tmp
* is true, then only copy files that are in our tmp dir (to avoid other files
* overwriting themselves).
* \return
* - SUCCESS if this file got copied
* - FORCE if subsequent calls should not ask for confirmation before
@ -62,17 +64,16 @@ static int checkOverwrite(FileName const & filename)
*/
CopyStatus copyFile(string const & format,
FileName const & sourceFile, FileName const & destFile,
string const & latexFile, bool force)
string const & latexFile, bool force, bool only_tmp)
{
CopyStatus ret = force ? FORCE : SUCCESS;
// Only copy files that are in our tmp dir, all other files would
// overwrite themselves. This check could be changed to
// This check could be changed to
// boost::filesystem::equivalent(sourceFile, destFile) if export to
// 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 ((only_tmp && !prefixIs(onlyPath(sourceFile.absFileName()), package().temp_dir().absFileName()))
|| sourceFile.checksum() == destFile.checksum())
return ret;

View File

@ -30,7 +30,9 @@ enum CopyStatus {
/** 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 only_tmp
* is true, then only copy files that are in our tmp dir (to avoid other files
* overwriting themselves).
* \return
* - SUCCESS if this file got copied
* - FORCE if subsequent calls should not ask for confirmation before
@ -39,7 +41,7 @@ enum CopyStatus {
*/
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 only_tmp = true);
class ExportedFile {

View File

@ -1054,6 +1054,9 @@ int parse_help(string const &, string const &, string &)
" 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 +1126,24 @@ int parse_execute(string const & arg, string const &, string & batch)
}
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 +1237,10 @@ void LyX::easyParse(int & argc, char * argv[])
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;

View File

@ -2947,7 +2947,7 @@ void LyXAction::init()
/*!
* \var lyx::FuncCode lyx::LFUN_BUFFER_EXPORT
* \li Action: Exports the current buffer (document) to the given format.
* \li Syntax: buffer-export <FORMAT>
* \li Syntax: buffer-export <FORMAT> [<DEST>]
* \li Params: <FORMAT> is either "custom" or one of the formats which you
can find in Tools->Preferences->File formats->Format.
Usual format you will enter is "pdf2" (pdflatex),
@ -2956,6 +2956,9 @@ void LyXAction::init()
want to start from and for the command that you want to
apply to this format. Internally the control is then passed
to #LFUN_BUFFER_EXPORT_CUSTOM.
<DEST> If present, this argument provides the export destination
filename. Its containing folder will also be the destination
folder, where all the needed external files will be copied.
* \li Origin: Lgb, 29 Jul 1997
* \endvar
*/

View File

@ -251,6 +251,9 @@ public:
/// Include all children notwithstanding the use of \includeonly
bool includeall;
/// Explicit output folder, if any is desired
std::string export_folder;
};

View File

@ -19,6 +19,7 @@
#include "Cursor.h"
#include "DispatchResult.h"
#include "Encoding.h"
#include "Exporter.h"
#include "Format.h"
#include "FuncRequest.h"
#include "FuncStatus.h"
@ -263,6 +264,9 @@ void InsetBibtex::latex(otexstream & os, OutputParams const & runparams) const
odocstringstream dbs;
bool didone = false;
// determine the export format
string const tex_format = flavor2format(runparams.flavor);
for (; it != en; ++it) {
string utf8input = to_utf8(*it);
string database =
@ -278,7 +282,6 @@ void InsetBibtex::latex(otexstream & os, OutputParams const & runparams) const
database = removeExtension(in_file.mangledFileName());
FileName const out_file = makeAbsPath(database + ".bib",
buffer().masterBuffer()->temppath());
bool const success = in_file.copyTo(out_file);
if (!success) {
lyxerr << "Failed to copy '" << in_file
@ -286,6 +289,7 @@ void InsetBibtex::latex(otexstream & os, OutputParams const & runparams) const
<< endl;
}
} else if (!runparams.inComment && runparams.nice && not_from_texmf) {
runparams.exportdata->addExternalFile(tex_format, try_in_file, database + ".bib");
if (!isValidLaTeXFileName(database)) {
frontend::Alert::warning(_("Invalid filename"),
_("The following filename will cause troubles "

View File

@ -789,7 +789,14 @@ void InsetGraphics::latex(otexstream & os,
// 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);

View File

@ -513,15 +513,21 @@ void InsetInclude::latex(otexstream & os, OutputParams const & runparams) const
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();
}
@ -702,6 +708,8 @@ void InsetInclude::latex(otexstream & os, OutputParams const & runparams) const
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 +840,14 @@ int InsetInclude::docbook(odocstream & os, OutputParams const & runparams) const
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();