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