1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
21 #include "lyx_cb.h" //ShowMessage()
22 #include "support/filetools.h"
24 #include "converter.h"
25 #include "frontends/Alert.h"
27 #include "BufferView.h"
32 bool Exporter::Export(Buffer * buffer, string const & format,
33 bool put_in_tempdir, string & result_file)
35 // There are so many different places that this function can be called
36 // from that the removal of auto insets is best done here. This ensures
37 // we always have a clean buffer for inserting errors found during export.
38 BufferView * bv = buffer->getUser();
40 // Remove all error insets
41 if (bv->removeAutoInsets()) {
42 #warning repaint() or update() or nothing ?
48 string backend_format;
49 vector<string> backends = Backends(buffer);
50 if (find(backends.begin(), backends.end(), format) == backends.end()) {
51 for (vector<string>::const_iterator it = backends.begin();
52 it != backends.end(); ++it) {
53 Converters::EdgePath p =
54 converters.getPath(*it, format);
56 lyxrc.pdf_mode = converters.usePdflatex(p);
61 if (backend_format.empty()) {
62 Alert::alert(_("Cannot export file"),
63 _("No information for exporting to ")
64 + formats.prettyName(format));
68 backend_format = format;
70 string filename = buffer->getLatexName(false);
71 if (!buffer->tmppath.empty())
72 filename = AddName(buffer->tmppath, filename);
73 filename = ChangeExtension(filename,
74 formats.extension(backend_format));
77 if (backend_format == "text")
78 buffer->writeFileAscii(filename, lyxrc.ascii_linelen);
80 else if (buffer->isLinuxDoc())
81 buffer->makeLinuxDocFile(filename, !put_in_tempdir);
83 else if (buffer->isDocBook())
84 buffer->makeDocBookFile(filename, !put_in_tempdir);
86 else if (backend_format == format)
87 buffer->makeLaTeXFile(filename, string(), true);
88 else if (contains(buffer->filePath(), ' ')) {
89 Alert::alert(_("Cannot run latex."),
90 _("The path to the lyx file cannot contain spaces."));
93 buffer->makeLaTeXFile(filename, buffer->filePath(), false);
95 string outfile_base = (put_in_tempdir)
96 ? filename : buffer->getLatexName(false);
98 if (!converters.convert(buffer, filename, outfile_base,
99 backend_format, format, result_file))
104 _("Document exported as ")
105 + formats.prettyName(format)
107 + MakeDisplayPath(result_file) +'\'');
112 bool Exporter::Export(Buffer * buffer, string const & format,
116 return Export(buffer, format, put_in_tempdir, result_file);
120 bool Exporter::Preview(Buffer * buffer, string const & format)
123 if (!Export(buffer, format, true, result_file))
125 return formats.view(buffer, result_file, format);
129 bool Exporter::IsExportable(Buffer const * buffer, string const & format)
131 vector<string> backends = Backends(buffer);
132 for (vector<string>::const_iterator it = backends.begin();
133 it != backends.end(); ++it)
134 if (converters.isReachable(*it, format))
140 vector<Format const *> const
141 Exporter::GetExportableFormats(Buffer const * buffer, bool only_viewable)
143 vector<string> backends = Backends(buffer);
144 vector<Format const *> result =
145 converters.getReachable(backends[0], only_viewable, true);
146 for (vector<string>::const_iterator it = backends.begin() + 1;
147 it != backends.end(); ++it) {
148 vector<Format const *> r =
149 converters.getReachable(*it, only_viewable, false);
150 result.insert(result.end(), r.begin(), r.end());
156 string const Exporter::BufferFormat(Buffer const * buffer)
158 if (buffer->isLinuxDoc())
160 else if (buffer->isDocBook())
162 else if (buffer->isLiterate())
169 vector<string> const Exporter::Backends(Buffer const * buffer)
172 v.push_back(BufferFormat(buffer));