]> git.lyx.org Git - lyx.git/blob - src/exporter.C
a8957f5eb0c5691049d5cb43fe27a882dabbcba6
[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  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include <algorithm>
14
15 #include "exporter.h"
16 #include "buffer.h"
17 #include "lyx_cb.h" //ShowMessage()
18 #include "support/filetools.h"
19 #include "support/BoostFormat.h"
20 #include "lyxrc.h"
21 #include "converter.h"
22 #include "format.h"
23 #include "frontends/Alert.h"
24 #include "gettext.h"
25 #include "BufferView.h"
26
27 using std::vector;
28 using std::find;
29
30 bool Exporter::Export(Buffer * buffer, string const & format,
31                       bool put_in_tempdir, string & result_file)
32 {
33         // There are so many different places that this function can be called
34         // from that the removal of auto insets is best done here.  This ensures
35         // we always have a clean buffer for inserting errors found during export.
36         BufferView * bv = buffer->getUser();
37         if (bv) {
38                 // Remove all error insets
39                 if (bv->removeAutoInsets()) {
40 #warning repaint() or update() or nothing ?
41                         bv->repaint();
42                         bv->fitCursor();
43                 }
44         }
45
46         string backend_format;
47         vector<string> backends = Backends(buffer);
48         if (find(backends.begin(), backends.end(), format) == backends.end()) {
49                 for (vector<string>::const_iterator it = backends.begin();
50                      it != backends.end(); ++it) {
51                         Graph::EdgePath p =
52                                 converters.getPath(*it, format);
53                         if (!p.empty()) {
54                                 lyxrc.pdf_mode = converters.usePdflatex(p);
55                                 backend_format = *it;
56                                 break;
57                         }
58                 }
59                 if (backend_format.empty()) {
60 #if USE_BOOST_FORMAT
61 // FIXME: better english ...
62                         Alert::error(_("Couldn't export file"),
63                                      boost::io::str(boost::format(_("No information for exporting the format %1$s."))
64                                    % formats.prettyName(format)));
65 #else
66                         Alert::error(_("Couldn't export file"),
67                                      _("No information for exporting the format ")
68                                      + formats.prettyName(format) + ".");
69 #endif
70                         return false;
71                 }
72         } else
73                 backend_format = format;
74
75         string filename = buffer->getLatexName(false);
76         if (!buffer->tmppath.empty())
77                 filename = AddName(buffer->tmppath, filename);
78         filename = ChangeExtension(filename,
79                                    formats.extension(backend_format));
80
81         // Ascii backend
82         if (backend_format == "text")
83                 buffer->writeFileAscii(filename, lyxrc.ascii_linelen);
84         // Linuxdoc backend
85         else if (buffer->isLinuxDoc())
86                 buffer->makeLinuxDocFile(filename, !put_in_tempdir);
87         // Docbook backend
88         else if (buffer->isDocBook())
89                 buffer->makeDocBookFile(filename, !put_in_tempdir);
90         // LaTeX backend
91         else if (backend_format == format)
92                 buffer->makeLaTeXFile(filename, string(), true);
93         else if (contains(buffer->filePath(), ' ')) {
94                 Alert::error(_("File name error"),
95                            _("The directory path to the document cannot contain spaces."));
96                 return false;
97         } else
98                 buffer->makeLaTeXFile(filename, buffer->filePath(), false);
99
100         string outfile_base = (put_in_tempdir)
101                 ? filename : buffer->getLatexName(false);
102
103         if (!converters.convert(buffer, filename, outfile_base,
104                                 backend_format, format, result_file))
105                 return false;
106
107         if (!put_in_tempdir)
108                 ShowMessage(buffer,
109                             _("Document exported as ")
110                             + formats.prettyName(format)
111                             + _(" to file `")
112                             + MakeDisplayPath(result_file) +'\'');
113         return true;
114 }
115
116
117 bool Exporter::Export(Buffer * buffer, string const & format,
118                       bool put_in_tempdir)
119 {
120         string result_file;
121         return Export(buffer, format, put_in_tempdir, result_file);
122 }
123
124
125 bool Exporter::Preview(Buffer * buffer, string const & format)
126 {
127         string result_file;
128         if (!Export(buffer, format, true, result_file))
129                 return false;
130         return formats.view(buffer, result_file, format);
131 }
132
133
134 bool Exporter::IsExportable(Buffer const * buffer, string const & format)
135 {
136         vector<string> backends = Backends(buffer);
137         for (vector<string>::const_iterator it = backends.begin();
138              it != backends.end(); ++it)
139                 if (converters.isReachable(*it, format))
140                         return true;
141         return false;
142 }
143
144
145 vector<Format const *> const
146 Exporter::GetExportableFormats(Buffer const * buffer, bool only_viewable)
147 {
148         vector<string> backends = Backends(buffer);
149         vector<Format const *> result =
150                 converters.getReachable(backends[0], only_viewable, true);
151         for (vector<string>::const_iterator it = backends.begin() + 1;
152              it != backends.end(); ++it) {
153                 vector<Format const *>  r =
154                         converters.getReachable(*it, only_viewable, false);
155                 result.insert(result.end(), r.begin(), r.end());
156         }
157         return result;
158 }
159
160
161 string const Exporter::BufferFormat(Buffer const * buffer)
162 {
163         if (buffer->isLinuxDoc())
164                 return "linuxdoc";
165         else if (buffer->isDocBook())
166                 return "docbook";
167         else if (buffer->isLiterate())
168                 return "literate";
169         else
170                 return "latex";
171 }
172
173
174 vector<string> const Exporter::Backends(Buffer const * buffer)
175 {
176         vector<string> v;
177         v.push_back(BufferFormat(buffer));
178         v.push_back("text");
179         return v;
180 }