]> git.lyx.org Git - lyx.git/blob - src/exporter.C
Fixed various "missing features" in the tabular/textinset code.
[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-2000 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 "lyx_gui_misc.h" //WriteAlert
26
27 using std::vector;
28 using std::find;
29
30 bool Exporter::Export(Buffer * buffer, string const & format,
31                       bool put_in_tempdir, string & result_file)
32 {
33         string backend_format;
34         vector<string> backends = Backends(buffer);
35         if (find(backends.begin(), backends.end(), format) == backends.end()) {
36                 for (vector<string>::const_iterator it = backends.begin();
37                      it != backends.end(); ++it) {
38                         Converters::EdgePath p =
39                                 converters.GetPath(*it, format);
40                         if (!p.empty()) {
41                                 lyxrc.pdf_mode = converters.UsePdflatex(p);
42                                 backend_format = *it;
43                                 break;
44                         }
45                 }
46                 if (backend_format.empty()) {
47                         WriteAlert(_("Can not export file"),
48                                    _("No information for exporting to ")
49                                    + formats.PrettyName(format));
50                         return false;
51                 }
52         } else
53                 backend_format = format;
54
55         string filename = buffer->getLatexName(false);
56         if (!buffer->tmppath.empty())
57                 filename = AddName(buffer->tmppath, filename);
58         filename = ChangeExtension(filename, 
59                                    formats.Extension(backend_format));
60
61         // Ascii backend
62         if (backend_format == "text")
63                 buffer->writeFileAscii(filename, lyxrc.ascii_linelen);
64         // Linuxdoc backend
65         else if (buffer->isLinuxDoc())
66                 buffer->makeLinuxDocFile(filename, true);
67         // Docbook backend
68         else if (buffer->isDocBook())
69                 buffer->makeDocBookFile(filename, true);
70         // LaTeX backend
71         else if (backend_format == format)
72                 buffer->makeLaTeXFile(filename, string(), true);
73         else
74                 buffer->makeLaTeXFile(filename, buffer->filepath, false);
75
76         string outfile_base = (put_in_tempdir)
77                 ? filename : buffer->getLatexName(false);
78
79         if (!converters.Convert(buffer, filename, outfile_base,
80                                 backend_format, format, result_file))
81                 return false;
82
83         if (!put_in_tempdir)
84                 ShowMessage(buffer,
85                             _("Document exported as ")
86                             + formats.PrettyName(format)
87                             + _(" to file `")
88                             + MakeDisplayPath(result_file) +'\'');
89         return true;
90 }
91
92 bool Exporter::Export(Buffer * buffer, string const & format,
93                       bool put_in_tempdir)
94 {
95         string result_file;
96         return Export(buffer, format, put_in_tempdir, result_file);
97 }
98
99 bool Exporter::Preview(Buffer * buffer, string const & format)
100 {
101         string result_file;
102         if (!Export(buffer, format, true, result_file))
103                 return false;
104         return formats.View(buffer, result_file, format);
105 }
106
107
108 bool Exporter::IsExportable(Buffer const * buffer, string const & format)
109 {
110         vector<string> backends = Backends(buffer);
111         for (vector<string>::const_iterator it = backends.begin();
112              it != backends.end(); ++it)
113                 if (converters.IsReachable(*it, format))
114                         return true;
115         return false;
116 }
117
118
119 vector<Format const *> const
120 Exporter::GetExportableFormats(Buffer const * buffer, bool only_viewable)
121 {
122         vector<string> backends = Backends(buffer);
123         vector<Format const *> result = 
124                 converters.GetReachable(backends[0], only_viewable, true);
125         for (vector<string>::const_iterator it = backends.begin() + 1;
126              it != backends.end(); ++it) {
127                 vector<Format const *>  r =
128                         converters.GetReachable(*it, only_viewable, false);
129                 result.insert(result.end(), r.begin(), r.end());
130         }
131         return result;
132 }
133
134
135 string const Exporter::BufferFormat(Buffer const * buffer)
136 {
137         if (buffer->isLinuxDoc())
138                 return "linuxdoc";
139         else if (buffer->isDocBook())
140                 return "docbook";
141         else if (buffer->isLiterate())
142                 return "literate";
143         else
144                 return "latex";
145 }
146
147 vector<string> const Exporter::Backends(Buffer const * buffer)
148 {
149         vector<string> v;
150         v.push_back(BufferFormat(buffer));
151         v.push_back("text");
152         return v;
153 }