1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
20 #include "texparser.h"
31 using std::istringstream;
34 using std::ostringstream;
42 void parse_preamble(Parser & p, ostream & os);
44 void parse(Parser & p, ostream & os, unsigned flags, const mode_type mode);
46 char const OPEN = '<';
47 char const CLOSE = '>';
49 /* rather brutish way to code table structure in a string:
53 \multicolumn{2}{c}{4} & 5 //
60 HLINE 2 MULT c MULT 4 TAB 5 LINE
64 char const TAB = '\001';
65 char const LINE = '\002';
66 char const MULT = '\003';
67 char const HLINE = '\004';
69 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
70 "breton", "british", "bulgarian", "catalan", "croatian", "czech", "danish",
71 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
72 "frenchb", "galician", "german", "germanb", "greek", "hebcal", "hebfont",
73 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
74 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
75 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
76 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
77 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
78 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
79 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
80 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
82 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
84 char const * known_headings[] = { "caption", "title", "author", "date",
85 "paragraph", "chapter", "section", "subsection", "subsubsection", 0 };
87 char const * known_math_envs[] = { "equation", "equation*",
88 "eqnarray", "eqnarray*", "align", "align*", 0};
90 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
93 // LaTeX names for quotes
94 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
95 "textquotedblleft", 0};
97 // the same as known_quotes with .lyx names
98 char const * known_coded_quotes[] = { "gld", "grd", "gld", "grd", 0};
103 ostringstream h_preamble;
104 string h_textclass = "article";
105 string h_options = "";
106 string h_language = "english";
107 string h_inputencoding = "latin1";
108 string h_fontscheme = "default";
109 string h_graphics = "default";
110 string h_paperfontsize = "default";
111 string h_spacing = "single";
112 string h_papersize = "default";
113 string h_paperpackage = "default";
114 string h_use_geometry = "0";
115 string h_use_amsmath = "0";
116 string h_use_natbib = "0";
117 string h_use_numerical_citations = "0";
118 string h_paperorientation = "portrait";
119 string h_secnumdepth = "3";
120 string h_tocdepth = "3";
121 string h_paragraph_separation = "indent";
122 string h_defskip = "medskip";
123 string h_quotes_language = "english";
124 string h_quotes_times = "2";
125 string h_papercolumns = "1";
126 string h_papersides = "1";
127 string h_paperpagestyle = "default";
128 string h_tracking_changes = "0";
130 // current stack of nested environments
131 stack<string> active_environments;
137 s[0] = toupper(s[0]);
142 string const trim(string const & a, char const * p = " \t\n\r")
146 if (a.empty() || !*p)
149 string::size_type r = a.find_last_not_of(p);
150 string::size_type l = a.find_first_not_of(p);
152 // Is this the minimal test? (lgb)
153 if (r == string::npos && l == string::npos)
156 return a.substr(l, r - l + 1);
160 void split(string const & s, vector<string> & result, char delim = ',')
164 while (getline(is, t, delim))
169 // splits "x=z, y=b" into a map
170 map<string, string> split_map(string const & s)
172 map<string, string> res;
175 for (size_t i = 0; i < v.size(); ++i) {
176 size_t const pos = v[i].find('=');
177 string const index = v[i].substr(0, pos);
178 string const value = v[i].substr(pos + 1, string::npos);
179 res[trim(index)] = trim(value);
185 string join(vector<string> const & input, char const * delim)
188 for (size_t i = 0; i != input.size(); ++i) {
197 char const ** is_known(string const & str, char const ** what)
199 for ( ; *what; ++what)
206 void handle_opt(vector<string> & opts, char const ** what, string & target)
211 for ( ; *what; ++what) {
212 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
213 if (it != opts.end()) {
214 //cerr << "### found option '" << *what << "'\n";
223 bool is_math_env(string const & name)
225 for (char const ** what = known_math_envs; *what; ++what)
232 void begin_inset(ostream & os, string const & name)
234 os << "\n\\begin_inset " << name;
238 void end_inset(ostream & os)
240 os << "\n\\end_inset\n\n";
246 return active_environments.empty() ? string() : active_environments.top();
250 void handle_ert(ostream & os, string const & s)
252 begin_inset(os, "ERT");
253 os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
254 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
256 os << "\n\\backslash\n";
264 void handle_par(ostream & os)
266 if (active_environments.empty())
269 string s = curr_env();
270 if (s == "document" || s == "table")
271 os << "Standard\n\n";
272 else if (s == "lyxcode")
273 os << "LyX-Code\n\n";
274 else if (s == "lyxlist")
276 else if (s == "thebibliography")
277 os << "Bibliography\n\n";
279 os << cap(s) << "\n\n";
283 void handle_package(string const & name, string const & options)
285 //cerr << "handle_package: '" << name << "'\n";
286 if (name == "a4wide") {
287 h_papersize = "a4paper";
288 h_paperpackage = "widemarginsa4";
289 } else if (name == "ae")
291 else if (name == "aecompl")
293 else if (name == "amsmath")
295 else if (name == "amssymb")
297 else if (name == "babel")
299 else if (name == "fontenc")
301 else if (name == "inputenc")
302 h_inputencoding = options;
303 else if (name == "makeidx")
305 else if (name == "verbatim")
307 else if (is_known(name, known_languages)) {
309 h_quotes_language = name;
312 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
314 h_preamble << "\\usepackage{" << name << "}\n";
319 vector<string> extract_col_align(string const & s)
323 for (size_t i = 0; i < s.size(); ++i) {
325 case 'c': res.push_back("center"); break;
326 case 'l': res.push_back("left"); break;
327 case 'r': res.push_back("right"); break;
328 //case '|': cerr << "ignoring vertical separator\n"; break;
329 //default : cerr << "ignoring special separator '" << s[i] << "'\n"; break;
337 void handle_tabular(Parser & p, ostream & os, mode_type mode)
339 begin_inset(os, "Tabular \n");
340 string posopts = p.getOpt();
342 cerr << "vertical tabular positioning '" << posopts << "' ignored\n";
343 string colopts = p.verbatimItem();
344 vector<string> colalign = extract_col_align(colopts);
346 parse(p, ss, FLAG_END, mode);
347 vector<string> lines;
348 split(ss.str(), lines, LINE);
349 const size_t cols = colalign.size();
350 const size_t rows = lines.size();
351 os << "<lyxtabular version=\"3\" rows=\"" << rows
352 << "\" columns=\"" << cols << "\">\n"
354 for (size_t c = 0; c < cols; ++c)
355 os << "<column alignment=\"" << colalign[c] << "\""
356 << " valignment=\"top\""
359 for (size_t r = 0; r < rows; ++r) {
360 vector<string> dummy;
362 split(lines[r], dummy, HLINE);
363 int hlines = dummy.size() - 1;
364 lines[r] = join(dummy, "");
365 // handle almost empty line
366 if (lines[r].empty() && hlines && r > 0) {
367 cerr << "last hline lost\n";
371 vector<string> cells;
372 split(lines[r], cells, TAB);
373 while (cells.size() < cols)
374 cells.push_back(string());
377 os << " topline=\"true\"";
379 for (size_t c = 0; c < cols; ++c) {
381 string alignment = "center";
382 vector<string> parts;
383 split(cells[c], parts, MULT);
384 if (parts.size() > 2) {
385 os << " multicolumn=\"" << parts[0] << "\"";
386 alignment = parts[1];
388 os << " alignment=\"" << alignment << "\""
389 << " valignment=\"top\""
390 << " topline=\"true\""
391 << " leftline=\"true\""
392 << " usebox=\"none\""
394 begin_inset(os, "Text");
395 os << "\n\n\\layout Standard\n\n";
403 os << "</lyxtabular>\n";
408 string wrap(string const & cmd, string const & str)
410 return OPEN + cmd + ' ' + str + CLOSE;
414 void end_preamble(ostream & os)
416 os << "# tex2lyx 0.0.2 created this file\n"
417 << "\\lyxformat 222\n"
418 << "\\textclass " << h_textclass << "\n"
419 << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n"
420 << "\\options " << h_options << "\n"
421 << "\\language " << h_language << "\n"
422 << "\\inputencoding " << h_inputencoding << "\n"
423 << "\\fontscheme " << h_fontscheme << "\n"
424 << "\\graphics " << h_graphics << "\n"
425 << "\\paperfontsize " << h_paperfontsize << "\n"
426 << "\\spacing " << h_spacing << "\n"
427 << "\\papersize " << h_papersize << "\n"
428 << "\\paperpackage " << h_paperpackage << "\n"
429 << "\\use_geometry " << h_use_geometry << "\n"
430 << "\\use_amsmath " << h_use_amsmath << "\n"
431 << "\\use_natbib " << h_use_natbib << "\n"
432 << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
433 << "\\paperorientation " << h_paperorientation << "\n"
434 << "\\secnumdepth " << h_secnumdepth << "\n"
435 << "\\tocdepth " << h_tocdepth << "\n"
436 << "\\paragraph_separation " << h_paragraph_separation << "\n"
437 << "\\defskip " << h_defskip << "\n"
438 << "\\quotes_language " << h_quotes_language << "\n"
439 << "\\quotes_times " << h_quotes_times << "\n"
440 << "\\papercolumns " << h_papercolumns << "\n"
441 << "\\papersides " << h_papersides << "\n"
442 << "\\paperpagestyle " << h_paperpagestyle << "\n"
443 << "\\tracking_changes " << h_tracking_changes << "\n";
447 void parse_preamble(Parser & p, ostream & os)
450 Token const & t = p.getToken();
453 cerr << "t: " << t << " flags: " << flags << "\n";
460 if (t.cat() == catLetter ||
461 t.cat() == catSpace ||
462 t.cat() == catSuper ||
464 t.cat() == catOther ||
465 t.cat() == catMath ||
466 t.cat() == catActive ||
467 t.cat() == catBegin ||
469 t.cat() == catAlign ||
470 t.cat() == catNewline ||
471 t.cat() == catParameter)
472 h_preamble << t.character();
474 else if (t.cat() == catComment) {
477 Token const & t = p.getToken();
478 if (t.cat() == catNewline)
482 //os << wrap("comment", s);
486 else if (t.cs() == "pagestyle")
487 h_paperpagestyle == p.verbatimItem();
489 else if (t.cs() == "makeatletter") {
490 p.setCatCode('@', catLetter);
491 h_preamble << "\\makeatletter\n";
494 else if (t.cs() == "makeatother") {
495 p.setCatCode('@', catOther);
496 h_preamble << "\\makeatother\n";
499 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
500 || t.cs() == "providecommand") {
501 string const name = p.verbatimItem();
502 string const opts = p.getOpt();
503 string const body = p.verbatimItem();
504 // only non-lyxspecific stuff
505 if (name != "\\noun "
506 && name != "\\tabularnewline "
508 && name != "\\lyxline "
509 && name != "\\lyxaddress "
510 && name != "\\lyxrightaddress "
511 && name != "\\boldsymbol "
512 && name != "\\lyxarrow ") {
514 ss << '\\' << t.cs() << '{' << name << '}'
515 << opts << '{' << body << "}\n";
516 h_preamble << ss.str();
518 ostream & out = in_preamble ? h_preamble : os;
519 out << "\\" << t.cs() << "{" << name << "}"
520 << opts << "{" << body << "}\n";
527 else if (t.cs() == "documentclass") {
529 split(p.getArg('[', ']'), opts, ',');
530 handle_opt(opts, known_languages, h_language);
531 handle_opt(opts, known_fontsizes, h_paperfontsize);
532 h_quotes_language = h_language;
533 h_options = join(opts, ",");
534 h_textclass = p.getArg('{', '}');
537 else if (t.cs() == "usepackage") {
538 string const options = p.getArg('[', ']');
539 string const name = p.getArg('{', '}');
540 if (options.empty() && name.find(',')) {
541 vector<string> vecnames;
542 split(name, vecnames, ',');
543 vector<string>::const_iterator it = vecnames.begin();
544 vector<string>::const_iterator end = vecnames.end();
545 for (; it != end; ++it)
546 handle_package(trim(*it), string());
548 handle_package(name, options);
552 else if (t.cs() == "newenvironment") {
553 string const name = p.getArg('{', '}');
555 ss << "\\newenvironment{" << name << "}";
558 ss << '{' << p.verbatimItem() << '}';
559 ss << '{' << p.verbatimItem() << '}';
561 if (name != "lyxcode" && name != "lyxlist"
562 && name != "lyxrightadress" && name != "lyxaddress")
563 h_preamble << ss.str();
566 else if (t.cs() == "def") {
567 string name = p.getToken().cs();
568 while (p.nextToken().cat() != catBegin)
569 name += p.getToken().asString();
570 h_preamble << "\\def\\" << name << '{' << p.verbatimItem() << "}\n";
573 else if (t.cs() == "setcounter") {
574 string const name = p.getArg('{', '}');
575 string const content = p.getArg('{', '}');
576 if (name == "secnumdepth")
577 h_secnumdepth = content;
578 else if (name == "tocdepth")
579 h_tocdepth = content;
581 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
584 else if (t.cs() == "setlength") {
585 string const name = p.verbatimItem();
586 string const content = p.verbatimItem();
587 if (name == "parskip")
588 h_paragraph_separation = "skip";
589 else if (name == "parindent")
590 h_paragraph_separation = "skip";
592 h_preamble << "\\setlength{" + name + "}{" + content + "}\n";
595 else if (t.cs() == "par")
598 else if (t.cs() == "begin") {
599 string const name = p.getArg('{', '}');
600 if (name == "document") {
602 os << "\n\n\\layout Standard\n\n";
605 h_preamble << "\\begin{" << name << "}";
608 else if (t.cs().size())
609 h_preamble << '\\' << t.cs() << ' ';
614 void parse(Parser & p, ostream & os, unsigned flags, const mode_type mode)
617 Token const & t = p.getToken();
620 cerr << "t: " << t << " flags: " << flags << "\n";
624 if (flags & FLAG_ITEM) {
625 if (t.cat() == catSpace)
629 if (t.cat() == catBegin) {
630 // skip the brace and collect everything to the next matching
632 flags |= FLAG_BRACE_LAST;
636 // handle only this single token, leave the loop if done
641 if (flags & FLAG_BRACED) {
642 if (t.cat() == catSpace)
645 if (t.cat() != catBegin) {
646 p.error("opening brace expected");
650 // skip the brace and collect everything to the next matching
652 flags = FLAG_BRACE_LAST;
656 if (flags & FLAG_OPTION) {
657 if (t.cat() == catOther && t.character() == '[') {
658 parse(p, os, FLAG_BRACK_LAST, mode);
660 // no option found, put back token and we are done
669 if (t.cat() == catMath) {
670 if (mode == TEXT_MODE || mode == MATHTEXT_MODE) {
671 // we are inside some text mode thingy, so opening new math is allowed
672 if (mode == TEXT_MODE)
673 begin_inset(os, "Formula ");
674 Token const & n = p.getToken();
675 if (n.cat() == catMath) {
676 // TeX's $$...$$ syntax for displayed math
678 parse(p, os, FLAG_SIMPLE, MATH_MODE);
680 p.getToken(); // skip the second '$' token
682 // simple $...$ stuff
685 parse(p, os, FLAG_SIMPLE, MATH_MODE);
688 if (mode == TEXT_MODE)
692 else if (flags & FLAG_SIMPLE) {
693 // this is the end of the formula
698 cerr << "mode: " << mode << endl;
699 p.error("something strange in the parser\n");
704 else if (t.cat() == catLetter ||
705 t.cat() == catSpace ||
706 t.cat() == catSuper ||
708 t.cat() == catOther ||
709 t.cat() == catParameter)
712 else if (t.cat() == catNewline)
715 else if (t.cat() == catActive) {
716 if (t.character() == '~') {
717 if (curr_env() == "lyxcode")
719 else if (mode == TEXT_MODE)
720 os << "\\SpecialChar ~\n";
727 else if (t.cat() == catBegin) {
728 if (mode == TEXT_MODE)
734 else if (t.cat() == catEnd) {
735 if (flags & FLAG_BRACE_LAST)
737 if (mode == TEXT_MODE)
743 else if (t.cat() == catAlign) {
744 if (mode == TEXT_MODE)
750 else if (t.cs() == "tabularnewline") {
751 if (mode == TEXT_MODE)
757 else if (t.cs() == "\\" && mode == MATH_MODE)
760 else if (t.cs() == "\\" && mode == TEXT_MODE && curr_env() == "tabular")
763 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
764 //cerr << "finished reading option\n";
768 else if (t.cat() == catOther)
769 os << string(1, t.character());
771 else if (t.cat() == catComment) {
774 Token const & t = p.getToken();
775 if (t.cat() == catNewline)
779 //os << wrap("comment", s);
787 else if (t.cs() == "ldots" && mode == MATH_MODE)
788 os << "\n\\SpecialChar \\ldots{}\n";
790 else if (t.cs() == "lyxlock")
793 else if (t.cs() == "makeatletter") {
794 p.setCatCode('@', catLetter);
795 handle_ert(os, "\\makeatletter\n");
798 else if (t.cs() == "makeatother") {
799 p.setCatCode('@', catOther);
800 handle_ert(os, "\\makeatother\n");
803 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
804 || t.cs() == "providecommand") {
805 string const name = p.verbatimItem();
806 string const opts = p.getOpt();
807 string const body = p.verbatimItem();
808 // only non-lyxspecific stuff
809 if (name != "\\noun " && name != "\\tabularnewline ") {
811 ss << '\\' << t.cs() << '{' << name << '}'
812 << opts << '{' << body << "}\n";
813 handle_ert(os, ss.str());
815 ostream & out = in_preamble ? h_preamble : os;
817 begin_inset(os, "FormulaMacro\n");
818 out << "\\" << t.cs() << "{" << name << "}"
819 << opts << "{" << body << "}\n";
826 else if (t.cs() == "newtheorem") {
828 ss << "\\newtheorem";
829 ss << '{' << p.verbatimItem() << '}';
831 ss << '{' << p.verbatimItem() << '}';
834 handle_ert(os, ss.str());
837 else if (t.cs() == "(") {
838 begin_inset(os, "Formula");
840 parse(p, os, FLAG_SIMPLE2, MATH_MODE);
845 else if (t.cs() == "[") {
846 begin_inset(os, "Formula");
848 parse(p, os, FLAG_EQUATION, MATH_MODE);
853 else if (t.cs() == "protect")
854 // ignore \\protect, will hopefully be re-added during output
857 else if (t.cs() == "begin") {
858 string const name = p.getArg('{', '}');
859 active_environments.push(name);
860 if (name == "abstract") {
862 parse(p, os, FLAG_END, mode);
863 } else if (is_math_env(name)) {
864 begin_inset(os, "Formula ");
865 os << "\\begin{" << name << "}";
866 parse(p, os, FLAG_END, MATH_MODE);
867 os << "\\end{" << name << "}";
869 } else if (name == "tabular") {
870 if (mode == TEXT_MODE)
871 handle_tabular(p, os, mode);
873 os << "\\begin{" << name << "}";
874 parse(p, os, FLAG_END, MATHTEXT_MODE);
875 os << "\\end{" << name << "}";
877 } else if (name == "table" || name == "figure") {
878 string opts = p.getOpt();
879 begin_inset(os, "Float " + name + "\n");
881 os << "placement " << opts << '\n';
883 << "collapsed false\n"
885 << "\\layout Standard\n";
886 parse(p, os, FLAG_END, mode);
888 } else if (name == "thebibliography") {
889 p.verbatimItem(); // swallow next arg
890 parse(p, os, FLAG_END, mode);
891 os << "\n\\layout Bibliography\n\n";
892 } else if (mode == MATH_MODE || mode == MATHTEXT_MODE) {
893 os << "\\begin{" << name << "}";
894 parse(p, os, FLAG_END, mode);
895 os << "\\end{" << name << "}";
897 parse(p, os, FLAG_END, mode);
901 else if (t.cs() == "end") {
902 if (flags & FLAG_END) {
903 // eat environment name
904 string const name = p.getArg('{', '}');
905 if (name != curr_env())
906 p.error("\\end{" + name + "} does not match \\begin{"
908 active_environments.pop();
911 p.error("found 'end' unexpectedly");
914 else if (t.cs() == "item")
917 else if (t.cs() == ")") {
918 if (flags & FLAG_SIMPLE2)
920 p.error("found '\\)' unexpectedly");
923 else if (t.cs() == "]") {
924 if (flags & FLAG_EQUATION)
926 p.error("found '\\]' unexpectedly");
929 else if (t.cs() == "documentclass") {
931 split(p.getArg('[', ']'), opts, ',');
932 handle_opt(opts, known_languages, h_language);
933 handle_opt(opts, known_fontsizes, h_paperfontsize);
934 h_quotes_language = h_language;
935 h_options = join(opts, ",");
936 h_textclass = p.getArg('{', '}');
939 else if (t.cs() == "usepackage") {
940 string const options = p.getArg('[', ']');
941 string const name = p.getArg('{', '}');
942 if (options.empty() && name.find(',')) {
943 vector<string> vecnames;
944 split(name, vecnames, ',');
945 vector<string>::const_iterator it = vecnames.begin();
946 vector<string>::const_iterator end = vecnames.end();
947 for (; it != end; ++it)
948 handle_package(trim(*it), string());
950 handle_package(name, options);
954 else if (t.cs() == "def") {
955 string name = p.getToken().cs();
956 while (p.nextToken().cat() != catBegin)
957 name += p.getToken().asString();
958 handle_ert(os, "\\def\\" + name + '{' + p.verbatimItem() + '}');
961 else if (t.cs() == "par")
964 else if (is_known(t.cs(), known_headings)) {
965 string name = t.cs();
966 if (p.nextToken().asInput() == "*") {
970 os << "\n\n\\layout " << cap(name) << "\n\n";
971 string opt = p.getOpt();
973 begin_inset(os, "OptArg\n");
974 os << "collapsed true\n\n\\layout Standard\n\n" << opt;
977 parse(p, os, FLAG_ITEM, mode);
978 os << "\n\n\\layout Standard\n\n";
981 else if (t.cs() == "includegraphics") {
982 if (mode == TEXT_MODE) {
983 map<string, string> opts = split_map(p.getArg('[', ']'));
984 string name = p.verbatimItem();
985 begin_inset(os, "Graphics ");
986 os << "\n\tfilename " << name << '\n';
987 if (opts.find("width") != opts.end())
988 os << "\twidth " << opts["width"] << '\n';
989 if (opts.find("height") != opts.end())
990 os << "\theight " << opts["height"] << '\n';
993 os << "\\includegraphics ";
997 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
1000 else if (t.cs() == "tableofcontents")
1001 p.verbatimItem(); // swallow this
1003 else if (t.cs() == "multicolumn" && mode == TEXT_MODE) {
1005 parse(p, os, FLAG_ITEM, mode);
1007 parse(p, os, FLAG_ITEM, mode);
1009 parse(p, os, FLAG_ITEM, mode);
1012 else if (t.cs() == "hline" && mode == TEXT_MODE)
1015 else if (t.cs() == "textrm") {
1016 if (mode == TEXT_MODE) {
1017 os << "\n\\family roman\n";
1018 parse(p, os, FLAG_ITEM, TEXT_MODE);
1019 os << "\n\\family default\n";
1021 os << '\\' << t.cs() << '{';
1022 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
1027 else if (t.cs() == "textsf") {
1028 if (mode == TEXT_MODE) {
1029 os << "\n\\family sans\n";
1030 parse(p, os, FLAG_ITEM, TEXT_MODE);
1031 os << "\n\\family default\n";
1033 os << '\\' << t.cs() << '{';
1034 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
1039 else if (t.cs() == "texttt") {
1040 if (mode == TEXT_MODE) {
1041 os << "\n\\family typewriter\n";
1042 parse(p, os, FLAG_ITEM, TEXT_MODE);
1043 os << "\n\\family default\n";
1045 os << '\\' << t.cs() << '{';
1046 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
1051 else if (t.cs() == "textsc") {
1052 if (mode == TEXT_MODE) {
1053 os << "\n\\noun on\n";
1054 parse(p, os, FLAG_ITEM, TEXT_MODE);
1055 os << "\n\\noun default\n";
1057 os << '\\' << t.cs() << '{';
1058 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
1063 else if (t.cs() == "textbf") {
1064 if (mode == TEXT_MODE) {
1065 os << "\n\\series bold\n";
1066 parse(p, os, FLAG_ITEM, TEXT_MODE);
1067 os << "\n\\series default\n";
1069 os << '\\' << t.cs() << '{';
1070 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
1075 else if (t.cs() == "underbar") {
1076 if (mode == TEXT_MODE) {
1077 os << "\n\\bar under\n";
1078 parse(p, os, FLAG_ITEM, TEXT_MODE);
1079 os << "\n\\bar default\n";
1081 os << '\\' << t.cs() << '{';
1082 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
1087 else if ((t.cs() == "emph" || t.cs() == "noun") && mode == TEXT_MODE) {
1088 os << "\n\\" << t.cs() << " on\n";
1089 parse(p, os, FLAG_ITEM, mode);
1090 os << "\n\\" << t.cs() << " default\n";
1093 else if (is_known(t.cs(), known_latex_commands) && mode == TEXT_MODE) {
1094 begin_inset(os, "LatexCommand ");
1095 os << '\\' << t.cs();
1098 os << '{' << p.verbatimItem() << '}';
1102 else if (t.cs() == "bibitem") {
1103 os << "\n\\layout Bibliography\n\\bibitem ";
1105 os << '{' << p.verbatimItem() << '}' << "\n\n";
1108 else if (mode == TEXT_MODE && is_known(t.cs(), known_quotes)) {
1109 char const ** where = is_known(t.cs(), known_quotes);
1110 begin_inset(os, "Quotes ");
1111 os << known_coded_quotes[where - known_quotes];
1115 else if (t.cs() == "LyX") {
1116 p.verbatimItem(); // eat {}
1120 else if (t.cs() == "TeX") {
1121 p.verbatimItem(); // eat {}
1125 else if (t.cs() == "LaTeX") {
1126 p.verbatimItem(); // eat {}
1130 else if (t.cs() == "LaTeXe") {
1131 p.verbatimItem(); // eat {}
1135 else if (t.cs() == "textasciitilde")
1138 else if (t.cs() == "_" && mode == TEXT_MODE)
1141 else if (t.cs() == "&" && mode == TEXT_MODE)
1144 else if (t.cs() == "\"") {
1145 string const name = p.verbatimItem();
1146 if (name == "a") os << 'ä';
1147 else if (name == "o") os << 'ö';
1148 else if (name == "u") os << 'ü';
1149 else if (name == "A") os << 'Ä';
1150 else if (name == "O") os << 'Ö';
1151 else if (name == "U") os << 'Ü';
1152 else handle_ert(os, "\"{" + name + "}");
1155 else if (t.cs() == "ss")
1158 else if (t.cs() == "input")
1159 handle_ert(os, "\\input{" + p.verbatimItem() + "}\n");
1161 else if (t.cs() == "fancyhead") {
1163 ss << "\\fancyhead";
1165 ss << '{' << p.verbatimItem() << "}\n";
1166 handle_ert(os, ss.str());
1170 //cerr << "#: " << t << " mode: " << mode << endl;
1171 if (mode == MATH_MODE || mode == MATHTEXT_MODE) {
1173 //cerr << "#: writing: '" << t.asInput() << "'\n";
1175 // heuristic: read up to next non-nested space
1177 string s = t.asInput();
1178 string z = p.verbatimItem();
1179 while (p.good() && z != " " && z.size()) {
1180 //cerr << "read: " << z << endl;
1182 z = p.verbatimItem();
1184 cerr << "found ERT: " << s << endl;
1185 handle_ert(os, s + ' ');
1187 handle_ert(os, t.asInput() + ' ');
1191 if (flags & FLAG_LEAVE) {
1192 flags &= ~FLAG_LEAVE;
1200 } // anonymous namespace
1203 int main(int argc, char * argv[])
1206 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
1210 ifstream is(argv[1]);
1212 parse_preamble(p, cout);
1213 active_environments.push("document");
1214 parse(p, cout, FLAG_END, TEXT_MODE);
1215 cout << "\n\\the_end";