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