]> git.lyx.org Git - lyx.git/blob - src/exporter.C
ws changes only
[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 "latexrunparams.h"
28 #include "lyxrc.h"
29
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         LatexRunParams runparams;
64         runparams.flavor = LatexRunParams::LATEX;
65         vector<string> backends = Backends(*buffer);
66         if (find(backends.begin(), backends.end(), format) == backends.end()) {
67                 for (vector<string>::const_iterator it = backends.begin();
68                      it != backends.end(); ++it) {
69                         Graph::EdgePath p =
70                                 converters.getPath(*it, format);
71                         if (!p.empty()) {
72                                 if (converters.usePdflatex(p))
73                                         runparams.flavor = LatexRunParams::PDFLATEX;
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                 buffer->writeFileAscii(filename, lyxrc.ascii_linelen);
96         // Linuxdoc backend
97         else if (buffer->isLinuxDoc())
98                 buffer->makeLinuxDocFile(filename, !put_in_tempdir);
99         // Docbook backend
100         else if (buffer->isDocBook())
101                 buffer->makeDocBookFile(filename, !put_in_tempdir);
102         // LaTeX backend
103         else if (backend_format == format) {
104                 runparams.nice = true;
105                 buffer->makeLaTeXFile(filename, string(), runparams);
106         } else if (contains(buffer->filePath(), ' ')) {
107                 Alert::error(_("File name error"),
108                            _("The directory path to the document cannot contain spaces."));
109                 return false;
110         } else {
111                 runparams.nice = false;
112                 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
113         }
114
115         string outfile_base = (put_in_tempdir)
116                 ? filename : buffer->getLatexName(false);
117
118         if (!converters.convert(buffer, filename, outfile_base,
119                                 backend_format, format, result_file))
120                 return false;
121
122         if (!put_in_tempdir)
123                 buffer->message(_("Document exported as ")
124                                       + formats.prettyName(format)
125                                       + _(" to file `")
126                                       + MakeDisplayPath(result_file) +'\'');
127         return true;
128 }
129
130
131 bool Exporter::Export(Buffer * buffer, string const & format,
132                       bool put_in_tempdir)
133 {
134         string result_file;
135         return Export(buffer, format, put_in_tempdir, result_file);
136 }
137
138
139 bool Exporter::Preview(Buffer * buffer, string const & format)
140 {
141         string result_file;
142         if (!Export(buffer, format, true, result_file))
143                 return false;
144         return formats.view(*buffer, result_file, format);
145 }
146
147
148 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
149 {
150         vector<string> backends = Backends(buffer);
151         for (vector<string>::const_iterator it = backends.begin();
152              it != backends.end(); ++it)
153                 if (converters.isReachable(*it, format))
154                         return true;
155         return false;
156 }
157
158
159 vector<Format const *> const
160 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
161 {
162         vector<string> backends = Backends(buffer);
163         vector<Format const *> result =
164                 converters.getReachable(backends[0], only_viewable, true);
165         for (vector<string>::const_iterator it = backends.begin() + 1;
166              it != backends.end(); ++it) {
167                 vector<Format const *>  r =
168                         converters.getReachable(*it, only_viewable, false);
169                 result.insert(result.end(), r.begin(), r.end());
170         }
171         return result;
172 }