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