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.check_end_layout(os);
164 Context context(true, parent_context.textclass, newlayout,
165 parent_context.layout);
166 context.check_deeper(os);
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.check_end_deeper(os);
184 void parse_environment(Parser & p, ostream & os, bool outer,
185 Context & parent_context)
187 LyXLayout_ptr newlayout;
188 string const name = p.getArg('{', '}');
189 const bool is_starred = suffixIs(name, '*');
190 string const unstarred_name = rtrim(name, "*");
191 active_environments.push_back(name);
192 if (is_math_env(name)) {
193 parent_context.check_layout(os);
194 begin_inset(os, "Formula ");
195 os << "\\begin{" << name << "}";
196 parse_math(p, os, FLAG_END, MATH_MODE);
197 os << "\\end{" << name << "}";
199 } else if (name == "tabular") {
200 parent_context.check_layout(os);
201 begin_inset(os, "Tabular ");
202 handle_tabular(p, os, parent_context);
204 } else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
205 parent_context.check_layout(os);
206 begin_inset(os, "Float " + unstarred_name + "\n");
207 if (p.next_token().asInput() == "[") {
208 os << "placement " << p.getArg('[', ']') << '\n';
210 os << "wide " << tostr(is_starred)
211 << "\ncollapsed false\n";
212 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
214 } else if (name == "center") {
215 parse_text(p, os, FLAG_END, outer, parent_context);
216 // The single '=' is meant here.
217 } else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
218 newlayout->isEnvironment()) {
219 Context context(true, parent_context.textclass, newlayout,
220 parent_context.layout);
221 parent_context.check_end_layout(os);
222 switch (context.layout->latextype) {
223 case LATEX_LIST_ENVIRONMENT:
224 context.extra_stuff = "\\labelwidthstring "
225 + p.verbatim_item() + '\n';
227 case LATEX_BIB_ENVIRONMENT:
228 p.verbatim_item(); // swallow next arg
233 context.check_deeper(os);
234 parse_text(p, os, FLAG_END, outer, context);
235 context.check_end_layout(os);
236 context.check_end_deeper(os);
238 parent_context.check_layout(os);
239 handle_ert(os, "\\begin{" + name + "}", parent_context);
240 parse_text_snippet(p, os, FLAG_END, outer, parent_context);
241 handle_ert(os, "\\end{" + name + "}", parent_context);
245 } // anonymous namespace
250 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
253 LyXLayout_ptr newlayout;
255 Token const & t = p.get_token();
258 cerr << "t: " << t << " flags: " << flags << "\n";
261 if (flags & FLAG_ITEM) {
262 if (t.cat() == catSpace)
266 if (t.cat() == catBegin) {
267 // skip the brace and collect everything to the next matching
269 flags |= FLAG_BRACE_LAST;
273 // handle only this single token, leave the loop if done
277 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
283 if (t.cat() == catMath) {
284 // we are inside some text mode thingy, so opening new math is allowed
285 context.check_layout(os);
286 begin_inset(os, "Formula ");
287 Token const & n = p.get_token();
288 if (n.cat() == catMath && outer) {
289 // TeX's $$...$$ syntax for displayed math
291 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
293 p.get_token(); // skip the second '$' token
295 // simple $...$ stuff
298 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
304 else if (t.cat() == catSuper || t.cat() == catSub)
305 cerr << "catcode " << t << " illegal in text mode\n";
307 // Basic support for english quotes. This should be
308 // extended to other quotes, but is not so easy (a
309 // left english quote is the same as a right german
311 else if (t.asInput() == "`"
312 && p.next_token().asInput() == "`") {
313 context.check_layout(os);
314 begin_inset(os, "Quotes ");
320 else if (t.asInput() == "'"
321 && p.next_token().asInput() == "'") {
322 context.check_layout(os);
323 begin_inset(os, "Quotes ");
331 else if (t.cat() == catLetter ||
332 t.cat() == catSpace ||
333 t.cat() == catOther ||
334 t.cat() == catAlign ||
335 t.cat() == catParameter) {
336 context.check_layout(os);
340 else if (t.cat() == catNewline) {
341 if (p.next_token().cat() == catNewline) {
342 // this should have been be done by
343 // the parser already
344 cerr << "what are we doing here?" << endl;
346 context.need_layout = true;
348 os << " "; // note the space
352 else if (t.cat() == catActive) {
353 context.check_layout(os);
354 if (t.character() == '~') {
355 if (context.layout->free_spacing)
358 os << "\\InsetSpace ~\n";
363 else if (t.cat() == catBegin) {
365 // special handling of size changes
366 context.check_layout(os);
367 bool const is_size = is_known(p.next_token().cs(), known_sizes);
368 Context newcontext(false, context.textclass);
369 // need_end_layout = false;
370 string const s = parse_text(p, FLAG_BRACE_LAST, outer, newcontext);
371 // need_end_layout = true;
372 if (s.empty() && p.next_token().character() == '`')
373 ; // ignore it in {}``
374 else if (is_size || s == "[" || s == "]" || s == "*")
377 handle_ert(os, "{", context);
379 handle_ert(os, "}", context);
383 else if (t.cat() == catEnd) {
384 if (flags & FLAG_BRACE_LAST) {
385 context.check_end_layout(os);
388 cerr << "stray '}' in text\n";
389 handle_ert(os, "}", context);
392 else if (t.cat() == catComment)
399 else if (t.cs() == "(") {
400 context.check_layout(os);
401 begin_inset(os, "Formula");
403 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
408 else if (t.cs() == "[") {
409 context.check_layout(os);
410 begin_inset(os, "Formula");
412 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
417 else if (t.cs() == "begin")
418 parse_environment(p, os, outer, context);
420 else if (t.cs() == "end") {
421 if (flags & FLAG_END) {
422 // eat environment name
423 string const name = p.getArg('{', '}');
424 if (name != active_environment())
425 cerr << "\\end{" + name + "} does not match \\begin{"
426 + active_environment() + "}\n";
427 active_environments.pop_back();
430 p.error("found 'end' unexpectedly");
433 else if (t.cs() == "item") {
434 // should be done automatically by Parser::tokenize
437 if (p.next_token().character() == '[') {
438 p.get_token(); // eat '['
439 Context newcontext(false, context.textclass);
440 s = parse_text(p, FLAG_BRACK_LAST, outer, newcontext);
442 context.need_layout = true;
443 context.check_layout(os);
448 else if (t.cs() == "def") {
449 string name = p.get_token().cs();
450 while (p.next_token().cat() != catBegin)
451 name += p.get_token().asString();
452 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
455 else if (t.cs() == "par") {
457 context.check_end_layout(os);
458 context.need_layout = true;
461 // Must attempt to parse "Section*" before "Section".
462 else if ((p.next_token().asInput() == "*") &&
463 // The single '=' is meant here.
464 (newlayout = findLayout(context.textclass,
465 t.cs() + '*')).get() &&
466 newlayout->isCommand()) {
468 output_command_layout(os, p, outer, context, newlayout);
471 // The single '=' is meant here.
472 else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
473 newlayout->isCommand()) {
474 output_command_layout(os, p, outer, context, newlayout);
477 else if (t.cs() == "includegraphics") {
478 map<string, string> opts = split_map(p.getArg('[', ']'));
479 string name = p.verbatim_item();
481 context.check_layout(os);
482 begin_inset(os, "Graphics ");
483 os << "\n\tfilename " << name << '\n';
484 if (opts.find("width") != opts.end())
485 os << "\twidth " << opts["width"] << '\n';
486 if (opts.find("height") != opts.end())
487 os << "\theight " << opts["height"] << '\n';
491 else if (t.cs() == "footnote") {
492 context.check_layout(os);
493 begin_inset(os, "Foot\n");
494 os << "collapsed true\n";
495 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
499 else if (t.cs() == "marginpar") {
500 context.check_layout(os);
501 begin_inset(os, "Marginal\n");
502 os << "collapsed true\n";
503 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
507 else if (t.cs() == "ensuremath") {
508 context.check_layout(os);
509 Context newcontext(false, context.textclass);
510 string s = parse_text(p, FLAG_ITEM, false, newcontext);
511 if (s == "±" || s == "³" || s == "²" || s == "µ")
514 handle_ert(os, "\\ensuremath{" + s + "}",
518 else if (t.cs() == "hfill") {
519 context.check_layout(os);
524 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
525 skip_braces(p); // swallow this
527 else if (t.cs() == "tableofcontents") {
528 context.check_layout(os);
529 begin_inset(os, "LatexCommand ");
530 os << '\\' << t.cs() << "{}\n";
532 skip_braces(p); // swallow this
536 else if (t.cs() == "textrm") {
537 context.check_layout(os);
538 os << "\n\\family roman \n";
539 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
540 os << "\n\\family default \n";
543 else if (t.cs() == "textsf") {
544 context.check_layout(os);
545 os << "\n\\family sans \n";
546 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
547 os << "\n\\family default \n";
550 else if (t.cs() == "texttt") {
551 context.check_layout(os);
552 os << "\n\\family typewriter \n";
553 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
554 os << "\n\\family default \n";
557 else if (t.cs() == "textit") {
558 context.check_layout(os);
559 os << "\n\\shape italic \n";
560 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
561 os << "\n\\shape default \n";
564 else if (t.cs() == "textsc") {
565 context.check_layout(os);
566 os << "\n\\noun on \n";
567 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
568 os << "\n\\noun default \n";
571 else if (t.cs() == "textbf") {
572 context.check_layout(os);
573 os << "\n\\series bold \n";
574 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
575 os << "\n\\series default \n";
578 else if (t.cs() == "underbar") {
579 context.check_layout(os);
580 os << "\n\\bar under \n";
581 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
582 os << "\n\\bar default \n";
585 else if (t.cs() == "emph" || t.cs() == "noun") {
586 context.check_layout(os);
587 os << "\n\\" << t.cs() << " on \n";
588 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
589 os << "\n\\" << t.cs() << " default \n";
592 else if (t.cs() == "bibitem") {
593 context.check_layout(os);
596 os << '{' << p.verbatim_item() << '}' << "\n";
599 else if (is_known(t.cs(), known_latex_commands)) {
600 context.check_layout(os);
601 begin_inset(os, "LatexCommand ");
602 os << '\\' << t.cs();
605 os << '{' << p.verbatim_item() << "}\n";
609 else if (is_known(t.cs(), known_quotes)) {
610 char const ** where = is_known(t.cs(), known_quotes);
611 begin_inset(os, "Quotes ");
612 os << known_coded_quotes[where - known_quotes];
617 else if (is_known(t.cs(), known_sizes)) {
618 char const ** where = is_known(t.cs(), known_sizes);
619 context.check_layout(os);
620 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
623 else if (t.cs() == "LyX" || t.cs() == "TeX"
624 || t.cs() == "LaTeX") {
625 context.check_layout(os);
627 skip_braces(p); // eat {}
630 else if (t.cs() == "LaTeXe") {
631 context.check_layout(os);
633 skip_braces(p); // eat {}
636 else if (t.cs() == "ldots") {
637 context.check_layout(os);
639 os << "\\SpecialChar \\ldots{}\n";
642 else if (t.cs() == "lyxarrow") {
643 context.check_layout(os);
644 os << "\\SpecialChar \\menuseparator\n";
648 else if (t.cs() == "ldots") {
649 context.check_layout(os);
650 os << "\\SpecialChar \\ldots{}\n";
654 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
655 context.check_layout(os);
656 os << "\\SpecialChar \\@.\n";
660 else if (t.cs() == "-") {
661 context.check_layout(os);
662 os << "\\SpecialChar \\-\n";
665 else if (t.cs() == "textasciitilde") {
666 context.check_layout(os);
671 else if (t.cs() == "textasciicircum") {
672 context.check_layout(os);
677 else if (t.cs() == "textbackslash") {
678 context.check_layout(os);
679 os << "\n\\backslash \n";
683 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
684 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
686 context.check_layout(os);
690 else if (t.cs() == "char") {
691 context.check_layout(os);
692 if (p.next_token().character() == '`') {
694 if (p.next_token().cs() == "\"") {
699 handle_ert(os, "\\char`", context);
702 handle_ert(os, "\\char", context);
706 else if (t.cs() == "\"") {
707 context.check_layout(os);
708 string const name = p.verbatim_item();
709 if (name == "a") os << 'ä';
710 else if (name == "o") os << 'ö';
711 else if (name == "u") os << 'ü';
712 else if (name == "A") os << 'Ä';
713 else if (name == "O") os << 'Ö';
714 else if (name == "U") os << 'Ü';
715 else handle_ert(os, "\"{" + name + "}", context);
718 else if (t.cs() == "=" || t.cs() == "H" || t.cs() == "c"
719 || t.cs() == "^" || t.cs() == "'" || t.cs() == "~") {
720 // we need the trim as the LyX parser chokes on such spaces
721 context.check_layout(os);
722 os << "\n\\i \\" << t.cs() << "{"
723 << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
726 else if (t.cs() == "ss") {
727 context.check_layout(os);
731 else if (t.cs() == "i" || t.cs() == "j") {
732 context.check_layout(os);
733 os << "\\" << t.cs() << ' ';
736 else if (t.cs() == "\\") {
737 context.check_layout(os);
738 os << "\n\\newline \n";
741 else if (t.cs() == "input") {
742 context.check_layout(os);
743 handle_ert(os, "\\input{" + p.verbatim_item() + "}\n",
746 else if (t.cs() == "fancyhead") {
747 context.check_layout(os);
751 ss << '{' << p.verbatim_item() << "}\n";
752 handle_ert(os, ss.str(), context);
756 //cerr << "#: " << t << " mode: " << mode << endl;
757 // heuristic: read up to next non-nested space
759 string s = t.asInput();
760 string z = p.verbatim_item();
761 while (p.good() && z != " " && z.size()) {
762 //cerr << "read: " << z << endl;
764 z = p.verbatim_item();
766 cerr << "found ERT: " << s << endl;
767 handle_ert(os, s + ' ', context);
769 context.check_layout(os);
770 handle_ert(os, t.asInput() + ' ', context);
773 if (flags & FLAG_LEAVE) {
774 flags &= ~FLAG_LEAVE;