]> git.lyx.org Git - lyx.git/blob - src/exporter.C
f91d98385558635e551022a38ea9cc548f68a1bc
[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 "output_plaintext.h"
29 #include "outputparams.h"
30 #include "frontends/Alert.h"
31
32 #include "support/FileInfo.h"
33 #include "support/filetools.h"
34 #include "support/lyxlib.h"
35 #include "support/os.h"
36
37 using lyx::support::AddName;
38 using lyx::support::bformat;
39 using lyx::support::ChangeExtension;
40 using lyx::support::contains;
41 using lyx::support::MakeAbsPath;
42 using lyx::support::MakeDisplayPath;
43 using lyx::support::OnlyFilename;
44 using lyx::support::OnlyPath;
45 using lyx::support::prefixIs;
46
47 using std::find;
48 using std::string;
49 using std::vector;
50
51
52 namespace {
53
54 vector<string> const Backends(Buffer const & buffer)
55 {
56         vector<string> v;
57         if (buffer.params().getLyXTextClass().isTeXClassAvailable())
58                 v.push_back(BufferFormat(buffer));
59         v.push_back("text");
60         return v;
61 }
62
63
64 /// ask the user what to do if a file already exists
65 int checkOverwrite(string const & filename)
66 {
67         if (lyx::support::FileInfo(filename, true).exist()) {
68                 string text = bformat(_("The file %1$s already exists.\n\n"
69                                         "Do you want to over-write that file?"),
70                                       MakeDisplayPath(filename));
71                 return Alert::prompt(_("Over-write file?"),
72                                      text, 0, 2,
73                                      _("&Over-write"), _("Over-write &all"),
74                                      _("&Cancel export"));
75         }
76         return 0;
77 }
78
79
80 enum CopyStatus {
81         SUCCESS,
82         FORCE,
83         CANCEL
84 };
85
86
87 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
88  *  will be asked before existing files are overwritten.
89  *  \return 
90  *  - SUCCESS if this file got copied
91  *  - FORCE   if subsequent calls should not ask for confirmation before
92  *            overwriting files anymore.
93  *  - CANCEL  if the export should be cancelled
94  */
95 CopyStatus copyFile(string const & sourceFile, string const & destFile,
96                    bool force)
97 {
98         CopyStatus ret = force ? FORCE : SUCCESS;
99
100         // Only copy files that are in our tmp dir, all other files would
101         // overwrite themselves. This check could be changed to
102         // boost::filesystem::equivalent(sourceFile, destFile) if export to
103         // other directories than the document directory is desired.
104         if (!prefixIs(OnlyPath(sourceFile), lyx::support::os::getTmpDir()))
105                 return ret;
106
107         if (!force) {
108                 switch(checkOverwrite(destFile)) {
109                 case 0:
110                         ret = SUCCESS;
111                         break;
112                 case 1:
113                         ret = FORCE;
114                         break;
115                 default:
116                         return CANCEL;
117                 }
118         }
119
120         if (!lyx::support::copy(sourceFile, destFile))
121                 Alert::error(_("Couldn't copy file"),
122                              bformat(_("Copying %1$s to %2$s failed."),
123                                      MakeDisplayPath(sourceFile),
124                                      MakeDisplayPath(destFile)));
125
126         return ret;
127 }
128
129 } //namespace anon
130
131
132 bool Exporter::Export(Buffer * buffer, string const & format,
133                       bool put_in_tempdir, string & result_file)
134 {
135         string backend_format;
136         OutputParams runparams;
137         runparams.flavor = OutputParams::LATEX;
138         runparams.linelen = lyxrc.ascii_linelen;
139         ExportData exportdata;
140         runparams.exportdata = &exportdata;
141         vector<string> backends = Backends(*buffer);
142         if (find(backends.begin(), backends.end(), format) == backends.end()) {
143                 for (vector<string>::const_iterator it = backends.begin();
144                      it != backends.end(); ++it) {
145                         Graph::EdgePath p =
146                                 converters.getPath(*it, format);
147                         if (!p.empty()) {
148                                 runparams.flavor = converters.getFlavor(p);
149                                 backend_format = *it;
150                                 break;
151                         }
152                 }
153                 if (backend_format.empty()) {
154                         Alert::error(_("Couldn't export file"),
155                                 bformat(_("No information for exporting the format %1$s."),
156                                    formats.prettyName(format)));
157                         return false;
158                 }
159         } else
160                 backend_format = format;
161
162         string filename = buffer->getLatexName(false);
163         filename = AddName(buffer->temppath(), filename);
164         filename = ChangeExtension(filename,
165                                    formats.extension(backend_format));
166
167         // Ascii backend
168         if (backend_format == "text")
169                 writeFileAscii(*buffer, filename, runparams);
170         // Linuxdoc backend
171         else if (buffer->isLinuxDoc()) {
172                 runparams.nice = !put_in_tempdir;
173                 buffer->makeLinuxDocFile(filename, runparams);
174         }
175         // Docbook backend
176         else if (buffer->isDocBook()) {
177                 runparams.nice = !put_in_tempdir;
178                 buffer->makeDocBookFile(filename, runparams);
179         }
180         // LaTeX backend
181         else if (backend_format == format) {
182                 runparams.nice = true;
183                 buffer->makeLaTeXFile(filename, string(), runparams);
184         } else if (contains(buffer->filePath(), ' ')) {
185                 Alert::error(_("File name error"),
186                            _("The directory path to the document cannot contain spaces."));
187                 return false;
188         } else {
189                 runparams.nice = false;
190                 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
191         }
192
193         if (!converters.convert(buffer, filename, filename,
194                                 backend_format, format, result_file))
195                 return false;
196
197         if (!put_in_tempdir) {
198                 string const tmp_result_file = result_file;
199                 result_file = ChangeExtension(buffer->fileName(),
200                                               formats.extension(format));
201                 // We need to copy referenced files (e. g. included graphics
202                 // if format == "dvi") to the result dir.
203                 vector<ExportedFile> const files = exportdata.externalFiles(format);
204                 string const dest = OnlyPath(result_file);
205                 CopyStatus status = SUCCESS;
206                 for (vector<ExportedFile>::const_iterator it = files.begin();
207                                 it != files.end() && status != CANCEL; ++it)
208                         status = copyFile(it->sourceName,
209                                           MakeAbsPath(it->exportName, dest),
210                                           status == FORCE);
211                 if (status == CANCEL) {
212                         buffer->message(_("Document export cancelled."));
213                 } else {
214                         // Finally copy the main file
215                         status = copyFile(tmp_result_file, result_file,
216                                           status == FORCE);
217                         buffer->message(bformat(_("Document exported as %1$s"
218                                                   "to file `%2$s'"),
219                                                 formats.prettyName(format),
220                                                 MakeDisplayPath(result_file)));
221                 }
222         }
223
224         return true;
225 }
226
227
228 bool Exporter::Export(Buffer * buffer, string const & format,
229                       bool put_in_tempdir)
230 {
231         string result_file;
232         return Export(buffer, format, put_in_tempdir, result_file);
233 }
234
235
236 bool Exporter::Preview(Buffer * buffer, string const & format)
237 {
238         string result_file;
239         if (!Export(buffer, format, true, result_file))
240                 return false;
241         return formats.view(*buffer, result_file, format);
242 }
243
244
245 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
246 {
247         vector<string> backends = Backends(buffer);
248         for (vector<string>::const_iterator it = backends.begin();
249              it != backends.end(); ++it)
250                 if (converters.isReachable(*it, format))
251                         return true;
252         return false;
253 }
254
255
256 vector<Format const *> const
257 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
258 {
259         vector<string> backends = Backends(buffer);
260         vector<Format const *> result =
261                 converters.getReachable(backends[0], only_viewable, true);
262         for (vector<string>::const_iterator it = backends.begin() + 1;
263              it != backends.end(); ++it) {
264                 vector<Format const *>  r =
265                         converters.getReachable(*it, only_viewable, false);
266                 result.insert(result.end(), r.begin(), r.end());
267         }
268         return result;
269 }
270
271
272 ExportedFile::ExportedFile(string const & s, string const & e) :
273         sourceName(s), exportName(e) {}
274
275
276 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
277 {
278         return f1.sourceName == f2.sourceName &&
279                f1.exportName == f2.exportName;
280
281 }
282
283
284 void ExportData::addExternalFile(string const & format,
285                                  string const & sourceName,
286                                  string const & exportName)
287 {
288         BOOST_ASSERT(lyx::support::AbsolutePath(sourceName));
289
290         // Make sure that we have every file only once, otherwise copyFile()
291         // would ask several times if it should overwrite a file.
292         vector<ExportedFile> & files = externalfiles[format];
293         ExportedFile file(sourceName, exportName);
294         if (find(files.begin(), files.end(), file) == files.end())
295                 files.push_back(file);
296 }
297
298
299 void ExportData::addExternalFile(string const & format,
300                                  string const & sourceName)
301 {
302         addExternalFile(format, sourceName, OnlyFilename(sourceName));
303 }
304
305
306 vector<ExportedFile> const
307 ExportData::externalFiles(string const & format) const
308 {
309         FileMap::const_iterator cit = externalfiles.find(format);
310         if (cit != externalfiles.end())
311                 return cit->second;
312         return vector<ExportedFile>();
313 }