3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Alfredo Braunstein
8 * \author Lars Gullik Bjønnes
9 * \author Jean Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
14 * Full author contact details are available in file CREDITS.
22 #include "buffer_funcs.h"
23 #include "bufferparams.h"
24 #include "converter.h"
29 #include "output_plaintext.h"
30 #include "outputparams.h"
31 #include "frontends/Alert.h"
33 #include "support/filetools.h"
34 #include "support/lyxlib.h"
35 #include "support/package.h"
37 #include <boost/filesystem/operations.hpp>
39 using lyx::support::AddName;
40 using lyx::support::bformat;
41 using lyx::support::ChangeExtension;
42 using lyx::support::contains;
43 using lyx::support::MakeAbsPath;
44 using lyx::support::MakeDisplayPath;
45 using lyx::support::OnlyFilename;
46 using lyx::support::OnlyPath;
47 using lyx::support::package;
48 using lyx::support::prefixIs;
54 namespace fs = boost::filesystem;
58 vector<string> const Backends(Buffer const & buffer)
61 if (buffer.params().getLyXTextClass().isTeXClassAvailable())
62 v.push_back(BufferFormat(buffer));
68 /// ask the user what to do if a file already exists
69 int checkOverwrite(string const & filename)
71 if (fs::exists(filename)) {
72 string text = bformat(_("The file %1$s already exists.\n\n"
73 "Do you want to over-write that file?"),
74 MakeDisplayPath(filename));
75 return Alert::prompt(_("Over-write file?"),
77 _("&Over-write"), _("Over-write &all"),
91 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
92 * will be asked before existing files are overwritten.
94 * - SUCCESS if this file got copied
95 * - FORCE if subsequent calls should not ask for confirmation before
96 * overwriting files anymore.
97 * - CANCEL if the export should be cancelled
99 CopyStatus copyFile(string const & format,
100 string const & sourceFile, string const & destFile,
101 string const & latexFile, bool force)
103 CopyStatus ret = force ? FORCE : SUCCESS;
105 // Only copy files that are in our tmp dir, all other files would
106 // overwrite themselves. This check could be changed to
107 // boost::filesystem::equivalent(sourceFile, destFile) if export to
108 // other directories than the document directory is desired.
109 if (!prefixIs(OnlyPath(sourceFile), package().temp_dir()))
113 switch(checkOverwrite(destFile)) {
125 Mover const & mover = movers(format);
126 if (!mover.copy(sourceFile, destFile, latexFile))
127 Alert::error(_("Couldn't copy file"),
128 bformat(_("Copying %1$s to %2$s failed."),
129 MakeDisplayPath(sourceFile),
130 MakeDisplayPath(destFile)));
138 bool Exporter::Export(Buffer * buffer, string const & format,
139 bool put_in_tempdir, string & result_file)
141 string backend_format;
142 OutputParams runparams;
143 runparams.flavor = OutputParams::LATEX;
144 runparams.linelen = lyxrc.ascii_linelen;
145 vector<string> backends = Backends(*buffer);
146 if (find(backends.begin(), backends.end(), format) == backends.end()) {
147 for (vector<string>::const_iterator it = backends.begin();
148 it != backends.end(); ++it) {
150 converters.getPath(*it, format);
152 runparams.flavor = converters.getFlavor(p);
153 backend_format = *it;
157 if (backend_format.empty()) {
158 Alert::error(_("Couldn't export file"),
159 bformat(_("No information for exporting the format %1$s."),
160 formats.prettyName(format)));
164 backend_format = format;
166 string filename = buffer->getLatexName(false);
167 filename = AddName(buffer->temppath(), filename);
168 filename = ChangeExtension(filename,
169 formats.extension(backend_format));
172 if (backend_format == "text")
173 writeFileAscii(*buffer, filename, runparams);
175 else if (buffer->isLinuxDoc()) {
176 runparams.nice = !put_in_tempdir;
177 buffer->makeLinuxDocFile(filename, runparams);
180 else if (buffer->isDocBook()) {
181 runparams.nice = !put_in_tempdir;
182 buffer->makeDocBookFile(filename, runparams);
185 else if (backend_format == format) {
186 runparams.nice = true;
187 buffer->makeLaTeXFile(filename, string(), runparams);
188 } else if (contains(buffer->filePath(), ' ')) {
189 Alert::error(_("File name error"),
190 _("The directory path to the document cannot contain spaces."));
193 runparams.nice = false;
194 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
197 if (!converters.convert(buffer, filename, filename,
198 backend_format, format, result_file))
201 if (!put_in_tempdir) {
202 string const tmp_result_file = result_file;
203 result_file = ChangeExtension(buffer->fileName(),
204 formats.extension(format));
205 // We need to copy referenced files (e. g. included graphics
206 // if format == "dvi") to the result dir.
207 vector<ExportedFile> const files =
208 runparams.exportdata->externalFiles(format);
209 string const dest = OnlyPath(result_file);
210 CopyStatus status = SUCCESS;
211 for (vector<ExportedFile>::const_iterator it = files.begin();
212 it != files.end() && status != CANCEL; ++it) {
214 formats.getFormatFromFile(it->sourceName);
215 status = copyFile(fmt, it->sourceName,
216 MakeAbsPath(it->exportName, dest),
217 it->exportName, status == FORCE);
219 if (status == CANCEL) {
220 buffer->message(_("Document export cancelled."));
222 // Finally copy the main file
223 status = copyFile(format, tmp_result_file,
224 result_file, result_file,
226 buffer->message(bformat(_("Document exported as %1$s"
228 formats.prettyName(format),
229 MakeDisplayPath(result_file)));
237 bool Exporter::Export(Buffer * buffer, string const & format,
241 return Export(buffer, format, put_in_tempdir, result_file);
245 bool Exporter::Preview(Buffer * buffer, string const & format)
248 if (!Export(buffer, format, true, result_file))
250 return formats.view(*buffer, result_file, format);
254 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
256 vector<string> backends = Backends(buffer);
257 for (vector<string>::const_iterator it = backends.begin();
258 it != backends.end(); ++it)
259 if (converters.isReachable(*it, format))
265 vector<Format const *> const
266 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
268 vector<string> backends = Backends(buffer);
269 vector<Format const *> result =
270 converters.getReachable(backends[0], only_viewable, true);
271 for (vector<string>::const_iterator it = backends.begin() + 1;
272 it != backends.end(); ++it) {
273 vector<Format const *> r =
274 converters.getReachable(*it, only_viewable, false);
275 result.insert(result.end(), r.begin(), r.end());
281 ExportedFile::ExportedFile(string const & s, string const & e) :
282 sourceName(s), exportName(e) {}
285 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
287 return f1.sourceName == f2.sourceName &&
288 f1.exportName == f2.exportName;
293 void ExportData::addExternalFile(string const & format,
294 string const & sourceName,
295 string const & exportName)
297 BOOST_ASSERT(lyx::support::AbsolutePath(sourceName));
299 // Make sure that we have every file only once, otherwise copyFile()
300 // would ask several times if it should overwrite a file.
301 vector<ExportedFile> & files = externalfiles[format];
302 ExportedFile file(sourceName, exportName);
303 if (find(files.begin(), files.end(), file) == files.end())
304 files.push_back(file);
308 void ExportData::addExternalFile(string const & format,
309 string const & sourceName)
311 addExternalFile(format, sourceName, OnlyFilename(sourceName));
315 vector<ExportedFile> const
316 ExportData::externalFiles(string const & format) const
318 FileMap::const_iterator cit = externalfiles.find(format);
319 if (cit != externalfiles.end())
321 return vector<ExportedFile>();