1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
21 using std::ostringstream;
28 char const * known_headings[] = { "caption", "title", "author", "date",
29 "paragraph", "chapter", "section", "subsection", "subsubsection", 0 };
31 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
32 "printindex", "pageref", "url", 0 };
34 // LaTeX names for quotes
35 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
36 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
38 // the same as known_quotes with .lyx names
39 char const * known_coded_quotes[] = { "gld", "grd", "gld",
40 "grd", "gls", "fls", "frd", 0};
42 char const * known_sizes[] = { "tiny", "scriptsize", "footnotesize",
43 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
45 char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
46 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
57 // splits "x=z, y=b" into a map
58 map<string, string> split_map(string const & s)
60 map<string, string> res;
63 for (size_t i = 0; i < v.size(); ++i) {
64 size_t const pos = v[i].find('=');
65 string const index = v[i].substr(0, pos);
66 string const value = v[i].substr(pos + 1, string::npos);
67 res[trim(index)] = trim(value);
73 void begin_inset(ostream & os, string const & name)
75 os << "\n\\begin_inset " << name;
79 void end_inset(ostream & os)
81 os << "\n\\end_inset\n\n";
85 void skip_braces(Parser & p)
87 if (p.next_token().cat() != catBegin)
90 if (p.next_token().cat() == catEnd) {
98 void handle_ert(ostream & os, string const & s)
100 begin_inset(os, "ERT");
101 os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
102 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
104 os << "\n\\backslash\n";
112 void handle_par(ostream & os)
114 if (active_environments.empty())
117 string s = active_environment();
118 if (s == "document" || s == "table" || s == "center")
119 os << "Standard\n\n";
120 else if (s == "lyxcode")
121 os << "LyX-Code\n\n";
122 else if (s == "lyxlist")
124 else if (s == "thebibliography")
125 os << "Bibliography\n\n";
127 os << cap(s) << "\n\n";
131 } // anonymous namespace
134 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
137 Token const & t = p.get_token();
140 cerr << "t: " << t << " flags: " << flags << "\n";
143 if (flags & FLAG_ITEM) {
144 if (t.cat() == catSpace)
148 if (t.cat() == catBegin) {
149 // skip the brace and collect everything to the next matching
151 flags |= FLAG_BRACE_LAST;
155 // handle only this single token, leave the loop if done
159 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
165 if (t.cat() == catMath) {
166 // we are inside some text mode thingy, so opening new math is allowed
167 begin_inset(os, "Formula ");
168 Token const & n = p.get_token();
169 if (n.cat() == catMath && outer) {
170 // TeX's $$...$$ syntax for displayed math
172 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
174 p.get_token(); // skip the second '$' token
176 // simple $...$ stuff
179 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
185 else if (t.cat() == catSuper || t.cat() == catSub)
186 cerr << "catcode " << t << " illegal in text mode\n";
188 else if (t.cat() == catLetter ||
189 t.cat() == catSpace ||
190 t.cat() == catOther ||
191 t.cat() == catAlign ||
192 t.cat() == catParameter)
195 else if (t.cat() == catNewline) {
196 if (p.next_token().cat() == catNewline) {
200 os << " "; // note the space
204 else if (t.cat() == catActive) {
205 if (t.character() == '~') {
206 if (active_environment() == "lyxcode")
209 os << "\\SpecialChar ~\n";
214 else if (t.cat() == catBegin) {
215 // special handling of size changes
216 bool const is_size = is_known(p.next_token().cs(), known_sizes);
217 string const s = parse_text(p, FLAG_BRACE_LAST, outer);
218 if (s.empty() && p.next_token().character() == '`')
219 ; // ignore it in {}``
220 else if (is_size || s == "[" || s == "]" || s == "*")
229 else if (t.cat() == catEnd) {
230 if (flags & FLAG_BRACE_LAST)
232 cerr << "stray '}' in text\n";
236 else if (t.cat() == catOther)
237 os << string(1, t.character());
239 else if (t.cat() == catComment)
246 else if (t.cs() == "ldots") {
248 os << "\n\\SpecialChar \\ldots{}\n";
251 else if (t.cs() == "(") {
252 begin_inset(os, "Formula");
254 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
259 else if (t.cs() == "[") {
260 begin_inset(os, "Formula");
262 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
267 else if (t.cs() == "begin") {
268 string const name = p.getArg('{', '}');
269 active_environments.push_back(name);
270 if (is_math_env(name)) {
271 begin_inset(os, "Formula ");
272 os << "\\begin{" << name << "}";
273 parse_math(p, os, FLAG_END, MATH_MODE);
274 os << "\\end{" << name << "}";
276 } else if (name == "tabular") {
277 begin_inset(os, "Tabular ");
278 handle_tabular(p, os);
280 } else if (name == "table" || name == "figure") {
281 string opts = p.getOpt();
282 begin_inset(os, "Float " + name + "\n");
284 os << "placement " << opts << '\n';
285 os << "wide false\ncollapsed false\n\n"
286 << "\\layout Standard\n";
287 parse_text(p, os, FLAG_END, outer);
289 } else if (name == "center") {
290 active_environments.pop_back();
292 active_environments.push_back(name);
293 os << "\\align center\n";
294 parse_text(p, os, FLAG_END, outer);
295 } else if (name == "enumerate" || name == "itemize"
296 || name == "lyxlist") {
297 size_t const n = active_environments.size();
298 string const s = active_environments[n - 2];
299 bool const deeper = s == "enumerate" || s == "itemize"
302 os << "\n\\begin_deeper";
303 os << "\n\\layout " << cap(name) << "\n\n";
304 if (name == "lyxlist")
305 p.verbatim_item(); // swallow next arg
306 parse_text(p, os, FLAG_END, outer);
308 os << "\n\\end_deeper\n";
310 } else if (name == "thebibliography") {
311 p.verbatim_item(); // swallow next arg
312 parse_text(p, os, FLAG_END, outer);
313 os << "\n\\layout Bibliography\n\n";
316 parse_text(p, os, FLAG_END, outer);
320 else if (t.cs() == "end") {
321 if (flags & FLAG_END) {
322 // eat environment name
323 string const name = p.getArg('{', '}');
324 if (name != active_environment())
325 cerr << "\\end{" + name + "} does not match \\begin{"
326 + active_environment() + "}\n";
327 active_environments.pop_back();
331 p.error("found 'end' unexpectedly");
334 else if (t.cs() == "item") {
337 if (p.next_token().character() == '[') {
338 p.get_token(); // eat '['
339 s = parse_text(p, FLAG_BRACK_LAST, outer);
345 else if (t.cs() == "def") {
346 string name = p.get_token().cs();
347 while (p.next_token().cat() != catBegin)
348 name += p.get_token().asString();
349 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}');
352 else if (t.cs() == "par") {
354 if (p.next_token().cs() != "\\begin")
356 //cerr << "next token: '" << p.next_token().cs() << "'\n";
359 else if (is_known(t.cs(), known_headings)) {
360 string name = t.cs();
361 if (p.next_token().asInput() == "*") {
365 os << "\n\n\\layout " << cap(name) << "\n\n";
366 string opt = p.getOpt();
368 begin_inset(os, "OptArg\n");
369 os << "collapsed true\n\n\\layout Standard\n\n" << opt;
372 parse_text(p, os, FLAG_ITEM, outer);
373 os << "\n\n\\layout Standard\n\n";
376 else if (t.cs() == "includegraphics") {
377 map<string, string> opts = split_map(p.getArg('[', ']'));
378 string name = p.verbatim_item();
379 begin_inset(os, "Graphics ");
380 os << "\n\tfilename " << name << '\n';
381 if (opts.find("width") != opts.end())
382 os << "\twidth " << opts["width"] << '\n';
383 if (opts.find("height") != opts.end())
384 os << "\theight " << opts["height"] << '\n';
388 else if (t.cs() == "footnote") {
389 begin_inset(os, "Foot\n");
390 os << "collapsed true\n\n\\layout Standard\n\n";
391 parse_text(p, os, FLAG_ITEM, false);
395 else if (t.cs() == "ensuremath") {
396 string s = parse_text(p, FLAG_ITEM, false);
397 if (s == "±" || s == "³" || s == "²" || s == "µ")
400 handle_ert(os, "\\ensuremath{" + s + "}");
403 else if (t.cs() == "marginpar") {
404 begin_inset(os, "Marginal\n");
405 os << "collapsed true\n\n\\layout Standard\n\n";
406 parse_text(p, os, FLAG_ITEM, false);
410 else if (t.cs() == "hfill") {
415 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
416 skip_braces(p); // swallow this
418 else if (t.cs() == "tableofcontents")
419 skip_braces(p); // swallow this
421 else if (t.cs() == "textrm") {
422 os << "\n\\family roman \n";
423 parse_text(p, os, FLAG_ITEM, outer);
424 os << "\n\\family default \n";
427 else if (t.cs() == "textsf") {
428 os << "\n\\family sans \n";
429 parse_text(p, os, FLAG_ITEM, outer);
430 os << "\n\\family default \n";
433 else if (t.cs() == "texttt") {
434 os << "\n\\family typewriter \n";
435 parse_text(p, os, FLAG_ITEM, outer);
436 os << "\n\\family default \n";
439 else if (t.cs() == "textit") {
440 os << "\n\\shape italic \n";
441 parse_text(p, os, FLAG_ITEM, outer);
442 os << "\n\\shape default \n";
445 else if (t.cs() == "textsc") {
446 os << "\n\\noun on \n";
447 parse_text(p, os, FLAG_ITEM, outer);
448 os << "\n\\noun default \n";
451 else if (t.cs() == "textbf") {
452 os << "\n\\series bold \n";
453 parse_text(p, os, FLAG_ITEM, outer);
454 os << "\n\\series default \n";
457 else if (t.cs() == "underbar") {
458 os << "\n\\bar under \n";
459 parse_text(p, os, FLAG_ITEM, outer);
460 os << "\n\\bar default \n";
463 else if (t.cs() == "emph" || t.cs() == "noun") {
464 os << "\n\\" << t.cs() << " on \n";
465 parse_text(p, os, FLAG_ITEM, outer);
466 os << "\n\\" << t.cs() << " default \n";
469 else if (t.cs() == "bibitem") {
470 os << "\n\\layout Bibliography\n\\bibitem ";
472 os << '{' << p.verbatim_item() << '}' << "\n\n";
475 else if (is_known(t.cs(), known_latex_commands)) {
476 begin_inset(os, "LatexCommand ");
477 os << '\\' << t.cs();
480 os << '{' << p.verbatim_item() << '}';
484 else if (is_known(t.cs(), known_quotes)) {
485 char const ** where = is_known(t.cs(), known_quotes);
486 begin_inset(os, "Quotes ");
487 os << known_coded_quotes[where - known_quotes];
492 else if (is_known(t.cs(), known_sizes)) {
493 char const ** where = is_known(t.cs(), known_sizes);
494 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
497 else if (t.cs() == "LyX" || t.cs() == "TeX"
498 || t.cs() == "LaTeX" || t.cs() == "LaTeXe") {
500 skip_braces(p); // eat {}
503 else if (t.cs() == "lyxarrow") {
504 os << "\\SpecialChar \\menuseparator\n";
508 else if (t.cs() == "ldots") {
509 os << "\\SpecialChar \\ldots{}\n";
513 else if (t.cs() == "@") {
514 os << "\\SpecialChar \\@";
518 else if (t.cs() == "textasciitilde") {
523 else if (t.cs() == "textasciicircum") {
528 else if (t.cs() == "textbackslash") {
529 os << "\n\\backslash\n";
533 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#" || t.cs() == "$"
534 || t.cs() == "{" || t.cs() == "}" || t.cs() == "%")
537 else if (t.cs() == "char") {
538 if (p.next_token().character() == '`') {
540 if (p.next_token().cs() == "\"") {
545 handle_ert(os, "\\char`");
548 handle_ert(os, "\\char");
552 else if (t.cs() == "\"") {
553 string const name = p.verbatim_item();
554 if (name == "a") os << 'ä';
555 else if (name == "o") os << 'ö';
556 else if (name == "u") os << 'ü';
557 else if (name == "A") os << 'Ä';
558 else if (name == "O") os << 'Ö';
559 else if (name == "U") os << 'Ü';
560 else handle_ert(os, "\"{" + name + "}");
563 else if (t.cs() == "=" || t.cs() == "H" || t.cs() == "c"
564 || t.cs() == "^" || t.cs() == "'" || t.cs() == "~") {
565 // we need the trim as the LyX parser chokes on such spaces
566 os << "\n\\i \\" << t.cs() << "{"
567 << trim(parse_text(p, FLAG_ITEM, outer), " ") << "}\n";
570 else if (t.cs() == "ss")
573 else if (t.cs() == "i" || t.cs() == "j")
574 os << "\\" << t.cs() << ' ';
576 else if (t.cs() == "-")
577 os << "\\SpecialChar \\-\n";
579 else if (t.cs() == "\\")
580 os << "\n\\newline\n";
582 else if (t.cs() == "lyxrightaddress") {
583 os << "\n\\layout Right Address\n";
584 parse_text(p, os, FLAG_ITEM, outer);
585 os << "\n\\layout Standard\n";
588 else if (t.cs() == "lyxaddress") {
589 os << "\n\\layout Address\n";
590 parse_text(p, os, FLAG_ITEM, outer);
591 os << "\n\\layout Standard\n";
594 else if (t.cs() == "input")
595 handle_ert(os, "\\input{" + p.verbatim_item() + "}\n");
597 else if (t.cs() == "fancyhead") {
601 ss << '{' << p.verbatim_item() << "}\n";
602 handle_ert(os, ss.str());
606 //cerr << "#: " << t << " mode: " << mode << endl;
607 // heuristic: read up to next non-nested space
609 string s = t.asInput();
610 string z = p.verbatim_item();
611 while (p.good() && z != " " && z.size()) {
612 //cerr << "read: " << z << endl;
614 z = p.verbatim_item();
616 cerr << "found ERT: " << s << endl;
617 handle_ert(os, s + ' ');
619 handle_ert(os, t.asInput() + ' ');
622 if (flags & FLAG_LEAVE) {
623 flags &= ~FLAG_LEAVE;
630 string parse_text(Parser & p, unsigned flags, const bool outer)
633 parse_text(p, os, flags, outer);