saner borderline between buffer and exporter

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21080 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2007-10-20 10:51:13 +00:00
parent f762cbf3c6
commit ee005614a3
7 changed files with 246 additions and 265 deletions

View File

@ -18,9 +18,10 @@
#include "buffer_funcs.h"
#include "BufferList.h"
#include "BufferParams.h"
#include "Counters.h"
#include "Bullet.h"
#include "Chktex.h"
#include "Converter.h"
#include "Counters.h"
#include "debug.h"
#include "DocIterator.h"
#include "Encoding.h"
@ -45,6 +46,7 @@
#include "output.h"
#include "output_docbook.h"
#include "output_latex.h"
#include "output_plaintext.h"
#include "Paragraph.h"
#include "paragraph_funcs.h"
#include "ParagraphParameters.h"
@ -1522,7 +1524,7 @@ bool Buffer::dispatch(FuncRequest const & func, bool * result)
switch (func.action) {
case LFUN_BUFFER_EXPORT: {
bool const tmp = Exporter::Export(this, to_utf8(func.argument()), false);
bool const tmp = doExport(to_utf8(func.argument()), false);
if (result)
*result = tmp;
break;
@ -2211,4 +2213,180 @@ string Buffer::bufferFormat() const
}
bool Buffer::doExport(string const & format,
bool put_in_tempdir, string & result_file)
{
string backend_format;
OutputParams runparams(&params().encoding());
runparams.flavor = OutputParams::LATEX;
runparams.linelen = lyxrc.plaintext_linelen;
vector<string> backs = backends();
if (find(backs.begin(), backs.end(), format) == backs.end()) {
// Get shortest path to format
Graph::EdgePath path;
for (vector<string>::const_iterator it = backs.begin();
it != backs.end(); ++it) {
Graph::EdgePath p = theConverters().getPath(*it, format);
if (!p.empty() && (path.empty() || p.size() < path.size())) {
backend_format = *it;
path = p;
}
}
if (!path.empty())
runparams.flavor = theConverters().getFlavor(path);
else {
Alert::error(_("Couldn't export file"),
bformat(_("No information for exporting the format %1$s."),
formats.prettyName(format)));
return false;
}
} else {
backend_format = format;
// FIXME: Don't hardcode format names here, but use a flag
if (backend_format == "pdflatex")
runparams.flavor = OutputParams::PDFLATEX;
}
string filename = latexName(false);
filename = addName(temppath(), filename);
filename = changeExtension(filename,
formats.extension(backend_format));
// Plain text backend
if (backend_format == "text")
writePlaintextFile(*this, FileName(filename), runparams);
// no backend
else if (backend_format == "lyx")
writeFile(FileName(filename));
// Docbook backend
else if (isDocBook()) {
runparams.nice = !put_in_tempdir;
makeDocBookFile(FileName(filename), runparams);
}
// LaTeX backend
else if (backend_format == format) {
runparams.nice = true;
if (!makeLaTeXFile(FileName(filename), string(), runparams))
return false;
} else if (!lyxrc.tex_allows_spaces
&& support::contains(filePath(), ' ')) {
Alert::error(_("File name error"),
_("The directory path to the document cannot contain spaces."));
return false;
} else {
runparams.nice = false;
if (!makeLaTeXFile(FileName(filename), filePath(), runparams))
return false;
}
string const error_type = (format == "program")
? "Build" : bufferFormat();
string const ext = formats.extension(format);
FileName const tmp_result_file(changeExtension(filename, ext));
bool const success = theConverters().convert(this, FileName(filename),
tmp_result_file, FileName(absFileName()), backend_format, format,
errorList(error_type));
// Emit the signal to show the error list.
if (format != backend_format)
errors(error_type);
if (!success)
return false;
if (put_in_tempdir)
result_file = tmp_result_file.absFilename();
else {
result_file = changeExtension(absFileName(), ext);
// We need to copy referenced files (e. g. included graphics
// if format == "dvi") to the result dir.
vector<ExportedFile> const files =
runparams.exportdata->externalFiles(format);
string const dest = onlyPath(result_file);
CopyStatus status = SUCCESS;
for (vector<ExportedFile>::const_iterator it = files.begin();
it != files.end() && status != CANCEL; ++it) {
string const fmt =
formats.getFormatFromFile(it->sourceName);
status = copyFile(fmt, it->sourceName,
makeAbsPath(it->exportName, dest),
it->exportName, status == FORCE);
}
if (status == CANCEL) {
message(_("Document export cancelled."));
} else if (tmp_result_file.exists()) {
// Finally copy the main file
status = copyFile(format, tmp_result_file,
FileName(result_file), result_file,
status == FORCE);
message(bformat(_("Document exported as %1$s "
"to file `%2$s'"),
formats.prettyName(format),
makeDisplayPath(result_file)));
} else {
// This must be a dummy converter like fax (bug 1888)
message(bformat(_("Document exported as %1$s"),
formats.prettyName(format)));
}
}
return true;
}
bool Buffer::doExport(string const & format, bool put_in_tempdir)
{
string result_file;
return doExport(format, put_in_tempdir, result_file);
}
bool Buffer::preview(string const & format)
{
string result_file;
if (!doExport(format, true, result_file))
return false;
return formats.view(*this, FileName(result_file), format);
}
bool Buffer::isExportable(string const & format) const
{
vector<string> backs = backends();
for (vector<string>::const_iterator it = backs.begin();
it != backs.end(); ++it)
if (theConverters().isReachable(*it, format))
return true;
return false;
}
vector<Format const *> Buffer::exportableFormats(bool only_viewable) const
{
vector<string> backs = backends();
vector<Format const *> result =
theConverters().getReachable(backs[0], only_viewable, true);
for (vector<string>::const_iterator it = backs.begin() + 1;
it != backs.end(); ++it) {
vector<Format const *> r =
theConverters().getReachable(*it, only_viewable, false);
result.insert(result.end(), r.begin(), r.end());
}
return result;
}
vector<string> Buffer::backends() const
{
vector<string> v;
if (params().getTextClass().isTeXClassAvailable()) {
v.push_back(bufferFormat());
// FIXME: Don't hardcode format names here, but use a flag
if (v.back() == "latex")
v.push_back("pdflatex");
}
v.push_back("text");
v.push_back("lyx");
return v;
}
} // namespace lyx

View File

@ -34,6 +34,7 @@ class ErrorList;
class FuncRequest;
class Inset;
class Font;
class Format;
class Lexer;
class LyXRC;
class Text;
@ -415,7 +416,22 @@ public:
/// return the format of the buffer on a string
std::string bufferFormat() const;
///
bool doExport(std::string const & format, bool put_in_tempdir,
std::string & result_file);
///
bool doExport(std::string const & format, bool put_in_tempdir);
///
bool preview(std::string const & format);
///
bool isExportable(std::string const & format) const;
///
std::vector<Format const *> exportableFormats(bool only_viewable) const;
private:
///
std::vector<std::string> backends() const;
/** Inserts a file into a document
\return \c false if method fails.
*/

View File

@ -18,20 +18,11 @@
#include "Exporter.h"
#include "Buffer.h"
#include "buffer_funcs.h"
#include "BufferParams.h"
#include "Converter.h"
#include "Format.h"
#include "gettext.h"
#include "LyXRC.h"
#include "Mover.h"
#include "output_plaintext.h"
#include "OutputParams.h"
#include "frontends/alert.h"
#include "support/filetools.h"
#include "support/lyxlib.h"
#include "support/Package.h"
#include <boost/filesystem/operations.hpp>
@ -43,12 +34,8 @@ using std::vector;
namespace lyx {
using support::addName;
using support::bformat;
using support::changeExtension;
using support::contains;
using support::FileName;
using support::makeAbsPath;
using support::makeDisplayPath;
using support::onlyFilename;
using support::onlyPath;
@ -58,46 +45,21 @@ using support::prefixIs;
namespace Alert = frontend::Alert;
namespace fs = boost::filesystem;
namespace {
vector<string> const Backends(Buffer const & buffer)
{
vector<string> v;
if (buffer.params().getTextClass().isTeXClassAvailable()) {
v.push_back(buffer.bufferFormat());
// FIXME: Don't hardcode format names here, but use a flag
if (v.back() == "latex")
v.push_back("pdflatex");
}
v.push_back("text");
v.push_back("lyx");
return v;
}
/// ask the user what to do if a file already exists
int checkOverwrite(FileName const & filename)
static int checkOverwrite(FileName const & filename)
{
if (filename.exists()) {
docstring text = bformat(_("The file %1$s already exists.\n\n"
"Do you want to overwrite that file?"),
makeDisplayPath(filename.absFilename()));
return Alert::prompt(_("Overwrite file?"),
text, 0, 2,
_("&Overwrite"), _("Overwrite &all"),
_("&Cancel export"));
}
return 0;
if (!filename.exists())
return 0;
docstring text = bformat(_("The file %1$s already exists.\n\n"
"Do you want to overwrite that file?"),
makeDisplayPath(filename.absFilename()));
return Alert::prompt(_("Overwrite file?"),
text, 0, 2,
_("&Overwrite"), _("Overwrite &all"),
_("&Cancel export"));
}
enum CopyStatus {
SUCCESS,
FORCE,
CANCEL
};
/** copy file \p sourceFile to \p destFile. If \p force is false, the user
* will be asked before existing files are overwritten.
* \return
@ -142,174 +104,10 @@ CopyStatus copyFile(string const & format,
return ret;
}
} //namespace anon
bool Exporter::Export(Buffer * buffer, string const & format,
bool put_in_tempdir, string & result_file)
{
string backend_format;
OutputParams runparams(&buffer->params().encoding());
runparams.flavor = OutputParams::LATEX;
runparams.linelen = lyxrc.plaintext_linelen;
vector<string> backends = Backends(*buffer);
if (find(backends.begin(), backends.end(), format) == backends.end()) {
// Get shortest path to format
Graph::EdgePath path;
for (vector<string>::const_iterator it = backends.begin();
it != backends.end(); ++it) {
Graph::EdgePath p = theConverters().getPath(*it, format);
if (!p.empty() && (path.empty() || p.size() < path.size())) {
backend_format = *it;
path = p;
}
}
if (!path.empty())
runparams.flavor = theConverters().getFlavor(path);
else {
Alert::error(_("Couldn't export file"),
bformat(_("No information for exporting the format %1$s."),
formats.prettyName(format)));
return false;
}
} else {
backend_format = format;
// FIXME: Don't hardcode format names here, but use a flag
if (backend_format == "pdflatex")
runparams.flavor = OutputParams::PDFLATEX;
}
string filename = buffer->latexName(false);
filename = addName(buffer->temppath(), filename);
filename = changeExtension(filename,
formats.extension(backend_format));
// Plain text backend
if (backend_format == "text")
writePlaintextFile(*buffer, FileName(filename), runparams);
// no backend
else if (backend_format == "lyx")
buffer->writeFile(FileName(filename));
// Docbook backend
else if (buffer->isDocBook()) {
runparams.nice = !put_in_tempdir;
buffer->makeDocBookFile(FileName(filename), runparams);
}
// LaTeX backend
else if (backend_format == format) {
runparams.nice = true;
if (!buffer->makeLaTeXFile(FileName(filename), string(), runparams))
return false;
} else if (!lyxrc.tex_allows_spaces
&& contains(buffer->filePath(), ' ')) {
Alert::error(_("File name error"),
_("The directory path to the document cannot contain spaces."));
return false;
} else {
runparams.nice = false;
if (!buffer->makeLaTeXFile(FileName(filename), buffer->filePath(), runparams))
return false;
}
string const error_type = (format == "program")
? "Build" : buffer->bufferFormat();
string const ext = formats.extension(format);
FileName const tmp_result_file(changeExtension(filename, ext));
bool const success = theConverters().convert(buffer, FileName(filename),
tmp_result_file, FileName(buffer->absFileName()), backend_format, format,
buffer->errorList(error_type));
// Emit the signal to show the error list.
if (format != backend_format)
buffer->errors(error_type);
if (!success)
return false;
if (put_in_tempdir)
result_file = tmp_result_file.absFilename();
else {
result_file = changeExtension(buffer->absFileName(), ext);
// We need to copy referenced files (e. g. included graphics
// if format == "dvi") to the result dir.
vector<ExportedFile> const files =
runparams.exportdata->externalFiles(format);
string const dest = onlyPath(result_file);
CopyStatus status = SUCCESS;
for (vector<ExportedFile>::const_iterator it = files.begin();
it != files.end() && status != CANCEL; ++it) {
string const fmt =
formats.getFormatFromFile(it->sourceName);
status = copyFile(fmt, it->sourceName,
makeAbsPath(it->exportName, dest),
it->exportName, status == FORCE);
}
if (status == CANCEL) {
buffer->message(_("Document export cancelled."));
} else if (tmp_result_file.exists()) {
// Finally copy the main file
status = copyFile(format, tmp_result_file,
FileName(result_file), result_file,
status == FORCE);
buffer->message(bformat(_("Document exported as %1$s "
"to file `%2$s'"),
formats.prettyName(format),
makeDisplayPath(result_file)));
} else {
// This must be a dummy converter like fax (bug 1888)
buffer->message(bformat(_("Document exported as %1$s"),
formats.prettyName(format)));
}
}
return true;
}
bool Exporter::Export(Buffer * buffer, string const & format,
bool put_in_tempdir)
{
string result_file;
return Export(buffer, format, put_in_tempdir, result_file);
}
bool Exporter::preview(Buffer * buffer, string const & format)
{
string result_file;
if (!Export(buffer, format, true, result_file))
return false;
return formats.view(*buffer, FileName(result_file), format);
}
bool Exporter::isExportable(Buffer const & buffer, string const & format)
{
vector<string> backends = Backends(buffer);
for (vector<string>::const_iterator it = backends.begin();
it != backends.end(); ++it)
if (theConverters().isReachable(*it, format))
return true;
return false;
}
vector<Format const *> const
Exporter::getExportableFormats(Buffer const & buffer, bool only_viewable)
{
vector<string> backends = Backends(buffer);
vector<Format const *> result =
theConverters().getReachable(backends[0], only_viewable, true);
for (vector<string>::const_iterator it = backends.begin() + 1;
it != backends.end(); ++it) {
vector<Format const *> r =
theConverters().getReachable(*it, only_viewable, false);
result.insert(result.end(), r.begin(), r.end());
}
return result;
}
ExportedFile::ExportedFile(FileName const & s, string const & e) :
sourceName(s), exportName(e) {}
ExportedFile::ExportedFile(FileName const & s, string const & e)
: sourceName(s), exportName(e)
{}
bool operator==(ExportedFile const & f1, ExportedFile const & f2)

View File

@ -22,34 +22,26 @@
namespace lyx {
class Buffer;
class Format;
class Exporter {
public:
///
static
bool Export(Buffer * buffer, std::string const & format,
bool put_in_tempdir, std::string & result_file);
///
static
bool Export(Buffer * buffer, std::string const & format,
bool put_in_tempdir);
///
static
bool preview(Buffer * buffer, std::string const & format);
///
static
bool isExportable(Buffer const & buffer, std::string const & format);
///
static
std::vector<Format const *> const
getExportableFormats(Buffer const & buffer, bool only_viewable);
///
enum CopyStatus {
SUCCESS,
FORCE,
CANCEL
};
/** copy file \p sourceFile to \p destFile. If \p force is false, the user
* will be asked before existing files are overwritten.
* \return
* - SUCCESS if this file got copied
* - FORCE if subsequent calls should not ask for confirmation before
* overwriting files anymore.
* - CANCEL if the export should be cancelled
*/
CopyStatus copyFile(std::string const & format,
support::FileName const & sourceFile, support::FileName const & destFile,
std::string const & latexFile, bool force);
class ExportedFile {
public:
ExportedFile(support::FileName const &, std::string const &);

View File

@ -35,7 +35,6 @@
#include "DispatchResult.h"
#include "Encoding.h"
#include "ErrorList.h"
#include "Exporter.h"
#include "Format.h"
#include "FuncRequest.h"
#include "FuncStatus.h"
@ -509,7 +508,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
case LFUN_BUFFER_EXPORT:
enable = cmd.argument() == "custom"
|| Exporter::isExportable(*buf, to_utf8(cmd.argument()));
|| buf->isExportable(to_utf8(cmd.argument()));
break;
case LFUN_BUFFER_CHKTEX:
@ -517,7 +516,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
break;
case LFUN_BUILD_PROGRAM:
enable = Exporter::isExportable(*buf, "program");
enable = buf->isExportable("program");
break;
case LFUN_VC_REGISTER:
@ -572,7 +571,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
|| name == "prefs"
|| name == "texinfo";
else if (name == "print")
enable = Exporter::isExportable(*buf, "dvi")
enable = buf->isExportable("dvi")
&& lyxrc.print_command != "none";
else if (name == "character") {
if (!view())
@ -983,27 +982,27 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
case LFUN_BUFFER_UPDATE:
BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
Exporter::Export(lyx_view_->buffer(), argument, true);
lyx_view_->buffer()->doExport(argument, true);
break;
case LFUN_BUFFER_VIEW:
BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
Exporter::preview(lyx_view_->buffer(), argument);
lyx_view_->buffer()->preview(argument);
break;
case LFUN_MASTER_BUFFER_UPDATE:
BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
Exporter::Export(lyx_view_->buffer()->masterBuffer(), argument, true);
lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
break;
case LFUN_MASTER_BUFFER_VIEW:
BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
Exporter::preview(lyx_view_->buffer()->masterBuffer(), argument);
lyx_view_->buffer()->masterBuffer()->preview(argument);
break;
case LFUN_BUILD_PROGRAM:
BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
Exporter::Export(lyx_view_->buffer(), "program", true);
lyx_view_->buffer()->doExport("program", true);
break;
case LFUN_BUFFER_CHKTEX:
@ -1015,9 +1014,8 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
if (argument == "custom")
lyx_view_->getDialogs().show("sendto");
else {
Exporter::Export(lyx_view_->buffer(), argument, false);
}
else
lyx_view_->buffer()->doExport(argument, false);
break;
case LFUN_BUFFER_EXPORT_CUSTOM: {
@ -1048,7 +1046,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
break;
} else {
Exporter::Export(buffer, format_name, true, filename);
buffer->doExport(format_name, true, filename);
}
// Substitute $$FName for filename
@ -1085,7 +1083,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
Buffer * buffer = lyx_view_->buffer();
if (!Exporter::Export(buffer, "dvi", true)) {
if (!buffer->doExport("dvi", true)) {
showPrintError(buffer->absFileName());
break;
}

View File

@ -23,7 +23,6 @@
#include "BufferParams.h"
#include "CutAndPaste.h"
#include "debug.h"
#include "Exporter.h"
#include "Floating.h"
#include "FloatList.h"
#include "Format.h"
@ -549,15 +548,15 @@ void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
action = LFUN_BUFFER_IMPORT;
break;
case MenuItem::ViewFormats:
formats = Exporter::getExportableFormats(*buf, true);
formats = buf->exportableFormats(true);
action = LFUN_BUFFER_VIEW;
break;
case MenuItem::UpdateFormats:
formats = Exporter::getExportableFormats(*buf, true);
formats = buf->exportableFormats(true);
action = LFUN_BUFFER_UPDATE;
break;
default:
formats = Exporter::getExportableFormats(*buf, false);
formats = buf->exportableFormats(false);
action = LFUN_BUFFER_EXPORT;
}
sort(formats.begin(), formats.end(), compare_format());

View File

@ -483,7 +483,7 @@ docstring const InsetGraphics::createDocBookAttributes() const
namespace {
enum CopyStatus {
enum GraphicsCopyStatus {
SUCCESS,
FAILURE,
IDENTICAL_PATHS,
@ -491,7 +491,7 @@ enum CopyStatus {
};
std::pair<CopyStatus, FileName> const
std::pair<GraphicsCopyStatus, FileName> const
copyFileIfNeeded(FileName const & file_in, FileName const & file_out)
{
unsigned long const checksum_in = support::sum(file_in);
@ -512,12 +512,12 @@ copyFileIfNeeded(FileName const & file_in, FileName const & file_out)
<< std::endl;
}
CopyStatus status = success ? SUCCESS : FAILURE;
GraphicsCopyStatus status = success ? SUCCESS : FAILURE;
return std::make_pair(status, file_out);
}
std::pair<CopyStatus, FileName> const
std::pair<GraphicsCopyStatus, FileName> const
copyToDirIfNeeded(DocFileName const & file, string const & dir)
{
using support::rtrim;
@ -619,7 +619,7 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
// This is necessary for DVI export.
string const temp_path = masterBuffer->temppath();
CopyStatus status;
GraphicsCopyStatus status;
boost::tie(status, temp_file) =
copyToDirIfNeeded(params().filename, temp_path);