]> git.lyx.org Git - lyx.git/blob - src/exporter.C
fix reading the author field.
[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         vector<string> backends = Backends(*buffer);
140         if (find(backends.begin(), backends.end(), format) == backends.end()) {
141                 for (vector<string>::const_iterator it = backends.begin();
142                      it != backends.end(); ++it) {
143                         Graph::EdgePath p =
144                                 converters.getPath(*it, format);
145                         if (!p.empty()) {
146                                 runparams.flavor = converters.getFlavor(p);
147                                 backend_format = *it;
148                                 break;
149                         }
150                 }
151                 if (backend_format.empty()) {
152                         Alert::error(_("Couldn't export file"),
153                                 bformat(_("No information for exporting the format %1$s."),
154                                    formats.prettyName(format)));
155                         return false;
156                 }
157         } else
158                 backend_format = format;
159
160         string filename = buffer->getLatexName(false);
161         filename = AddName(buffer->temppath(), filename);
162         filename = ChangeExtension(filename,
163                                    formats.extension(backend_format));
164
165         // Ascii backend
166         if (backend_format == "text")
167                 writeFileAscii(*buffer, filename, runparams);
168         // Linuxdoc backend
169         else if (buffer->isLinuxDoc()) {
170                 runparams.nice = !put_in_tempdir;
171                 buffer->makeLinuxDocFile(filename, runparams);
172         }
173         // Docbook backend
174         else if (buffer->isDocBook()) {
175                 runparams.nice = !put_in_tempdir;
176                 buffer->makeDocBookFile(filename, runparams);
177         }
178         // LaTeX backend
179         else if (backend_format == format) {
180                 runparams.nice = true;
181                 buffer->makeLaTeXFile(filename, string(), runparams);
182         } else if (contains(buffer->filePath(), ' ')) {
183                 Alert::error(_("File name error"),
184                            _("The directory path to the document cannot contain spaces."));
185                 return false;
186         } else {
187                 runparams.nice = false;
188                 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
189         }
190
191         if (!converters.convert(buffer, filename, filename,
192                                 backend_format, format, result_file))
193                 return false;
194
195         if (!put_in_tempdir) {
196                 string const tmp_result_file = result_file;
197                 result_file = ChangeExtension(buffer->fileName(),
198                                               formats.extension(format));
199                 // We need to copy referenced files (e. g. included graphics
200                 // if format == "dvi") to the result dir.
201                 vector<ExportedFile> const files =
202                         runparams.exportdata->externalFiles(format);
203                 string const dest = OnlyPath(result_file);
204                 CopyStatus status = SUCCESS;
205                 for (vector<ExportedFile>::const_iterator it = files.begin();
206                                 it != files.end() && status != CANCEL; ++it)
207                         status = copyFile(it->sourceName,
208                                           MakeAbsPath(it->exportName, dest),
209                                           status == FORCE);
210                 if (status == CANCEL) {
211                         buffer->message(_("Document export cancelled."));
212                 } else {
213                         // Finally copy the main file
214                         status = copyFile(tmp_result_file, result_file,
215                                           status == FORCE);
216                         buffer->message(bformat(_("Document exported as %1$s"
217                                                   "to file `%2$s'"),
218                                                 formats.prettyName(format),
219                                                 MakeDisplayPath(result_file)));
220                 }
221         }
222
223         return true;
224 }
225
226
227 bool Exporter::Export(Buffer * buffer, string const & format,
228                       bool put_in_tempdir)
229 {
230         string result_file;
231         return Export(buffer, format, put_in_tempdir, result_file);
232 }
233
234
235 bool Exporter::Preview(Buffer * buffer, string const & format)
236 {
237         string result_file;
238         if (!Export(buffer, format, true, result_file))
239                 return false;
240         return formats.view(*buffer, result_file, format);
241 }
242
243
244 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
245 {
246         vector<string> backends = Backends(buffer);
247         for (vector<string>::const_iterator it = backends.begin();
248              it != backends.end(); ++it)
249                 if (converters.isReachable(*it, format))
250                         return true;
251         return false;
252 }
253
254
255 vector<Format const *> const
256 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
257 {
258         vector<string> backends = Backends(buffer);
259         vector<Format const *> result =
260                 converters.getReachable(backends[0], only_viewable, true);
261         for (vector<string>::const_iterator it = backends.begin() + 1;
262              it != backends.end(); ++it) {
263                 vector<Format const *>  r =
264                         converters.getReachable(*it, only_viewable, false);
265                 result.insert(result.end(), r.begin(), r.end());
266         }
267         return result;
268 }
269
270
271 ExportedFile::ExportedFile(string const & s, string const & e) :
272         sourceName(s), exportName(e) {}
273
274
275 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
276 {
277         return f1.sourceName == f2.sourceName &&
278                f1.exportName == f2.exportName;
279
280 }
281
282
283 void ExportData::addExternalFile(string const & format,
284                                  string const & sourceName,
285                                  string const & exportName)
286 {
287         BOOST_ASSERT(lyx::support::AbsolutePath(sourceName));
288
289         // Make sure that we have every file only once, otherwise copyFile()
290         // would ask several times if it should overwrite a file.
291         vector<ExportedFile> & files = externalfiles[format];
292         ExportedFile file(sourceName, exportName);
293         if (find(files.begin(), files.end(), file) == files.end())
294                 files.push_back(file);
295 }
296
297
298 void ExportData::addExternalFile(string const & format,
299                                  string const & sourceName)
300 {
301         addExternalFile(format, sourceName, OnlyFilename(sourceName));
302 }
303
304
305 vector<ExportedFile> const
306 ExportData::externalFiles(string const & format) const
307 {
308         FileMap::const_iterator cit = externalfiles.find(format);
309         if (cit != externalfiles.end())
310                 return cit->second;
311         return vector<ExportedFile>();
312 }