3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
19 #include "lyxtextclass.h"
21 #include "support/convert.h"
22 #include "support/filetools.h"
23 #include "support/fs_extras.h"
24 #include "support/lstrings.h"
25 #include "support/lyxlib.h"
26 #include "support/os.h"
27 #include "support/package.h"
29 #include <boost/function.hpp>
30 #include <boost/filesystem/operations.hpp>
47 using std::istringstream;
48 using std::ostringstream;
49 using std::stringstream;
54 using lyx::support::isStrUnsignedInt;
55 using lyx::support::ltrim;
56 using lyx::support::rtrim;
57 using lyx::support::IsFileReadable;
59 namespace fs = boost::filesystem;
62 // Hacks to allow the thing to link in the lyxlayout stuff
63 LyXErr lyxerr(std::cerr.rdbuf());
66 string const trim(string const & a, char const * p)
73 string::size_type r = a.find_last_not_of(p);
74 string::size_type l = a.find_first_not_of(p);
76 // Is this the minimal test? (lgb)
77 if (r == string::npos && l == string::npos)
80 return a.substr(l, r - l + 1);
84 void split(string const & s, vector<string> & result, char delim)
86 //cerr << "split 1: '" << s << "'\n";
89 while (getline(is, t, delim))
91 //cerr << "split 2\n";
95 string join(vector<string> const & input, char const * delim)
98 for (size_t i = 0; i < input.size(); ++i) {
107 char const * const * is_known(string const & str, char const * const * what)
109 for ( ; *what; ++what)
117 // current stack of nested environments
118 vector<string> active_environments;
121 string active_environment()
123 return active_environments.empty() ? string() : active_environments.back();
127 map<string, vector<ArgumentType> > known_commands;
130 void add_known_command(string const & command, string const & o1,
133 // We have to handle the following cases:
134 // definition o1 o2 invocation result
135 // \newcommand{\foo}{bar} "" false \foo bar
136 // \newcommand{\foo}[1]{bar #1} "[1]" false \foo{x} bar x
137 // \newcommand{\foo}[1][]{bar #1} "[1]" true \foo bar
138 // \newcommand{\foo}[1][]{bar #1} "[1]" true \foo[x] bar x
139 // \newcommand{\foo}[1][x]{bar #1} "[1]" true \foo[x] bar x
140 unsigned int nargs = 0;
141 vector<ArgumentType> arguments;
142 string const opt1 = rtrim(ltrim(o1, "["), "]");
143 if (isStrUnsignedInt(opt1)) {
144 // The command has arguments
145 nargs = convert<unsigned int>(opt1);
146 if (nargs > 0 && o2) {
147 // The first argument is optional
148 arguments.push_back(optional);
152 for (unsigned int i = 0; i < nargs; ++i)
153 arguments.push_back(required);
154 known_commands[command] = arguments;
162 * Read a list of TeX commands from a reLyX compatible syntax file.
163 * Since this list is used after all commands that have a LyX counterpart
164 * are handled, it does not matter that the "syntax.default" file from reLyX
165 * has almost all of them listed. For the same reason the reLyX-specific
166 * reLyXre environment is ignored.
168 void read_syntaxfile(string const & file_name)
170 if (!IsFileReadable(file_name)) {
171 cerr << "Could not open syntax file \"" << file_name
172 << "\" for reading." << endl;
175 ifstream is(file_name.c_str());
176 // We can use our TeX parser, since the syntax of the layout file is
177 // modeled after TeX.
178 // Unknown tokens are just silently ignored, this helps us to skip some
179 // reLyX specific things.
182 Token const & t = p.get_token();
183 if (t.cat() == catEscape) {
184 string command = t.asInput();
185 if (p.next_token().asInput() == "*") {
190 vector<ArgumentType> arguments;
191 while (p.next_token().cat() == catBegin ||
192 p.next_token().asInput() == "[") {
193 if (p.next_token().cat() == catBegin) {
194 string const arg = p.getArg('{', '}');
195 if (arg == "translate")
196 arguments.push_back(required);
198 arguments.push_back(verbatim);
201 arguments.push_back(optional);
204 known_commands[command] = arguments;
210 string documentclass;
212 bool overwrite_files = false;
215 /// return the number of arguments consumed
216 typedef boost::function<int(string const &, string const &)> cmd_helper;
219 int parse_help(string const &, string const &)
221 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
222 "Command line switches (case sensitive):\n"
223 "\t-help summarize tex2lyx usage\n"
224 "\t-f Force creation of .lyx files even if they exist already\n"
225 "\t-userdir dir try to set user directory to dir\n"
226 "\t-sysdir dir try to set system directory to dir\n"
227 "\t-c textclass declare the textclass\n"
228 "\t-s syntaxfile read additional syntax file" << endl;
233 int parse_class(string const & arg, string const &)
236 cerr << "Missing textclass string after -c switch" << endl;
244 int parse_syntaxfile(string const & arg, string const &)
247 cerr << "Missing syntaxfile string after -s switch" << endl;
255 // Filled with the command line arguments "foo" of "-sysdir foo" or
257 string cl_system_support;
258 string cl_user_support;
261 int parse_sysdir(string const & arg, string const &)
264 cerr << "Missing directory for -sysdir switch" << endl;
267 cl_system_support = arg;
272 int parse_userdir(string const & arg, string const &)
275 cerr << "Missing directory for -userdir switch" << endl;
278 cl_user_support = arg;
283 int parse_force(string const &, string const &)
285 overwrite_files = true;
290 void easyParse(int & argc, char * argv[])
292 map<string, cmd_helper> cmdmap;
294 cmdmap["-c"] = parse_class;
295 cmdmap["-f"] = parse_force;
296 cmdmap["-s"] = parse_syntaxfile;
297 cmdmap["-help"] = parse_help;
298 cmdmap["--help"] = parse_help;
299 cmdmap["-sysdir"] = parse_sysdir;
300 cmdmap["-userdir"] = parse_userdir;
302 for (int i = 1; i < argc; ++i) {
303 std::map<string, cmd_helper>::const_iterator it
304 = cmdmap.find(argv[i]);
306 // don't complain if not found - may be parsed later
307 if (it == cmdmap.end())
310 string arg((i + 1 < argc) ? argv[i + 1] : "");
311 string arg2((i + 2 < argc) ? argv[i + 2] : "");
313 int const remove = 1 + it->second(arg, arg2);
315 // Now, remove used arguments by shifting
316 // the following ones remove places down.
318 for (int j = i; j < argc; ++j)
319 argv[j] = argv[j + remove];
325 // path of the parsed file
326 string masterFilePath;
328 } // anonymous namespace
331 string getMasterFilePath()
333 return masterFilePath;
337 void tex2lyx(std::istream &is, std::ostream &os)
343 LyXTextClass textclass = parse_preamble(p, ss, documentclass);
345 active_environments.push_back("document");
346 Context context(true, textclass);
347 parse_text(p, ss, FLAG_END, true, context);
348 context.check_end_layout(ss);
349 ss << "\n\\end_body\n\\end_document\n";
350 active_environments.pop_back();
355 ofstream parsertest("parsertest.tex");
357 parsertest << p.get_token().asInput();
358 // <origfile> and parsertest.tex should now have identical content
363 bool tex2lyx(string const &infilename, string const &outfilename)
365 if (!(IsFileReadable(infilename) && fs::is_writable(outfilename))) {
368 if (!overwrite_files && IsFileReadable(outfilename)) {
369 cerr << "Not overwriting existing file " << outfilename << "\n";
372 ifstream is(infilename.c_str());
373 ofstream os(outfilename.c_str());
375 cerr << "File: " << infilename << "\n";
382 int main(int argc, char * argv[])
384 easyParse(argc, argv);
387 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
388 "See tex2lyx -help." << endl;
392 lyx::support::os::init(argc, argv);
393 lyx::support::init_package(argv[0], cl_system_support, cl_user_support);
395 string const system_syntaxfile = lyx::support::LibFileSearch("reLyX", "syntax.default");
396 if (system_syntaxfile.empty()) {
397 cerr << "Error: Could not find syntax file \"syntax.default\"." << endl;
400 read_syntaxfile(system_syntaxfile);
401 if (!syntaxfile.empty())
402 read_syntaxfile(syntaxfile);
404 if (!IsFileReadable(argv[1])) {
405 cerr << "Could not open input file \"" << argv[1]
406 << "\" for reading." << endl;
410 if (lyx::support::AbsolutePath(argv[1]))
411 masterFilePath = lyx::support::OnlyPath(argv[1]);
413 masterFilePath = lyx::support::getcwd();
415 ifstream is(argv[1]);