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:
51 // \begin{tabular}{ccc}
53 // \multicolumn{2}{c}{4} & 5\\
57 // gets "translated" to:
60 // HLINE 2 MULT c MULT 4 TAB 5 LINE
63 char const TAB = '\001';
64 char const LINE = '\002';
65 char const MULT = '\003';
66 char const HLINE = '\004';
68 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
69 "breton", "british", "bulgarian", "catalan", "croatian", "czech", "danish",
70 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
71 "frenchb", "galician", "german", "germanb", "greek", "hebcal", "hebfont",
72 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
73 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
74 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
75 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
76 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
77 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
78 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
79 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
81 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
83 char const * known_headings[] = { "caption", "title", "author", "date",
84 "paragraph", "chapter", "section", "subsection", "subsubsection", 0 };
86 char const * known_math_envs[] = { "equation", "equation*",
87 "eqnarray", "eqnarray*", "align", "align*", 0};
89 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
92 // LaTeX names for quotes
93 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
94 "textquotedblleft", 0};
96 // the same as known_quotes with .lyx names
97 char const * known_coded_quotes[] = { "gld", "grd", "gld", "grd", 0};
102 ostringstream h_preamble;
103 string h_textclass = "article";
104 string h_options = "";
105 string h_language = "english";
106 string h_inputencoding = "latin1";
107 string h_fontscheme = "default";
108 string h_graphics = "default";
109 string h_paperfontsize = "default";
110 string h_spacing = "single";
111 string h_papersize = "default";
112 string h_paperpackage = "default";
113 string h_use_geometry = "0";
114 string h_use_amsmath = "0";
115 string h_use_natbib = "0";
116 string h_use_numerical_citations = "0";
117 string h_paperorientation = "portrait";
118 string h_secnumdepth = "3";
119 string h_tocdepth = "3";
120 string h_paragraph_separation = "indent";
121 string h_defskip = "medskip";
122 string h_quotes_language = "english";
123 string h_quotes_times = "2";
124 string h_papercolumns = "1";
125 string h_papersides = "1";
126 string h_paperpagestyle = "default";
127 string h_tracking_changes = "0";
129 // current stack of nested environments
130 stack<string> active_environments;
136 s[0] = toupper(s[0]);
141 string const trim(string const & a, char const * p = " \t\n\r")
145 if (a.empty() || !*p)
148 string::size_type r = a.find_last_not_of(p);
149 string::size_type l = a.find_first_not_of(p);
151 // Is this the minimal test? (lgb)
152 if (r == string::npos && l == string::npos)
155 return a.substr(l, r - l + 1);
159 void split(string const & s, vector<string> & result, char delim = ',')
163 while (getline(is, t, delim))
168 // splits "x=z, y=b" into a map
169 map<string, string> split_map(string const & s)
171 map<string, string> res;
174 for (size_t i = 0; i < v.size(); ++i) {
175 size_t const pos = v[i].find('=');
176 string const index = v[i].substr(0, pos);
177 string const value = v[i].substr(pos + 1, string::npos);
178 res[trim(index)] = trim(value);
184 string join(vector<string> const & input, char const * delim)
187 for (size_t i = 0; i != input.size(); ++i) {
196 char const ** is_known(string const & str, char const ** what)
198 for ( ; *what; ++what)
205 void handle_opt(vector<string> & opts, char const ** what, string & target)
210 for ( ; *what; ++what) {
211 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
212 if (it != opts.end()) {
213 //cerr << "### found option '" << *what << "'\n";
222 bool is_math_env(string const & name)
224 for (char const ** what = known_math_envs; *what; ++what)
231 void begin_inset(ostream & os, string const & name)
233 os << "\n\\begin_inset " << name;
237 void end_inset(ostream & os)
239 os << "\n\\end_inset\n\n";
245 return active_environments.empty() ? string() : active_environments.top();
249 void handle_ert(ostream & os, string const & s)
251 begin_inset(os, "ERT");
252 os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
253 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
255 os << "\n\\backslash\n";
263 void handle_par(ostream & os)
265 if (active_environments.empty())
268 string s = curr_env();
269 if (s == "document" || s == "table")
270 os << "Standard\n\n";
271 else if (s == "lyxcode")
272 os << "LyX-Code\n\n";
273 else if (s == "lyxlist")
275 else if (s == "thebibliography")
276 os << "Bibliography\n\n";
278 os << cap(s) << "\n\n";
282 void handle_package(string const & name, string const & options)
284 //cerr << "handle_package: '" << name << "'\n";
285 if (name == "a4wide") {
286 h_papersize = "a4paper";
287 h_paperpackage = "widemarginsa4";
288 } else if (name == "ae")
290 else if (name == "aecompl")
292 else if (name == "amsmath")
294 else if (name == "amssymb")
296 else if (name == "babel")
298 else if (name == "fontenc")
300 else if (name == "inputenc")
301 h_inputencoding = options;
302 else if (name == "makeidx")
304 else if (name == "verbatim")
306 else if (is_known(name, known_languages)) {
308 h_quotes_language = name;
311 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
313 h_preamble << "\\usepackage{" << name << "}\n";
318 vector<string> extract_col_align(string const & s)
322 for (size_t i = 0; i < s.size(); ++i) {
324 case 'c': res.push_back("center"); break;
325 case 'l': res.push_back("left"); break;
326 case 'r': res.push_back("right"); break;
327 //case '|': cerr << "ignoring vertical separator\n"; break;
328 //default : cerr << "ignoring special separator '" << s[i] << "'\n"; break;
336 void handle_tabular(Parser & p, ostream & os, mode_type mode)
338 begin_inset(os, "Tabular \n");
339 string posopts = p.getOpt();
341 cerr << "vertical tabular positioning '" << posopts << "' ignored\n";
342 string colopts = p.verbatimItem();
343 vector<string> colalign = extract_col_align(colopts);
345 parse(p, ss, FLAG_END, mode);
346 vector<string> lines;
347 split(ss.str(), lines, LINE);
348 const size_t cols = colalign.size();
349 const size_t rows = lines.size();
350 os << "<lyxtabular version=\"3\" rows=\"" << rows
351 << "\" columns=\"" << cols << "\">\n"
353 for (size_t c = 0; c < cols; ++c)
354 os << "<column alignment=\"" << colalign[c] << "\""
355 << " valignment=\"top\""
358 for (size_t r = 0; r < rows; ++r) {
359 vector<string> dummy;
361 split(lines[r], dummy, HLINE);
362 int hlines = dummy.size() - 1;
363 lines[r] = join(dummy, "");
364 // handle almost empty line
365 if (lines[r].empty() && hlines && r > 0) {
366 cerr << "last hline lost\n";
370 vector<string> cells;
371 split(lines[r], cells, TAB);
372 while (cells.size() < cols)
373 cells.push_back(string());
376 os << " topline=\"true\"";
378 for (size_t c = 0; c < cols; ++c) {
380 string alignment = "center";
381 vector<string> parts;
382 split(cells[c], parts, MULT);
383 if (parts.size() > 2) {
384 os << " multicolumn=\"" << parts[0] << "\"";
385 alignment = parts[1];
387 os << " alignment=\"" << alignment << "\""
388 << " valignment=\"top\""
389 << " topline=\"true\""
390 << " leftline=\"true\""
391 << " usebox=\"none\""
393 begin_inset(os, "Text");
394 os << "\n\n\\layout Standard\n\n";
402 os << "</lyxtabular>\n";
407 string wrap(string const & cmd, string const & str)
409 return OPEN + cmd + ' ' + str + CLOSE;
413 void end_preamble(ostream & os)
415 os << "# tex2lyx 0.0.2 created this file\n"
416 << "\\lyxformat 222\n"
417 << "\\textclass " << h_textclass << "\n"
418 << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n"
419 << "\\options " << h_options << "\n"
420 << "\\language " << h_language << "\n"
421 << "\\inputencoding " << h_inputencoding << "\n"
422 << "\\fontscheme " << h_fontscheme << "\n"
423 << "\\graphics " << h_graphics << "\n"
424 << "\\paperfontsize " << h_paperfontsize << "\n"
425 << "\\spacing " << h_spacing << "\n"
426 << "\\papersize " << h_papersize << "\n"
427 << "\\paperpackage " << h_paperpackage << "\n"
428 << "\\use_geometry " << h_use_geometry << "\n"
429 << "\\use_amsmath " << h_use_amsmath << "\n"
430 << "\\use_natbib " << h_use_natbib << "\n"
431 << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
432 << "\\paperorientation " << h_paperorientation << "\n"
433 << "\\secnumdepth " << h_secnumdepth << "\n"
434 << "\\tocdepth " << h_tocdepth << "\n"
435 << "\\paragraph_separation " << h_paragraph_separation << "\n"
436 << "\\defskip " << h_defskip << "\n"
437 << "\\quotes_language " << h_quotes_language << "\n"
438 << "\\quotes_times " << h_quotes_times << "\n"
439 << "\\papercolumns " << h_papercolumns << "\n"
440 << "\\papersides " << h_papersides << "\n"
441 << "\\paperpagestyle " << h_paperpagestyle << "\n"
442 << "\\tracking_changes " << h_tracking_changes << "\n";
446 void parse_preamble(Parser & p, ostream & os)
449 Token const & t = p.getToken();
452 cerr << "t: " << t << " flags: " << flags << "\n";
459 if (t.cat() == catLetter ||
460 t.cat() == catSpace ||
461 t.cat() == catSuper ||
463 t.cat() == catOther ||
464 t.cat() == catMath ||
465 t.cat() == catActive ||
466 t.cat() == catBegin ||
468 t.cat() == catAlign ||
469 t.cat() == catNewline ||
470 t.cat() == catParameter)
471 h_preamble << t.character();
473 else if (t.cat() == catComment) {
476 Token const & t = p.getToken();
477 if (t.cat() == catNewline)
481 //os << wrap("comment", s);
485 else if (t.cs() == "pagestyle")
486 h_paperpagestyle == p.verbatimItem();
488 else if (t.cs() == "makeatletter") {
489 p.setCatCode('@', catLetter);
490 h_preamble << "\\makeatletter\n";
493 else if (t.cs() == "makeatother") {
494 p.setCatCode('@', catOther);
495 h_preamble << "\\makeatother\n";
498 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
499 || t.cs() == "providecommand") {
500 string const name = p.verbatimItem();
501 string const opts = p.getOpt();
502 string const body = p.verbatimItem();
503 // only non-lyxspecific stuff
504 if (name != "\\noun "
505 && name != "\\tabularnewline "
507 && name != "\\lyxline "
508 && name != "\\lyxaddress "
509 && name != "\\lyxrightaddress "
510 && name != "\\boldsymbol "
511 && name != "\\lyxarrow ") {
513 ss << '\\' << t.cs() << '{' << name << '}'
514 << opts << '{' << body << "}\n";
515 h_preamble << ss.str();
517 ostream & out = in_preamble ? h_preamble : os;
518 out << "\\" << t.cs() << "{" << name << "}"
519 << opts << "{" << body << "}\n";
526 else if (t.cs() == "documentclass") {
528 split(p.getArg('[', ']'), opts, ',');
529 handle_opt(opts, known_languages, h_language);
530 handle_opt(opts, known_fontsizes, h_paperfontsize);
531 h_quotes_language = h_language;
532 h_options = join(opts, ",");
533 h_textclass = p.getArg('{', '}');
536 else if (t.cs() == "usepackage") {
537 string const options = p.getArg('[', ']');
538 string const name = p.getArg('{', '}');
539 if (options.empty() && name.find(',')) {
540 vector<string> vecnames;
541 split(name, vecnames, ',');
542 vector<string>::const_iterator it = vecnames.begin();
543 vector<string>::const_iterator end = vecnames.end();
544 for (; it != end; ++it)
545 handle_package(trim(*it), string());
547 handle_package(name, options);
551 else if (t.cs() == "newenvironment") {
552 string const name = p.getArg('{', '}');
554 ss << "\\newenvironment{" << name << "}";
557 ss << '{' << p.verbatimItem() << '}';
558 ss << '{' << p.verbatimItem() << '}';
560 if (name != "lyxcode" && name != "lyxlist"
561 && name != "lyxrightadress" && name != "lyxaddress")
562 h_preamble << ss.str();
565 else if (t.cs() == "def") {
566 string name = p.getToken().cs();
567 while (p.nextToken().cat() != catBegin)
568 name += p.getToken().asString();
569 h_preamble << "\\def\\" << name << '{' << p.verbatimItem() << "}\n";
572 else if (t.cs() == "setcounter") {
573 string const name = p.getArg('{', '}');
574 string const content = p.getArg('{', '}');
575 if (name == "secnumdepth")
576 h_secnumdepth = content;
577 else if (name == "tocdepth")
578 h_tocdepth = content;
580 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
583 else if (t.cs() == "setlength") {
584 string const name = p.verbatimItem();
585 string const content = p.verbatimItem();
586 if (name == "parskip")
587 h_paragraph_separation = "skip";
588 else if (name == "parindent")
589 h_paragraph_separation = "skip";
591 h_preamble << "\\setlength{" + name + "}{" + content + "}\n";
594 else if (t.cs() == "par")
597 else if (t.cs() == "begin") {
598 string const name = p.getArg('{', '}');
599 if (name == "document") {
601 os << "\n\n\\layout Standard\n\n";
604 h_preamble << "\\begin{" << name << "}";
607 else if (t.cs().size())
608 h_preamble << '\\' << t.cs() << ' ';
613 void parse(Parser & p, ostream & os, unsigned flags, const mode_type mode)
616 Token const & t = p.getToken();
619 cerr << "t: " << t << " flags: " << flags << "\n";
623 if (flags & FLAG_ITEM) {
624 if (t.cat() == catSpace)
628 if (t.cat() == catBegin) {
629 // skip the brace and collect everything to the next matching
631 flags |= FLAG_BRACE_LAST;
635 // handle only this single token, leave the loop if done
640 if (flags & FLAG_BRACED) {
641 if (t.cat() == catSpace)
644 if (t.cat() != catBegin) {
645 p.error("opening brace expected");
649 // skip the brace and collect everything to the next matching
651 flags = FLAG_BRACE_LAST;
655 if (flags & FLAG_OPTION) {
656 if (t.cat() == catOther && t.character() == '[') {
657 parse(p, os, FLAG_BRACK_LAST, mode);
659 // no option found, put back token and we are done
668 if (t.cat() == catMath) {
669 if (mode == TEXT_MODE || mode == MATHTEXT_MODE) {
670 // we are inside some text mode thingy, so opening new math is allowed
671 if (mode == TEXT_MODE)
672 begin_inset(os, "Formula ");
673 Token const & n = p.getToken();
674 if (n.cat() == catMath) {
675 // TeX's $$...$$ syntax for displayed math
677 parse(p, os, FLAG_SIMPLE, MATH_MODE);
679 p.getToken(); // skip the second '$' token
681 // simple $...$ stuff
684 parse(p, os, FLAG_SIMPLE, MATH_MODE);
687 if (mode == TEXT_MODE)
691 else if (flags & FLAG_SIMPLE) {
692 // this is the end of the formula
697 cerr << "mode: " << mode << endl;
698 p.error("something strange in the parser\n");
703 else if (t.cat() == catLetter ||
704 t.cat() == catSpace ||
705 t.cat() == catSuper ||
707 t.cat() == catOther ||
708 t.cat() == catParameter)
711 else if (t.cat() == catNewline)
714 else if (t.cat() == catActive) {
715 if (t.character() == '~') {
716 if (curr_env() == "lyxcode")
718 else if (mode == TEXT_MODE)
719 os << "\\SpecialChar ~\n";
726 else if (t.cat() == catBegin) {
727 if (mode == TEXT_MODE)
733 else if (t.cat() == catEnd) {
734 if (flags & FLAG_BRACE_LAST)
736 if (mode == TEXT_MODE)
742 else if (t.cat() == catAlign) {
743 if (mode == TEXT_MODE)
749 else if (t.cs() == "tabularnewline") {
750 if (mode == TEXT_MODE)
756 else if (t.cs() == "\\" && mode == MATH_MODE)
759 else if (t.cs() == "\\" && mode == TEXT_MODE && curr_env() == "tabular")
762 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
763 //cerr << "finished reading option\n";
767 else if (t.cat() == catOther)
768 os << string(1, t.character());
770 else if (t.cat() == catComment) {
773 Token const & t = p.getToken();
774 if (t.cat() == catNewline)
778 //os << wrap("comment", s);
786 else if (t.cs() == "ldots" && mode == MATH_MODE)
787 os << "\n\\SpecialChar \\ldots{}\n";
789 else if (t.cs() == "lyxlock")
792 else if (t.cs() == "makeatletter") {
793 p.setCatCode('@', catLetter);
794 handle_ert(os, "\\makeatletter\n");
797 else if (t.cs() == "makeatother") {
798 p.setCatCode('@', catOther);
799 handle_ert(os, "\\makeatother\n");
802 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
803 || t.cs() == "providecommand") {
804 string const name = p.verbatimItem();
805 string const opts = p.getOpt();
806 string const body = p.verbatimItem();
807 // only non-lyxspecific stuff
808 if (name != "\\noun " && name != "\\tabularnewline ") {
810 ss << '\\' << t.cs() << '{' << name << '}'
811 << opts << '{' << body << "}\n";
812 handle_ert(os, ss.str());
814 ostream & out = in_preamble ? h_preamble : os;
816 begin_inset(os, "FormulaMacro\n");
817 out << "\\" << t.cs() << "{" << name << "}"
818 << opts << "{" << body << "}\n";
825 else if (t.cs() == "newtheorem") {
827 ss << "\\newtheorem";
828 ss << '{' << p.verbatimItem() << '}';
830 ss << '{' << p.verbatimItem() << '}';
833 handle_ert(os, ss.str());
836 else if (t.cs() == "(") {
837 begin_inset(os, "Formula");
839 parse(p, os, FLAG_SIMPLE2, MATH_MODE);
844 else if (t.cs() == "[") {
845 begin_inset(os, "Formula");
847 parse(p, os, FLAG_EQUATION, MATH_MODE);
852 else if (t.cs() == "protect")
853 // ignore \\protect, will hopefully be re-added during output
856 else if (t.cs() == "begin") {
857 string const name = p.getArg('{', '}');
858 active_environments.push(name);
859 if (name == "abstract") {
861 parse(p, os, FLAG_END, mode);
862 } else if (is_math_env(name)) {
863 begin_inset(os, "Formula ");
864 os << "\\begin{" << name << "}";
865 parse(p, os, FLAG_END, MATH_MODE);
866 os << "\\end{" << name << "}";
868 } else if (name == "tabular") {
869 if (mode == TEXT_MODE)
870 handle_tabular(p, os, mode);
872 os << "\\begin{" << name << "}";
873 parse(p, os, FLAG_END, MATHTEXT_MODE);
874 os << "\\end{" << name << "}";
876 } else if (name == "table" || name == "figure") {
877 string opts = p.getOpt();
878 begin_inset(os, "Float " + name + "\n");
880 os << "placement " << opts << '\n';
882 << "collapsed false\n"
884 << "\\layout Standard\n";
885 parse(p, os, FLAG_END, mode);
887 } else if (name == "thebibliography") {
888 p.verbatimItem(); // swallow next arg
889 parse(p, os, FLAG_END, mode);
890 os << "\n\\layout Bibliography\n\n";
891 } else if (mode == MATH_MODE || mode == MATHTEXT_MODE) {
892 os << "\\begin{" << name << "}";
893 parse(p, os, FLAG_END, mode);
894 os << "\\end{" << name << "}";
896 parse(p, os, FLAG_END, mode);
900 else if (t.cs() == "end") {
901 if (flags & FLAG_END) {
902 // eat environment name
903 string const name = p.getArg('{', '}');
904 if (name != curr_env())
905 p.error("\\end{" + name + "} does not match \\begin{"
907 active_environments.pop();
910 p.error("found 'end' unexpectedly");
913 else if (t.cs() == "item")
916 else if (t.cs() == ")") {
917 if (flags & FLAG_SIMPLE2)
919 p.error("found '\\)' unexpectedly");
922 else if (t.cs() == "]") {
923 if (flags & FLAG_EQUATION)
925 p.error("found '\\]' unexpectedly");
928 else if (t.cs() == "documentclass") {
930 split(p.getArg('[', ']'), opts, ',');
931 handle_opt(opts, known_languages, h_language);
932 handle_opt(opts, known_fontsizes, h_paperfontsize);
933 h_quotes_language = h_language;
934 h_options = join(opts, ",");
935 h_textclass = p.getArg('{', '}');
938 else if (t.cs() == "usepackage") {
939 string const options = p.getArg('[', ']');
940 string const name = p.getArg('{', '}');
941 if (options.empty() && name.find(',')) {
942 vector<string> vecnames;
943 split(name, vecnames, ',');
944 vector<string>::const_iterator it = vecnames.begin();
945 vector<string>::const_iterator end = vecnames.end();
946 for (; it != end; ++it)
947 handle_package(trim(*it), string());
949 handle_package(name, options);
953 else if (t.cs() == "def") {
954 string name = p.getToken().cs();
955 while (p.nextToken().cat() != catBegin)
956 name += p.getToken().asString();
957 handle_ert(os, "\\def\\" + name + '{' + p.verbatimItem() + '}');
960 else if (t.cs() == "par")
963 else if (is_known(t.cs(), known_headings)) {
964 string name = t.cs();
965 if (p.nextToken().asInput() == "*") {
969 os << "\n\n\\layout " << cap(name) << "\n\n";
970 string opt = p.getOpt();
972 begin_inset(os, "OptArg\n");
973 os << "collapsed true\n\n\\layout Standard\n\n" << opt;
976 parse(p, os, FLAG_ITEM, mode);
977 os << "\n\n\\layout Standard\n\n";
980 else if (t.cs() == "includegraphics") {
981 if (mode == TEXT_MODE) {
982 map<string, string> opts = split_map(p.getArg('[', ']'));
983 string name = p.verbatimItem();
984 begin_inset(os, "Graphics ");
985 os << "\n\tfilename " << name << '\n';
986 if (opts.find("width") != opts.end())
987 os << "\twidth " << opts["width"] << '\n';
988 if (opts.find("height") != opts.end())
989 os << "\theight " << opts["height"] << '\n';
992 os << "\\includegraphics ";
996 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
999 else if (t.cs() == "tableofcontents")
1000 p.verbatimItem(); // swallow this
1002 else if (t.cs() == "multicolumn" && mode == TEXT_MODE) {
1004 parse(p, os, FLAG_ITEM, mode);
1006 parse(p, os, FLAG_ITEM, mode);
1008 parse(p, os, FLAG_ITEM, mode);
1011 else if (t.cs() == "hline" && mode == TEXT_MODE)
1014 else if (t.cs() == "textrm") {
1015 if (mode == TEXT_MODE) {
1016 os << "\n\\family roman\n";
1017 parse(p, os, FLAG_ITEM, TEXT_MODE);
1018 os << "\n\\family default\n";
1020 os << '\\' << t.cs() << '{';
1021 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
1026 else if (t.cs() == "textsf") {
1027 if (mode == TEXT_MODE) {
1028 os << "\n\\family sans\n";
1029 parse(p, os, FLAG_ITEM, TEXT_MODE);
1030 os << "\n\\family default\n";
1032 os << '\\' << t.cs() << '{';
1033 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
1038 else if (t.cs() == "texttt") {
1039 if (mode == TEXT_MODE) {
1040 os << "\n\\family typewriter\n";
1041 parse(p, os, FLAG_ITEM, TEXT_MODE);
1042 os << "\n\\family default\n";
1044 os << '\\' << t.cs() << '{';
1045 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
1050 else if (t.cs() == "textsc") {
1051 if (mode == TEXT_MODE) {
1052 os << "\n\\noun on\n";
1053 parse(p, os, FLAG_ITEM, TEXT_MODE);
1054 os << "\n\\noun default\n";
1056 os << '\\' << t.cs() << '{';
1057 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
1062 else if (t.cs() == "textbf") {
1063 if (mode == TEXT_MODE) {
1064 os << "\n\\series bold\n";
1065 parse(p, os, FLAG_ITEM, TEXT_MODE);
1066 os << "\n\\series default\n";
1068 os << '\\' << t.cs() << '{';
1069 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
1074 else if (t.cs() == "underbar") {
1075 if (mode == TEXT_MODE) {
1076 os << "\n\\bar under\n";
1077 parse(p, os, FLAG_ITEM, TEXT_MODE);
1078 os << "\n\\bar default\n";
1080 os << '\\' << t.cs() << '{';
1081 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
1086 else if ((t.cs() == "emph" || t.cs() == "noun") && mode == TEXT_MODE) {
1087 os << "\n\\" << t.cs() << " on\n";
1088 parse(p, os, FLAG_ITEM, mode);
1089 os << "\n\\" << t.cs() << " default\n";
1092 else if (is_known(t.cs(), known_latex_commands) && mode == TEXT_MODE) {
1093 begin_inset(os, "LatexCommand ");
1094 os << '\\' << t.cs();
1097 os << '{' << p.verbatimItem() << '}';
1101 else if (t.cs() == "bibitem") {
1102 os << "\n\\layout Bibliography\n\\bibitem ";
1104 os << '{' << p.verbatimItem() << '}' << "\n\n";
1107 else if (mode == TEXT_MODE && is_known(t.cs(), known_quotes)) {
1108 char const ** where = is_known(t.cs(), known_quotes);
1109 begin_inset(os, "Quotes ");
1110 os << known_coded_quotes[where - known_quotes];
1114 else if (t.cs() == "LyX") {
1115 p.verbatimItem(); // eat {}
1119 else if (t.cs() == "TeX") {
1120 p.verbatimItem(); // eat {}
1124 else if (t.cs() == "LaTeX") {
1125 p.verbatimItem(); // eat {}
1129 else if (t.cs() == "LaTeXe") {
1130 p.verbatimItem(); // eat {}
1134 else if (t.cs() == "textasciitilde")
1137 else if (t.cs() == "_" && mode == TEXT_MODE)
1140 else if (t.cs() == "&" && mode == TEXT_MODE)
1143 else if (t.cs() == "\"") {
1144 string const name = p.verbatimItem();
1145 if (name == "a") os << 'ä';
1146 else if (name == "o") os << 'ö';
1147 else if (name == "u") os << 'ü';
1148 else if (name == "A") os << 'Ä';
1149 else if (name == "O") os << 'Ö';
1150 else if (name == "U") os << 'Ü';
1151 else handle_ert(os, "\"{" + name + "}");
1154 else if (t.cs() == "ss")
1157 else if (t.cs() == "input")
1158 handle_ert(os, "\\input{" + p.verbatimItem() + "}\n");
1160 else if (t.cs() == "fancyhead") {
1162 ss << "\\fancyhead";
1164 ss << '{' << p.verbatimItem() << "}\n";
1165 handle_ert(os, ss.str());
1169 //cerr << "#: " << t << " mode: " << mode << endl;
1170 if (mode == MATH_MODE || mode == MATHTEXT_MODE) {
1172 //cerr << "#: writing: '" << t.asInput() << "'\n";
1174 // heuristic: read up to next non-nested space
1176 string s = t.asInput();
1177 string z = p.verbatimItem();
1178 while (p.good() && z != " " && z.size()) {
1179 //cerr << "read: " << z << endl;
1181 z = p.verbatimItem();
1183 cerr << "found ERT: " << s << endl;
1184 handle_ert(os, s + ' ');
1186 handle_ert(os, t.asInput() + ' ');
1190 if (flags & FLAG_LEAVE) {
1191 flags &= ~FLAG_LEAVE;
1199 } // anonymous namespace
1202 int main(int argc, char * argv[])
1205 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
1209 ifstream is(argv[1]);
1211 parse_preamble(p, cout);
1212 active_environments.push("document");
1213 parse(p, cout, FLAG_END, TEXT_MODE);
1214 cout << "\n\\the_end";