]> git.lyx.org Git - lyx.git/blob - src/exporter.C
remove dummy captions; fix text message
[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         return v;
65 }
66
67
68 /// ask the user what to do if a file already exists
69 int checkOverwrite(string const & filename)
70 {
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?"),
76                                      text, 0, 2,
77                                      _("&Over-write"), _("Over-write &all"),
78                                      _("&Cancel export"));
79         }
80         return 0;
81 }
82
83
84 enum CopyStatus {
85         SUCCESS,
86         FORCE,
87         CANCEL
88 };
89
90
91 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
92  *  will be asked before existing files are overwritten.
93  *  \return
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
98  */
99 CopyStatus copyFile(string const & format,
100                     string const & sourceFile, string const & destFile,
101                     string const & latexFile, bool force)
102 {
103         CopyStatus ret = force ? FORCE : SUCCESS;
104
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()))
110                 return ret;
111
112         if (!force) {
113                 switch(checkOverwrite(destFile)) {
114                 case 0:
115                         ret = SUCCESS;
116                         break;
117                 case 1:
118                         ret = FORCE;
119                         break;
120                 default:
121                         return CANCEL;
122                 }
123         }
124
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)));
131
132         return ret;
133 }
134
135 } //namespace anon
136
137
138 bool Exporter::Export(Buffer * buffer, string const & format,
139                       bool put_in_tempdir, string & result_file)
140 {
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) {
149                         Graph::EdgePath p =
150                                 converters.getPath(*it, format);
151                         if (!p.empty()) {
152                                 runparams.flavor = converters.getFlavor(p);
153                                 backend_format = *it;
154                                 break;
155                         }
156                 }
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)));
161                         return false;
162                 }
163         } else
164                 backend_format = format;
165
166         string filename = buffer->getLatexName(false);
167         filename = AddName(buffer->temppath(), filename);
168         filename = ChangeExtension(filename,
169                                    formats.extension(backend_format));
170
171         // Ascii backend
172         if (backend_format == "text")
173                 writeFileAscii(*buffer, filename, runparams);
174         // Linuxdoc backend
175         else if (buffer->isLinuxDoc()) {
176                 runparams.nice = !put_in_tempdir;
177                 buffer->makeLinuxDocFile(filename, runparams);
178         }
179         // Docbook backend
180         else if (buffer->isDocBook()) {
181                 runparams.nice = !put_in_tempdir;
182                 buffer->makeDocBookFile(filename, runparams);
183         }
184         // LaTeX backend
185         else if (backend_format == format) {
186                 runparams.nice = true;
187                 buffer->makeLaTeXFile(filename, string(), runparams);
188         } else if (!lyxrc.tex_allows_spaces
189                    && contains(buffer->filePath(), ' ')) {
190                 Alert::error(_("File name error"),
191                            _("The directory path to the document cannot contain spaces."));
192                 return false;
193         } else {
194                 runparams.nice = false;
195                 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
196         }
197
198         if (!converters.convert(buffer, filename, filename,
199                                 backend_format, format, result_file))
200                 return false;
201
202         if (!put_in_tempdir) {
203                 string const tmp_result_file = result_file;
204                 result_file = ChangeExtension(buffer->fileName(),
205                                               formats.extension(format));
206                 // We need to copy referenced files (e. g. included graphics
207                 // if format == "dvi") to the result dir.
208                 vector<ExportedFile> const files =
209                         runparams.exportdata->externalFiles(format);
210                 string const dest = OnlyPath(result_file);
211                 CopyStatus status = SUCCESS;
212                 for (vector<ExportedFile>::const_iterator it = files.begin();
213                                 it != files.end() && status != CANCEL; ++it) {
214                         string const fmt =
215                                 formats.getFormatFromFile(it->sourceName);
216                         status = copyFile(fmt, it->sourceName,
217                                           MakeAbsPath(it->exportName, dest),
218                                           it->exportName, status == FORCE);
219                 }
220                 if (status == CANCEL) {
221                         buffer->message(_("Document export cancelled."));
222                 } else {
223                         // Finally copy the main file
224                         status = copyFile(format, tmp_result_file,
225                                           result_file, result_file,
226                                           status == FORCE);
227                         buffer->message(bformat(_("Document exported as %1$s "
228                                                   "to file `%2$s'"),
229                                                 formats.prettyName(format),
230                                                 MakeDisplayPath(result_file)));
231                 }
232         }
233
234         return true;
235 }
236
237
238 bool Exporter::Export(Buffer * buffer, string const & format,
239                       bool put_in_tempdir)
240 {
241         string result_file;
242         return Export(buffer, format, put_in_tempdir, result_file);
243 }
244
245
246 bool Exporter::Preview(Buffer * buffer, string const & format)
247 {
248         string result_file;
249         if (!Export(buffer, format, true, result_file))
250                 return false;
251         return formats.view(*buffer, result_file, format);
252 }
253
254
255 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
256 {
257         vector<string> backends = Backends(buffer);
258         for (vector<string>::const_iterator it = backends.begin();
259              it != backends.end(); ++it)
260                 if (converters.isReachable(*it, format))
261                         return true;
262         return false;
263 }
264
265
266 vector<Format const *> const
267 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
268 {
269         vector<string> backends = Backends(buffer);
270         vector<Format const *> result =
271                 converters.getReachable(backends[0], only_viewable, true);
272         for (vector<string>::const_iterator it = backends.begin() + 1;
273              it != backends.end(); ++it) {
274                 vector<Format const *>  r =
275                         converters.getReachable(*it, only_viewable, false);
276                 result.insert(result.end(), r.begin(), r.end());
277         }
278         return result;
279 }
280
281
282 ExportedFile::ExportedFile(string const & s, string const & e) :
283         sourceName(s), exportName(e) {}
284
285
286 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
287 {
288         return f1.sourceName == f2.sourceName &&
289                f1.exportName == f2.exportName;
290
291 }
292
293
294 void ExportData::addExternalFile(string const & format,
295                                  string const & sourceName,
296                                  string const & exportName)
297 {
298         BOOST_ASSERT(lyx::support::AbsolutePath(sourceName));
299
300         // Make sure that we have every file only once, otherwise copyFile()
301         // would ask several times if it should overwrite a file.
302         vector<ExportedFile> & files = externalfiles[format];
303         ExportedFile file(sourceName, exportName);
304         if (find(files.begin(), files.end(), file) == files.end())
305                 files.push_back(file);
306 }
307
308
309 void ExportData::addExternalFile(string const & format,
310                                  string const & sourceName)
311 {
312         addExternalFile(format, sourceName, OnlyFilename(sourceName));
313 }
314
315
316 vector<ExportedFile> const
317 ExportData::externalFiles(string const & format) const
318 {
319         FileMap::const_iterator cit = externalfiles.find(format);
320         if (cit != externalfiles.end())
321                 return cit->second;
322         return vector<ExportedFile>();
323 }