1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
10 #include "FloatList.h"
11 #include "lyxtextclass.h"
12 #include "support/lstrings.h"
13 #include "support/tostr.h"
24 using std::ostringstream;
28 using lyx::support::rtrim;
29 using lyx::support::suffixIs;
33 char const * known_headings[] = { "caption", "title", "author", "date",
34 "paragraph", "chapter", "section", "subsection", "subsubsection", 0 };
36 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
37 "printindex", "pageref", "url", 0 };
39 // LaTeX names for quotes
40 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
41 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
43 // the same as known_quotes with .lyx names
44 char const * known_coded_quotes[] = { "gld", "grd", "gld",
45 "grd", "gls", "fls", "frd", 0};
47 char const * known_sizes[] = { "tiny", "scriptsize", "footnotesize",
48 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
50 char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
51 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
62 // splits "x=z, y=b" into a map
63 map<string, string> split_map(string const & s)
65 map<string, string> res;
68 for (size_t i = 0; i < v.size(); ++i) {
69 size_t const pos = v[i].find('=');
70 string const index = v[i].substr(0, pos);
71 string const value = v[i].substr(pos + 1, string::npos);
72 res[trim(index)] = trim(value);
78 void begin_inset(ostream & os, string const & name)
80 os << "\n\\begin_inset " << name;
84 void end_inset(ostream & os)
86 os << "\n\\end_inset\n\n";
90 void skip_braces(Parser & p)
92 if (p.next_token().cat() != catBegin)
95 if (p.next_token().cat() == catEnd) {
103 void handle_ert(ostream & os, string const & s)
105 begin_inset(os, "ERT");
106 os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
107 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
109 os << "\n\\backslash\n";
117 void handle_par(ostream & os)
119 if (active_environments.empty())
122 string s = active_environment();
123 if (s == "document" || s == "table")
124 os << "Standard\n\n";
125 else if (s == "center")
126 os << "Standard\n\n\\align center\n";
127 else if (s == "lyxcode")
128 os << "LyX-Code\n\n";
129 else if (s == "lyxlist")
131 else if (s == "thebibliography")
132 os << "Bibliography\n\n";
134 os << cap(s) << "\n\n";
138 } // anonymous namespace
141 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
142 LyXTextClass const & textclass)
145 Token const & t = p.get_token();
148 cerr << "t: " << t << " flags: " << flags << "\n";
151 if (flags & FLAG_ITEM) {
152 if (t.cat() == catSpace)
156 if (t.cat() == catBegin) {
157 // skip the brace and collect everything to the next matching
159 flags |= FLAG_BRACE_LAST;
163 // handle only this single token, leave the loop if done
167 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
173 if (t.cat() == catMath) {
174 // we are inside some text mode thingy, so opening new math is allowed
175 begin_inset(os, "Formula ");
176 Token const & n = p.get_token();
177 if (n.cat() == catMath && outer) {
178 // TeX's $$...$$ syntax for displayed math
180 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
182 p.get_token(); // skip the second '$' token
184 // simple $...$ stuff
187 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
193 else if (t.cat() == catSuper || t.cat() == catSub)
194 cerr << "catcode " << t << " illegal in text mode\n";
196 else if (t.cat() == catLetter ||
197 t.cat() == catSpace ||
198 t.cat() == catOther ||
199 t.cat() == catAlign ||
200 t.cat() == catParameter)
203 else if (t.cat() == catNewline) {
204 if (p.next_token().cat() == catNewline) {
208 os << " "; // note the space
212 else if (t.cat() == catActive) {
213 if (t.character() == '~') {
214 if (active_environment() == "lyxcode")
217 os << "\\SpecialChar ~\n";
222 else if (t.cat() == catBegin) {
223 // special handling of size changes
224 bool const is_size = is_known(p.next_token().cs(), known_sizes);
225 string const s = parse_text(p, FLAG_BRACE_LAST, outer, textclass);
226 if (s.empty() && p.next_token().character() == '`')
227 ; // ignore it in {}``
228 else if (is_size || s == "[" || s == "]" || s == "*")
237 else if (t.cat() == catEnd) {
238 if (flags & FLAG_BRACE_LAST)
240 cerr << "stray '}' in text\n";
244 else if (t.cat() == catOther)
245 os << string(1, t.character());
247 else if (t.cat() == catComment)
254 else if (t.cs() == "ldots") {
256 os << "\n\\SpecialChar \\ldots{}\n";
259 else if (t.cs() == "(") {
260 begin_inset(os, "Formula");
262 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
267 else if (t.cs() == "[") {
268 begin_inset(os, "Formula");
270 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
275 else if (t.cs() == "begin") {
276 string const name = p.getArg('{', '}');
277 active_environments.push_back(name);
278 if (is_math_env(name)) {
279 begin_inset(os, "Formula ");
280 os << "\\begin{" << name << "}";
281 parse_math(p, os, FLAG_END, MATH_MODE);
282 os << "\\end{" << name << "}";
287 if (name == "tabular") {
288 begin_inset(os, "Tabular ");
289 handle_tabular(p, os, textclass);
294 bool is_starred = suffixIs(name, '*');
295 string unstarred_name = rtrim(name, "*");
296 if (textclass.floats().typeExist(unstarred_name)) {
297 string opts = p.getOpt();
298 begin_inset(os, "Float " + unstarred_name + "\n");
300 os << "placement " << opts << '\n';
301 os << "wide " << tostr(is_starred)
302 << "\ncollapsed false\n\n"
303 << "\\layout Standard\n";
304 parse_text(p, os, FLAG_END, outer,
307 } else if (name == "center") {
309 parse_text(p, os, FLAG_END, outer,
311 } else if (name == "enumerate" || name == "itemize"
312 || name == "lyxlist") {
313 size_t const n = active_environments.size();
314 string const s = active_environments[n - 2];
315 bool const deeper = s == "enumerate" || s == "itemize"
318 os << "\n\\begin_deeper";
319 os << "\n\\layout " << cap(name) << "\n\n";
320 if (name == "lyxlist")
321 p.verbatim_item(); // swallow next arg
322 parse_text(p, os, FLAG_END, outer, textclass);
324 os << "\n\\end_deeper\n";
326 } else if (name == "thebibliography") {
327 p.verbatim_item(); // swallow next arg
328 parse_text(p, os, FLAG_END, outer, textclass);
329 os << "\n\\layout Bibliography\n\n";
332 parse_text(p, os, FLAG_END, outer, textclass);
336 else if (t.cs() == "end") {
337 if (flags & FLAG_END) {
338 // eat environment name
339 string const name = p.getArg('{', '}');
340 if (name != active_environment())
341 cerr << "\\end{" + name + "} does not match \\begin{"
342 + active_environment() + "}\n";
343 active_environments.pop_back();
347 p.error("found 'end' unexpectedly");
350 else if (t.cs() == "item") {
353 if (p.next_token().character() == '[') {
354 p.get_token(); // eat '['
355 s = parse_text(p, FLAG_BRACK_LAST, outer, textclass);
361 else if (t.cs() == "def") {
362 string name = p.get_token().cs();
363 while (p.next_token().cat() != catBegin)
364 name += p.get_token().asString();
365 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}');
368 else if (t.cs() == "par") {
370 if (p.next_token().cs() != "\\begin")
372 //cerr << "next token: '" << p.next_token().cs() << "'\n";
375 else if (is_known(t.cs(), known_headings)) {
376 string name = t.cs();
377 if (p.next_token().asInput() == "*") {
381 os << "\n\n\\layout " << cap(name) << "\n\n";
382 string opt = p.getOpt();
384 begin_inset(os, "OptArg\n");
385 os << "collapsed true\n\n\\layout Standard\n\n" << opt;
388 parse_text(p, os, FLAG_ITEM, outer, textclass);
389 os << "\n\n\\layout Standard\n\n";
392 else if (t.cs() == "includegraphics") {
393 map<string, string> opts = split_map(p.getArg('[', ']'));
394 string name = p.verbatim_item();
395 begin_inset(os, "Graphics ");
396 os << "\n\tfilename " << name << '\n';
397 if (opts.find("width") != opts.end())
398 os << "\twidth " << opts["width"] << '\n';
399 if (opts.find("height") != opts.end())
400 os << "\theight " << opts["height"] << '\n';
404 else if (t.cs() == "footnote") {
405 begin_inset(os, "Foot\n");
406 os << "collapsed true\n\n\\layout Standard\n\n";
407 parse_text(p, os, FLAG_ITEM, false, textclass);
411 else if (t.cs() == "ensuremath") {
412 string s = parse_text(p, FLAG_ITEM, false, textclass);
413 if (s == "±" || s == "³" || s == "²" || s == "µ")
416 handle_ert(os, "\\ensuremath{" + s + "}");
419 else if (t.cs() == "marginpar") {
420 begin_inset(os, "Marginal\n");
421 os << "collapsed true\n\n\\layout Standard\n\n";
422 parse_text(p, os, FLAG_ITEM, false, textclass);
426 else if (t.cs() == "hfill") {
431 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
432 skip_braces(p); // swallow this
434 else if (t.cs() == "tableofcontents")
435 skip_braces(p); // swallow this
437 else if (t.cs() == "textrm") {
438 os << "\n\\family roman \n";
439 parse_text(p, os, FLAG_ITEM, outer, textclass);
440 os << "\n\\family default \n";
443 else if (t.cs() == "textsf") {
444 os << "\n\\family sans \n";
445 parse_text(p, os, FLAG_ITEM, outer, textclass);
446 os << "\n\\family default \n";
449 else if (t.cs() == "texttt") {
450 os << "\n\\family typewriter \n";
451 parse_text(p, os, FLAG_ITEM, outer, textclass);
452 os << "\n\\family default \n";
455 else if (t.cs() == "textit") {
456 os << "\n\\shape italic \n";
457 parse_text(p, os, FLAG_ITEM, outer, textclass);
458 os << "\n\\shape default \n";
461 else if (t.cs() == "textsc") {
462 os << "\n\\noun on \n";
463 parse_text(p, os, FLAG_ITEM, outer, textclass);
464 os << "\n\\noun default \n";
467 else if (t.cs() == "textbf") {
468 os << "\n\\series bold \n";
469 parse_text(p, os, FLAG_ITEM, outer, textclass);
470 os << "\n\\series default \n";
473 else if (t.cs() == "underbar") {
474 os << "\n\\bar under \n";
475 parse_text(p, os, FLAG_ITEM, outer, textclass);
476 os << "\n\\bar default \n";
479 else if (t.cs() == "emph" || t.cs() == "noun") {
480 os << "\n\\" << t.cs() << " on \n";
481 parse_text(p, os, FLAG_ITEM, outer, textclass);
482 os << "\n\\" << t.cs() << " default \n";
485 else if (t.cs() == "bibitem") {
486 os << "\n\\layout Bibliography\n\\bibitem ";
488 os << '{' << p.verbatim_item() << '}' << "\n\n";
491 else if (is_known(t.cs(), known_latex_commands)) {
492 begin_inset(os, "LatexCommand ");
493 os << '\\' << t.cs();
496 os << '{' << p.verbatim_item() << '}';
500 else if (is_known(t.cs(), known_quotes)) {
501 char const ** where = is_known(t.cs(), known_quotes);
502 begin_inset(os, "Quotes ");
503 os << known_coded_quotes[where - known_quotes];
508 else if (is_known(t.cs(), known_sizes)) {
509 char const ** where = is_known(t.cs(), known_sizes);
510 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
513 else if (t.cs() == "LyX" || t.cs() == "TeX"
514 || t.cs() == "LaTeX") {
516 skip_braces(p); // eat {}
519 else if (t.cs() == "LaTeXe") {
521 skip_braces(p); // eat {}
524 else if (t.cs() == "lyxarrow") {
525 os << "\\SpecialChar \\menuseparator\n";
529 else if (t.cs() == "ldots") {
530 os << "\\SpecialChar \\ldots{}\n";
534 else if (t.cs() == "@") {
535 os << "\\SpecialChar \\@";
539 else if (t.cs() == "textasciitilde") {
544 else if (t.cs() == "textasciicircum") {
549 else if (t.cs() == "textbackslash") {
550 os << "\n\\backslash\n";
554 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#" || t.cs() == "$"
555 || t.cs() == "{" || t.cs() == "}" || t.cs() == "%")
558 else if (t.cs() == "char") {
559 if (p.next_token().character() == '`') {
561 if (p.next_token().cs() == "\"") {
566 handle_ert(os, "\\char`");
569 handle_ert(os, "\\char");
573 else if (t.cs() == "\"") {
574 string const name = p.verbatim_item();
575 if (name == "a") os << 'ä';
576 else if (name == "o") os << 'ö';
577 else if (name == "u") os << 'ü';
578 else if (name == "A") os << 'Ä';
579 else if (name == "O") os << 'Ö';
580 else if (name == "U") os << 'Ü';
581 else handle_ert(os, "\"{" + name + "}");
584 else if (t.cs() == "=" || t.cs() == "H" || t.cs() == "c"
585 || t.cs() == "^" || t.cs() == "'" || t.cs() == "~") {
586 // we need the trim as the LyX parser chokes on such spaces
587 os << "\n\\i \\" << t.cs() << "{"
588 << trim(parse_text(p, FLAG_ITEM, outer, textclass), " ") << "}\n";
591 else if (t.cs() == "ss")
594 else if (t.cs() == "i" || t.cs() == "j")
595 os << "\\" << t.cs() << ' ';
597 else if (t.cs() == "-")
598 os << "\\SpecialChar \\-\n";
600 else if (t.cs() == "\\")
601 os << "\n\\newline\n";
603 else if (t.cs() == "lyxrightaddress") {
604 os << "\n\\layout Right Address\n";
605 parse_text(p, os, FLAG_ITEM, outer, textclass);
606 os << "\n\\layout Standard\n";
609 else if (t.cs() == "lyxaddress") {
610 os << "\n\\layout Address\n";
611 parse_text(p, os, FLAG_ITEM, outer, textclass);
612 os << "\n\\layout Standard\n";
615 else if (t.cs() == "input")
616 handle_ert(os, "\\input{" + p.verbatim_item() + "}\n");
618 else if (t.cs() == "fancyhead") {
622 ss << '{' << p.verbatim_item() << "}\n";
623 handle_ert(os, ss.str());
627 //cerr << "#: " << t << " mode: " << mode << endl;
628 // heuristic: read up to next non-nested space
630 string s = t.asInput();
631 string z = p.verbatim_item();
632 while (p.good() && z != " " && z.size()) {
633 //cerr << "read: " << z << endl;
635 z = p.verbatim_item();
637 cerr << "found ERT: " << s << endl;
638 handle_ert(os, s + ' ');
640 handle_ert(os, t.asInput() + ' ');
643 if (flags & FLAG_LEAVE) {
644 flags &= ~FLAG_LEAVE;
651 string parse_text(Parser & p, unsigned flags, const bool outer,
652 LyXTextClass const & textclass)
655 parse_text(p, os, flags, outer, textclass);