]> git.lyx.org Git - lyx.git/blob - src/exporter.C
a58c23e6a1cf4369f9fb64d9a6fb59998ae05ebe
[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                                 runparams.flavor = converters.getFlavor(p);
74                                 backend_format = *it;
75                                 break;
76                         }
77                 }
78                 if (backend_format.empty()) {
79                         Alert::error(_("Couldn't export file"),
80                                 bformat(_("No information for exporting the format %1$s."),
81                                    formats.prettyName(format)));
82                         return false;
83                 }
84         } else
85                 backend_format = format;
86
87         string filename = buffer->getLatexName(false);
88         if (!buffer->temppath().empty())
89                 filename = AddName(buffer->temppath(), filename);
90         filename = ChangeExtension(filename,
91                                    formats.extension(backend_format));
92
93         // Ascii backend
94         if (backend_format == "text")
95                 writeFileAscii(*buffer, filename, runparams);
96         // Linuxdoc backend
97         else if (buffer->isLinuxDoc()) {
98                 runparams.nice = !put_in_tempdir;
99                 buffer->makeLinuxDocFile(filename, runparams);
100         }
101         // Docbook backend
102         else if (buffer->isDocBook()) {
103                 runparams.nice = !put_in_tempdir;
104                 buffer->makeDocBookFile(filename, runparams);
105         }
106         // LaTeX backend
107         else if (backend_format == format) {
108                 runparams.nice = true;
109                 buffer->makeLaTeXFile(filename, string(), runparams);
110         } else if (contains(buffer->filePath(), ' ')) {
111                 Alert::error(_("File name error"),
112                            _("The directory path to the document cannot contain spaces."));
113                 return false;
114         } else {
115                 runparams.nice = false;
116                 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
117         }
118
119         string outfile_base = (put_in_tempdir)
120                 ? filename : buffer->getLatexName(false);
121
122         if (!converters.convert(buffer, filename, outfile_base,
123                                 backend_format, format, result_file))
124                 return false;
125
126         if (!put_in_tempdir)
127                 buffer->message(_("Document exported as ")
128                                       + formats.prettyName(format)
129                                       + _(" to file `")
130                                       + MakeDisplayPath(result_file) +'\'');
131         return true;
132 }
133
134
135 bool Exporter::Export(Buffer * buffer, string const & format,
136                       bool put_in_tempdir)
137 {
138         string result_file;
139         return Export(buffer, format, put_in_tempdir, result_file);
140 }
141
142
143 bool Exporter::Preview(Buffer * buffer, string const & format)
144 {
145         string result_file;
146         if (!Export(buffer, format, true, result_file))
147                 return false;
148         return formats.view(*buffer, result_file, format);
149 }
150
151
152 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
153 {
154         vector<string> backends = Backends(buffer);
155         for (vector<string>::const_iterator it = backends.begin();
156              it != backends.end(); ++it)
157                 if (converters.isReachable(*it, format))
158                         return true;
159         return false;
160 }
161
162
163 vector<Format const *> const
164 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
165 {
166         vector<string> backends = Backends(buffer);
167         vector<Format const *> result =
168                 converters.getReachable(backends[0], only_viewable, true);
169         for (vector<string>::const_iterator it = backends.begin() + 1;
170              it != backends.end(); ++it) {
171                 vector<Format const *>  r =
172                         converters.getReachable(*it, only_viewable, false);
173                 result.insert(result.end(), r.begin(), r.end());
174         }
175         return result;
176 }