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.
20 #include "TextClass.h"
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/ExceptionMessage.h"
25 #include "support/filetools.h"
26 #include "support/lassert.h"
27 #include "support/lstrings.h"
28 #include "support/Messages.h"
29 #include "support/os.h"
30 #include "support/Package.h"
40 using namespace lyx::support;
41 using namespace lyx::support::os;
45 // Dummy translation support
47 Messages & getMessages(std::string const &)
53 Messages & getGuiMessages()
59 string const trim(string const & a, char const * p)
66 size_t r = a.find_last_not_of(p);
67 size_t l = a.find_first_not_of(p);
69 // Is this the minimal test? (lgb)
70 if (r == string::npos && l == string::npos)
73 return a.substr(l, r - l + 1);
77 void split(string const & s, vector<string> & result, char delim)
79 //cerr << "split 1: '" << s << "'\n";
82 while (getline(is, t, delim))
84 //cerr << "split 2\n";
88 string join(vector<string> const & input, char const * delim)
91 for (size_t i = 0; i != input.size(); ++i) {
100 char const * const * is_known(string const & str, char const * const * what)
102 for ( ; *what; ++what)
110 // current stack of nested environments
111 vector<string> active_environments;
114 string active_environment()
116 return active_environments.empty() ? string() : active_environments.back();
120 CommandMap known_commands;
121 CommandMap known_environments;
122 CommandMap known_math_environments;
125 void add_known_command(string const & command, string const & o1,
128 // We have to handle the following cases:
129 // definition o1 o2 invocation result
130 // \newcommand{\foo}{bar} "" false \foo bar
131 // \newcommand{\foo}[1]{bar #1} "[1]" false \foo{x} bar x
132 // \newcommand{\foo}[1][]{bar #1} "[1]" true \foo bar
133 // \newcommand{\foo}[1][]{bar #1} "[1]" true \foo[x] bar x
134 // \newcommand{\foo}[1][x]{bar #1} "[1]" true \foo[x] bar x
135 unsigned int nargs = 0;
136 vector<ArgumentType> arguments;
137 string const opt1 = rtrim(ltrim(o1, "["), "]");
138 if (isStrUnsignedInt(opt1)) {
139 // The command has arguments
140 nargs = convert<unsigned int>(opt1);
141 if (nargs > 0 && o2) {
142 // The first argument is optional
143 arguments.push_back(optional);
147 for (unsigned int i = 0; i < nargs; ++i)
148 arguments.push_back(required);
149 known_commands[command] = arguments;
153 bool noweb_mode = false;
160 * Read one command definition from the syntax file
162 void read_command(Parser & p, string command, CommandMap & commands)
164 if (p.next_token().asInput() == "*") {
168 vector<ArgumentType> arguments;
169 while (p.next_token().cat() == catBegin ||
170 p.next_token().asInput() == "[") {
171 if (p.next_token().cat() == catBegin) {
172 string const arg = p.getArg('{', '}');
173 if (arg == "translate")
174 arguments.push_back(required);
176 arguments.push_back(verbatim);
179 arguments.push_back(optional);
182 commands[command] = arguments;
187 * Read a class of environments from the syntax file
189 void read_environment(Parser & p, string const & begin,
190 CommandMap & environments)
194 Token const & t = p.get_token();
195 if (t.cat() == catLetter)
196 environment += t.asInput();
197 else if (!environment.empty()) {
199 read_command(p, environment, environments);
202 if (t.cat() == catEscape && t.asInput() == "\\end") {
203 string const end = p.getArg('{', '}');
212 * Read a list of TeX commands from a reLyX compatible syntax file.
213 * Since this list is used after all commands that have a LyX counterpart
214 * are handled, it does not matter that the "syntax.default" file
215 * has almost all of them listed. For the same reason the reLyX-specific
216 * reLyXre environment is ignored.
218 void read_syntaxfile(FileName const & file_name)
220 ifdocstream is(file_name.toFilesystemEncoding().c_str());
222 cerr << "Could not open syntax file \"" << file_name
223 << "\" for reading." << endl;
226 // We can use our TeX parser, since the syntax of the layout file is
227 // modeled after TeX.
228 // Unknown tokens are just silently ignored, this helps us to skip some
229 // reLyX specific things.
232 Token const & t = p.get_token();
233 if (t.cat() == catEscape) {
234 string const command = t.asInput();
235 if (command == "\\begin") {
236 string const name = p.getArg('{', '}');
237 if (name == "environments" || name == "reLyXre")
238 // We understand "reLyXre", but it is
239 // not as powerful as "environments".
240 read_environment(p, name,
242 else if (name == "mathenvironments")
243 read_environment(p, name,
244 known_math_environments);
246 read_command(p, command, known_commands);
253 string documentclass;
254 string default_encoding;
256 bool overwrite_files = false;
259 /// return the number of arguments consumed
260 typedef int (*cmd_helper)(string const &, string const &);
263 int parse_help(string const &, string const &)
265 cerr << "Usage: tex2lyx [options] infile.tex [outfile.lyx]\n"
267 "\t-c textclass Declare the textclass.\n"
268 "\t-e encoding Set the default encoding (latex name).\n"
269 "\t-f Force overwrite of .lyx files.\n"
270 "\t-help Print this message and quit.\n"
271 "\t-n translate a noweb (aka literate programming) file.\n"
272 "\t-s syntaxfile read additional syntax file.\n"
273 "\t-sysdir dir Set system directory to DIR.\n"
274 "\t-userdir DIR Set user directory to DIR."
280 void error_message(string const & message)
282 cerr << "tex2lyx: " << message << "\n\n";
284 parse_help(string(), string());
288 int parse_class(string const & arg, string const &)
291 error_message("Missing textclass string after -c switch");
297 int parse_encoding(string const & arg, string const &)
300 error_message("Missing encoding string after -e switch");
301 default_encoding = arg;
306 int parse_syntaxfile(string const & arg, string const &)
309 error_message("Missing syntaxfile string after -s switch");
310 syntaxfile = internal_path(arg);
315 // Filled with the command line arguments "foo" of "-sysdir foo" or
317 string cl_system_support;
318 string cl_user_support;
321 int parse_sysdir(string const & arg, string const &)
324 error_message("Missing directory for -sysdir switch");
325 cl_system_support = internal_path(arg);
330 int parse_userdir(string const & arg, string const &)
333 error_message("Missing directory for -userdir switch");
334 cl_user_support = internal_path(arg);
339 int parse_force(string const &, string const &)
341 overwrite_files = true;
346 int parse_noweb(string const &, string const &)
353 void easyParse(int & argc, char * argv[])
355 map<string, cmd_helper> cmdmap;
357 cmdmap["-c"] = parse_class;
358 cmdmap["-e"] = parse_encoding;
359 cmdmap["-f"] = parse_force;
360 cmdmap["-s"] = parse_syntaxfile;
361 cmdmap["-help"] = parse_help;
362 cmdmap["--help"] = parse_help;
363 cmdmap["-n"] = parse_noweb;
364 cmdmap["-sysdir"] = parse_sysdir;
365 cmdmap["-userdir"] = parse_userdir;
367 for (int i = 1; i < argc; ++i) {
368 map<string, cmd_helper>::const_iterator it
369 = cmdmap.find(argv[i]);
371 // don't complain if not found - may be parsed later
372 if (it == cmdmap.end()) {
373 if (argv[i][0] == '-')
374 error_message(string("Unknown option `") + argv[i] + "'.");
379 string arg(to_utf8(from_local8bit((i + 1 < argc) ? argv[i + 1] : "")));
380 string arg2(to_utf8(from_local8bit((i + 2 < argc) ? argv[i + 2] : "")));
382 int const remove = 1 + it->second(arg, arg2);
384 // Now, remove used arguments by shifting
385 // the following ones remove places down.
387 for (int j = i; j < argc; ++j)
388 argv[j] = argv[j + remove];
394 // path of the first parsed file
395 string masterFilePath;
396 // path of the currently parsed file
397 string parentFilePath;
399 } // anonymous namespace
402 string getMasterFilePath()
404 return masterFilePath;
407 string getParentFilePath()
409 return parentFilePath;
416 * Reads tex input from \a is and writes lyx output to \a os.
417 * Uses some common settings for the preamble, so this should only
418 * be used more than once for included documents.
419 * Caution: Overwrites the existing preamble settings if the new document
420 * contains a preamble.
421 * You must ensure that \p parentFilePath is properly set before calling
424 void tex2lyx(idocstream & is, ostream & os, string const & encoding)
427 if (!encoding.empty())
428 p.setEncoding(encoding);
432 TeX2LyXDocClass textclass;
433 parse_preamble(p, ss, documentclass, textclass);
435 active_environments.push_back("document");
436 Context context(true, textclass);
437 parse_text(p, ss, FLAG_END, true, context);
439 // Empty document body. LyX needs at least one paragraph.
440 context.check_layout(ss);
441 context.check_end_layout(ss);
442 ss << "\n\\end_body\n\\end_document\n";
443 active_environments.pop_back();
448 ofdocstream parsertest("parsertest.tex");
450 parsertest << p.get_token().asInput();
451 // <origfile> and parsertest.tex should now have identical content
456 /// convert TeX from \p infilename to LyX and write it to \p os
457 bool tex2lyx(FileName const & infilename, ostream & os, string const & encoding)
460 // forbid buffering on this stream
461 is.rdbuf()->pubsetbuf(0,0);
462 is.open(infilename.toFilesystemEncoding().c_str());
464 cerr << "Could not open input file \"" << infilename
465 << "\" for reading." << endl;
468 string const oldParentFilePath = parentFilePath;
469 parentFilePath = onlyPath(infilename.absFilename());
470 tex2lyx(is, os, encoding);
471 parentFilePath = oldParentFilePath;
475 } // anonymous namespace
478 bool tex2lyx(string const & infilename, FileName const & outfilename,
479 string const & encoding)
481 if (outfilename.isReadableFile()) {
482 if (overwrite_files) {
483 cerr << "Overwriting existing file "
484 << outfilename << endl;
486 cerr << "Not overwriting existing file "
487 << outfilename << endl;
491 cerr << "Creating file " << outfilename << endl;
493 ofstream os(outfilename.toFilesystemEncoding().c_str());
495 cerr << "Could not open output file \"" << outfilename
496 << "\" for writing." << endl;
500 cerr << "Input file: " << infilename << "\n";
501 cerr << "Output file: " << outfilename << "\n";
503 return tex2lyx(FileName(infilename), os, encoding);
509 int main(int argc, char * argv[])
513 //setlocale(LC_CTYPE, "");
515 lyxerr.setStream(cerr);
517 easyParse(argc, argv);
520 error_message("Not enough arguments.");
521 os::init(argc, argv);
524 init_package(internal_path(to_utf8(from_local8bit(argv[0]))),
525 cl_system_support, cl_user_support,
526 top_build_dir_is_two_levels_up);
527 } catch (ExceptionMessage const & message) {
528 cerr << to_utf8(message.title_) << ":\n"
529 << to_utf8(message.details_) << endl;
530 if (message.type_ == ErrorException)
534 // Now every known option is parsed. Look for input and output
535 // file name (the latter is optional).
536 string infilename = internal_path(to_utf8(from_local8bit(argv[1])));
537 infilename = makeAbsPath(infilename).absFilename();
541 outfilename = internal_path(to_utf8(from_local8bit(argv[2])));
542 if (outfilename != "-")
543 outfilename = makeAbsPath(outfilename).absFilename();
545 outfilename = changeExtension(infilename, ".lyx");
547 // Read the syntax tables
548 FileName const system_syntaxfile = libFileSearch("", "syntax.default");
549 if (system_syntaxfile.empty()) {
550 cerr << "Error: Could not find syntax file \"syntax.default\"." << endl;
553 read_syntaxfile(system_syntaxfile);
554 if (!syntaxfile.empty())
555 read_syntaxfile(makeAbsPath(syntaxfile));
557 // Read the encodings table.
558 FileName const symbols_path = libFileSearch(string(), "unicodesymbols");
559 if (symbols_path.empty()) {
560 cerr << "Error: Could not find file \"unicodesymbols\"."
564 FileName const enc_path = libFileSearch(string(), "encodings");
565 if (enc_path.empty()) {
566 cerr << "Error: Could not find file \"encodings\"."
570 encodings.read(enc_path, symbols_path);
571 if (!default_encoding.empty() && !encodings.fromLaTeXName(default_encoding))
572 error_message("Unknown LaTeX encoding `" + default_encoding + "'");
574 // The real work now.
575 masterFilePath = onlyPath(infilename);
576 parentFilePath = masterFilePath;
577 if (outfilename == "-") {
578 if (tex2lyx(FileName(infilename), cout, default_encoding))
583 if (tex2lyx(infilename, FileName(outfilename), default_encoding))