]> git.lyx.org Git - lyx.git/blob - src/Converter.h
Avoid full metrics computation with Update:FitCursor
[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 "support/trivstring.h"
17
18 #include <vector>
19 #include <set>
20 #include <string>
21
22
23 namespace lyx {
24
25 namespace support { class FileName; }
26
27 class Buffer;
28 class ErrorList;
29 class Format;
30 class Formats;
31 class OutputParams;
32
33 enum class Flavor : int;
34
35 class ConversionException : public std::exception {
36 public:
37         ConversionException() {}
38         virtual ~ConversionException() noexcept {}
39         const char * what() const noexcept override
40                 { return "Exception caught in conversion routine!"; }
41 };
42
43
44 typedef std::vector<Format const *> FormatList;
45
46 ///
47 class Converter {
48 public:
49         ///
50         Converter(std::string const & f, std::string const & t, std::string const & c,
51                   std::string const & l);
52         ///
53         void readFlags();
54         ///
55         std::string const from() const { return from_; }
56         ///
57         std::string const to() const { return to_; }
58         ///
59         std::string const command() const { return command_; }
60         ///
61         void setCommand(std::string const & command);
62         ///
63         std::string const flags() const { return flags_; }
64         ///
65         void setFlags(std::string const & flags) { flags_ = flags; }
66         ///
67         Format const * From() const { return From_; }
68         ///
69         void setFrom(Format const * From) { From_ = From; }
70         ///
71         void setTo(Format const * To) { To_ = To; }
72         ///
73         Format const * To() const { return To_; }
74         ///
75         bool latex() const { return latex_; }
76         ///
77         std::string const latex_flavor() const { return latex_flavor_; }
78         ///
79         bool docbook() const { return docbook_; }
80         ///
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_; }
84         ///
85         bool nice() const { return nice_; }
86         ///
87         std::string const result_dir() const { return result_dir_; }
88         ///
89         std::string const result_file() const { return result_file_; }
90         ///
91         std::string const parselog() const { return parselog_; }
92         ///
93         std::string const hyperref_driver() const { return href_driver_; }
94
95 private:
96         ///
97         trivstring from_;
98         ///
99         trivstring to_;
100         ///
101         trivstring command_;
102         ///
103         trivstring flags_;
104         ///
105         Format const * From_;
106         ///
107         Format const * To_;
108
109         /// The converter is latex or its derivatives
110         bool latex_;
111         /// The latex derivate
112         trivstring latex_flavor_;
113         /// The converter is DocBook
114         bool docbook_;
115         /// This converter needs the .aux files
116         bool need_aux_;
117         /// we need a "nice" file from the backend, c.f. OutputParams.nice.
118         bool nice_;
119         /// Use of this converter needs explicit user authorization
120         bool need_auth_;
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_;
131 };
132
133
134 ///
135 class Converters {
136 public:
137         ///
138         typedef std::vector<Converter> ConverterList;
139         ///
140         typedef ConverterList::const_iterator const_iterator;
141         /// Return values for converter runs
142         enum RetVal {
143                 SUCCESS = 0,
144                 FAILURE = 1,
145                 KILLED  = 1000
146         };
147
148         ///
149         Converter const & get(int i) const { return converterlist_[i]; }
150         ///
151         Converter const * getConverter(std::string const & from,
152                                        std::string const & to) const;
153         ///
154         int getNumber(std::string const & from, std::string const & to) const;
155         ///
156         void add(std::string const & from, std::string const & to,
157                  std::string const & command, std::string const & flags);
158         //
159         void erase(std::string const & from, std::string const & to);
160         ///
161         FormatList const
162                 getReachableTo(std::string const & target, bool clear_visited);
163         ///
164         FormatList const
165                 getReachable(std::string const & from, bool only_viewable,
166                     bool clear_visited,
167                     std::set<std::string> const & excludes = std::set<std::string>());
168
169         FormatList importableFormats();
170         FormatList exportableFormats(bool only_viewable);
171
172         std::vector<std::string> loaders() const;
173         std::vector<std::string> savers() const;
174
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);
177         ///
178         Graph::EdgePath getPath(std::string const & from, std::string const & to);
179         ///
180         Flavor getFlavor(Graph::EdgePath const & path,
181                                            Buffer const * buffer = nullptr) const;
182         ///
183         std::string getHyperrefDriver(Graph::EdgePath const & path) const;
184         /// Flags for converting files
185         enum ConversionFlags {
186                 /// No special flags
187                 none = 0,
188                 /// Use the default converter if no converter is defined
189                 try_default = 1 << 0,
190                 /// Get the converted file from cache if possible
191                 try_cache = 1 << 1
192         };
193         ///
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);
199         ///
200         void update(Formats const & formats);
201         ///
202         void updateLast(Formats const & formats);
203         ///
204         bool formatIsUsed(std::string const & format) const;
205         ///
206         const_iterator begin() const { return converterlist_.begin(); }
207         ///
208         const_iterator end() const { return converterlist_.end(); }
209         ///
210         void buildGraph();
211
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);
226
227 private:
228         ///
229         FormatList const
230         intToFormat(std::vector<int> const & input);
231         ///
232         bool scanLog(Buffer const & buffer, std::string const & command,
233                      support::FileName const & filename, ErrorList & errorList);
234         ///
235         RetVal runLaTeX(Buffer const & buffer, std::string const & command,
236                       OutputParams const &, ErrorList & errorList);
237         ///
238         ConverterList converterlist_;
239         ///
240         trivstring latex_command_;
241         ///
242         trivstring dvilualatex_command_;
243         ///
244         trivstring lualatex_command_;
245         ///
246         trivstring pdflatex_command_;
247         ///
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,
253                   bool copy);
254         ///
255         Graph G_;
256 };
257
258 /// The global instance.
259 /// Implementation is in LyX.cpp.
260 extern Converters & theConverters();
261
262 /// The global copy after reading lyxrc.defaults.
263 /// Implementation is in LyX.cpp.
264 extern Converters & theSystemConverters();
265
266 } // namespace lyx
267
268 #endif //CONVERTER_H