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