1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
11 #include "FloatList.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;
32 // thin wrapper around parse_text using a string
33 string parse_text(Parser & p, unsigned flags, const bool outer,
37 parse_text(p, os, flags, outer, context);
41 // parses a subdocument, usually useful in insets (whence the name)
42 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
45 Context newcontext(true, context.textclass);
46 parse_text(p, os, flags, outer, newcontext);
47 newcontext.check_end_layout(os);
51 // parses a paragraph snippet, useful for example for \emph{...}
52 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
55 Context newcontext(false, context.textclass);
56 parse_text(p, os, flags, outer, newcontext);
57 // should not be needed
58 newcontext.check_end_layout(os);
64 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
65 "printindex", "pageref", "url", 0 };
67 // LaTeX names for quotes
68 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
69 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
71 // the same as known_quotes with .lyx names
72 char const * known_coded_quotes[] = { "gld", "grd", "gld",
73 "grd", "gls", "fls", "frd", 0};
75 char const * known_sizes[] = { "tiny", "scriptsize", "footnotesize",
76 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
78 char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
79 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
81 // splits "x=z, y=b" into a map
82 map<string, string> split_map(string const & s)
84 map<string, string> res;
87 for (size_t i = 0; i < v.size(); ++i) {
88 size_t const pos = v[i].find('=');
89 string const index = v[i].substr(0, pos);
90 string const value = v[i].substr(pos + 1, string::npos);
91 res[trim(index)] = trim(value);
97 void begin_inset(ostream & os, string const & name)
99 os << "\n\\begin_inset " << name;
103 void end_inset(ostream & os)
105 os << "\n\\end_inset \n\n";
109 void skip_braces(Parser & p)
111 if (p.next_token().cat() != catBegin)
114 if (p.next_token().cat() == catEnd) {
122 void handle_ert(ostream & os, string const & s, Context const & context)
124 Context newcontext(true, context.textclass);
125 begin_inset(os, "ERT");
126 os << "\nstatus Collapsed\n";
127 newcontext.check_layout(os);
128 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
130 os << "\n\\backslash \n";
134 newcontext.check_end_layout(os);
140 isLayout(string const name) : name_(name) {}
141 bool operator()(LyXLayout_ptr const & ptr) {
142 return ptr.get() && ptr->latexname() == name_;
149 LyXLayout_ptr findLayout(LyXTextClass const & textclass,
152 LyXTextClass::const_iterator it = textclass.begin();
153 LyXTextClass::const_iterator end = textclass.end();
154 it = std::find_if(it, end, isLayout(name));
155 return (it == end) ? LyXLayout_ptr() : *it;
159 void output_command_layout(ostream & os, Parser & p, bool outer,
160 Context & parent_context,
161 LyXLayout_ptr newlayout)
163 // parent_context.dump(os, "#parent_context before output_command_layout");
164 parent_context.check_end_layout(os);
165 Context context(true, parent_context.textclass, newlayout,
166 parent_context.layout);
167 context.check_layout(os);
168 if (context.layout->optionalargs > 0) {
170 if (p.next_token().character() == '[') {
171 p.get_token(); // eat '['
172 begin_inset(os, "OptArg\n");
173 os << "collapsed true\n";
174 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
178 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
179 context.check_end_layout(os);
180 // context.dump(os, "#context after output_command_layout");
181 // parent_context.dump(os, "#parent_context after output_command_layout");
185 void parse_environment(Parser & p, ostream & os, bool outer,
186 Context & parent_context)
188 // parent_context.dump(os, "#parent_context before parse_environment");
189 LyXLayout_ptr newlayout;
190 string const name = p.getArg('{', '}');
191 const bool is_starred = suffixIs(name, '*');
192 string const unstarred_name = rtrim(name, "*");
193 active_environments.push_back(name);
194 if (is_math_env(name)) {
195 parent_context.check_layout(os);
196 begin_inset(os, "Formula ");
197 os << "\\begin{" << name << "}";
198 parse_math(p, os, FLAG_END, MATH_MODE);
199 os << "\\end{" << name << "}";
201 } else if (name == "tabular") {
202 parent_context.check_layout(os);
203 begin_inset(os, "Tabular ");
204 handle_tabular(p, os, parent_context);
206 } else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
207 parent_context.check_layout(os);
208 begin_inset(os, "Float " + unstarred_name + "\n");
209 if (p.next_token().asInput() == "[") {
210 os << "placement " << p.getArg('[', ']') << '\n';
212 os << "wide " << tostr(is_starred)
213 << "\ncollapsed false\n";
214 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
216 } else if (name == "center") {
217 parse_text(p, os, FLAG_END, outer, parent_context);
218 // The single '=' is meant here.
219 } else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
220 newlayout->isEnvironment()) {
221 Context context(true, parent_context.textclass, newlayout,
222 parent_context.layout);
223 parent_context.check_end_layout(os);
224 // context.dump(os, "#context in parse_environment");
225 switch (context.layout->latextype) {
226 case LATEX_LIST_ENVIRONMENT:
227 context.extra_stuff = "\\labelwidthstring "
228 + p.verbatim_item() + '\n';
230 case LATEX_BIB_ENVIRONMENT:
231 p.verbatim_item(); // swallow next arg
236 //context.check_layout(os);
237 parse_text(p, os, FLAG_END, outer, context);
238 // context.dump(os, "#context after parse_environment");
239 context.check_end_layout(os);
241 parent_context.check_layout(os);
242 handle_ert(os, "\\begin{" + name + "}", parent_context);
243 parse_text_snippet(p, os, FLAG_END, outer, parent_context);
244 handle_ert(os, "\\end{" + name + "}", parent_context);
248 } // anonymous namespace
253 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
256 LyXLayout_ptr newlayout;
258 Token const & t = p.get_token();
261 cerr << "t: " << t << " flags: " << flags << "\n";
264 if (flags & FLAG_ITEM) {
265 if (t.cat() == catSpace)
269 if (t.cat() == catBegin) {
270 // skip the brace and collect everything to the next matching
272 flags |= FLAG_BRACE_LAST;
276 // handle only this single token, leave the loop if done
280 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
286 if (t.cat() == catMath) {
287 // we are inside some text mode thingy, so opening new math is allowed
288 context.check_layout(os);
289 begin_inset(os, "Formula ");
290 Token const & n = p.get_token();
291 if (n.cat() == catMath && outer) {
292 // TeX's $$...$$ syntax for displayed math
294 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
296 p.get_token(); // skip the second '$' token
298 // simple $...$ stuff
301 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
307 else if (t.cat() == catSuper || t.cat() == catSub)
308 cerr << "catcode " << t << " illegal in text mode\n";
310 // Basic support for english quotes. This should be
311 // extended to other quotes, but is not so easy (a
312 // left english quote is the same as a right german
314 else if (t.asInput() == "`"
315 && p.next_token().asInput() == "`") {
316 context.check_layout(os);
317 begin_inset(os, "Quotes ");
323 else if (t.asInput() == "'"
324 && p.next_token().asInput() == "'") {
325 context.check_layout(os);
326 begin_inset(os, "Quotes ");
334 else if (t.cat() == catLetter ||
335 t.cat() == catSpace ||
336 t.cat() == catOther ||
337 t.cat() == catAlign ||
338 t.cat() == catParameter) {
339 context.check_layout(os);
343 else if (t.cat() == catNewline) {
344 if (p.next_token().cat() == catNewline) {
346 context.need_layout = true;
347 // this should be done by the parser already
348 cerr << "what are we doing here?" << endl;
350 os << " "; // note the space
354 else if (t.cat() == catActive) {
355 context.check_layout(os);
356 if (t.character() == '~') {
357 if (context.layout->free_spacing)
360 os << "\\InsetSpace ~\n";
365 else if (t.cat() == catBegin) {
367 // special handling of size changes
368 context.check_layout(os);
369 bool const is_size = is_known(p.next_token().cs(), known_sizes);
370 Context newcontext(false, context.textclass);
371 // need_end_layout = false;
372 string const s = parse_text(p, FLAG_BRACE_LAST, outer, newcontext);
373 // need_end_layout = true;
374 if (s.empty() && p.next_token().character() == '`')
375 ; // ignore it in {}``
376 else if (is_size || s == "[" || s == "]" || s == "*")
379 handle_ert(os, "{", context);
381 handle_ert(os, "}", context);
385 else if (t.cat() == catEnd) {
386 if (flags & FLAG_BRACE_LAST) {
387 context.check_end_layout(os);
390 cerr << "stray '}' in text\n";
391 handle_ert(os, "}", context);
394 else if (t.cat() == catComment)
401 else if (t.cs() == "(") {
402 context.check_layout(os);
403 begin_inset(os, "Formula");
405 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
410 else if (t.cs() == "[") {
411 context.check_layout(os);
412 begin_inset(os, "Formula");
414 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
419 else if (t.cs() == "begin")
420 parse_environment(p, os, outer, context);
422 else if (t.cs() == "end") {
423 if (flags & FLAG_END) {
424 // eat environment name
425 string const name = p.getArg('{', '}');
426 if (name != active_environment())
427 cerr << "\\end{" + name + "} does not match \\begin{"
428 + active_environment() + "}\n";
429 active_environments.pop_back();
430 context.check_end_layout(os);
433 p.error("found 'end' unexpectedly");
436 else if (t.cs() == "item") {
437 // should be done automatically by Parser::tokenize
440 if (p.next_token().character() == '[') {
441 p.get_token(); // eat '['
442 Context newcontext(false, context.textclass);
443 s = parse_text(p, FLAG_BRACK_LAST, outer, newcontext);
445 context.need_layout = true;
446 context.check_layout(os);
451 else if (t.cs() == "def") {
452 string name = p.get_token().cs();
453 while (p.next_token().cat() != catBegin)
454 name += p.get_token().asString();
455 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
458 else if (t.cs() == "par") {
460 context.check_end_layout(os);
461 context.need_layout = true;
462 // if (p.next_token().cs() != "\\begin")
464 //cerr << "next token: '" << p.next_token().cs() << "'\n";
467 // Must attempt to parse "Section*" before "Section".
468 else if ((p.next_token().asInput() == "*") &&
469 // The single '=' is meant here.
470 (newlayout = findLayout(context.textclass,
471 t.cs() + '*')).get() &&
472 newlayout->isCommand()) {
474 output_command_layout(os, p, outer, context, newlayout);
477 // The single '=' is meant here.
478 else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
479 newlayout->isCommand()) {
480 output_command_layout(os, p, outer, context, newlayout);
483 else if (t.cs() == "includegraphics") {
484 map<string, string> opts = split_map(p.getArg('[', ']'));
485 string name = p.verbatim_item();
487 context.check_layout(os);
488 begin_inset(os, "Graphics ");
489 os << "\n\tfilename " << name << '\n';
490 if (opts.find("width") != opts.end())
491 os << "\twidth " << opts["width"] << '\n';
492 if (opts.find("height") != opts.end())
493 os << "\theight " << opts["height"] << '\n';
497 else if (t.cs() == "footnote") {
498 context.check_layout(os);
499 begin_inset(os, "Foot\n");
500 os << "collapsed true\n";
501 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
505 else if (t.cs() == "marginpar") {
506 context.check_layout(os);
507 begin_inset(os, "Marginal\n");
508 os << "collapsed true\n";
509 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
513 else if (t.cs() == "ensuremath") {
514 context.check_layout(os);
515 Context newcontext(false, context.textclass);
516 string s = parse_text(p, FLAG_ITEM, false, newcontext);
517 if (s == "±" || s == "³" || s == "²" || s == "µ")
520 handle_ert(os, "\\ensuremath{" + s + "}",
524 else if (t.cs() == "hfill") {
525 context.check_layout(os);
530 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
531 skip_braces(p); // swallow this
533 else if (t.cs() == "tableofcontents") {
534 context.check_layout(os);
535 begin_inset(os, "LatexCommand ");
536 os << '\\' << t.cs() << "{}\n";
538 skip_braces(p); // swallow this
542 else if (t.cs() == "textrm") {
543 context.check_layout(os);
544 os << "\n\\family roman \n";
545 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
546 os << "\n\\family default \n";
549 else if (t.cs() == "textsf") {
550 context.check_layout(os);
551 os << "\n\\family sans \n";
552 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
553 os << "\n\\family default \n";
556 else if (t.cs() == "texttt") {
557 context.check_layout(os);
558 os << "\n\\family typewriter \n";
559 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
560 os << "\n\\family default \n";
563 else if (t.cs() == "textit") {
564 context.check_layout(os);
565 os << "\n\\shape italic \n";
566 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
567 os << "\n\\shape default \n";
570 else if (t.cs() == "textsc") {
571 context.check_layout(os);
572 os << "\n\\noun on \n";
573 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
574 os << "\n\\noun default \n";
577 else if (t.cs() == "textbf") {
578 context.check_layout(os);
579 os << "\n\\series bold \n";
580 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
581 os << "\n\\series default \n";
584 else if (t.cs() == "underbar") {
585 context.check_layout(os);
586 os << "\n\\bar under \n";
587 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
588 os << "\n\\bar default \n";
591 else if (t.cs() == "emph" || t.cs() == "noun") {
592 context.check_layout(os);
593 os << "\n\\" << t.cs() << " on \n";
594 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
595 os << "\n\\" << t.cs() << " default \n";
598 else if (t.cs() == "bibitem") {
599 context.check_layout(os);
602 os << '{' << p.verbatim_item() << '}' << "\n";
605 else if (is_known(t.cs(), known_latex_commands)) {
606 context.check_layout(os);
607 begin_inset(os, "LatexCommand ");
608 os << '\\' << t.cs();
611 os << '{' << p.verbatim_item() << "}\n";
615 else if (is_known(t.cs(), known_quotes)) {
616 char const ** where = is_known(t.cs(), known_quotes);
617 begin_inset(os, "Quotes ");
618 os << known_coded_quotes[where - known_quotes];
623 else if (is_known(t.cs(), known_sizes)) {
624 char const ** where = is_known(t.cs(), known_sizes);
625 context.check_layout(os);
626 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
629 else if (t.cs() == "LyX" || t.cs() == "TeX"
630 || t.cs() == "LaTeX") {
631 context.check_layout(os);
633 skip_braces(p); // eat {}
636 else if (t.cs() == "LaTeXe") {
637 context.check_layout(os);
639 skip_braces(p); // eat {}
642 else if (t.cs() == "ldots") {
643 context.check_layout(os);
645 os << "\\SpecialChar \\ldots{}\n";
648 else if (t.cs() == "lyxarrow") {
649 context.check_layout(os);
650 os << "\\SpecialChar \\menuseparator\n";
654 else if (t.cs() == "ldots") {
655 context.check_layout(os);
656 os << "\\SpecialChar \\ldots{}\n";
660 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
661 context.check_layout(os);
662 os << "\\SpecialChar \\@.\n";
666 else if (t.cs() == "-") {
667 context.check_layout(os);
668 os << "\\SpecialChar \\-\n";
671 else if (t.cs() == "textasciitilde") {
672 context.check_layout(os);
677 else if (t.cs() == "textasciicircum") {
678 context.check_layout(os);
683 else if (t.cs() == "textbackslash") {
684 context.check_layout(os);
685 os << "\n\\backslash \n";
689 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
690 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
692 context.check_layout(os);
696 else if (t.cs() == "char") {
697 context.check_layout(os);
698 if (p.next_token().character() == '`') {
700 if (p.next_token().cs() == "\"") {
705 handle_ert(os, "\\char`", context);
708 handle_ert(os, "\\char", context);
712 else if (t.cs() == "\"") {
713 context.check_layout(os);
714 string const name = p.verbatim_item();
715 if (name == "a") os << 'ä';
716 else if (name == "o") os << 'ö';
717 else if (name == "u") os << 'ü';
718 else if (name == "A") os << 'Ä';
719 else if (name == "O") os << 'Ö';
720 else if (name == "U") os << 'Ü';
721 else handle_ert(os, "\"{" + name + "}", context);
724 else if (t.cs() == "=" || t.cs() == "H" || t.cs() == "c"
725 || t.cs() == "^" || t.cs() == "'" || t.cs() == "~") {
726 // we need the trim as the LyX parser chokes on such spaces
727 context.check_layout(os);
728 os << "\n\\i \\" << t.cs() << "{"
729 << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
732 else if (t.cs() == "ss") {
733 context.check_layout(os);
737 else if (t.cs() == "i" || t.cs() == "j") {
738 context.check_layout(os);
739 os << "\\" << t.cs() << ' ';
742 else if (t.cs() == "\\") {
743 context.check_layout(os);
744 os << "\n\\newline \n";
747 else if (t.cs() == "input") {
748 context.check_layout(os);
749 handle_ert(os, "\\input{" + p.verbatim_item() + "}\n",
752 else if (t.cs() == "fancyhead") {
753 context.check_layout(os);
757 ss << '{' << p.verbatim_item() << "}\n";
758 handle_ert(os, ss.str(), context);
762 //cerr << "#: " << t << " mode: " << mode << endl;
763 // heuristic: read up to next non-nested space
765 string s = t.asInput();
766 string z = p.verbatim_item();
767 while (p.good() && z != " " && z.size()) {
768 //cerr << "read: " << z << endl;
770 z = p.verbatim_item();
772 cerr << "found ERT: " << s << endl;
773 handle_ert(os, s + ' ', context);
775 context.check_layout(os);
776 handle_ert(os, t.asInput() + ' ', context);
779 if (flags & FLAG_LEAVE) {
780 flags &= ~FLAG_LEAVE;