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