]> git.lyx.org Git - lyx.git/blob - src/exporter.C
add missing 'else'
[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 #include <algorithm>
14
15 #include "exporter.h"
16 #include "buffer.h"
17 #include "lyx_cb.h" //ShowMessage()
18 #include "support/filetools.h"
19 #include "lyxrc.h"
20 #include "converter.h"
21 #include "format.h"
22 #include "frontends/Alert.h"
23 #include "gettext.h"
24 #include "BufferView.h"
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         if (bv) {
37                 // Remove all error insets
38                 if (bv->removeAutoInsets()) {
39 #warning repaint() or update() or nothing ?
40                         bv->repaint();
41                         bv->fitCursor();
42                 }
43         }
44
45         string backend_format;
46         vector<string> backends = Backends(buffer);
47         if (find(backends.begin(), backends.end(), format) == backends.end()) {
48                 for (vector<string>::const_iterator it = backends.begin();
49                      it != backends.end(); ++it) {
50                         Graph::EdgePath p =
51                                 converters.getPath(*it, format);
52                         if (!p.empty()) {
53                                 lyxrc.pdf_mode = converters.usePdflatex(p);
54                                 backend_format = *it;
55                                 break;
56                         }
57                 }
58                 if (backend_format.empty()) {
59                         Alert::alert(_("Cannot export file"),
60                                    _("No information for exporting to ")
61                                    + formats.prettyName(format));
62                         return false;
63                 }
64         } else
65                 backend_format = format;
66
67         string filename = buffer->getLatexName(false);
68         if (!buffer->tmppath.empty())
69                 filename = AddName(buffer->tmppath, filename);
70         filename = ChangeExtension(filename,
71                                    formats.extension(backend_format));
72
73         // Ascii backend
74         if (backend_format == "text")
75                 buffer->writeFileAscii(filename, lyxrc.ascii_linelen);
76         // Linuxdoc backend
77         else if (buffer->isLinuxDoc())
78                 buffer->makeLinuxDocFile(filename, !put_in_tempdir);
79         // Docbook backend
80         else if (buffer->isDocBook())
81                 buffer->makeDocBookFile(filename, !put_in_tempdir);
82         // LaTeX backend
83         else if (backend_format == format)
84                 buffer->makeLaTeXFile(filename, string(), true);
85         else if (contains(buffer->filePath(), ' ')) {
86                 Alert::alert(_("Cannot run LaTeX."),
87                            _("The path to the lyx file cannot contain spaces."));
88                 return false;
89         } else
90                 buffer->makeLaTeXFile(filename, buffer->filePath(), false);
91
92         string outfile_base = (put_in_tempdir)
93                 ? filename : buffer->getLatexName(false);
94
95         if (!converters.convert(buffer, filename, outfile_base,
96                                 backend_format, format, result_file))
97                 return false;
98
99         if (!put_in_tempdir)
100                 ShowMessage(buffer,
101                             _("Document exported as ")
102                             + formats.prettyName(format)
103                             + _(" to file `")
104                             + MakeDisplayPath(result_file) +'\'');
105         return true;
106 }
107
108
109 bool Exporter::Export(Buffer * buffer, string const & format,
110                       bool put_in_tempdir)
111 {
112         string result_file;
113         return Export(buffer, format, put_in_tempdir, result_file);
114 }
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
166 vector<string> const Exporter::Backends(Buffer const * buffer)
167 {
168         vector<string> v;
169         v.push_back(BufferFormat(buffer));
170         v.push_back("text");
171         return v;
172 }