]> git.lyx.org Git - lyx.git/blob - src/exporter.C
Rename ascii to plaintext and LatexRunParams to OutputParams.
[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/filetools.h"
33
34 using lyx::support::AddName;
35 using lyx::support::bformat;
36 using lyx::support::ChangeExtension;
37 using lyx::support::contains;
38 using lyx::support::MakeDisplayPath;
39
40 using std::find;
41 using std::string;
42 using std::vector;
43
44
45 namespace {
46
47 vector<string> const Backends(Buffer const & buffer)
48 {
49         vector<string> v;
50         if (buffer.params().getLyXTextClass().isTeXClassAvailable())
51                 v.push_back(BufferFormat(buffer));
52         v.push_back("text");
53         return v;
54 }
55
56 } //namespace anon
57
58
59 bool Exporter::Export(Buffer * buffer, string const & format,
60                       bool put_in_tempdir, string & result_file)
61 {
62         string backend_format;
63         OutputParams runparams;
64         runparams.flavor = OutputParams::LATEX;
65         runparams.linelen = lyxrc.ascii_linelen;
66         vector<string> backends = Backends(*buffer);
67         if (find(backends.begin(), backends.end(), format) == backends.end()) {
68                 for (vector<string>::const_iterator it = backends.begin();
69                      it != backends.end(); ++it) {
70                         Graph::EdgePath p =
71                                 converters.getPath(*it, format);
72                         if (!p.empty()) {
73                                 if (converters.usePdflatex(p))
74                                         runparams.flavor = OutputParams::PDFLATEX;
75                                 backend_format = *it;
76                                 break;
77                         }
78                 }
79                 if (backend_format.empty()) {
80                         Alert::error(_("Couldn't export file"),
81                                 bformat(_("No information for exporting the format %1$s."),
82                                    formats.prettyName(format)));
83                         return false;
84                 }
85         } else
86                 backend_format = format;
87
88         string filename = buffer->getLatexName(false);
89         if (!buffer->temppath().empty())
90                 filename = AddName(buffer->temppath(), filename);
91         filename = ChangeExtension(filename,
92                                    formats.extension(backend_format));
93
94         // Ascii backend
95         if (backend_format == "text")
96                 writeFileAscii(*buffer, filename, runparams);
97         // Linuxdoc backend
98         else if (buffer->isLinuxDoc()) {
99                 runparams.nice = !put_in_tempdir;
100                 buffer->makeLinuxDocFile(filename, runparams);
101         }
102         // Docbook backend
103         else if (buffer->isDocBook()) {
104                 runparams.nice = !put_in_tempdir;
105                 buffer->makeDocBookFile(filename, runparams);
106         }
107         // LaTeX backend
108         else if (backend_format == format) {
109                 runparams.nice = true;
110                 buffer->makeLaTeXFile(filename, string(), runparams);
111         } else if (contains(buffer->filePath(), ' ')) {
112                 Alert::error(_("File name error"),
113                            _("The directory path to the document cannot contain spaces."));
114                 return false;
115         } else {
116                 runparams.nice = false;
117                 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
118         }
119
120         string outfile_base = (put_in_tempdir)
121                 ? filename : buffer->getLatexName(false);
122
123         if (!converters.convert(buffer, filename, outfile_base,
124                                 backend_format, format, result_file))
125                 return false;
126
127         if (!put_in_tempdir)
128                 buffer->message(_("Document exported as ")
129                                       + formats.prettyName(format)
130                                       + _(" to file `")
131                                       + MakeDisplayPath(result_file) +'\'');
132         return true;
133 }
134
135
136 bool Exporter::Export(Buffer * buffer, string const & format,
137                       bool put_in_tempdir)
138 {
139         string result_file;
140         return Export(buffer, format, put_in_tempdir, result_file);
141 }
142
143
144 bool Exporter::Preview(Buffer * buffer, string const & format)
145 {
146         string result_file;
147         if (!Export(buffer, format, true, result_file))
148                 return false;
149         return formats.view(*buffer, result_file, format);
150 }
151
152
153 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
154 {
155         vector<string> backends = Backends(buffer);
156         for (vector<string>::const_iterator it = backends.begin();
157              it != backends.end(); ++it)
158                 if (converters.isReachable(*it, format))
159                         return true;
160         return false;
161 }
162
163
164 vector<Format const *> const
165 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
166 {
167         vector<string> backends = Backends(buffer);
168         vector<Format const *> result =
169                 converters.getReachable(backends[0], only_viewable, true);
170         for (vector<string>::const_iterator it = backends.begin() + 1;
171              it != backends.end(); ++it) {
172                 vector<Format const *>  r =
173                         converters.getReachable(*it, only_viewable, false);
174                 result.insert(result.end(), r.begin(), r.end());
175         }
176         return result;
177 }