]> git.lyx.org Git - lyx.git/blob - src/exporter.C
split LyXText::rowlist_ into individual Paragraph::rows_ chunks
[lyx.git] / src / exporter.C
1 /**
2  * \file exporter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "exporter.h"
14 #include "buffer.h"
15 #include "buffer_funcs.h"
16 #include "support/filetools.h"
17 #include "lyxrc.h"
18 #include "converter.h"
19 #include "format.h"
20 #include "frontends/Alert.h"
21 #include "gettext.h"
22
23 #include <algorithm>
24
25 using namespace lyx::support;
26
27 using std::vector;
28 using std::find;
29
30
31 namespace {
32
33 vector<string> const Backends(Buffer const * buffer)
34 {
35         vector<string> v;
36         if (buffer->params.getLyXTextClass().isTeXClassAvailable())
37                 v.push_back(BufferFormat(*buffer));
38         v.push_back("text");
39         return v;
40 }
41
42 } //namespace anon
43
44
45 bool Exporter::Export(Buffer * buffer, string const & format,
46                       bool put_in_tempdir, string & result_file)
47 {
48         string backend_format;
49         LatexRunParams runparams;
50         runparams.flavor = LatexRunParams::LATEX;
51         vector<string> backends = Backends(buffer);
52         if (find(backends.begin(), backends.end(), format) == backends.end()) {
53                 for (vector<string>::const_iterator it = backends.begin();
54                      it != backends.end(); ++it) {
55                         Graph::EdgePath p =
56                                 converters.getPath(*it, format);
57                         if (!p.empty()) {
58                                 if (converters.usePdflatex(p))
59                                         runparams.flavor = LatexRunParams::PDFLATEX;
60                                 backend_format = *it;
61                                 break;
62                         }
63                 }
64                 if (backend_format.empty()) {
65                         Alert::error(_("Couldn't export file"),
66                                 bformat(_("No information for exporting the format %1$s."),
67                                    formats.prettyName(format)));
68                         return false;
69                 }
70         } else
71                 backend_format = format;
72
73         string filename = buffer->getLatexName(false);
74         if (!buffer->tmppath.empty())
75                 filename = AddName(buffer->tmppath, filename);
76         filename = ChangeExtension(filename,
77                                    formats.extension(backend_format));
78
79         // Ascii backend
80         if (backend_format == "text")
81                 buffer->writeFileAscii(filename, lyxrc.ascii_linelen);
82         // Linuxdoc backend
83         else if (buffer->isLinuxDoc())
84                 buffer->makeLinuxDocFile(filename, !put_in_tempdir);
85         // Docbook backend
86         else if (buffer->isDocBook())
87                 buffer->makeDocBookFile(filename, !put_in_tempdir);
88         // LaTeX backend
89         else if (backend_format == format) {
90                 runparams.nice = true;
91                 buffer->makeLaTeXFile(filename, string(), runparams);
92         } else if (contains(buffer->filePath(), ' ')) {
93                 Alert::error(_("File name error"),
94                            _("The directory path to the document cannot contain spaces."));
95                 return false;
96         } else {
97                 runparams.nice = false;
98                 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
99         }
100
101         string outfile_base = (put_in_tempdir)
102                 ? filename : buffer->getLatexName(false);
103
104         if (!converters.convert(buffer, filename, outfile_base,
105                                 backend_format, format, result_file))
106                 return false;
107
108         if (!put_in_tempdir)
109                 buffer->message(_("Document exported as ")
110                                       + formats.prettyName(format)
111                                       + _(" to file `")
112                                       + MakeDisplayPath(result_file) +'\'');
113         return true;
114 }
115
116
117 bool Exporter::Export(Buffer * buffer, string const & format,
118                       bool put_in_tempdir)
119 {
120         string result_file;
121         return Export(buffer, format, put_in_tempdir, result_file);
122 }
123
124
125 bool Exporter::Preview(Buffer * buffer, string const & format)
126 {
127         string result_file;
128         if (!Export(buffer, format, true, result_file))
129                 return false;
130         return formats.view(buffer, result_file, format);
131 }
132
133
134 bool Exporter::IsExportable(Buffer const * buffer, string const & format)
135 {
136         vector<string> backends = Backends(buffer);
137         for (vector<string>::const_iterator it = backends.begin();
138              it != backends.end(); ++it)
139                 if (converters.isReachable(*it, format))
140                         return true;
141         return false;
142 }
143
144
145 vector<Format const *> const
146 Exporter::GetExportableFormats(Buffer const * buffer, bool only_viewable)
147 {
148         vector<string> backends = Backends(buffer);
149         vector<Format const *> result =
150                 converters.getReachable(backends[0], only_viewable, true);
151         for (vector<string>::const_iterator it = backends.begin() + 1;
152              it != backends.end(); ++it) {
153                 vector<Format const *>  r =
154                         converters.getReachable(*it, only_viewable, false);
155                 result.insert(result.end(), r.begin(), r.end());
156         }
157         return result;
158 }