1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
17 #include "lyx_cb.h" //ShowMessage()
18 #include "support/filetools.h"
20 #include "converter.h"
22 #include "frontends/Alert.h"
24 #include "BufferView.h"
29 bool Exporter::Export(Buffer * buffer, string const & format,
30 bool put_in_tempdir, string & result_file)
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();
37 // Remove all error insets
38 if (bv->removeAutoInsets()) {
39 #warning repaint() or update() or nothing ?
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) {
51 converters.getPath(*it, format);
53 lyxrc.pdf_mode = converters.usePdflatex(p);
58 if (backend_format.empty()) {
59 Alert::alert(_("Cannot export file"),
60 _("No information for exporting to ")
61 + formats.prettyName(format));
65 backend_format = format;
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));
74 if (backend_format == "text")
75 buffer->writeFileAscii(filename, lyxrc.ascii_linelen);
77 else if (buffer->isLinuxDoc())
78 buffer->makeLinuxDocFile(filename, !put_in_tempdir);
80 else if (buffer->isDocBook())
81 buffer->makeDocBookFile(filename, !put_in_tempdir);
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."));
90 buffer->makeLaTeXFile(filename, buffer->filePath(), false);
92 string outfile_base = (put_in_tempdir)
93 ? filename : buffer->getLatexName(false);
95 if (!converters.convert(buffer, filename, outfile_base,
96 backend_format, format, result_file))
101 _("Document exported as ")
102 + formats.prettyName(format)
104 + MakeDisplayPath(result_file) +'\'');
109 bool Exporter::Export(Buffer * buffer, string const & format,
113 return Export(buffer, format, put_in_tempdir, result_file);
117 bool Exporter::Preview(Buffer * buffer, string const & format)
120 if (!Export(buffer, format, true, result_file))
122 return formats.view(buffer, result_file, format);
126 bool Exporter::IsExportable(Buffer const * buffer, string const & format)
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))
137 vector<Format const *> const
138 Exporter::GetExportableFormats(Buffer const * buffer, bool only_viewable)
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());
153 string const Exporter::BufferFormat(Buffer const * buffer)
155 if (buffer->isLinuxDoc())
157 else if (buffer->isDocBook())
159 else if (buffer->isLiterate())
166 vector<string> const Exporter::Backends(Buffer const * buffer)
169 v.push_back(BufferFormat(buffer));