]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.h
Merge branch 'master' of git.lyx.org:lyx
[lyx.git] / src / tex2lyx / tex2lyx.h
1 // -*- C++ -*-
2 /**
3  * \file tex2lyx.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  * \author Jean-Marc Lasgouttes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef TEX2LYX_H
14 #define TEX2LYX_H
15
16 #include "Parser.h"
17 #include "TextClass.h"
18
19 #include <iosfwd>
20 #include <string>
21 #include <vector>
22 #include <map>
23
24
25 namespace lyx {
26
27 namespace support { class FileName; }
28
29 class Context;
30
31 /// A trivial subclass, just to give us a public default constructor
32 class TeX2LyXDocClass : public DocumentClass
33 {
34 public:
35         void setName(std::string const & name) { name_ = name; }
36 };
37
38 /// Translate babel language name to LyX language name
39 extern std::string babel2lyx(std::string const & language);
40 /// Translate LyX language name to babel language name
41 extern std::string lyx2babel(std::string const & language);
42 /// Translate polyglossia language name to LyX language name
43 extern std::string polyglossia2lyx(std::string const & language);
44 /// Translate basic color name or RGB color in LaTeX syntax to LyX color code
45 extern std::string rgbcolor2code(std::string const & name);
46
47 /// in text.cpp
48 std::string translate_len(std::string const &);
49
50 void parse_text(Parser & p, std::ostream & os, unsigned flags, bool outer,
51                 Context & context);
52
53 /*!
54  * Parses a subdocument, usually useful in insets (whence the name).
55  *
56  * It ignores \c context.need_layout and \c context.need_end_layout and
57  * starts and ends always a new layout.
58  * Therefore this may only be used to parse text in insets or table cells.
59  */
60 void parse_text_in_inset(Parser & p, std::ostream & os, unsigned flags,
61                          bool outer, Context const & context,
62                          InsetLayout const * layout = 0);
63
64 /// Guess document language from \p p if CJK is used.
65 /// \p lang is used for all non-CJK contents.
66 std::string guessLanguage(Parser & p, std::string const & lang);
67
68
69 /// in math.cpp
70 void parse_math(Parser & p, std::ostream & os, unsigned flags, mode_type mode);
71
72
73 /// in table.cpp
74 void handle_tabular(Parser & p, std::ostream & os, std::string const & name,
75                     std::string const & width, Context & context);
76
77
78 /// in tex2lyx.cpp
79 std::string const trimSpaceAndEol(std::string const & a);
80
81 void split(std::string const & s, std::vector<std::string> & result,
82         char delim = ',');
83 std::string join(std::vector<std::string> const & input,
84         char const * delim);
85
86 bool is_math_env(std::string const & name);
87 bool is_display_math_env(std::string const & name);
88 /// Is first string in the array of strings (second parameter)
89 char const * const * is_known(std::string const &, char const * const *);
90
91 /*!
92  * Adds the command \p command to the list of known commands.
93  * \param o1 first optional parameter to the latex command \\newcommand
94  * (with brackets), or the empty string if there were no optional arguments.
95  * \param o2 wether \\newcommand had a second optional parameter.
96  * If \p definition is not empty the command is assumed to be from the LyX
97  * preamble and added to possible_textclass_commands.
98  */
99 void add_known_command(std::string const & command, std::string const & o1,
100         bool o2, docstring const & definition = docstring());
101 extern void add_known_environment(std::string const & environment,
102         std::string const & o1, bool o2, docstring const & beg,
103         docstring const & end);
104 extern void add_known_theorem(std::string const & theorem,
105         std::string const & o1, bool o2, docstring const & definition);
106 extern Layout const * findLayoutWithoutModule(TextClass const & textclass,
107         std::string const & name, bool command);
108 extern InsetLayout const * findInsetLayoutWithoutModule(
109         TextClass const & textclass, std::string const & name, bool command);
110 /*!
111  * Check whether a module provides command (if \p command is true) or
112  * environment (if \p command is false) \p name, and add the module to the
113  * list of used modules if yes.
114  */
115 extern bool checkModule(std::string const & name, bool command);
116 /// Is this feature already provided e.g. by the document class?
117 extern bool isProvided(std::string const & name);
118 // Access to environment stack
119 extern std::vector<std::string> active_environments;
120 std::string active_environment();
121
122 enum ArgumentType {
123         required,
124         req_group,
125         verbatim,
126         item,
127         optional,
128         opt_group,
129         displaymath,
130 };
131
132 class FullCommand {
133 public:
134         FullCommand() {}
135         FullCommand(std::vector<ArgumentType> const & a, docstring const & d)
136                 : args(a), def(d) {}
137         std::vector<ArgumentType> args;
138         docstring def;
139 };
140
141 class FullEnvironment {
142 public:
143         FullEnvironment() {}
144         FullEnvironment(std::vector<ArgumentType> const & a,
145                         docstring const & b, docstring const & e)
146                 : args(a), beg(b), end(e) {}
147         std::vector<ArgumentType> args;
148         docstring beg;
149         docstring end;
150 };
151
152 typedef std::map<std::string, std::vector<ArgumentType> > CommandMap;
153 typedef std::map<std::string, FullCommand> FullCommandMap;
154 typedef std::map<std::string, FullEnvironment> FullEnvironmentMap;
155
156 /// Known TeX commands with arguments that get parsed into ERT.
157 extern CommandMap known_commands;
158 /// Known TeX environments with arguments that get parsed into ERT.
159 extern CommandMap known_environments;
160 /// Known TeX math environments with arguments that get parsed into LyX mathed.
161 extern CommandMap known_math_environments;
162 /// Commands that might be defined by the document class or modules
163 extern FullCommandMap possible_textclass_commands;
164 /// Environments that might be defined by the document class or modules
165 extern FullEnvironmentMap possible_textclass_environments;
166 /// Theorems that might be defined by the document class or modules
167 extern FullCommandMap possible_textclass_theorems;
168 ///
169 extern bool noweb_mode;
170 /// Did we recognize any pdflatex-only construct?
171 extern bool pdflatex;
172 /// Did we recognize any xetex-only construct?
173 extern bool xetex;
174 /// Do we have non-CJK Japanese?
175 extern bool is_nonCJKJapanese;
176 /// LyX format that is created by tex2lyx
177 extern int const LYX_FORMAT;
178
179 /// Absolute path of the master .lyx or .tex file
180 extern std::string getMasterFilePath(bool input);
181 /// Absolute path of the currently processed .lyx or .tex file
182 extern std::string getParentFilePath(bool input);
183 /// Is it allowed to overwrite existing files?
184 extern bool overwriteFiles();
185 /// Do we need to copy included files to the output directory?
186 extern bool copyFiles();
187 /// Shall we skip child documents and keep them as TeX?
188 extern bool skipChildren();
189 /// Does tex2lyx run in roundtrip mode?
190 extern bool roundtripMode();
191
192
193 /*!
194  *  Reads tex input from \a infilename and writes lyx output to \a outfilename.
195  *  The iconv name of the encoding can be provided as \a encoding.
196  *  Uses some common settings for the preamble, so this should only
197  *  be used more than once for included documents.
198  *  Caution: Overwrites the existing preamble settings if the new document
199  *  contains a preamble.
200  *  \return true if the conversion was successful, else false.
201  */
202 bool tex2lyx(std::string const & infilename,
203              support::FileName const & outfilename,
204              std::string const & encoding);
205
206
207 } // namespace lyx
208
209 #endif