1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
20 using std::ostringstream;
27 char const * known_headings[] = { "caption", "title", "author", "date",
28 "paragraph", "chapter", "section", "subsection", "subsubsection", 0 };
30 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
31 "printindex", "pageref", "url", 0 };
33 // LaTeX names for quotes
34 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
35 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
37 // the same as known_quotes with .lyx names
38 char const * known_coded_quotes[] = { "gld", "grd", "gld",
39 "grd", "gls", "fls", "frd", 0};
41 char const * known_sizes[] = { "tiny", "scriptsize", "footnotesize",
42 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
44 char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
45 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
56 // splits "x=z, y=b" into a map
57 map<string, string> split_map(string const & s)
59 map<string, string> res;
62 for (size_t i = 0; i < v.size(); ++i) {
63 size_t const pos = v[i].find('=');
64 string const index = v[i].substr(0, pos);
65 string const value = v[i].substr(pos + 1, string::npos);
66 res[trim(index)] = trim(value);
72 void begin_inset(ostream & os, string const & name)
74 os << "\n\\begin_inset " << name;
78 void end_inset(ostream & os)
80 os << "\n\\end_inset\n\n";
84 void skip_braces(Parser & p)
86 if (p.next_token().cat() != catBegin)
89 if (p.next_token().cat() == catEnd) {
97 void handle_ert(ostream & os, string const & s)
99 begin_inset(os, "ERT");
100 os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
101 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
103 os << "\n\\backslash\n";
111 void handle_par(ostream & os)
113 if (active_environments.empty())
116 string s = active_environment();
117 if (s == "document" || s == "table")
118 os << "Standard\n\n";
119 else if (s == "center")
120 os << "Standard\n\n\\align center\n";
121 else if (s == "lyxcode")
122 os << "LyX-Code\n\n";
123 else if (s == "lyxlist")
125 else if (s == "thebibliography")
126 os << "Bibliography\n\n";
128 os << cap(s) << "\n\n";
132 } // anonymous namespace
135 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
138 Token const & t = p.get_token();
141 cerr << "t: " << t << " flags: " << flags << "\n";
144 if (flags & FLAG_ITEM) {
145 if (t.cat() == catSpace)
149 if (t.cat() == catBegin) {
150 // skip the brace and collect everything to the next matching
152 flags |= FLAG_BRACE_LAST;
156 // handle only this single token, leave the loop if done
160 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
166 if (t.cat() == catMath) {
167 // we are inside some text mode thingy, so opening new math is allowed
168 begin_inset(os, "Formula ");
169 Token const & n = p.get_token();
170 if (n.cat() == catMath && outer) {
171 // TeX's $$...$$ syntax for displayed math
173 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
175 p.get_token(); // skip the second '$' token
177 // simple $...$ stuff
180 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
186 else if (t.cat() == catSuper || t.cat() == catSub)
187 cerr << "catcode " << t << " illegal in text mode\n";
189 else if (t.cat() == catLetter ||
190 t.cat() == catSpace ||
191 t.cat() == catOther ||
192 t.cat() == catAlign ||
193 t.cat() == catParameter)
196 else if (t.cat() == catNewline) {
197 if (p.next_token().cat() == catNewline) {
201 os << " "; // note the space
205 else if (t.cat() == catActive) {
206 if (t.character() == '~') {
207 if (active_environment() == "lyxcode")
210 os << "\\SpecialChar ~\n";
215 else if (t.cat() == catBegin) {
216 // special handling of size changes
217 bool const is_size = is_known(p.next_token().cs(), known_sizes);
218 string const s = parse_text(p, FLAG_BRACE_LAST, outer);
219 if (s.empty() && p.next_token().character() == '`')
220 ; // ignore it in {}``
221 else if (is_size || s == "[" || s == "]" || s == "*")
230 else if (t.cat() == catEnd) {
231 if (flags & FLAG_BRACE_LAST)
233 cerr << "stray '}' in text\n";
237 else if (t.cat() == catOther)
238 os << string(1, t.character());
240 else if (t.cat() == catComment)
247 else if (t.cs() == "ldots") {
249 os << "\n\\SpecialChar \\ldots{}\n";
252 else if (t.cs() == "(") {
253 begin_inset(os, "Formula");
255 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
260 else if (t.cs() == "[") {
261 begin_inset(os, "Formula");
263 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
268 else if (t.cs() == "begin") {
269 string const name = p.getArg('{', '}');
270 active_environments.push_back(name);
271 if (is_math_env(name)) {
272 begin_inset(os, "Formula ");
273 os << "\\begin{" << name << "}";
274 parse_math(p, os, FLAG_END, MATH_MODE);
275 os << "\\end{" << name << "}";
277 } else if (name == "tabular") {
278 begin_inset(os, "Tabular ");
279 handle_tabular(p, os);
281 } else if (name == "table" || name == "figure") {
282 string opts = p.getOpt();
283 begin_inset(os, "Float " + name + "\n");
285 os << "placement " << opts << '\n';
286 os << "wide false\ncollapsed false\n\n"
287 << "\\layout Standard\n";
288 parse_text(p, os, FLAG_END, outer);
290 } else if (name == "center") {
292 parse_text(p, os, FLAG_END, outer);
293 } else if (name == "enumerate" || name == "itemize"
294 || name == "lyxlist") {
295 size_t const n = active_environments.size();
296 string const s = active_environments[n - 2];
297 bool const deeper = s == "enumerate" || s == "itemize"
300 os << "\n\\begin_deeper";
301 os << "\n\\layout " << cap(name) << "\n\n";
302 if (name == "lyxlist")
303 p.verbatim_item(); // swallow next arg
304 parse_text(p, os, FLAG_END, outer);
306 os << "\n\\end_deeper\n";
308 } else if (name == "thebibliography") {
309 p.verbatim_item(); // swallow next arg
310 parse_text(p, os, FLAG_END, outer);
311 os << "\n\\layout Bibliography\n\n";
314 parse_text(p, os, FLAG_END, outer);
318 else if (t.cs() == "end") {
319 if (flags & FLAG_END) {
320 // eat environment name
321 string const name = p.getArg('{', '}');
322 if (name != active_environment())
323 cerr << "\\end{" + name + "} does not match \\begin{"
324 + active_environment() + "}\n";
325 active_environments.pop_back();
329 p.error("found 'end' unexpectedly");
332 else if (t.cs() == "item") {
335 if (p.next_token().character() == '[') {
336 p.get_token(); // eat '['
337 s = parse_text(p, FLAG_BRACK_LAST, outer);
343 else if (t.cs() == "def") {
344 string name = p.get_token().cs();
345 while (p.next_token().cat() != catBegin)
346 name += p.get_token().asString();
347 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}');
350 else if (t.cs() == "par") {
352 if (p.next_token().cs() != "\\begin")
354 //cerr << "next token: '" << p.next_token().cs() << "'\n";
357 else if (is_known(t.cs(), known_headings)) {
358 string name = t.cs();
359 if (p.next_token().asInput() == "*") {
363 os << "\n\n\\layout " << cap(name) << "\n\n";
364 string opt = p.getOpt();
366 begin_inset(os, "OptArg\n");
367 os << "collapsed true\n\n\\layout Standard\n\n" << opt;
370 parse_text(p, os, FLAG_ITEM, outer);
371 os << "\n\n\\layout Standard\n\n";
374 else if (t.cs() == "includegraphics") {
375 map<string, string> opts = split_map(p.getArg('[', ']'));
376 string name = p.verbatim_item();
377 begin_inset(os, "Graphics ");
378 os << "\n\tfilename " << name << '\n';
379 if (opts.find("width") != opts.end())
380 os << "\twidth " << opts["width"] << '\n';
381 if (opts.find("height") != opts.end())
382 os << "\theight " << opts["height"] << '\n';
386 else if (t.cs() == "footnote") {
387 begin_inset(os, "Foot\n");
388 os << "collapsed true\n\n\\layout Standard\n\n";
389 parse_text(p, os, FLAG_ITEM, false);
393 else if (t.cs() == "ensuremath") {
394 string s = parse_text(p, FLAG_ITEM, false);
395 if (s == "±" || s == "³" || s == "²" || s == "µ")
398 handle_ert(os, "\\ensuremath{" + s + "}");
401 else if (t.cs() == "marginpar") {
402 begin_inset(os, "Marginal\n");
403 os << "collapsed true\n\n\\layout Standard\n\n";
404 parse_text(p, os, FLAG_ITEM, false);
408 else if (t.cs() == "hfill") {
413 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
414 skip_braces(p); // swallow this
416 else if (t.cs() == "tableofcontents")
417 skip_braces(p); // swallow this
419 else if (t.cs() == "textrm") {
420 os << "\n\\family roman \n";
421 parse_text(p, os, FLAG_ITEM, outer);
422 os << "\n\\family default \n";
425 else if (t.cs() == "textsf") {
426 os << "\n\\family sans \n";
427 parse_text(p, os, FLAG_ITEM, outer);
428 os << "\n\\family default \n";
431 else if (t.cs() == "texttt") {
432 os << "\n\\family typewriter \n";
433 parse_text(p, os, FLAG_ITEM, outer);
434 os << "\n\\family default \n";
437 else if (t.cs() == "textit") {
438 os << "\n\\shape italic \n";
439 parse_text(p, os, FLAG_ITEM, outer);
440 os << "\n\\shape default \n";
443 else if (t.cs() == "textsc") {
444 os << "\n\\noun on \n";
445 parse_text(p, os, FLAG_ITEM, outer);
446 os << "\n\\noun default \n";
449 else if (t.cs() == "textbf") {
450 os << "\n\\series bold \n";
451 parse_text(p, os, FLAG_ITEM, outer);
452 os << "\n\\series default \n";
455 else if (t.cs() == "underbar") {
456 os << "\n\\bar under \n";
457 parse_text(p, os, FLAG_ITEM, outer);
458 os << "\n\\bar default \n";
461 else if (t.cs() == "emph" || t.cs() == "noun") {
462 os << "\n\\" << t.cs() << " on \n";
463 parse_text(p, os, FLAG_ITEM, outer);
464 os << "\n\\" << t.cs() << " default \n";
467 else if (t.cs() == "bibitem") {
468 os << "\n\\layout Bibliography\n\\bibitem ";
470 os << '{' << p.verbatim_item() << '}' << "\n\n";
473 else if (is_known(t.cs(), known_latex_commands)) {
474 begin_inset(os, "LatexCommand ");
475 os << '\\' << t.cs();
478 os << '{' << p.verbatim_item() << '}';
482 else if (is_known(t.cs(), known_quotes)) {
483 char const ** where = is_known(t.cs(), known_quotes);
484 begin_inset(os, "Quotes ");
485 os << known_coded_quotes[where - known_quotes];
490 else if (is_known(t.cs(), known_sizes)) {
491 char const ** where = is_known(t.cs(), known_sizes);
492 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
495 else if (t.cs() == "LyX" || t.cs() == "TeX"
496 || t.cs() == "LaTeX" || t.cs() == "LaTeXe") {
498 skip_braces(p); // eat {}
501 else if (t.cs() == "lyxarrow") {
502 os << "\\SpecialChar \\menuseparator\n";
506 else if (t.cs() == "ldots") {
507 os << "\\SpecialChar \\ldots{}\n";
511 else if (t.cs() == "@") {
512 os << "\\SpecialChar \\@";
516 else if (t.cs() == "textasciitilde") {
521 else if (t.cs() == "textasciicircum") {
526 else if (t.cs() == "textbackslash") {
527 os << "\n\\backslash\n";
531 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#" || t.cs() == "$"
532 || t.cs() == "{" || t.cs() == "}" || t.cs() == "%")
535 else if (t.cs() == "char") {
536 if (p.next_token().character() == '`') {
538 if (p.next_token().cs() == "\"") {
543 handle_ert(os, "\\char`");
546 handle_ert(os, "\\char");
550 else if (t.cs() == "\"") {
551 string const name = p.verbatim_item();
552 if (name == "a") os << 'ä';
553 else if (name == "o") os << 'ö';
554 else if (name == "u") os << 'ü';
555 else if (name == "A") os << 'Ä';
556 else if (name == "O") os << 'Ö';
557 else if (name == "U") os << 'Ü';
558 else handle_ert(os, "\"{" + name + "}");
561 else if (t.cs() == "=" || t.cs() == "H" || t.cs() == "c"
562 || t.cs() == "^" || t.cs() == "'" || t.cs() == "~") {
563 // we need the trim as the LyX parser chokes on such spaces
564 os << "\n\\i \\" << t.cs() << "{"
565 << trim(parse_text(p, FLAG_ITEM, outer), " ") << "}\n";
568 else if (t.cs() == "ss")
571 else if (t.cs() == "i" || t.cs() == "j")
572 os << "\\" << t.cs() << ' ';
574 else if (t.cs() == "-")
575 os << "\\SpecialChar \\-\n";
577 else if (t.cs() == "\\")
578 os << "\n\\newline\n";
580 else if (t.cs() == "lyxrightaddress") {
581 os << "\n\\layout Right Address\n";
582 parse_text(p, os, FLAG_ITEM, outer);
583 os << "\n\\layout Standard\n";
586 else if (t.cs() == "lyxaddress") {
587 os << "\n\\layout Address\n";
588 parse_text(p, os, FLAG_ITEM, outer);
589 os << "\n\\layout Standard\n";
592 else if (t.cs() == "input")
593 handle_ert(os, "\\input{" + p.verbatim_item() + "}\n");
595 else if (t.cs() == "fancyhead") {
599 ss << '{' << p.verbatim_item() << "}\n";
600 handle_ert(os, ss.str());
604 //cerr << "#: " << t << " mode: " << mode << endl;
605 // heuristic: read up to next non-nested space
607 string s = t.asInput();
608 string z = p.verbatim_item();
609 while (p.good() && z != " " && z.size()) {
610 //cerr << "read: " << z << endl;
612 z = p.verbatim_item();
614 cerr << "found ERT: " << s << endl;
615 handle_ert(os, s + ' ');
617 handle_ert(os, t.asInput() + ' ');
620 if (flags & FLAG_LEAVE) {
621 flags &= ~FLAG_LEAVE;
628 string parse_text(Parser & p, unsigned flags, const bool outer)
631 parse_text(p, os, flags, outer);