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_latex_commands[] = { "ref", "cite", "label", "index",
34 "printindex", "pageref", "url", 0 };
36 // LaTeX names for quotes
37 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
38 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
40 // the same as known_quotes with .lyx names
41 char const * known_coded_quotes[] = { "gld", "grd", "gld",
42 "grd", "gls", "fls", "frd", 0};
44 char const * known_sizes[] = { "tiny", "scriptsize", "footnotesize",
45 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
47 char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
48 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
59 // splits "x=z, y=b" into a map
60 map<string, string> split_map(string const & s)
62 map<string, string> res;
65 for (size_t i = 0; i < v.size(); ++i) {
66 size_t const pos = v[i].find('=');
67 string const index = v[i].substr(0, pos);
68 string const value = v[i].substr(pos + 1, string::npos);
69 res[trim(index)] = trim(value);
75 void begin_inset(ostream & os, string const & name)
77 os << "\n\\begin_inset " << name;
81 void end_inset(ostream & os)
83 os << "\n\\end_inset \n\n";
87 void skip_braces(Parser & p)
89 if (p.next_token().cat() != catBegin)
92 if (p.next_token().cat() == catEnd) {
100 void handle_ert(ostream & os, string const & s)
102 begin_inset(os, "ERT");
103 os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
104 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
106 os << "\n\\backslash \n";
114 void handle_par(ostream & os)
116 if (active_environments.empty())
119 string s = active_environment();
120 if (s == "document" || s == "table")
121 os << "Standard\n\n";
122 else if (s == "center")
123 os << "Standard\n\n\\align center\n";
124 else if (s == "lyxcode")
125 os << "LyX-Code\n\n";
126 else if (s == "lyxlist")
128 else if (s == "thebibliography")
129 os << "Bibliography\n\n";
131 os << cap(s) << "\n\n";
136 isLayout(string const name) : name_(name) {}
137 bool operator()(LyXLayout_ptr const & ptr) {
138 return ptr.get() && ptr->latexname() == name_;
145 LyXLayout_ptr findLayout(LyXTextClass const & textclass,
148 LyXTextClass::const_iterator it = textclass.begin();
149 LyXTextClass::const_iterator end = textclass.end();
150 it = std::find_if(it, end, isLayout(name));
151 return (it == end) ? LyXLayout_ptr() : *it;
155 void output_layout(ostream & os, LyXLayout_ptr const & layout_ptr,
156 Parser & p, bool outer, LyXTextClass const & textclass)
158 string name = layout_ptr->name();
159 os << "\n\n\\layout " << name << "\n\n";
160 if (layout_ptr->optionalargs > 0) {
162 if (p.next_token().character() == '[') {
163 p.get_token(); // eat '['
164 begin_inset(os, "OptArg\n");
165 os << "collapsed true\n\n\\layout Standard\n\n";
166 parse_text(p, os, FLAG_BRACK_LAST, outer, textclass);
170 parse_text(p, os, FLAG_ITEM, outer, textclass);
171 os << "\n\n\\layout Standard\n\n";
174 } // anonymous namespace
177 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
178 LyXTextClass const & textclass)
181 LyXLayout_ptr layout_ptr;
182 Token const & t = p.get_token();
185 cerr << "t: " << t << " flags: " << flags << "\n";
188 if (flags & FLAG_ITEM) {
189 if (t.cat() == catSpace)
193 if (t.cat() == catBegin) {
194 // skip the brace and collect everything to the next matching
196 flags |= FLAG_BRACE_LAST;
200 // handle only this single token, leave the loop if done
204 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
210 if (t.cat() == catMath) {
211 // we are inside some text mode thingy, so opening new math is allowed
212 begin_inset(os, "Formula ");
213 Token const & n = p.get_token();
214 if (n.cat() == catMath && outer) {
215 // TeX's $$...$$ syntax for displayed math
217 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
219 p.get_token(); // skip the second '$' token
221 // simple $...$ stuff
224 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
230 else if (t.cat() == catSuper || t.cat() == catSub)
231 cerr << "catcode " << t << " illegal in text mode\n";
233 // Basic support for english quotes. This should be
234 // extended to other quotes, but is not so easy (a
235 // left english quote is the same as a right german
237 else if (t.asInput() == "`"
238 && p.next_token().asInput() == "`") {
239 begin_inset(os, "Quotes ");
245 else if (t.asInput() == "'"
246 && p.next_token().asInput() == "'") {
247 begin_inset(os, "Quotes ");
255 else if (t.cat() == catLetter ||
256 t.cat() == catSpace ||
257 t.cat() == catOther ||
258 t.cat() == catAlign ||
259 t.cat() == catParameter)
262 else if (t.cat() == catNewline) {
263 if (p.next_token().cat() == catNewline) {
267 os << " "; // note the space
271 else if (t.cat() == catActive) {
272 if (t.character() == '~') {
273 if (active_environment() == "lyxcode")
276 os << "\\InsetSpace ~\n";
281 else if (t.cat() == catBegin) {
282 // special handling of size changes
283 bool const is_size = is_known(p.next_token().cs(), known_sizes);
284 string const s = parse_text(p, FLAG_BRACE_LAST, outer, textclass);
285 if (s.empty() && p.next_token().character() == '`')
286 ; // ignore it in {}``
287 else if (is_size || s == "[" || s == "]" || s == "*")
296 else if (t.cat() == catEnd) {
297 if (flags & FLAG_BRACE_LAST)
299 cerr << "stray '}' in text\n";
303 else if (t.cat() == catOther)
304 os << string(1, t.character());
306 else if (t.cat() == catComment)
313 else if (t.cs() == "(") {
314 begin_inset(os, "Formula");
316 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
321 else if (t.cs() == "[") {
322 begin_inset(os, "Formula");
324 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
329 else if (t.cs() == "begin") {
330 string const name = p.getArg('{', '}');
331 const bool is_starred = suffixIs(name, '*');
332 string const unstarred_name = rtrim(name, "*");
333 active_environments.push_back(name);
334 if (is_math_env(name)) {
335 begin_inset(os, "Formula ");
336 os << "\\begin{" << name << "}";
337 parse_math(p, os, FLAG_END, MATH_MODE);
338 os << "\\end{" << name << "}";
340 } else if (name == "tabular") {
341 begin_inset(os, "Tabular ");
342 handle_tabular(p, os, textclass);
344 } else if (textclass.floats().typeExist(unstarred_name)) {
345 begin_inset(os, "Float " + unstarred_name + "\n");
346 if (p.next_token().asInput() == "[") {
348 << p.getArg('[', ']') << '\n';
350 os << "wide " << tostr(is_starred)
351 << "\ncollapsed false\n\n"
352 << "\\layout Standard\n";
353 parse_text(p, os, FLAG_END, outer,
356 } else if (name == "center") {
358 parse_text(p, os, FLAG_END, outer,
360 // The single '=' is meant here.
361 } else if ((layout_ptr = findLayout(textclass, t.cs())).get() &&
362 layout_ptr->isEnvironment()) {
363 size_t const n = active_environments.size();
364 string const s = active_environments[n - 2];
365 bool const deeper = s == "enumerate" || s == "itemize"
368 os << "\n\\begin_deeper";
369 os << "\n\\layout " << layout_ptr->name()
371 switch (layout_ptr->latextype) {
372 case LATEX_LIST_ENVIRONMENT:
373 os << "\\labelwidthstring "
374 << p.verbatim_item() << '\n';
376 case LATEX_BIB_ENVIRONMENT:
377 p.verbatim_item(); // swallow next arg
382 parse_text(p, os, FLAG_END, outer, textclass);
384 os << "\n\\end_deeper\n";
388 parse_text(p, os, FLAG_END, outer, textclass);
392 else if (t.cs() == "end") {
393 if (flags & FLAG_END) {
394 // eat environment name
395 string const name = p.getArg('{', '}');
396 if (name != active_environment())
397 cerr << "\\end{" + name + "} does not match \\begin{"
398 + active_environment() + "}\n";
399 active_environments.pop_back();
403 p.error("found 'end' unexpectedly");
406 else if (t.cs() == "item") {
407 // should be done automatically by Parser::tokenize
410 if (p.next_token().character() == '[') {
411 p.get_token(); // eat '['
412 s = parse_text(p, FLAG_BRACK_LAST, outer, textclass);
419 else if (t.cs() == "def") {
420 string name = p.get_token().cs();
421 while (p.next_token().cat() != catBegin)
422 name += p.get_token().asString();
423 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}');
426 else if (t.cs() == "par") {
428 if (p.next_token().cs() != "\\begin")
430 //cerr << "next token: '" << p.next_token().cs() << "'\n";
433 // Must attempt to parse "Section*" before "Section".
434 else if ((p.next_token().asInput() == "*") &&
435 // The single '=' is meant here.
436 (layout_ptr = findLayout(textclass,
437 t.cs() + '*')).get() &&
438 layout_ptr->isCommand()) {
440 output_layout(os, layout_ptr, p, outer, textclass);
443 // The single '=' is meant here.
444 else if ((layout_ptr = findLayout(textclass, t.cs())).get() &&
445 layout_ptr->isCommand()) {
446 output_layout(os, layout_ptr, p, outer, textclass);
449 else if (t.cs() == "includegraphics") {
450 map<string, string> opts = split_map(p.getArg('[', ']'));
451 string name = p.verbatim_item();
452 begin_inset(os, "Graphics ");
453 os << "\n\tfilename " << name << '\n';
454 if (opts.find("width") != opts.end())
455 os << "\twidth " << opts["width"] << '\n';
456 if (opts.find("height") != opts.end())
457 os << "\theight " << opts["height"] << '\n';
461 else if (t.cs() == "footnote") {
462 begin_inset(os, "Foot\n");
463 os << "collapsed true\n\n\\layout Standard\n\n";
464 parse_text(p, os, FLAG_ITEM, false, textclass);
468 else if (t.cs() == "ensuremath") {
469 string s = parse_text(p, FLAG_ITEM, false, textclass);
470 if (s == "±" || s == "³" || s == "²" || s == "µ")
473 handle_ert(os, "\\ensuremath{" + s + "}");
476 else if (t.cs() == "marginpar") {
477 begin_inset(os, "Marginal\n");
478 os << "collapsed true\n\n\\layout Standard\n\n";
479 parse_text(p, os, FLAG_ITEM, false, textclass);
483 else if (t.cs() == "hfill") {
488 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
489 skip_braces(p); // swallow this
491 else if (t.cs() == "tableofcontents") {
492 begin_inset(os, "LatexCommand ");
493 os << '\\' << t.cs() << "{}\n";
495 skip_braces(p); // swallow this
499 else if (t.cs() == "textrm") {
500 os << "\n\\family roman \n";
501 parse_text(p, os, FLAG_ITEM, outer, textclass);
502 os << "\n\\family default \n";
505 else if (t.cs() == "textsf") {
506 os << "\n\\family sans \n";
507 parse_text(p, os, FLAG_ITEM, outer, textclass);
508 os << "\n\\family default \n";
511 else if (t.cs() == "texttt") {
512 os << "\n\\family typewriter \n";
513 parse_text(p, os, FLAG_ITEM, outer, textclass);
514 os << "\n\\family default \n";
517 else if (t.cs() == "textit") {
518 os << "\n\\shape italic \n";
519 parse_text(p, os, FLAG_ITEM, outer, textclass);
520 os << "\n\\shape default \n";
523 else if (t.cs() == "textsc") {
524 os << "\n\\noun on \n";
525 parse_text(p, os, FLAG_ITEM, outer, textclass);
526 os << "\n\\noun default \n";
529 else if (t.cs() == "textbf") {
530 os << "\n\\series bold \n";
531 parse_text(p, os, FLAG_ITEM, outer, textclass);
532 os << "\n\\series default \n";
535 else if (t.cs() == "underbar") {
536 os << "\n\\bar under \n";
537 parse_text(p, os, FLAG_ITEM, outer, textclass);
538 os << "\n\\bar default \n";
541 else if (t.cs() == "emph" || t.cs() == "noun") {
542 os << "\n\\" << t.cs() << " on \n";
543 parse_text(p, os, FLAG_ITEM, outer, textclass);
544 os << "\n\\" << t.cs() << " default \n";
547 else if (t.cs() == "bibitem") {
548 os << "\n\\layout Bibliography\n\\bibitem ";
550 os << '{' << p.verbatim_item() << '}' << "\n";
553 else if (is_known(t.cs(), known_latex_commands)) {
554 begin_inset(os, "LatexCommand ");
555 os << '\\' << t.cs();
558 os << '{' << p.verbatim_item() << "}\n";
562 else if (is_known(t.cs(), known_quotes)) {
563 char const ** where = is_known(t.cs(), known_quotes);
564 begin_inset(os, "Quotes ");
565 os << known_coded_quotes[where - known_quotes];
570 else if (is_known(t.cs(), known_sizes)) {
571 char const ** where = is_known(t.cs(), known_sizes);
572 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
575 else if (t.cs() == "LyX" || t.cs() == "TeX"
576 || t.cs() == "LaTeX") {
578 skip_braces(p); // eat {}
581 else if (t.cs() == "LaTeXe") {
583 skip_braces(p); // eat {}
586 else if (t.cs() == "ldots") {
588 os << "\\SpecialChar \\ldots{}\n";
591 else if (t.cs() == "lyxarrow") {
592 os << "\\SpecialChar \\menuseparator\n";
596 else if (t.cs() == "ldots") {
597 os << "\\SpecialChar \\ldots{}\n";
601 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
602 os << "\\SpecialChar \\@.\n";
606 else if (t.cs() == "-")
607 os << "\\SpecialChar \\-\n";
609 else if (t.cs() == "textasciitilde") {
614 else if (t.cs() == "textasciicircum") {
619 else if (t.cs() == "textbackslash") {
620 os << "\n\\backslash \n";
624 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
625 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
629 else if (t.cs() == "char") {
630 if (p.next_token().character() == '`') {
632 if (p.next_token().cs() == "\"") {
637 handle_ert(os, "\\char`");
640 handle_ert(os, "\\char");
644 else if (t.cs() == "\"") {
645 string const name = p.verbatim_item();
646 if (name == "a") os << 'ä';
647 else if (name == "o") os << 'ö';
648 else if (name == "u") os << 'ü';
649 else if (name == "A") os << 'Ä';
650 else if (name == "O") os << 'Ö';
651 else if (name == "U") os << 'Ü';
652 else handle_ert(os, "\"{" + name + "}");
655 else if (t.cs() == "=" || t.cs() == "H" || t.cs() == "c"
656 || t.cs() == "^" || t.cs() == "'" || t.cs() == "~") {
657 // we need the trim as the LyX parser chokes on such spaces
658 os << "\n\\i \\" << t.cs() << "{"
659 << trim(parse_text(p, FLAG_ITEM, outer, textclass), " ") << "}\n";
662 else if (t.cs() == "ss")
665 else if (t.cs() == "i" || t.cs() == "j")
666 os << "\\" << t.cs() << ' ';
668 else if (t.cs() == "\\")
669 os << "\n\\newline \n";
671 else if (t.cs() == "input")
672 handle_ert(os, "\\input{" + p.verbatim_item() + "}\n");
674 else if (t.cs() == "fancyhead") {
678 ss << '{' << p.verbatim_item() << "}\n";
679 handle_ert(os, ss.str());
683 //cerr << "#: " << t << " mode: " << mode << endl;
684 // heuristic: read up to next non-nested space
686 string s = t.asInput();
687 string z = p.verbatim_item();
688 while (p.good() && z != " " && z.size()) {
689 //cerr << "read: " << z << endl;
691 z = p.verbatim_item();
693 cerr << "found ERT: " << s << endl;
694 handle_ert(os, s + ' ');
696 handle_ert(os, t.asInput() + ' ');
699 if (flags & FLAG_LEAVE) {
700 flags &= ~FLAG_LEAVE;
707 string parse_text(Parser & p, unsigned flags, const bool outer,
708 LyXTextClass const & textclass)
711 parse_text(p, os, flags, outer, textclass);