]> git.lyx.org Git - lyx.git/blob - src/exporter.C
Fix bug 2195: Slowness in rendering inside insets, especially on the Mac
[lyx.git] / src / exporter.C
1 /**
2  * \file exporter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author Alfredo Braunstein
8  * \author Lars Gullik Bjønnes
9  * \author Jean Marc Lasgouttes
10  * \author Angus Leeming
11  * \author John Levon
12  * \author André Pönitz
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #include <config.h>
18
19 #include "exporter.h"
20
21 #include "buffer.h"
22 #include "buffer_funcs.h"
23 #include "bufferparams.h"
24 #include "converter.h"
25 #include "format.h"
26 #include "gettext.h"
27 #include "lyxrc.h"
28 #include "mover.h"
29 #include "output_plaintext.h"
30 #include "outputparams.h"
31 #include "frontends/Alert.h"
32
33 #include "support/filetools.h"
34 #include "support/lyxlib.h"
35 #include "support/package.h"
36
37 #include <boost/filesystem/operations.hpp>
38
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;
49
50 using std::find;
51 using std::string;
52 using std::vector;
53
54 namespace fs = boost::filesystem;
55
56 namespace {
57
58 vector<string> const Backends(Buffer const & buffer)
59 {
60         vector<string> v;
61         if (buffer.params().getLyXTextClass().isTeXClassAvailable())
62                 v.push_back(BufferFormat(buffer));
63         v.push_back("text");
64         v.push_back("lyx");
65         return v;
66 }
67
68
69 /// ask the user what to do if a file already exists
70 int checkOverwrite(string const & filename)
71 {
72         if (fs::exists(filename)) {
73                 string text = bformat(_("The file %1$s already exists.\n\n"
74                                         "Do you want to over-write that file?"),
75                                       MakeDisplayPath(filename));
76                 return Alert::prompt(_("Over-write file?"),
77                                      text, 0, 2,
78                                      _("&Over-write"), _("Over-write &all"),
79                                      _("&Cancel export"));
80         }
81         return 0;
82 }
83
84
85 enum CopyStatus {
86         SUCCESS,
87         FORCE,
88         CANCEL
89 };
90
91
92 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
93  *  will be asked before existing files are overwritten.
94  *  \return
95  *  - SUCCESS if this file got copied
96  *  - FORCE   if subsequent calls should not ask for confirmation before
97  *            overwriting files anymore.
98  *  - CANCEL  if the export should be cancelled
99  */
100 CopyStatus copyFile(string const & format,
101                     string const & sourceFile, string const & destFile,
102                     string const & latexFile, bool force)
103 {
104         CopyStatus ret = force ? FORCE : SUCCESS;
105
106         // Only copy files that are in our tmp dir, all other files would
107         // overwrite themselves. This check could be changed to
108         // boost::filesystem::equivalent(sourceFile, destFile) if export to
109         // other directories than the document directory is desired.
110         if (!prefixIs(OnlyPath(sourceFile), package().temp_dir()))
111                 return ret;
112
113         if (!force) {
114                 switch(checkOverwrite(destFile)) {
115                 case 0:
116                         ret = SUCCESS;
117                         break;
118                 case 1:
119                         ret = FORCE;
120                         break;
121                 default:
122                         return CANCEL;
123                 }
124         }
125
126         Mover const & mover = movers(format);
127         if (!mover.copy(sourceFile, destFile, latexFile))
128                 Alert::error(_("Couldn't copy file"),
129                              bformat(_("Copying %1$s to %2$s failed."),
130                                      MakeDisplayPath(sourceFile),
131                                      MakeDisplayPath(destFile)));
132
133         return ret;
134 }
135
136 } //namespace anon
137
138
139 bool Exporter::Export(Buffer * buffer, string const & format,
140                       bool put_in_tempdir, string & result_file)
141 {
142         string backend_format;
143         OutputParams runparams;
144         runparams.flavor = OutputParams::LATEX;
145         runparams.linelen = lyxrc.ascii_linelen;
146         vector<string> backends = Backends(*buffer);
147         // FIXME: Without this test export to lyx13 would be through
148         // latex -> lyx -> lyx13, because the first backend below with a
149         // working conversion path is used. We should replace this test and
150         // the explicit loop below with a method
151         // getShortestPath(vector<string> const & from, string const & to)
152         // which returns the shortest path from one of the formats in 'from'
153         // to 'to'.
154         if (format == "lyx13x" && !converters.getPath("lyx", format).empty())
155                 backend_format = "lyx";
156         else if (find(backends.begin(), backends.end(), format) == backends.end()) {
157                 for (vector<string>::const_iterator it = backends.begin();
158                      it != backends.end(); ++it) {
159                         Graph::EdgePath p = converters.getPath(*it, format);
160                         if (!p.empty()) {
161                                 runparams.flavor = converters.getFlavor(p);
162                                 backend_format = *it;
163                                 break;
164                         }
165                 }
166                 if (backend_format.empty()) {
167                         Alert::error(_("Couldn't export file"),
168                                 bformat(_("No information for exporting the format %1$s."),
169                                    formats.prettyName(format)));
170                         return false;
171                 }
172         } else
173                 backend_format = format;
174
175         string filename = buffer->getLatexName(false);
176         filename = AddName(buffer->temppath(), filename);
177         filename = ChangeExtension(filename,
178                                    formats.extension(backend_format));
179
180         // Ascii backend
181         if (backend_format == "text")
182                 writeFileAscii(*buffer, filename, runparams);
183         // no backend
184         else if (backend_format == "lyx")
185                 buffer->writeFile(filename);
186         // Linuxdoc backend
187         else if (buffer->isLinuxDoc()) {
188                 runparams.nice = !put_in_tempdir;
189                 buffer->makeLinuxDocFile(filename, runparams);
190         }
191         // Docbook backend
192         else if (buffer->isDocBook()) {
193                 runparams.nice = !put_in_tempdir;
194                 buffer->makeDocBookFile(filename, runparams);
195         }
196         // LaTeX backend
197         else if (backend_format == format) {
198                 runparams.nice = true;
199                 buffer->makeLaTeXFile(filename, string(), runparams);
200         } else if (!lyxrc.tex_allows_spaces
201                    && contains(buffer->filePath(), ' ')) {
202                 Alert::error(_("File name error"),
203                            _("The directory path to the document cannot contain spaces."));
204                 return false;
205         } else {
206                 runparams.nice = false;
207                 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
208         }
209
210         if (!converters.convert(buffer, filename, filename,
211                                 backend_format, format, result_file))
212                 return false;
213
214         if (!put_in_tempdir) {
215                 string const tmp_result_file = result_file;
216                 result_file = ChangeExtension(buffer->fileName(),
217                                               formats.extension(format));
218                 // We need to copy referenced files (e. g. included graphics
219                 // if format == "dvi") to the result dir.
220                 vector<ExportedFile> const files =
221                         runparams.exportdata->externalFiles(format);
222                 string const dest = OnlyPath(result_file);
223                 CopyStatus status = SUCCESS;
224                 for (vector<ExportedFile>::const_iterator it = files.begin();
225                                 it != files.end() && status != CANCEL; ++it) {
226                         string const fmt =
227                                 formats.getFormatFromFile(it->sourceName);
228                         status = copyFile(fmt, it->sourceName,
229                                           MakeAbsPath(it->exportName, dest),
230                                           it->exportName, status == FORCE);
231                 }
232                 if (status == CANCEL) {
233                         buffer->message(_("Document export cancelled."));
234                 } else if (fs::exists(tmp_result_file)) {
235                         // Finally copy the main file
236                         status = copyFile(format, tmp_result_file,
237                                           result_file, result_file,
238                                           status == FORCE);
239                         buffer->message(bformat(_("Document exported as %1$s "
240                                                   "to file `%2$s'"),
241                                                 formats.prettyName(format),
242                                                 MakeDisplayPath(result_file)));
243                 } else {
244                         // This must be a dummy converter like fax (bug 1888)
245                         buffer->message(bformat(_("Document exported as %1$s"),
246                                                 formats.prettyName(format)));
247                 }
248         }
249
250         return true;
251 }
252
253
254 bool Exporter::Export(Buffer * buffer, string const & format,
255                       bool put_in_tempdir)
256 {
257         string result_file;
258         return Export(buffer, format, put_in_tempdir, result_file);
259 }
260
261
262 bool Exporter::Preview(Buffer * buffer, string const & format)
263 {
264         string result_file;
265         if (!Export(buffer, format, true, result_file))
266                 return false;
267         return formats.view(*buffer, result_file, format);
268 }
269
270
271 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
272 {
273         vector<string> backends = Backends(buffer);
274         for (vector<string>::const_iterator it = backends.begin();
275              it != backends.end(); ++it)
276                 if (converters.isReachable(*it, format))
277                         return true;
278         return false;
279 }
280
281
282 vector<Format const *> const
283 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
284 {
285         vector<string> backends = Backends(buffer);
286         vector<Format const *> result =
287                 converters.getReachable(backends[0], only_viewable, true);
288         for (vector<string>::const_iterator it = backends.begin() + 1;
289              it != backends.end(); ++it) {
290                 vector<Format const *>  r =
291                         converters.getReachable(*it, only_viewable, false);
292                 result.insert(result.end(), r.begin(), r.end());
293         }
294         return result;
295 }
296
297
298 ExportedFile::ExportedFile(string const & s, string const & e) :
299         sourceName(s), exportName(e) {}
300
301
302 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
303 {
304         return f1.sourceName == f2.sourceName &&
305                f1.exportName == f2.exportName;
306
307 }
308
309
310 void ExportData::addExternalFile(string const & format,
311                                  string const & sourceName,
312                                  string const & exportName)
313 {
314         BOOST_ASSERT(lyx::support::AbsolutePath(sourceName));
315
316         // Make sure that we have every file only once, otherwise copyFile()
317         // would ask several times if it should overwrite a file.
318         vector<ExportedFile> & files = externalfiles[format];
319         ExportedFile file(sourceName, exportName);
320         if (find(files.begin(), files.end(), file) == files.end())
321                 files.push_back(file);
322 }
323
324
325 void ExportData::addExternalFile(string const & format,
326                                  string const & sourceName)
327 {
328         addExternalFile(format, sourceName, OnlyFilename(sourceName));
329 }
330
331
332 vector<ExportedFile> const
333 ExportData::externalFiles(string const & format) const
334 {
335         FileMap::const_iterator cit = externalfiles.find(format);
336         if (cit != externalfiles.end())
337                 return cit->second;
338         return vector<ExportedFile>();
339 }