4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
16 #include "support/trivstring.h"
25 namespace support { class FileName; }
33 enum class Flavor : int;
35 class ConversionException : public std::exception {
37 ConversionException() {}
38 virtual ~ConversionException() noexcept {}
39 const char * what() const noexcept override
40 { return "Exception caught in conversion routine!"; }
44 typedef std::vector<Format const *> FormatList;
50 Converter(std::string const & f, std::string const & t, std::string const & c,
51 std::string const & l);
55 std::string const from() const { return from_; }
57 std::string const to() const { return to_; }
59 std::string const command() const { return command_; }
61 void setCommand(std::string const & command) { command_ = command; }
63 std::string const flags() const { return flags_; }
65 void setFlags(std::string const & flags) { flags_ = flags; }
67 Format const * From() const { return From_; }
69 void setFrom(Format const * From) { From_ = From; }
71 void setTo(Format const * To) { To_ = To; }
73 Format const * To() const { return To_; }
75 bool latex() const { return latex_; }
77 std::string const latex_flavor() const { return latex_flavor_; }
79 bool docbook() const { return docbook_; }
81 bool need_aux() const { return need_aux_; }
82 /// Return whether or not the needauth option is set for this converter
83 bool need_auth() const { return need_auth_; }
85 bool nice() const { return nice_; }
87 std::string const result_dir() const { return result_dir_; }
89 std::string const result_file() const { return result_file_; }
91 std::string const parselog() const { return parselog_; }
93 std::string const hyperref_driver() const { return href_driver_; }
105 Format const * From_;
109 /// The converter is latex or its derivatives
111 /// The latex derivate
112 trivstring latex_flavor_;
113 /// The converter is DocBook
115 /// This converter needs the .aux files
117 /// we need a "nice" file from the backend, c.f. OutputParams.nice.
119 /// Use of this converter needs explicit user authorization
121 /// If the converter put the result in a directory, then result_dir
122 /// is the name of the directory
123 trivstring result_dir_;
124 /// If the converter put the result in a directory, then result_file
125 /// is the name of the main file in that directory
126 trivstring result_file_;
127 /// Command to convert the program output to a LaTeX log file format
128 trivstring parselog_;
129 /// The hyperref driver
130 trivstring href_driver_;
138 typedef std::vector<Converter> ConverterList;
140 typedef ConverterList::const_iterator const_iterator;
141 /// Return values for converter runs
149 Converter const & get(int i) const { return converterlist_[i]; }
151 Converter const * getConverter(std::string const & from,
152 std::string const & to) const;
154 int getNumber(std::string const & from, std::string const & to) const;
156 void add(std::string const & from, std::string const & to,
157 std::string const & command, std::string const & flags);
159 void erase(std::string const & from, std::string const & to);
162 getReachableTo(std::string const & target, bool clear_visited);
165 getReachable(std::string const & from, bool only_viewable,
167 std::set<std::string> const & excludes = std::set<std::string>());
169 FormatList importableFormats();
170 FormatList exportableFormats(bool only_viewable);
172 std::vector<std::string> loaders() const;
173 std::vector<std::string> savers() const;
175 /// Does a conversion path from format \p from to format \p to exist?
176 bool isReachable(std::string const & from, std::string const & to);
178 Graph::EdgePath getPath(std::string const & from, std::string const & to);
180 Flavor getFlavor(Graph::EdgePath const & path,
181 Buffer const * buffer = nullptr) const;
183 std::string getHyperrefDriver(Graph::EdgePath const & path) const;
184 /// Flags for converting files
185 enum ConversionFlags {
188 /// Use the default converter if no converter is defined
189 try_default = 1 << 0,
190 /// Get the converted file from cache if possible
194 RetVal convert(Buffer const * buffer,
195 support::FileName const & from_file, support::FileName const & to_file,
196 support::FileName const & orig_from,
197 std::string const & from_format, std::string const & to_format,
198 ErrorList & errorList, int conversionflags = none, bool includeall = false);
200 void update(Formats const & formats);
202 void updateLast(Formats const & formats);
204 bool formatIsUsed(std::string const & format) const;
206 const_iterator begin() const { return converterlist_.begin(); }
208 const_iterator end() const { return converterlist_.end(); }
212 /// Check whether converter conv is authorized to be run for elements
213 /// within document doc_fname.
214 /// The check succeeds for safe converters, whilst for those potentially
215 /// able to execute arbitrary code, tagged with the 'needauth' option,
216 /// authorization is: always denied if lyxrc.use_converter_needauth_forbidden
217 /// is enabled; always allowed if the lyxrc.use_converter_needauth
218 /// is disabled; user is prompted otherwise.
219 /// However, if use_shell_escape is true and a LaTeX backend is
220 /// going to be executed, both lyxrc.use_converter_needauth and
221 /// lyxrc.use_converter_needauth_forbidden are ignored, because in
222 /// this case the backend has to be executed and LyX will add the
223 /// -shell-escape option, so that user consent is always needed.
224 bool checkAuth(Converter const & conv, std::string const & doc_fname,
225 bool use_shell_escape = false);
230 intToFormat(std::vector<int> const & input);
232 bool scanLog(Buffer const & buffer, std::string const & command,
233 support::FileName const & filename, ErrorList & errorList);
235 RetVal runLaTeX(Buffer const & buffer, std::string const & command,
236 OutputParams const &, ErrorList & errorList);
238 ConverterList converterlist_;
240 trivstring latex_command_;
242 trivstring dvilualatex_command_;
244 trivstring lualatex_command_;
246 trivstring pdflatex_command_;
248 trivstring xelatex_command_;
249 /// If \p from = /path/file.ext and \p to = /path2/file2.ext2 then
250 /// this method moves each /path/file*.ext file to /path2/file2*.ext2
251 bool move(std::string const & fmt,
252 support::FileName const & from, support::FileName const & to,
258 /// The global instance.
259 /// Implementation is in LyX.cpp.
260 extern Converters & theConverters();
262 /// The global copy after reading lyxrc.defaults.
263 /// Implementation is in LyX.cpp.
264 extern Converters & theSystemConverters();