]> git.lyx.org Git - lyx.git/blob - src/exporter.C
Make it compile when USE_BOOST_FORMAT is unset
[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 #warning repaint() or update() or nothing ?
43                         bv->repaint();
44                         bv->fitCursor();
45                 }
46         }
47
48         string backend_format;
49         vector<string> backends = Backends(buffer);
50         if (find(backends.begin(), backends.end(), format) == backends.end()) {
51                 for (vector<string>::const_iterator it = backends.begin();
52                      it != backends.end(); ++it) {
53                         Converters::EdgePath p =
54                                 converters.getPath(*it, format);
55                         if (!p.empty()) {
56                                 lyxrc.pdf_mode = converters.usePdflatex(p);
57                                 backend_format = *it;
58                                 break;
59                         }
60                 }
61                 if (backend_format.empty()) {
62                         Alert::alert(_("Cannot export file"),
63                                    _("No information for exporting to ")
64                                    + formats.prettyName(format));
65                         return false;
66                 }
67         } else
68                 backend_format = format;
69
70         string filename = buffer->getLatexName(false);
71         if (!buffer->tmppath.empty())
72                 filename = AddName(buffer->tmppath, filename);
73         filename = ChangeExtension(filename,
74                                    formats.extension(backend_format));
75
76         // Ascii backend
77         if (backend_format == "text")
78                 buffer->writeFileAscii(filename, lyxrc.ascii_linelen);
79         // Linuxdoc backend
80         else if (buffer->isLinuxDoc())
81                 buffer->makeLinuxDocFile(filename, !put_in_tempdir);
82         // Docbook backend
83         else if (buffer->isDocBook())
84                 buffer->makeDocBookFile(filename, !put_in_tempdir);
85         // LaTeX backend
86         else if (backend_format == format)
87                 buffer->makeLaTeXFile(filename, string(), true);
88         else if (contains(buffer->filePath(), ' ')) {
89                 Alert::alert(_("Cannot run latex."),
90                            _("The path to the lyx file cannot contain spaces."));
91                 return false;
92         } else
93                 buffer->makeLaTeXFile(filename, buffer->filePath(), false);
94
95         string outfile_base = (put_in_tempdir)
96                 ? filename : buffer->getLatexName(false);
97
98         if (!converters.convert(buffer, filename, outfile_base,
99                                 backend_format, format, result_file))
100                 return false;
101
102         if (!put_in_tempdir)
103                 ShowMessage(buffer,
104                             _("Document exported as ")
105                             + formats.prettyName(format)
106                             + _(" to file `")
107                             + MakeDisplayPath(result_file) +'\'');
108         return true;
109 }
110
111
112 bool Exporter::Export(Buffer * buffer, string const & format,
113                       bool put_in_tempdir)
114 {
115         string result_file;
116         return Export(buffer, format, put_in_tempdir, result_file);
117 }
118
119
120 bool Exporter::Preview(Buffer * buffer, string const & format)
121 {
122         string result_file;
123         if (!Export(buffer, format, true, result_file))
124                 return false;
125         return formats.view(buffer, result_file, format);
126 }
127
128
129 bool Exporter::IsExportable(Buffer const * buffer, string const & format)
130 {
131         vector<string> backends = Backends(buffer);
132         for (vector<string>::const_iterator it = backends.begin();
133              it != backends.end(); ++it)
134                 if (converters.isReachable(*it, format))
135                         return true;
136         return false;
137 }
138
139
140 vector<Format const *> const
141 Exporter::GetExportableFormats(Buffer const * buffer, bool only_viewable)
142 {
143         vector<string> backends = Backends(buffer);
144         vector<Format const *> result =
145                 converters.getReachable(backends[0], only_viewable, true);
146         for (vector<string>::const_iterator it = backends.begin() + 1;
147              it != backends.end(); ++it) {
148                 vector<Format const *>  r =
149                         converters.getReachable(*it, only_viewable, false);
150                 result.insert(result.end(), r.begin(), r.end());
151         }
152         return result;
153 }
154
155
156 string const Exporter::BufferFormat(Buffer const * buffer)
157 {
158         if (buffer->isLinuxDoc())
159                 return "linuxdoc";
160         else if (buffer->isDocBook())
161                 return "docbook";
162         else if (buffer->isLiterate())
163                 return "literate";
164         else
165                 return "latex";
166 }
167
168
169 vector<string> const Exporter::Backends(Buffer const * buffer)
170 {
171         vector<string> v;
172         v.push_back(BufferFormat(buffer));
173         v.push_back("text");
174         return v;
175 }