]> git.lyx.org Git - lyx.git/blob - src/exporter.C
Alfredo's second 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 <algorithm>
14
15 #include "exporter.h"
16 #include "buffer.h"
17 #include "lyx_cb.h" //ShowMessage()
18 #include "support/filetools.h"
19 #include "support/BoostFormat.h"
20 #include "lyxrc.h"
21 #include "converter.h"
22 #include "format.h"
23 #include "frontends/Alert.h"
24 #include "gettext.h"
25 #include "BufferView.h"
26
27 using std::vector;
28 using std::find;
29
30 bool pdf_mode = false;
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                         Graph::EdgePath p =
54                                 converters.getPath(*it, format);
55                         if (!p.empty()) {
56                                 pdf_mode = converters.usePdflatex(p);
57                                 backend_format = *it;
58                                 break;
59                         }
60                 }
61                 if (backend_format.empty()) {
62 #if USE_BOOST_FORMAT
63 // FIXME: better english ...
64                         Alert::error(_("Couldn't export file"),
65                                      boost::io::str(boost::format(_("No information for exporting the format %1$s."))
66                                    % formats.prettyName(format)));
67 #else
68                         Alert::error(_("Couldn't export file"),
69                                      _("No information for exporting the format ")
70                                      + formats.prettyName(format) + ".");
71 #endif
72                         return false;
73                 }
74         } else
75                 backend_format = format;
76
77         string filename = buffer->getLatexName(false);
78         if (!buffer->tmppath.empty())
79                 filename = AddName(buffer->tmppath, filename);
80         filename = ChangeExtension(filename,
81                                    formats.extension(backend_format));
82
83         // Ascii backend
84         if (backend_format == "text")
85                 buffer->writeFileAscii(filename, lyxrc.ascii_linelen);
86         // Linuxdoc backend
87         else if (buffer->isLinuxDoc())
88                 buffer->makeLinuxDocFile(filename, !put_in_tempdir);
89         // Docbook backend
90         else if (buffer->isDocBook())
91                 buffer->makeDocBookFile(filename, !put_in_tempdir);
92         // LaTeX backend
93         else if (backend_format == format)
94                 buffer->makeLaTeXFile(filename, string(), true);
95         else if (contains(buffer->filePath(), ' ')) {
96                 Alert::error(_("File name error"),
97                            _("The directory path to the document cannot contain spaces."));
98                 return false;
99         } else
100                 buffer->makeLaTeXFile(filename, buffer->filePath(), false);
101
102         string outfile_base = (put_in_tempdir)
103                 ? filename : buffer->getLatexName(false);
104
105         if (!converters.convert(buffer, filename, outfile_base,
106                                 backend_format, format, result_file))
107                 return false;
108
109         if (!put_in_tempdir)
110                 ShowMessage(buffer,
111                             _("Document exported as ")
112                             + formats.prettyName(format)
113                             + _(" to file `")
114                             + MakeDisplayPath(result_file) +'\'');
115         return true;
116 }
117
118
119 bool Exporter::Export(Buffer * buffer, string const & format,
120                       bool put_in_tempdir)
121 {
122         string result_file;
123         return Export(buffer, format, put_in_tempdir, result_file);
124 }
125
126
127 bool Exporter::Preview(Buffer * buffer, string const & format)
128 {
129         string result_file;
130         if (!Export(buffer, format, true, result_file))
131                 return false;
132         return formats.view(buffer, result_file, format);
133 }
134
135
136 bool Exporter::IsExportable(Buffer const * buffer, string const & format)
137 {
138         vector<string> backends = Backends(buffer);
139         for (vector<string>::const_iterator it = backends.begin();
140              it != backends.end(); ++it)
141                 if (converters.isReachable(*it, format))
142                         return true;
143         return false;
144 }
145
146
147 vector<Format const *> const
148 Exporter::GetExportableFormats(Buffer const * buffer, bool only_viewable)
149 {
150         vector<string> backends = Backends(buffer);
151         vector<Format const *> result =
152                 converters.getReachable(backends[0], only_viewable, true);
153         for (vector<string>::const_iterator it = backends.begin() + 1;
154              it != backends.end(); ++it) {
155                 vector<Format const *>  r =
156                         converters.getReachable(*it, only_viewable, false);
157                 result.insert(result.end(), r.begin(), r.end());
158         }
159         return result;
160 }
161
162
163 string const Exporter::BufferFormat(Buffer const * buffer)
164 {
165         if (buffer->isLinuxDoc())
166                 return "linuxdoc";
167         else if (buffer->isDocBook())
168                 return "docbook";
169         else if (buffer->isLiterate())
170                 return "literate";
171         else
172                 return "latex";
173 }
174
175
176 vector<string> const Exporter::Backends(Buffer const * buffer)
177 {
178         vector<string> v;
179         v.push_back(BufferFormat(buffer));
180         v.push_back("text");
181         return v;
182 }