1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
18 #include "texparser.h"
28 using std::istringstream;
30 using std::ostringstream;
38 char const OPEN = '<';
39 char const CLOSE = '>';
41 const char * known_languages[] = { "austrian", "babel", "bahasa",
42 "basque", "breton", "bulgarian", "catalan", "croatian", "czech", "danish",
43 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
44 "frenchb", "galician", "germanb", "greek", "hebcal", "hebfont", "hebrew",
45 "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen", "icelandic",
46 "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss", "lgrcmtt",
47 "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr", "lhecmss",
48 "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
49 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
50 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
51 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
52 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
54 const char * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
57 const char * known_math_envs[] = {"equation", "eqnarray", "eqnarray*",
58 "align", "align*", 0};
62 ostringstream h_preamble;
63 string h_textclass = "FIXME";
64 string h_options = "FIXME";
65 string h_language = "FIXME";
66 string h_inputencoding = "FIXME";
67 string h_fontscheme = "FIXME";
68 string h_graphics = "default";
69 string h_paperfontsize = "FIXME";
70 string h_spacing = "single";
71 string h_papersize = "FIXME";
72 string h_paperpackage = "FIXME";
73 string h_use_geometry = "0";
74 string h_use_amsmath = "0";
75 string h_use_natbib = "0";
76 string h_use_numerical_citations = "0";
77 string h_paperorientation = "portrait";
78 string h_secnumdepth = "3";
79 string h_tocdepth = "3";
80 string h_paragraph_separation = "indent";
81 string h_defskip = "medskip";
82 string h_quotes_language = "FIXME";
83 string h_quotes_times = "1";
84 string h_papercolumns = "1";
85 string h_papersides = "1";
86 string h_paperpagestyle = "default";
87 string h_tracking_changes = "0";
89 // indicates whether we are in the preamble
90 bool in_preamble = true;
92 // current stack of nested environments
93 stack<string> active_environments;
104 string const trim(string const & a, char const * p = " ")
108 if (a.empty() || !*p)
111 string::size_type r = a.find_last_not_of(p);
112 string::size_type l = a.find_first_not_of(p);
114 // Is this the minimal test? (lgb)
115 if (r == string::npos && l == string::npos)
118 return a.substr(l, r - l + 1);
122 void split(string const & s, vector<string> & result, char delim)
126 while (getline(is, t, delim))
131 string join(vector<string> const & input, char delim)
134 for (size_t i = 0; i != input.size(); ++i) {
143 void handle_opt(vector<string> & opts, char const ** what, string & target)
148 for ( ; what; ++what) {
149 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
150 if (it != opts.end()) {
151 //cerr << "### found option '" << *what << "'\n";
160 bool is_math_env(string const & name)
162 for (char const ** what = known_math_envs; *what; ++what)
169 bool is_heading(string const & name)
175 name == "paragraph" ||
177 name == "chapter*" ||
179 name == "section*" ||
180 name == "subsection" ||
181 name == "subsection*" ||
182 name == "subsubsection" ||
183 name == "subsubsection*";
187 bool is_latex_command(string const & name)
194 name == "printindex";
198 void begin_inset(ostream & os, string const & name)
200 os << "\n\\begin_inset " << name;
204 void end_inset(ostream & os)
206 os << "\n\\end_inset\n";
212 return active_environments.empty() ? string() : active_environments.top();
216 void handle_ert(ostream & os, string const & s)
218 begin_inset(os, "ERT");
219 os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
220 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
222 os << "\n\\backslash\n";
230 void handle_par(ostream & os)
232 if (active_environments.empty())
235 string s = curr_env();
236 if (s == "document") {
237 os << "Standard\n\n";
240 if (s == "lyxcode") {
241 os << "LyX-Code\n\n";
244 os << cap(s) << "\n\n";
248 void handle_package(string const & name, string const & options)
250 if (name == "a4wide") {
251 h_papersize = "a4paper";
252 h_paperpackage = "widemarginsa4";
253 } else if (name == "ae")
255 else if (name == "aecompl")
257 else if (name == "amsmath")
259 else if (name == "amssymb")
261 else if (name == "babel")
263 else if (name == "fontenc")
265 else if (name == "inputenc")
266 h_inputencoding = options;
267 else if (name == "makeidx")
269 else if (name == "verbatim")
273 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
275 h_preamble << "\\usepackage{" << name << "}\n";
280 void handle_table(Parser &, ostream &)
282 // \begin{table} has been read
287 string wrap(string const & cmd, string const & str)
289 return OPEN + cmd + ' ' + str + CLOSE;
293 void end_preamble(ostream & os)
296 os << "# tex2lyx 0.0.2 created this file\n"
297 << "\\lyxformat 222\n"
298 << "\\textclass " << h_textclass << "\n"
299 << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n"
300 << "\\options " << h_options << "\n"
301 << "\\language " << h_language << "\n"
302 << "\\inputencoding " << h_inputencoding << "\n"
303 << "\\fontscheme " << h_fontscheme << "\n"
304 << "\\graphics " << h_graphics << "\n"
305 << "\\paperfontsize " << h_paperfontsize << "\n"
306 << "\\spacing " << h_spacing << "\n"
307 << "\\papersize " << h_papersize << "\n"
308 << "\\paperpackage " << h_paperpackage << "\n"
309 << "\\use_geometry " << h_use_geometry << "\n"
310 << "\\use_amsmath " << h_use_amsmath << "\n"
311 << "\\use_natbib " << h_use_natbib << "\n"
312 << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
313 << "\\paperorientation " << h_paperorientation << "\n"
314 << "\\secnumdepth " << h_secnumdepth << "\n"
315 << "\\tocdepth " << h_tocdepth << "\n"
316 << "\\paragraph_separation " << h_paragraph_separation << "\n"
317 << "\\defskip " << h_defskip << "\n"
318 << "\\quotes_language " << h_quotes_language << "\n"
319 << "\\quotes_times " << h_quotes_times << "\n"
320 << "\\papercolumns " << h_papercolumns << "\n"
321 << "\\papersides " << h_papersides << "\n"
322 << "\\paperpagestyle " << h_paperpagestyle << "\n"
323 << "\\tracking_changes " << h_tracking_changes << "\n";
327 void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
330 Token const & t = p.getToken();
333 cerr << "t: " << t << " flags: " << flags << "\n";
337 if (flags & FLAG_ITEM) {
338 if (t.cat() == catSpace)
342 if (t.cat() == catBegin) {
343 // skip the brace and collect everything to the next matching
345 flags |= FLAG_BRACE_LAST;
349 // handle only this single token, leave the loop if done
354 if (flags & FLAG_BRACED) {
355 if (t.cat() == catSpace)
358 if (t.cat() != catBegin) {
359 p.error("opening brace expected");
363 // skip the brace and collect everything to the next matching
365 flags = FLAG_BRACE_LAST;
369 if (flags & FLAG_OPTION) {
370 if (t.cat() == catOther && t.character() == '[') {
371 parse(p, os, FLAG_BRACK_LAST, mode);
373 // no option found, put back token and we are done
382 if (t.cat() == catMath) {
383 if (mode == TEXT_MODE || mode == MATHTEXT_MODE) {
384 // we are inside some text mode thingy, so opening new math is allowed
385 if (mode == TEXT_MODE)
386 begin_inset(os, "Formula ");
387 Token const & n = p.getToken();
388 if (n.cat() == catMath) {
389 // TeX's $$...$$ syntax for displayed math
391 parse(p, os, FLAG_SIMPLE, MATH_MODE);
393 p.getToken(); // skip the second '$' token
395 // simple $...$ stuff
398 parse(p, os, FLAG_SIMPLE, MATH_MODE);
401 if (mode == TEXT_MODE)
405 else if (flags & FLAG_SIMPLE) {
406 // this is the end of the formula
411 cerr << "mode: " << mode << endl;
412 p.error("something strange in the parser\n");
417 else if (t.cat() == catLetter)
420 else if (t.cat() == catSpace)
423 else if (t.cat() == catNewline)
426 else if (t.cat() == catSuper)
429 else if (t.cat() == catSub)
432 else if (t.cat() == catParameter) {
433 Token const & n = p.getToken();
434 os << wrap("macroarg", string(1, n.character()));
437 else if (t.cat() == catActive) {
438 if (t.character() == '~')
439 os << (curr_env() == "lyxcode" ? ' ' : '~');
444 else if (t.cat() == catBegin) {
445 if (mode == MATH_MODE)
451 else if (t.cat() == catEnd) {
452 if (flags & FLAG_BRACE_LAST)
454 if (mode == MATH_MODE)
460 else if (t.cat() == catAlign)
465 //cerr << " column now " << cellcol << " max: " << grid.ncols() << "\n";
466 if (cellcol == grid.ncols()) {
467 //cerr << "adding column " << cellcol << "\n";
468 grid.addCol(cellcol - 1);
470 cell = &grid.cell(grid.index(cellrow, cellcol));
474 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
475 //cerr << "finished reading option\n";
479 else if (t.cat() == catOther)
480 os << string(1, t.character());
482 else if (t.cat() == catComment) {
485 Token const & t = p.getToken();
486 if (t.cat() == catNewline)
490 //os << wrap("comment", s);
498 else if (t.cs() == "lyxlock")
501 else if (t.cs() == "newcommand" || t.cs() == "providecommand") {
502 string const name = p.verbatimItem();
503 string const opts = p.getArg('[', ']');
504 string const body = p.verbatimItem();
505 // only non-lyxspecific stuff
506 if (name != "noun" && name != "tabularnewline") {
507 ostream & out = in_preamble ? h_preamble : os;
509 begin_inset(os, "FormulaMacro\n");
510 out << "\\" << t.cs() << "{" << name << "}";
512 out << "[" << opts << "]";
513 out << "{" << body << "}";
519 else if (t.cs() == "(") {
520 begin_inset(os, "Formula");
522 parse(p, os, FLAG_SIMPLE2, MATH_MODE);
527 else if (t.cs() == "[") {
528 begin_inset(os, "Formula");
530 parse(p, os, FLAG_EQUATION, MATH_MODE);
535 else if (t.cs() == "protect")
536 // ignore \\protect, will hopefully be re-added during output
539 else if (t.cs() == "begin") {
540 string const name = p.getArg('{', '}');
541 active_environments.push(name);
542 if (name == "document") {
544 parse(p, os, FLAG_END, mode);
545 } else if (name == "abstract") {
547 parse(p, os, FLAG_END, mode);
548 } else if (is_math_env(name)) {
549 begin_inset(os, "Formula ");
550 os << "\\begin{" << name << "}";
551 parse(p, os, FLAG_END, MATH_MODE);
552 } else if (name == "table") {
554 parse(p, os, FLAG_END, mode);
555 } else if (name == "thebibliography") {
556 p.verbatimItem(); // swallow next arg
558 parse(p, os, FLAG_END, mode);
559 } else if (mode == MATH_MODE) {
560 os << "\\begin{" << name << "}";
561 parse(p, os, FLAG_END, mode);
563 parse(p, os, FLAG_END, mode);
567 else if (t.cs() == "end") {
568 if (flags & FLAG_END) {
569 // eat environment name
570 string const name = p.getArg('{', '}');
571 if (name != curr_env())
572 p.error("\\end{" + name + "} does not match \\begin{"
574 active_environments.pop();
575 if (is_math_env(name)) {
576 os << "\\end{" << name << "}";
578 } else if (mode == MATH_MODE) {
579 os << "\\end{" << name << "}";
581 os << "\n\\layout Standard\n\n";
584 p.error("found 'end' unexpectedly");
587 else if (t.cs() == "item")
590 else if (t.cs() == ")") {
591 if (flags & FLAG_SIMPLE2)
593 p.error("found '\\)' unexpectedly");
596 else if (t.cs() == "]") {
597 if (flags & FLAG_EQUATION)
599 p.error("found '\\]' unexpectedly");
602 else if (t.cs() == "documentclass") {
604 split(p.getArg('[', ']'), opts, ',');
605 handle_opt(opts, known_languages, h_language);
606 handle_opt(opts, known_fontsizes, h_paperfontsize);
607 h_quotes_language = h_language;
608 h_options = join(opts, ',');
609 h_textclass = p.getArg('{', '}');
612 else if (t.cs() == "usepackage") {
613 string const options = p.getArg('[', ']');
614 string const name = p.getArg('{', '}');
615 if (options.empty() && name.find(',')) {
616 vector<string> vecnames;
617 split(name, vecnames, ',');
618 vector<string>::const_iterator it = vecnames.begin();
619 vector<string>::const_iterator end = vecnames.end();
620 for (; it != end; ++it)
621 handle_package(trim(*it), string());
623 handle_package(name, options);
627 else if (t.cs() == "newenvironment") {
628 string const name = p.getArg('{', '}');
630 string const begin = p.verbatimItem();
632 string const end = p.verbatimItem();
634 if (name != "lyxcode")
635 os << wrap("newenvironment", begin + end);
638 else if (t.cs() == "def") {
639 string name = p.getToken().cs();
640 while (p.nextToken().cat() != catBegin)
641 name += p.getToken().asString();
642 handle_ert(os, "\\def\\" + name + '{' + p.verbatimItem() + '}');
645 else if (t.cs() == "setcounter") {
646 string const name = p.getArg('{', '}');
647 string const content = p.getArg('{', '}');
648 if (name == "secnumdepth")
649 h_secnumdepth = content;
650 else if (name == "tocdepth")
651 h_tocdepth = content;
653 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
656 else if (t.cs() == "setlength") {
657 string const name = p.getToken().cs();
658 string const content = p.getArg('{', '}');
659 if (name == "parskip")
660 h_paragraph_separation = "skip";
661 else if (name == "parindent")
662 h_paragraph_separation = "skip";
664 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
667 else if (t.cs() == "par")
670 else if (is_heading(t.cs())) {
671 os << "\\layout " << cap(t.cs()) << "\n\n";
672 parse(p, os, FLAG_ITEM, mode);
675 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
678 else if (t.cs() == "tableofcontents")
679 p.verbatimItem(); // swallow this
681 else if (t.cs() == "textrm") {
682 os << '\\' << t.cs() << '{';
683 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
687 else if ((t.cs() == "emph" || t.cs() == "noun") && mode == TEXT_MODE) {
688 os << "\n\\" << t.cs() << " on\n";
689 parse(p, os, FLAG_ITEM, mode);
690 os << "\n\\" << t.cs() << " default\n";
693 else if (is_latex_command(t.cs()) && mode == TEXT_MODE) {
694 begin_inset(os, "LatexCommand ");
695 os << '\\' << t.cs() << '{';
696 parse(p, os, FLAG_ITEM, TEXT_MODE);
701 else if (t.cs() == "bibitem") {
702 os << "\n\\layout Bibliography\n\\bibitem ";
703 string opt = p.getArg('[',']');
705 os << '[' << opt << ']';
706 os << '{' << p.getArg('{','}') << '}' << "\n\n";
709 else if (t.cs() == "textasciitilde")
712 else if (t.cs() == "_" && mode == TEXT_MODE)
715 else if (t.cs() == "&" && mode == TEXT_MODE)
719 if (mode == MATH_MODE)
721 else if (in_preamble)
722 h_preamble << t.asInput();
724 // heuristic: read up to next non-nested space
726 string s = t.asInput();
727 string z = p.verbatimItem();
728 while (p.good() && z != " " && z.size()) {
729 //cerr << "read: " << z << endl;
731 z = p.verbatimItem();
733 cerr << "found ERT: " << s << endl;
734 handle_ert(os, s + ' ');
736 handle_ert(os, t.asInput() + ' ');
740 if (flags & FLAG_LEAVE) {
741 flags &= ~FLAG_LEAVE;
749 } // anonymous namespace
752 int main(int argc, char * argv[])
755 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
759 ifstream is(argv[1]);
761 parse(p, cout, 0, TEXT_MODE);
762 cout << "\n\\the_end";