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