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