Hi,
the attached patch adds the "-C" option to LyX, allowing to use its
converters
for arbitrary files conversions, e.g.:
lyx -C /path/to/source.dia /path/to/dest.eps
lyx --convert-to /path/to/source.dia /path/to/dest.eps
Despite being useful in general (as this is more powerful than
ImageMagik's convert),
this might be specifically useful when invoking elyxer for conversions
to HTML.
In such a case, instead of using the default converter for elyxer, one might
use "lyx -C" as the converter used by elyxer, and be able this way to
support
arbitrary image formats.
This option does not need any LyX buffer to operate with nor any LyX
file to be opened
when it is activated, that's why there's some unusual stuff in LyX.cpp
around
parse_convert_to().
Comments ?
Bye,
T.
Index: src/FuncCode.h
===================================================================
--- src/FuncCode.h (revisione 39791)
+++ src/FuncCode.h (copia locale)
@@ -449,6 +449,7 @@
LFUN_FORWARD_SEARCH,
LFUN_SCRIPT_INSERT, // gb, 20101123
// 350
+ LFUN_FILE_CONVERT_TO, // tommaso, 20111101
LFUN_LASTACTION // end of the table
};
Index: src/LyXAction.cpp
===================================================================
--- src/LyXAction.cpp (revisione 39791)
+++ src/LyXAction.cpp (copia locale)
@@ -3517,6 +3517,16 @@
*/
{ LFUN_INSET_COPY_AS, "inset-copy-as", ReadOnly | NoUpdate | AtPoint, Edit },
+/*!
+ * \var lyx::FuncCode lyx::LFUN_FILE_CONVERT_TO
+ * \li Action: Converts an input file (image, LyX, latex, etc.) to a different format
+ * \li Notion: Supports the --convert-to option, which is useful in combination with elyxer
+ * \li Syntax: file-convert-to <dest_format> <source_file> <dest_file>
+ * \li Origin: tommaso, 1 Oct 2011
+ * \endvar
+ */
+ { LFUN_FILE_CONVERT_TO, "file-convert-to", NoBuffer, System },
+
{ LFUN_NOACTION, "", Noop, Hidden }
#ifndef DOXYGEN_SHOULD_SKIP_THIS
};
Index: src/LyX.cpp
===================================================================
--- src/LyX.cpp (revisione 39791)
+++ src/LyX.cpp (copia locale)
@@ -103,6 +103,10 @@
OverwriteFiles force_overwrite = UNSPECIFIED;
+/// parse functions may drop here a command to be run without buffer, if any
+string batch_nobuf;
+
+
namespace {
// Filled with the command line arguments "foo" of "-sysdir foo" or
@@ -190,6 +194,8 @@
bool first_start;
/// the parsed command line batch command if any
vector<string> batch_commands;
+ /// the parsed command line batch commands to be run without buffer
+ vector<string> batch_commands_nobuf;
///
SpellChecker * spell_checker_;
@@ -323,7 +329,8 @@
// this is correct, since return values are inverted.
exit_status = !loadFiles();
- if (pimpl_->batch_commands.empty() || pimpl_->buffer_list_.empty()) {
+ if (pimpl_->batch_commands_nobuf.empty() &&
+ (pimpl_->batch_commands.empty() || pimpl_->buffer_list_.empty())) {
prepareExit();
return exit_status;
}
@@ -344,6 +351,15 @@
final_success |= !dr.error();
}
}
+
+ vector<string>::const_iterator bcit = pimpl_->batch_commands_nobuf.begin();
+ vector<string>::const_iterator bcend = pimpl_->batch_commands_nobuf.end();
+ DispatchResult dr;
+ for (; bcit != bcend; bcit++) {
+ LYXERR(Debug::ACTION, "dispatch: cmd: " << *bcit << " -> " << lyxaction.lookupFunc(*bcit));
+ lyx::dispatch(lyxaction.lookupFunc(*bcit), dr);
+ final_success |= !dr.error();
+ }
prepareExit();
return !final_success;
}
@@ -1065,6 +1081,8 @@
" specifying whether all files, main file only, or no files,\n"
" respectively, are to be overwritten during a batch export.\n"
" Anything else is equivalent to `all', but is not consumed.\n"
+ "\t-C [--convert-to] sourcefile destfile\n"
+ " convert sourcefile (image, latex, lyx, dvi, ...) to destfile.\n"
"\t-n [--no-remote]\n"
" open documents in a new instance\n"
"\t-r [--remote]\n"
@@ -1174,6 +1192,24 @@
}
+int parse_convert_to(string const & from, string const & dest, string &)
+{
+ if (from.empty()) {
+ lyxerr << to_utf8(_("Missing source filename after "
+ "--convert-to switch")) << endl;
+ exit(1);
+ }
+ if (dest.empty()) {
+ lyxerr << to_utf8(_("Missing destination filename after "
+ "--convert-to switch")) << endl;
+ exit(1);
+ }
+ batch_nobuf = "file-convert-to " + from + " " + dest;
+ use_gui = false;
+ return 1;
+}
+
+
int parse_geometry(string const & arg1, string const &, string &)
{
geometryArg = arg1;
@@ -1243,6 +1279,8 @@
cmdmap["--export-to"] = parse_export_to;
cmdmap["-i"] = parse_import;
cmdmap["--import"] = parse_import;
+ cmdmap["-C"] = parse_convert_to;
+ cmdmap["--convert-to"] = parse_convert_to;
cmdmap["-geometry"] = parse_geometry;
cmdmap["-batch"] = parse_batch;
cmdmap["-f"] = parse_force;
@@ -1266,9 +1304,12 @@
(i + 2 < argc) ? os::utf8_argv(i + 2) : string();
string batch;
+ batch_nobuf.clear();
int const remove = 1 + it->second(arg, arg2, batch);
if (!batch.empty())
pimpl_->batch_commands.push_back(batch);
+ if (!batch_nobuf.empty())
+ pimpl_->batch_commands_nobuf.push_back(batch_nobuf);
// Now, remove used arguments by shifting
// the following ones remove places down.
@@ -1283,24 +1324,62 @@
}
-FuncStatus getStatus(FuncRequest const & action)
+FuncStatus getStatus(FuncRequest const & cmd)
{
- LASSERT(theApp(), /**/);
- return theApp()->getStatus(action);
+ FuncStatus status;
+ if (cmd.action() == LFUN_FILE_CONVERT_TO) {
+ status.setEnabled(true);
+ return status;
+ } else {
+ LASSERT(theApp(), /**/);
+ return theApp()->getStatus(cmd);
+ }
+ return status;
}
void dispatch(FuncRequest const & action)
{
- LASSERT(theApp(), /**/);
- return theApp()->dispatch(action);
+ DispatchResult dr;
+ dispatch(action, dr);
}
-void dispatch(FuncRequest const & action, DispatchResult & dr)
+void dispatch(FuncRequest const & cmd, DispatchResult & dr)
{
+ FuncCode const action = cmd.action();
+ string const argument = to_utf8(cmd.argument());
+ FuncStatus const flag = getStatus(cmd);
+ if (!flag.enabled()) {
+ LYXERR(Debug::ACTION, "action "
+ << lyxaction.getActionName(action)
+ << " [" << action << "] is disabled at this location");
+ dr.setMessage(flag.message());
+ dr.setError(true);
+ dr.dispatched(false);
+ return;
+ };
+
+ LYXERR(Debug::ACTION, "cmd: " << cmd);
+ if (action == LFUN_FILE_CONVERT_TO) {
+ size_t p1 = argument.find(" ");
+ string const from = argument.substr(0, p1);
+ FileName from_file(from);
+ string const from_fmt = formats.getFormatFromFile(from_file);
+ string const to = argument.substr(p1+1, argument.length()-p1-1);;
+ FileName to_file(to);
+ string const fmt = formats.getFormatFromExtension(to_file.extension());
+ lyxerr << "fmt=" << fmt << ", from=" << from << ", to=" << to << std::endl;
+ ErrorList errors;
+ theConverters().convert(NULL, from_file, to_file, from_file,
+ from_fmt, fmt, errors);
+ dr.dispatched(true);
+ return;
+ }
+
+
LASSERT(theApp(), /**/);
- return theApp()->dispatch(action, dr);
+ return theApp()->dispatch(cmd, dr);
}