]> git.lyx.org Git - lyx.git/blob - src/Converter.h
Provide proper fallback if a bibliography processor is not found
[lyx.git] / src / Converter.h
1 // -*- C++ -*-
2 /**
3  * \file Converter.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Dekel Tsur
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef CONVERTER_H
13 #define CONVERTER_H
14
15 #include "Graph.h"
16 #include "OutputParams.h"
17 #include "support/trivstring.h"
18
19 #include <vector>
20 #include <set>
21 #include <string>
22
23
24 namespace lyx {
25
26 namespace support { class FileName; }
27
28 class Buffer;
29 class ErrorList;
30 class Format;
31 class Formats;
32
33 typedef std::vector<Format const *> FormatList;
34
35
36 ///
37 class Converter {
38 public:
39         ///
40         Converter(std::string const & f, std::string const & t, std::string const & c,
41                   std::string const & l);
42         ///
43         void readFlags();
44         ///
45         std::string const from() const { return from_; }
46         ///
47         std::string const to() const { return to_; }
48         ///
49         std::string const command() const { return command_; }
50         ///
51         void setCommand(std::string const & command) { command_ = command; }
52         ///
53         std::string const flags() const { return flags_; }
54         ///
55         void setFlags(std::string const & flags) { flags_ = flags; }
56         ///
57         Format const * From() const { return From_; }
58         ///
59         void setFrom(Format const * From) { From_ = From; }
60         ///
61         void setTo(Format const * To) { To_ = To; }
62         ///
63         Format const * To() const { return To_; }
64         ///
65         bool latex() const { return latex_; }
66         ///
67         std::string const latex_flavor() const { return latex_flavor_; }
68         ///
69         bool xml() const { return xml_; }
70         ///
71         bool need_aux() const { return need_aux_; }
72         /// Return whether or not the needauth option is set for this converter
73         bool need_auth() const { return need_auth_; }
74         ///
75         bool nice() const { return nice_; }
76         ///
77         std::string const result_dir() const { return result_dir_; }
78         ///
79         std::string const result_file() const { return result_file_; }
80         ///
81         std::string const parselog() const { return parselog_; }
82         ///
83         std::string const hyperref_driver() const { return href_driver_; }
84
85 private:
86         ///
87         trivstring from_;
88         ///
89         trivstring to_;
90         ///
91         trivstring command_;
92         ///
93         trivstring flags_;
94         ///
95         Format const * From_;
96         ///
97         Format const * To_;
98
99         /// The converter is latex or its derivatives
100         bool latex_;
101         /// The latex derivate
102         trivstring latex_flavor_;
103         /// The converter is xml
104         bool xml_;
105         /// This converter needs the .aux files
106         bool need_aux_;
107         /// we need a "nice" file from the backend, c.f. OutputParams.nice.
108         bool nice_;
109         /// Use of this converter needs explicit user authorization
110         bool need_auth_;
111         /// If the converter put the result in a directory, then result_dir
112         /// is the name of the directory
113         trivstring result_dir_;
114         /// If the converter put the result in a directory, then result_file
115         /// is the name of the main file in that directory
116         trivstring result_file_;
117         /// Command to convert the program output to a LaTeX log file format
118         trivstring parselog_;
119         /// The hyperref driver
120         trivstring href_driver_;
121 };
122
123
124 ///
125 class Converters {
126 public:
127         ///
128         typedef std::vector<Converter> ConverterList;
129         ///
130         typedef ConverterList::const_iterator const_iterator;
131
132         ///
133         Converter const & get(int i) const { return converterlist_[i]; }
134         ///
135         Converter const * getConverter(std::string const & from,
136                                        std::string const & to) const;
137         ///
138         int getNumber(std::string const & from, std::string const & to) const;
139         ///
140         void add(std::string const & from, std::string const & to,
141                  std::string const & command, std::string const & flags);
142         //
143         void erase(std::string const & from, std::string const & to);
144         ///
145         FormatList const
146                 getReachableTo(std::string const & target, bool clear_visited);
147         ///
148         FormatList const
149                 getReachable(std::string const & from, bool only_viewable,
150                     bool clear_visited,
151                     std::set<std::string> const & excludes = std::set<std::string>());
152
153         FormatList importableFormats();
154         FormatList exportableFormats(bool only_viewable);
155
156         std::vector<std::string> loaders() const;
157         std::vector<std::string> savers() const;
158
159         /// Does a conversion path from format \p from to format \p to exist?
160         bool isReachable(std::string const & from, std::string const & to);
161         ///
162         Graph::EdgePath getPath(std::string const & from, std::string const & to);
163         ///
164         OutputParams::FLAVOR getFlavor(Graph::EdgePath const & path,
165                                        Buffer const * buffer = 0);
166         ///
167         std::string getHyperrefDriver(Graph::EdgePath const & path);
168         /// Flags for converting files
169         enum ConversionFlags {
170                 /// No special flags
171                 none = 0,
172                 /// Use the default converter if no converter is defined
173                 try_default = 1 << 0,
174                 /// Get the converted file from cache if possible
175                 try_cache = 1 << 1
176         };
177         ///
178         bool convert(Buffer const * buffer,
179                      support::FileName const & from_file, support::FileName const & to_file,
180                      support::FileName const & orig_from,
181                      std::string const & from_format, std::string const & to_format,
182                      ErrorList & errorList, int conversionflags = none);
183         ///
184         void update(Formats const & formats);
185         ///
186         void updateLast(Formats const & formats);
187         ///
188         bool formatIsUsed(std::string const & format);
189         ///
190         const_iterator begin() const { return converterlist_.begin(); }
191         ///
192         const_iterator end() const { return converterlist_.end(); }
193         ///
194         void buildGraph();
195
196         /// Check whether converter conv is authorized to be run for elements
197         /// within document doc_fname.
198         /// The check succeeds for safe converters, whilst for those potentially
199         /// able to execute arbitrary code, tagged with the 'needauth' option,
200         /// authorization is: always denied if lyxrc.use_converter_needauth_forbidden
201         /// is enabled; always allowed if the lyxrc.use_converter_needauth
202         /// is disabled; user is prompted otherwise.
203         /// However, if use_shell_escape is true and a LaTeX backend is
204         /// going to be executed, both lyxrc.use_converter_needauth and
205         /// lyxrc.use_converter_needauth_forbidden are ignored, because in
206         /// this case the backend has to be executed and LyX will add the
207         /// -shell-escape option, so that user consent is always needed.
208         bool checkAuth(Converter const & conv, std::string const & doc_fname,
209                        bool use_shell_escape = false);
210
211 private:
212         ///
213         FormatList const
214         intToFormat(std::vector<int> const & input);
215         ///
216         bool scanLog(Buffer const & buffer, std::string const & command,
217                      support::FileName const & filename, ErrorList & errorList);
218         ///
219         bool runLaTeX(Buffer const & buffer, std::string const & command,
220                       OutputParams const &, ErrorList & errorList);
221         ///
222         ConverterList converterlist_;
223         ///
224         trivstring latex_command_;
225         ///
226         trivstring dvilualatex_command_;
227         ///
228         trivstring lualatex_command_;
229         ///
230         trivstring pdflatex_command_;
231         ///
232         trivstring xelatex_command_;
233         /// If \p from = /path/file.ext and \p to = /path2/file2.ext2 then
234         /// this method moves each /path/file*.ext file to /path2/file2*.ext2
235         bool move(std::string const & fmt,
236                   support::FileName const & from, support::FileName const & to,
237                   bool copy);
238         ///
239         Graph G_;
240 };
241
242 /// The global instance.
243 /// Implementation is in LyX.cpp.
244 extern Converters & theConverters();
245
246 /// The global copy after reading lyxrc.defaults.
247 /// Implementation is in LyX.cpp.
248 extern Converters & theSystemConverters();
249
250 } // namespace lyx
251
252 #endif //CONVERTER_H