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"
31 #include "support/Systemcall.h"
41 using namespace lyx::support;
42 using namespace lyx::support::os;
48 void warning(docstring const & title, docstring const & message,
58 // Dummy translation support
60 Messages const & getMessages(std::string const &)
66 Messages const & getGuiMessages()
72 // Keep the linker happy on Windows
77 string const trim(string const & a, char const * p)
84 size_t r = a.find_last_not_of(p);
85 size_t l = a.find_first_not_of(p);
87 // Is this the minimal test? (lgb)
88 if (r == string::npos && l == string::npos)
91 return a.substr(l, r - l + 1);
95 void split(string const & s, vector<string> & result, char delim)
97 //cerr << "split 1: '" << s << "'\n";
100 while (getline(is, t, delim))
102 //cerr << "split 2\n";
106 string join(vector<string> const & input, char const * delim)
109 for (size_t i = 0; i != input.size(); ++i) {
118 char const * const * is_known(string const & str, char const * const * what)
120 for ( ; *what; ++what)
128 // current stack of nested environments
129 vector<string> active_environments;
132 string active_environment()
134 return active_environments.empty() ? string() : active_environments.back();
138 CommandMap known_commands;
139 CommandMap known_environments;
140 CommandMap known_math_environments;
143 void add_known_command(string const & command, string const & o1,
146 // We have to handle the following cases:
147 // definition o1 o2 invocation result
148 // \newcommand{\foo}{bar} "" false \foo bar
149 // \newcommand{\foo}[1]{bar #1} "[1]" false \foo{x} bar x
150 // \newcommand{\foo}[1][]{bar #1} "[1]" true \foo bar
151 // \newcommand{\foo}[1][]{bar #1} "[1]" true \foo[x] bar x
152 // \newcommand{\foo}[1][x]{bar #1} "[1]" true \foo[x] bar x
153 unsigned int nargs = 0;
154 vector<ArgumentType> arguments;
155 string const opt1 = rtrim(ltrim(o1, "["), "]");
156 if (isStrUnsignedInt(opt1)) {
157 // The command has arguments
158 nargs = convert<unsigned int>(opt1);
159 if (nargs > 0 && o2) {
160 // The first argument is optional
161 arguments.push_back(optional);
165 for (unsigned int i = 0; i < nargs; ++i)
166 arguments.push_back(required);
167 known_commands[command] = arguments;
171 bool noweb_mode = false;
172 bool pdflatex = false;
173 bool roundtrip = false;
180 * Read one command definition from the syntax file
182 void read_command(Parser & p, string command, CommandMap & commands)
184 if (p.next_token().asInput() == "*") {
188 vector<ArgumentType> arguments;
189 while (p.next_token().cat() == catBegin ||
190 p.next_token().asInput() == "[") {
191 if (p.next_token().cat() == catBegin) {
192 string const arg = p.getArg('{', '}');
193 if (arg == "translate")
194 arguments.push_back(required);
195 else if (arg == "item")
196 arguments.push_back(item);
198 arguments.push_back(verbatim);
201 arguments.push_back(optional);
204 commands[command] = arguments;
209 * Read a class of environments from the syntax file
211 void read_environment(Parser & p, string const & begin,
212 CommandMap & environments)
216 Token const & t = p.get_token();
217 if (t.cat() == catLetter)
218 environment += t.asInput();
219 else if (!environment.empty()) {
221 read_command(p, environment, environments);
224 if (t.cat() == catEscape && t.asInput() == "\\end") {
225 string const end = p.getArg('{', '}');
234 * Read a list of TeX commands from a reLyX compatible syntax file.
235 * Since this list is used after all commands that have a LyX counterpart
236 * are handled, it does not matter that the "syntax.default" file
237 * has almost all of them listed. For the same reason the reLyX-specific
238 * reLyXre environment is ignored.
240 void read_syntaxfile(FileName const & file_name)
242 ifdocstream is(file_name.toFilesystemEncoding().c_str());
244 cerr << "Could not open syntax file \"" << file_name
245 << "\" for reading." << endl;
248 // We can use our TeX parser, since the syntax of the layout file is
249 // modeled after TeX.
250 // Unknown tokens are just silently ignored, this helps us to skip some
251 // reLyX specific things.
254 Token const & t = p.get_token();
255 if (t.cat() == catEscape) {
256 string const command = t.asInput();
257 if (command == "\\begin") {
258 string const name = p.getArg('{', '}');
259 if (name == "environments" || name == "reLyXre")
260 // We understand "reLyXre", but it is
261 // not as powerful as "environments".
262 read_environment(p, name,
264 else if (name == "mathenvironments")
265 read_environment(p, name,
266 known_math_environments);
268 read_command(p, command, known_commands);
275 string documentclass;
276 string default_encoding;
278 bool overwrite_files = false;
281 /// return the number of arguments consumed
282 typedef int (*cmd_helper)(string const &, string const &);
285 int parse_help(string const &, string const &)
287 cerr << "Usage: tex2lyx [options] infile.tex [outfile.lyx]\n"
289 "\t-c textclass Declare the textclass.\n"
290 "\t-e encoding Set the default encoding (latex name).\n"
291 "\t-f Force overwrite of .lyx files.\n"
292 "\t-help Print this message and quit.\n"
293 "\t-n translate a noweb (aka literate programming) file.\n"
294 "\t-roundtrip re-export created .lyx file infile.lyx.lyx to infile.lyx.tex.\n"
295 "\t-s syntaxfile read additional syntax file.\n"
296 "\t-sysdir dir Set system directory to DIR.\n"
297 "\t-userdir DIR Set user directory to DIR."
303 void error_message(string const & message)
305 cerr << "tex2lyx: " << message << "\n\n";
307 parse_help(string(), string());
311 int parse_class(string const & arg, string const &)
314 error_message("Missing textclass string after -c switch");
320 int parse_encoding(string const & arg, string const &)
323 error_message("Missing encoding string after -e switch");
324 default_encoding = arg;
329 int parse_syntaxfile(string const & arg, string const &)
332 error_message("Missing syntaxfile string after -s switch");
333 syntaxfile = internal_path(arg);
338 // Filled with the command line arguments "foo" of "-sysdir foo" or
340 string cl_system_support;
341 string cl_user_support;
344 int parse_sysdir(string const & arg, string const &)
347 error_message("Missing directory for -sysdir switch");
348 cl_system_support = internal_path(arg);
353 int parse_userdir(string const & arg, string const &)
356 error_message("Missing directory for -userdir switch");
357 cl_user_support = internal_path(arg);
362 int parse_force(string const &, string const &)
364 overwrite_files = true;
369 int parse_noweb(string const &, string const &)
376 int parse_roundtrip(string const &, string const &)
383 void easyParse(int & argc, char * argv[])
385 map<string, cmd_helper> cmdmap;
387 cmdmap["-c"] = parse_class;
388 cmdmap["-e"] = parse_encoding;
389 cmdmap["-f"] = parse_force;
390 cmdmap["-s"] = parse_syntaxfile;
391 cmdmap["-help"] = parse_help;
392 cmdmap["--help"] = parse_help;
393 cmdmap["-n"] = parse_noweb;
394 cmdmap["-sysdir"] = parse_sysdir;
395 cmdmap["-userdir"] = parse_userdir;
396 cmdmap["-roundtrip"] = parse_roundtrip;
398 for (int i = 1; i < argc; ++i) {
399 map<string, cmd_helper>::const_iterator it
400 = cmdmap.find(argv[i]);
402 // don't complain if not found - may be parsed later
403 if (it == cmdmap.end()) {
404 if (argv[i][0] == '-')
405 error_message(string("Unknown option `") + argv[i] + "'.");
410 string arg = (i + 1 < argc) ? os::utf8_argv(i + 1) : string();
411 string arg2 = (i + 2 < argc) ? os::utf8_argv(i + 2) : string();
413 int const remove = 1 + it->second(arg, arg2);
415 // Now, remove used arguments by shifting
416 // the following ones remove places down.
417 os::remove_internal_args(i, remove);
419 for (int j = i; j < argc; ++j)
420 argv[j] = argv[j + remove];
426 // path of the first parsed file
427 string masterFilePath;
428 // path of the currently parsed file
429 string parentFilePath;
431 } // anonymous namespace
434 string getMasterFilePath()
436 return masterFilePath;
439 string getParentFilePath()
441 return parentFilePath;
448 * Reads tex input from \a is and writes lyx output to \a os.
449 * Uses some common settings for the preamble, so this should only
450 * be used more than once for included documents.
451 * Caution: Overwrites the existing preamble settings if the new document
452 * contains a preamble.
453 * You must ensure that \p parentFilePath is properly set before calling
456 void tex2lyx(idocstream & is, ostream & os, string const & encoding)
459 if (!encoding.empty())
460 p.setEncoding(encoding);
464 TeX2LyXDocClass textclass;
465 parse_preamble(p, ss, documentclass, textclass);
467 active_environments.push_back("document");
468 Context context(true, textclass);
469 parse_text(p, ss, FLAG_END, true, context);
471 // Empty document body. LyX needs at least one paragraph.
472 context.check_layout(ss);
473 context.check_end_layout(ss);
474 ss << "\n\\end_body\n\\end_document\n";
475 active_environments.pop_back();
480 ofdocstream parsertest("parsertest.tex");
482 parsertest << p.get_token().asInput();
483 // <origfile> and parsertest.tex should now have identical content
488 /// convert TeX from \p infilename to LyX and write it to \p os
489 bool tex2lyx(FileName const & infilename, ostream & os, string const & encoding)
492 // forbid buffering on this stream
493 is.rdbuf()->pubsetbuf(0,0);
494 is.open(infilename.toFilesystemEncoding().c_str());
496 cerr << "Could not open input file \"" << infilename
497 << "\" for reading." << endl;
500 string const oldParentFilePath = parentFilePath;
501 parentFilePath = onlyPath(infilename.absFileName());
502 tex2lyx(is, os, encoding);
503 parentFilePath = oldParentFilePath;
507 } // anonymous namespace
510 bool tex2lyx(string const & infilename, FileName const & outfilename,
511 string const & encoding)
513 if (outfilename.isReadableFile()) {
514 if (overwrite_files) {
515 cerr << "Overwriting existing file "
516 << outfilename << endl;
518 cerr << "Not overwriting existing file "
519 << outfilename << endl;
523 cerr << "Creating file " << outfilename << endl;
525 ofstream os(outfilename.toFilesystemEncoding().c_str());
527 cerr << "Could not open output file \"" << outfilename
528 << "\" for writing." << endl;
532 cerr << "Input file: " << infilename << "\n";
533 cerr << "Output file: " << outfilename << "\n";
535 return tex2lyx(FileName(infilename), os, encoding);
539 bool tex2tex(string const & infilename, FileName const & outfilename,
540 string const & encoding)
542 if (!tex2lyx(infilename, outfilename, encoding))
544 string command = quoteName(package().lyx_binary().toFilesystemEncoding());
546 command += " -f main";
548 command += " -f none";
550 command += " -e pdflatex ";
552 command += " -e latex ";
553 command += quoteName(outfilename.toFilesystemEncoding());
555 if (one.startscript(Systemcall::Wait, command) == 0)
557 cerr << "Error: Running '" << command << "' failed." << endl;
564 int main(int argc, char * argv[])
568 //setlocale(LC_CTYPE, "");
570 lyxerr.setStream(cerr);
572 os::init(argc, argv);
574 easyParse(argc, argv);
577 error_message("Not enough arguments.");
580 init_package(internal_path(os::utf8_argv(0)),
581 cl_system_support, cl_user_support,
582 top_build_dir_is_two_levels_up);
583 } catch (ExceptionMessage const & message) {
584 cerr << to_utf8(message.title_) << ":\n"
585 << to_utf8(message.details_) << endl;
586 if (message.type_ == ErrorException)
590 // Now every known option is parsed. Look for input and output
591 // file name (the latter is optional).
592 string infilename = internal_path(os::utf8_argv(1));
593 infilename = makeAbsPath(infilename).absFileName();
598 // Do not allow a user supplied output filename
599 // (otherwise it could easily happen that LyX would
600 // overwrite the original .tex file)
601 cerr << "Error: output filename must not be given in roundtrip mode."
605 outfilename = changeExtension(infilename, ".lyx.lyx");
606 } else if (argc > 2) {
607 outfilename = internal_path(os::utf8_argv(2));
608 if (outfilename != "-")
609 outfilename = makeAbsPath(outfilename).absFileName();
611 outfilename = changeExtension(infilename, ".lyx");
613 // Read the syntax tables
614 FileName const system_syntaxfile = libFileSearch("", "syntax.default");
615 if (system_syntaxfile.empty()) {
616 cerr << "Error: Could not find syntax file \"syntax.default\"." << endl;
619 read_syntaxfile(system_syntaxfile);
620 if (!syntaxfile.empty())
621 read_syntaxfile(makeAbsPath(syntaxfile));
623 // Read the encodings table.
624 FileName const symbols_path = libFileSearch(string(), "unicodesymbols");
625 if (symbols_path.empty()) {
626 cerr << "Error: Could not find file \"unicodesymbols\"."
630 FileName const enc_path = libFileSearch(string(), "encodings");
631 if (enc_path.empty()) {
632 cerr << "Error: Could not find file \"encodings\"."
636 encodings.read(enc_path, symbols_path);
637 if (!default_encoding.empty() && !encodings.fromLaTeXName(default_encoding))
638 error_message("Unknown LaTeX encoding `" + default_encoding + "'");
640 // The real work now.
641 masterFilePath = onlyPath(infilename);
642 parentFilePath = masterFilePath;
643 if (outfilename == "-") {
644 if (tex2lyx(FileName(infilename), cout, default_encoding))
646 } else if (roundtrip) {
647 if (tex2tex(infilename, FileName(outfilename), default_encoding))
650 if (tex2lyx(infilename, FileName(outfilename), default_encoding))