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