]> git.lyx.org Git - lyx.git/blob - src/Converter.h
Account for old versions of Pygments
[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 private:
84         ///
85         trivstring from_;
86         ///
87         trivstring to_;
88         ///
89         trivstring command_;
90         ///
91         trivstring flags_;
92         ///
93         Format const * From_;
94         ///
95         Format const * To_;
96
97         /// The converter is latex or its derivatives
98         bool latex_;
99         /// The latex derivate
100         trivstring latex_flavor_;
101         /// The converter is xml
102         bool xml_;
103         /// This converter needs the .aux files
104         bool need_aux_;
105         /// we need a "nice" file from the backend, c.f. OutputParams.nice.
106         bool nice_;
107         /// Use of this converter needs explicit user authorization
108         bool need_auth_;
109         /// If the converter put the result in a directory, then result_dir
110         /// is the name of the directory
111         trivstring result_dir_;
112         /// If the converter put the result in a directory, then result_file
113         /// is the name of the main file in that directory
114         trivstring result_file_;
115         /// Command to convert the program output to a LaTeX log file format
116         trivstring parselog_;
117 };
118
119
120 ///
121 class Converters {
122 public:
123         ///
124         typedef std::vector<Converter> ConverterList;
125         ///
126         typedef ConverterList::const_iterator const_iterator;
127
128         ///
129         Converter const & get(int i) const { return converterlist_[i]; }
130         ///
131         Converter const * getConverter(std::string const & from,
132                                        std::string const & to) const;
133         ///
134         int getNumber(std::string const & from, std::string const & to) const;
135         ///
136         void add(std::string const & from, std::string const & to,
137                  std::string const & command, std::string const & flags);
138         //
139         void erase(std::string const & from, std::string const & to);
140         ///
141         FormatList const 
142                 getReachableTo(std::string const & target, bool clear_visited);
143         ///
144         FormatList const
145                 getReachable(std::string const & from, bool only_viewable,
146                     bool clear_visited,
147                     std::set<std::string> const & excludes = std::set<std::string>());
148
149         FormatList importableFormats();
150         FormatList exportableFormats(bool only_viewable);
151
152         std::vector<std::string> loaders() const;
153         std::vector<std::string> savers() const;
154
155         /// Does a conversion path from format \p from to format \p to exist?
156         bool isReachable(std::string const & from, std::string const & to);
157         ///
158         Graph::EdgePath getPath(std::string const & from, std::string const & to);
159         ///
160         OutputParams::FLAVOR getFlavor(Graph::EdgePath const & path,
161                                        Buffer const * buffer = 0);
162         /// Flags for converting files
163         enum ConversionFlags {
164                 /// No special flags
165                 none = 0,
166                 /// Use the default converter if no converter is defined
167                 try_default = 1 << 0,
168                 /// Get the converted file from cache if possible
169                 try_cache = 1 << 1
170         };
171         ///
172         bool convert(Buffer const * buffer,
173                      support::FileName const & from_file, support::FileName const & to_file,
174                      support::FileName const & orig_from,
175                      std::string const & from_format, std::string const & to_format,
176                      ErrorList & errorList, int conversionflags = none);
177         ///
178         void update(Formats const & formats);
179         ///
180         void updateLast(Formats const & formats);
181         ///
182         bool formatIsUsed(std::string const & format);
183         ///
184         const_iterator begin() const { return converterlist_.begin(); }
185         ///
186         const_iterator end() const { return converterlist_.end(); }
187         ///
188         void buildGraph();
189
190         /// Check whether converter conv is authorized to be run for elements
191         /// within document doc_fname.
192         /// The check succeeds for safe converters, whilst for those potentially
193         /// able to execute arbitrary code, tagged with the 'needauth' option,
194         /// authorization is: always denied if lyxrc.use_converter_needauth_forbidden
195         /// is enabled; always allowed if the lyxrc.use_converter_needauth
196         /// is disabled; user is prompted otherwise
197         bool checkAuth(Converter const & conv, std::string const & doc_fname);
198
199 private:
200         ///
201         FormatList const
202         intToFormat(std::vector<int> const & input);
203         ///
204         bool scanLog(Buffer const & buffer, std::string const & command,
205                      support::FileName const & filename, ErrorList & errorList);
206         ///
207         bool runLaTeX(Buffer const & buffer, std::string const & command,
208                       OutputParams const &, ErrorList & errorList);
209         ///
210         ConverterList converterlist_;
211         ///
212         trivstring latex_command_;
213         ///
214         trivstring dvilualatex_command_;
215         ///
216         trivstring lualatex_command_;
217         ///
218         trivstring pdflatex_command_;
219         ///
220         trivstring xelatex_command_;
221         /// If \p from = /path/file.ext and \p to = /path2/file2.ext2 then
222         /// this method moves each /path/file*.ext file to /path2/file2*.ext2
223         bool move(std::string const & fmt,
224                   support::FileName const & from, support::FileName const & to,
225                   bool copy);
226         ///
227         Graph G_;
228 };
229
230 /// The global instance.
231 /// Implementation is in LyX.cpp.
232 extern Converters & theConverters();
233
234 /// The global copy after reading lyxrc.defaults.
235 /// Implementation is in LyX.cpp.
236 extern Converters & theSystemConverters();
237
238 } // namespace lyx
239
240 #endif //CONVERTER_H