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