]> git.lyx.org Git - lyx.git/blob - src/exporter.C
move pasteParagraph to global scope
[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 bool Exporter::Export(Buffer * buffer, string const & format,
112                       bool put_in_tempdir)
113 {
114         string result_file;
115         return Export(buffer, format, put_in_tempdir, result_file);
116 }
117
118 bool Exporter::Preview(Buffer * buffer, string const & format)
119 {
120         string result_file;
121         if (!Export(buffer, format, true, result_file))
122                 return false;
123         return formats.view(buffer, result_file, format);
124 }
125
126
127 bool Exporter::IsExportable(Buffer const * buffer, string const & format)
128 {
129         vector<string> backends = Backends(buffer);
130         for (vector<string>::const_iterator it = backends.begin();
131              it != backends.end(); ++it)
132                 if (converters.isReachable(*it, format))
133                         return true;
134         return false;
135 }
136
137
138 vector<Format const *> const
139 Exporter::GetExportableFormats(Buffer const * buffer, bool only_viewable)
140 {
141         vector<string> backends = Backends(buffer);
142         vector<Format const *> result =
143                 converters.getReachable(backends[0], only_viewable, true);
144         for (vector<string>::const_iterator it = backends.begin() + 1;
145              it != backends.end(); ++it) {
146                 vector<Format const *>  r =
147                         converters.getReachable(*it, only_viewable, false);
148                 result.insert(result.end(), r.begin(), r.end());
149         }
150         return result;
151 }
152
153
154 string const Exporter::BufferFormat(Buffer const * buffer)
155 {
156         if (buffer->isLinuxDoc())
157                 return "linuxdoc";
158         else if (buffer->isDocBook())
159                 return "docbook";
160         else if (buffer->isLiterate())
161                 return "literate";
162         else
163                 return "latex";
164 }
165
166 vector<string> const Exporter::Backends(Buffer const * buffer)
167 {
168         vector<string> v;
169         v.push_back(BufferFormat(buffer));
170         v.push_back("text");
171         return v;
172 }