]> git.lyx.org Git - lyx.git/blob - src/exporter.C
Import patch from Dekel.
[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 "buffer.h"
19 #include "lyx_cb.h" //ShowMessage()
20 #include "support/filetools.h"
21 #include "lyxrc.h"
22 #include "converter.h"
23
24 using std::vector;
25
26 bool Exporter::Export(Buffer * buffer, string const & format0,
27                       bool put_in_tempdir, string & result_file)
28 {
29         string format;
30         string using_format = Converter::SplitFormat(format0, format);
31
32         string backend_format = (format == "text") 
33                 ? format : BufferFormat(buffer);
34         bool only_backend = backend_format == format;
35
36         string filename = buffer->getLatexName(false);
37         if (!buffer->tmppath.empty())
38                 filename = AddName(buffer->tmppath, filename);
39         filename = ChangeExtension(filename, 
40                                    formats.Extension(backend_format));
41
42         // Ascii backend
43         if (backend_format == "text")
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_base = (put_in_tempdir)
58                 ? filename : buffer->getLatexName(false);
59
60         if (!Converter::Convert(buffer, filename, outfile_base,
61                                 backend_format, format, using_format,
62                                 result_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(result_file) +'\'');
71         return true;
72 }
73
74 bool Exporter::Export(Buffer * buffer, string const & format,
75                       bool put_in_tempdir)
76 {
77         string result_file;
78         return Export(buffer, format, put_in_tempdir, result_file);
79 }
80
81 bool Exporter::Preview(Buffer * buffer, string const & format0)
82 {
83         string result_file;
84         if (!Export(buffer, format0, true, result_file))
85                 return false;
86         string format;
87         Converter::SplitFormat(format0, format);
88         return formats.View(buffer, result_file, format);
89 }
90
91
92 bool Exporter::IsExportable(Buffer const * buffer, string const & format)
93 {
94         return format == "text" ||
95                 Converter::IsReachable(BufferFormat(buffer), format);
96 }
97
98
99 vector<FormatPair> const
100 Exporter::GetExportableFormats(Buffer const * buffer, bool only_viewable)
101 {
102         vector<FormatPair> result = 
103                 Converter::GetReachable(BufferFormat(buffer), only_viewable);
104         Format * format = formats.GetFormat("text");
105         if (format && (!only_viewable || !format->viewer.empty()))
106                 result.push_back(FormatPair(format , 0, ""));
107         return result;
108 }
109
110
111 string const Exporter::BufferFormat(Buffer const * buffer)
112 {
113         if (buffer->isLinuxDoc())
114                 return "linuxdoc";
115         else if (buffer->isDocBook())
116                 return "docbook";
117         else if (buffer->isLiterate())
118                 return "literate";
119         else
120                 return "latex";
121 }