]> git.lyx.org Git - lyx.git/blob - src/exporter.C
2ce558e6cad435febde5ba866ad944860d422dd3
[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 "exporter.h"
18 #include "converter.h"
19 #include "buffer.h"
20 #include "lyx_cb.h" //ShowMessage()
21 #include "support/filetools.h"
22 #include "lyxrc.h"
23
24 using std::vector;
25 using std::pair;
26
27 bool Exporter::Export(Buffer * buffer, string const & format0,
28                       bool put_in_tempdir, string * view_file)
29 {
30         string format;
31         string using_format = Converter::SplitFormat(format0, format);
32
33         string backend_format = (format == "txt") 
34                 ? format : BufferExtension(buffer);
35         bool only_backend = backend_format == format;
36
37         string filename = buffer->getLatexName(false);
38         if (!buffer->tmppath.empty())
39                 filename = AddName(buffer->tmppath, filename);
40         filename = ChangeExtension(filename, backend_format);
41
42         // Ascii backend
43         if (backend_format == "txt")
44                 buffer->writeFileAscii(filename, lyxrc.ascii_linelen);
45         // Linuxdoc backend
46         else if (buffer->isLinuxDoc())
47                 buffer->makeLinuxDocFile(filename, true);
48         // Docbook backend
49         else if (buffer->isDocBook())
50                 buffer->makeDocBookFile(filename, true);
51         // LaTeX backend
52         else if (only_backend)
53                 buffer->makeLaTeXFile(filename, string(), true);
54         else
55                 buffer->makeLaTeXFile(filename, buffer->filepath, false);
56
57         string outfile = (put_in_tempdir)
58                 ? ChangeExtension(filename, format)
59                 : ChangeExtension(buffer->getLatexName(false), format);
60
61         if (!Converter::Convert(buffer, filename, outfile, using_format, 
62                                 view_file))
63                 return false;
64
65         if (!put_in_tempdir)
66                 ShowMessage(buffer,
67                             _("Document exported as ")
68                             + Formats::PrettyName(format)
69                             + _(" to file `")
70                             + MakeDisplayPath(outfile) +'\'');
71         return true;
72 }
73
74
75 bool Exporter::Preview(Buffer * buffer, string const & format0)
76 {
77         string view_file;
78         if (!Export(buffer, format0, true, &view_file))
79                 return false;
80
81         return Formats::View(buffer, view_file);
82 }
83
84
85 vector<pair<string, string> > const
86 Exporter::GetExportableFormats(Buffer const * buffer)
87 {
88         vector<pair<string, string> > result = 
89                 Converter::GetReachable(BufferExtension(buffer), false);
90         result.push_back(pair<string,string>("txt", "Ascii"));
91         return result;
92 }
93
94
95 vector<pair<string, string> > const
96 Exporter::GetViewableFormats(Buffer const * buffer)
97 {
98         vector<pair<string, string> > result = 
99                 Converter::GetReachable(BufferExtension(buffer), false);
100         Format * format = Formats::GetFormat("txt");
101         if (format && !format->viewer.empty())
102                 result.push_back(pair<string,string>("txt", "Ascii"));
103         return result;
104 }
105
106
107 string const Exporter::BufferExtension(Buffer const * buffer)
108 {
109         if (buffer->isLinuxDoc())
110                 return "sgml";
111         else if (buffer->isDocBook())
112                 return "docbook";
113         else
114                 return "tex";
115 }