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"
21 #include "frontends/Alert.h"
23 #include "BufferView.h"
28 bool Exporter::Export(Buffer * buffer, string const & format,
29 bool put_in_tempdir, string & result_file)
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();
36 // Remove all error insets
37 if (bv->removeAutoInsets()) {
38 #warning repaint() or update() or nothing ?
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);
52 lyxrc.pdf_mode = converters.usePdflatex(p);
57 if (backend_format.empty()) {
58 Alert::alert(_("Cannot export file"),
59 _("No information for exporting to ")
60 + formats.prettyName(format));
64 backend_format = format;
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));
73 if (backend_format == "text")
74 buffer->writeFileAscii(filename, lyxrc.ascii_linelen);
76 else if (buffer->isLinuxDoc())
77 buffer->makeLinuxDocFile(filename, !put_in_tempdir);
79 else if (buffer->isDocBook())
80 buffer->makeDocBookFile(filename, !put_in_tempdir);
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."));
89 buffer->makeLaTeXFile(filename, buffer->filePath(), false);
91 string outfile_base = (put_in_tempdir)
92 ? filename : buffer->getLatexName(false);
94 if (!converters.convert(buffer, filename, outfile_base,
95 backend_format, format, result_file))
100 _("Document exported as ")
101 + formats.prettyName(format)
103 + MakeDisplayPath(result_file) +'\'');
108 bool Exporter::Export(Buffer * buffer, string const & format,
112 return Export(buffer, format, put_in_tempdir, result_file);
116 bool Exporter::Preview(Buffer * buffer, string const & format)
119 if (!Export(buffer, format, true, result_file))
121 return formats.view(buffer, result_file, format);
125 bool Exporter::IsExportable(Buffer const * buffer, string const & format)
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))
136 vector<Format const *> const
137 Exporter::GetExportableFormats(Buffer const * buffer, bool only_viewable)
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());
152 string const Exporter::BufferFormat(Buffer const * buffer)
154 if (buffer->isLinuxDoc())
156 else if (buffer->isDocBook())
158 else if (buffer->isLiterate())
165 vector<string> const Exporter::Backends(Buffer const * buffer)
168 v.push_back(BufferFormat(buffer));