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