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) {
446 parse(p, os, FLAG_BRACE_LAST, mode);
450 else if (t.cat() == catEnd) {
451 if (flags & FLAG_BRACE_LAST)
453 p.error("found '}' unexpectedly");
455 //add(cell, '}', LM_TC_TEX);
458 else if (t.cat() == catAlign)
463 //cerr << " column now " << cellcol << " max: " << grid.ncols() << "\n";
464 if (cellcol == grid.ncols()) {
465 //cerr << "adding column " << cellcol << "\n";
466 grid.addCol(cellcol - 1);
468 cell = &grid.cell(grid.index(cellrow, cellcol));
472 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
473 //cerr << "finished reading option\n";
477 else if (t.cat() == catOther)
478 os << string(1, t.character());
480 else if (t.cat() == catComment) {
483 Token const & t = p.getToken();
484 if (t.cat() == catNewline)
488 //os << wrap("comment", s);
496 else if (t.cs() == "lyxlock") {
500 else if (t.cs() == "newcommand" || t.cs() == "providecommand") {
501 string const name = p.verbatimItem();
502 string const opts = p.getArg('[', ']');
503 string const body = p.verbatimItem();
504 // only non-lyxspecific stuff
505 if (name != "noun" && name != "tabularnewline") {
506 ostream & out = in_preamble ? h_preamble : os;
508 begin_inset(os, "FormulaMacro\n");
509 out << "\\" << t.cs() << "{" << name << "}";
511 out << "[" << opts << "]";
512 out << "{" << body << "}";
518 else if (t.cs() == "(") {
519 begin_inset(os, "Formula");
521 parse(p, os, FLAG_SIMPLE2, MATH_MODE);
526 else if (t.cs() == "[") {
527 begin_inset(os, "Formula");
529 parse(p, os, FLAG_EQUATION, MATH_MODE);
534 else if (t.cs() == "protect")
535 // ignore \\protect, will hopefully be re-added during output
538 else if (t.cs() == "begin") {
539 string const name = p.getArg('{', '}');
540 active_environments.push(name);
541 if (name == "document") {
543 parse(p, os, FLAG_END, mode);
544 } else if (name == "abstract") {
546 parse(p, os, FLAG_END, mode);
547 } else if (is_math_env(name)) {
548 begin_inset(os, "Formula ");
549 os << "\\begin{" << name << "}";
550 parse(p, os, FLAG_END, MATH_MODE);
551 } else if (name == "table") {
553 parse(p, os, FLAG_END, mode);
554 } else if (name == "thebibliography") {
555 p.verbatimItem(); // swallow next arg
557 parse(p, os, FLAG_END, mode);
558 } else if (mode == MATH_MODE) {
559 os << "\\begin{" << name << "}";
560 parse(p, os, FLAG_END, mode);
562 parse(p, os, FLAG_END, mode);
566 else if (t.cs() == "end") {
567 if (flags & FLAG_END) {
568 // eat environment name
569 string const name = p.getArg('{', '}');
570 if (name != curr_env())
571 p.error("\\end{" + name + "} does not match \\begin{"
573 active_environments.pop();
574 if (is_math_env(name)) {
576 os << "\\end{" << name << "}";
577 } else if (mode == MATH_MODE) {
578 os << "\\end{" << name << "}";
583 p.error("found 'end' unexpectedly");
586 else if (t.cs() == "item")
589 else if (t.cs() == ")") {
590 if (flags & FLAG_SIMPLE2)
592 p.error("found '\\)' unexpectedly");
595 else if (t.cs() == "]") {
596 if (flags & FLAG_EQUATION)
598 p.error("found '\\]' unexpectedly");
601 else if (t.cs() == "documentclass") {
603 split(p.getArg('[', ']'), opts, ',');
604 handle_opt(opts, known_languages, h_language);
605 handle_opt(opts, known_fontsizes, h_paperfontsize);
606 h_quotes_language = h_language;
607 h_options = join(opts, ',');
608 h_textclass = p.getArg('{', '}');
611 else if (t.cs() == "usepackage") {
612 string const options = p.getArg('[', ']');
613 string const name = p.getArg('{', '}');
614 if (options.empty() && name.find(',')) {
615 vector<string> vecnames;
616 split(name, vecnames, ',');
617 vector<string>::const_iterator it = vecnames.begin();
618 vector<string>::const_iterator end = vecnames.end();
619 for (; it != end; ++it)
620 handle_package(trim(*it), string());
622 handle_package(name, options);
626 else if (t.cs() == "newenvironment") {
627 string const name = p.getArg('{', '}');
629 string const begin = p.verbatimItem();
631 string const end = p.verbatimItem();
633 if (name != "lyxcode")
634 os << wrap("newenvironment", begin + end);
637 else if (t.cs() == "def") {
638 string name = p.getToken().cs();
639 while (p.nextToken().cat() != catBegin)
640 name += p.getToken().asString();
641 handle_ert(os, "\\def\\" + name + '{' + p.verbatimItem() + '}');
644 else if (t.cs() == "setcounter") {
645 string const name = p.getArg('{', '}');
646 string const content = p.getArg('{', '}');
647 if (name == "secnumdepth")
648 h_secnumdepth = content;
649 else if (name == "tocdepth")
650 h_tocdepth = content;
652 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
655 else if (t.cs() == "setlength") {
656 string const name = p.getToken().cs();
657 string const content = p.getArg('{', '}');
658 if (name == "parskip")
659 h_paragraph_separation = "skip";
660 else if (name == "parindent")
661 h_paragraph_separation = "skip";
663 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
666 else if (t.cs() == "par")
669 else if (is_heading(t.cs())) {
670 os << "\\layout " << cap(t.cs()) << "\n\n";
671 parse(p, os, FLAG_ITEM, mode);
674 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
677 else if (t.cs() == "tableofcontents")
678 p.verbatimItem(); // swallow this
680 else if (t.cs() == "textrm") {
681 os << '\\' << t.cs() << '{';
682 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
686 else if ((t.cs() == "emph" || t.cs() == "noun") && mode == TEXT_MODE) {
687 os << "\n\\" << t.cs() << " on\n";
688 parse(p, os, FLAG_ITEM, mode);
689 os << "\n\\" << t.cs() << " default\n";
692 else if (is_latex_command(t.cs()) && mode == TEXT_MODE) {
693 begin_inset(os, "LatexCommand ");
694 os << '\\' << t.cs() << '{';
695 parse(p, os, FLAG_ITEM, TEXT_MODE);
700 else if (t.cs() == "bibitem") {
701 os << "\n\\layout Bibliography\n\\bibitem ";
702 string opt = p.getArg('[',']');
704 os << '[' << opt << ']';
705 os << '{' << p.getArg('{','}') << '}' << "\n\n";
708 else if (t.cs() == "textasciitilde")
711 else if (t.cs() == "_" && mode == TEXT_MODE)
714 else if (t.cs() == "&" && mode == TEXT_MODE)
718 if (mode == MATH_MODE)
720 else if (in_preamble)
721 h_preamble << t.asInput();
723 // heuristic: read up to next non-nested space
724 string s = t.asInput();
725 string z = p.verbatimItem();
726 while (p.good() && z != " " && z.size()) {
727 //cerr << "read: " << z << endl;
729 z = p.verbatimItem();
731 cerr << "found ERT: " << s << endl;
732 handle_ert(os, s + ' ');
736 if (flags & FLAG_LEAVE) {
737 flags &= ~FLAG_LEAVE;
745 } // anonymous namespace
748 int main(int argc, char * argv[])
751 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
755 ifstream is(argv[1]);
757 parse(p, cout, 0, TEXT_MODE);
758 cout << "\n\\the_end";