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>
42 using support::addName;
43 using support::bformat;
44 using support::changeExtension;
45 using support::contains;
46 using support::FileName;
47 using support::makeAbsPath;
48 using support::makeDisplayPath;
49 using support::onlyFilename;
50 using support::onlyPath;
51 using support::package;
52 using support::prefixIs;
58 namespace Alert = frontend::Alert;
59 namespace fs = boost::filesystem;
63 vector<string> const Backends(Buffer const & buffer)
66 if (buffer.params().getLyXTextClass().isTeXClassAvailable()) {
67 v.push_back(bufferFormat(buffer));
68 // FIXME: Don't hardcode format names here, but use a flag
69 if (v.back() == "latex")
70 v.push_back("pdflatex");
78 /// ask the user what to do if a file already exists
79 int checkOverwrite(FileName const & filename)
81 if (fs::exists(filename.toFilesystemEncoding())) {
82 docstring text = bformat(_("The file %1$s already exists.\n\n"
83 "Do you want to over-write that file?"),
84 makeDisplayPath(filename.absFilename()));
85 return Alert::prompt(_("Over-write file?"),
87 _("&Over-write"), _("Over-write &all"),
101 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
102 * will be asked before existing files are overwritten.
104 * - SUCCESS if this file got copied
105 * - FORCE if subsequent calls should not ask for confirmation before
106 * overwriting files anymore.
107 * - CANCEL if the export should be cancelled
109 CopyStatus copyFile(string const & format,
110 FileName const & sourceFile, FileName const & destFile,
111 string const & latexFile, bool force)
113 CopyStatus ret = force ? FORCE : SUCCESS;
115 // Only copy files that are in our tmp dir, all other files would
116 // overwrite themselves. This check could be changed to
117 // boost::filesystem::equivalent(sourceFile, destFile) if export to
118 // other directories than the document directory is desired.
119 if (!prefixIs(onlyPath(sourceFile.absFilename()), package().temp_dir()))
123 switch(checkOverwrite(destFile)) {
135 Mover const & mover = getMover(format);
136 if (!mover.copy(sourceFile, destFile, latexFile))
137 Alert::error(_("Couldn't copy file"),
138 bformat(_("Copying %1$s to %2$s failed."),
139 makeDisplayPath(sourceFile.absFilename()),
140 makeDisplayPath(destFile.absFilename())));
148 bool Exporter::Export(Buffer * buffer, string const & format,
149 bool put_in_tempdir, string & result_file)
151 string backend_format;
152 OutputParams runparams;
153 runparams.flavor = OutputParams::LATEX;
154 runparams.linelen = lyxrc.plaintext_linelen;
155 vector<string> backends = Backends(*buffer);
156 // FIXME: Without this test export to lyx1[34] would be through
157 // latex -> lyx -> lyx1[34], because the first backend below with a
158 // working conversion path is used. We should replace this test and
159 // the explicit loop below with a method
160 // getShortestPath(vector<string> const & from, string const & to)
161 // which returns the shortest path from one of the formats in 'from'
163 if ((format == "lyx13x" || format == "lyx14x") &&
164 !theConverters().getPath("lyx", format).empty())
165 backend_format = "lyx";
166 else if (find(backends.begin(), backends.end(), format) == backends.end()) {
167 for (vector<string>::const_iterator it = backends.begin();
168 it != backends.end(); ++it) {
169 Graph::EdgePath p = theConverters().getPath(*it, format);
171 runparams.flavor = theConverters().getFlavor(p);
172 backend_format = *it;
176 if (backend_format.empty()) {
177 Alert::error(_("Couldn't export file"),
178 bformat(_("No information for exporting the format %1$s."),
179 formats.prettyName(format)));
183 backend_format = format;
184 // FIXME: Don't hardcode format names here, but use a flag
185 if (backend_format == "pdflatex")
186 runparams.flavor = OutputParams::PDFLATEX;
189 string filename = buffer->getLatexName(false);
190 filename = addName(buffer->temppath(), filename);
191 filename = changeExtension(filename,
192 formats.extension(backend_format));
195 if (backend_format == "text")
196 writeFileAscii(*buffer, FileName(filename), runparams);
198 else if (backend_format == "lyx")
199 buffer->writeFile(FileName(filename));
201 else if (buffer->isDocBook()) {
202 runparams.nice = !put_in_tempdir;
203 buffer->makeDocBookFile(FileName(filename), runparams);
206 else if (backend_format == format) {
207 runparams.nice = true;
208 if (!buffer->makeLaTeXFile(FileName(filename), string(), runparams))
210 } else if (!lyxrc.tex_allows_spaces
211 && contains(buffer->filePath(), ' ')) {
212 Alert::error(_("File name error"),
213 _("The directory path to the document cannot contain spaces."));
216 runparams.nice = false;
217 if (!buffer->makeLaTeXFile(FileName(filename), buffer->filePath(), runparams))
221 string const error_type = (format == "program")? "Build" : bufferFormat(*buffer);
222 string const ext = formats.extension(format);
223 FileName const tmp_result_file(changeExtension(filename, ext));
224 bool const success = theConverters().convert(buffer, FileName(filename),
225 tmp_result_file, FileName(buffer->fileName()), backend_format, format,
226 buffer->errorList(error_type));
227 // Emit the signal to show the error list.
228 buffer->errors(error_type);
233 result_file = tmp_result_file.absFilename();
235 result_file = changeExtension(buffer->fileName(), ext);
236 // We need to copy referenced files (e. g. included graphics
237 // if format == "dvi") to the result dir.
238 vector<ExportedFile> const files =
239 runparams.exportdata->externalFiles(format);
240 string const dest = onlyPath(result_file);
241 CopyStatus status = SUCCESS;
242 for (vector<ExportedFile>::const_iterator it = files.begin();
243 it != files.end() && status != CANCEL; ++it) {
245 formats.getFormatFromFile(it->sourceName);
246 status = copyFile(fmt, it->sourceName,
247 makeAbsPath(it->exportName, dest),
248 it->exportName, status == FORCE);
250 if (status == CANCEL) {
251 buffer->message(_("Document export cancelled."));
252 } else if (fs::exists(tmp_result_file.toFilesystemEncoding())) {
253 // Finally copy the main file
254 status = copyFile(format, tmp_result_file,
255 FileName(result_file), result_file,
257 buffer->message(bformat(_("Document exported as %1$s "
259 formats.prettyName(format),
260 makeDisplayPath(result_file)));
262 // This must be a dummy converter like fax (bug 1888)
263 buffer->message(bformat(_("Document exported as %1$s"),
264 formats.prettyName(format)));
272 bool Exporter::Export(Buffer * buffer, string const & format,
276 return Export(buffer, format, put_in_tempdir, result_file);
280 bool Exporter::preview(Buffer * buffer, string const & format)
283 if (!Export(buffer, format, true, result_file))
285 return formats.view(*buffer, FileName(result_file), format);
289 bool Exporter::isExportable(Buffer const & buffer, string const & format)
291 vector<string> backends = Backends(buffer);
292 for (vector<string>::const_iterator it = backends.begin();
293 it != backends.end(); ++it)
294 if (theConverters().isReachable(*it, format))
300 vector<Format const *> const
301 Exporter::getExportableFormats(Buffer const & buffer, bool only_viewable)
303 vector<string> backends = Backends(buffer);
304 vector<Format const *> result =
305 theConverters().getReachable(backends[0], only_viewable, true);
306 for (vector<string>::const_iterator it = backends.begin() + 1;
307 it != backends.end(); ++it) {
308 vector<Format const *> r =
309 theConverters().getReachable(*it, only_viewable, false);
310 result.insert(result.end(), r.begin(), r.end());
316 ExportedFile::ExportedFile(FileName const & s, string const & e) :
317 sourceName(s), exportName(e) {}
320 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
322 return f1.sourceName == f2.sourceName &&
323 f1.exportName == f2.exportName;
328 void ExportData::addExternalFile(string const & format,
329 FileName const & sourceName,
330 string const & exportName)
332 // Make sure that we have every file only once, otherwise copyFile()
333 // would ask several times if it should overwrite a file.
334 vector<ExportedFile> & files = externalfiles[format];
335 ExportedFile file(sourceName, exportName);
336 if (find(files.begin(), files.end(), file) == files.end())
337 files.push_back(file);
341 void ExportData::addExternalFile(string const & format,
342 FileName const & sourceName)
344 addExternalFile(format, sourceName, onlyFilename(sourceName.absFilename()));
348 vector<ExportedFile> const
349 ExportData::externalFiles(string const & format) const
351 FileMap::const_iterator cit = externalfiles.find(format);
352 if (cit != externalfiles.end())
354 return vector<ExportedFile>();