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