]> git.lyx.org Git - lyx.git/blob - src/exporter.C
94405ac999d58ccb28891c94595b3b523b68b6c1
[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 bool Exporter::IsExportable(Buffer const * buffer, string const & format)
86 {
87         // This is not efficient
88         vector<pair<string, string> > const v = GetExportableFormats(buffer);
89         for (vector<pair<string, string> >::const_iterator it = v.begin();
90              it != v.end(); ++it)
91                 if ((*it).first == format)
92                         return true;
93         return false;
94 }
95
96 vector<pair<string, string> > const
97 Exporter::GetExportableFormats(Buffer const * buffer)
98 {
99         vector<pair<string, string> > result = 
100                 Converter::GetReachable(BufferExtension(buffer), false);
101         result.push_back(pair<string,string>("txt", "Ascii"));
102         return result;
103 }
104
105
106 vector<pair<string, string> > const
107 Exporter::GetViewableFormats(Buffer const * buffer)
108 {
109         vector<pair<string, string> > result = 
110                 Converter::GetReachable(BufferExtension(buffer), true);
111         Format * format = Formats::GetFormat("txt");
112         if (format && !format->viewer.empty())
113                 result.push_back(pair<string,string>("txt", "Ascii"));
114         return result;
115 }
116
117
118 string const Exporter::BufferExtension(Buffer const * buffer)
119 {
120         if (buffer->isLinuxDoc())
121                 return "sgml";
122         else if (buffer->isDocBook())
123                 return "docbook";
124         else if (buffer->isLiterate())
125                 return lyxrc.literate_extension;
126         else
127                 return "tex";
128 }