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;
31 // Do we need to output some \begin_layout command before the next characters?
32 bool need_layout = true;
33 // We may need to add something after this \begin_layout command
35 // Do we need to output some \end_layout command
36 bool need_end_layout = false;
38 void check_end_layout(ostream & os)
40 if (need_end_layout) {
41 os << "\n\\end_layout\n";
42 need_end_layout = false;
49 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
50 "printindex", "pageref", "url", 0 };
52 // LaTeX names for quotes
53 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
54 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
56 // the same as known_quotes with .lyx names
57 char const * known_coded_quotes[] = { "gld", "grd", "gld",
58 "grd", "gls", "fls", "frd", 0};
60 char const * known_sizes[] = { "tiny", "scriptsize", "footnotesize",
61 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
63 char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
64 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
74 // splits "x=z, y=b" into a map
75 map<string, string> split_map(string const & s)
77 map<string, string> res;
80 for (size_t i = 0; i < v.size(); ++i) {
81 size_t const pos = v[i].find('=');
82 string const index = v[i].substr(0, pos);
83 string const value = v[i].substr(pos + 1, string::npos);
84 res[trim(index)] = trim(value);
90 void check_layout(ostream & os, LyXLayout_ptr layout)
94 os << "\n\\begin_layout " << layout->name() << "\n\n";
95 need_end_layout = true;
97 if (!extra_stuff.empty()) {
105 void begin_inset(ostream & os, string const & name)
107 os << "\n\\begin_inset " << name;
111 void end_inset(ostream & os)
113 os << "\n\\end_inset \n\n";
117 void skip_braces(Parser & p)
119 if (p.next_token().cat() != catBegin)
122 if (p.next_token().cat() == catEnd) {
130 void handle_ert(ostream & os, string const & s)
132 begin_inset(os, "ERT");
133 os << "\nstatus Collapsed\n\n\\begin_layout Standard\n\n";
134 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
136 os << "\n\\backslash \n";
140 need_end_layout = true;
141 check_end_layout(os);
147 isLayout(string const name) : name_(name) {}
148 bool operator()(LyXLayout_ptr const & ptr) {
149 return ptr.get() && ptr->latexname() == name_;
156 LyXLayout_ptr findLayout(LyXTextClass const & textclass,
159 LyXTextClass::const_iterator it = textclass.begin();
160 LyXTextClass::const_iterator end = textclass.end();
161 it = std::find_if(it, end, isLayout(name));
162 return (it == end) ? LyXLayout_ptr() : *it;
166 void output_command_layout(ostream & os, LyXLayout_ptr const & layout,
167 Parser & p, bool outer, LyXTextClass const & textclass)
170 check_layout(os, layout);
171 if (layout->optionalargs > 0) {
173 if (p.next_token().character() == '[') {
174 p.get_token(); // eat '['
175 begin_inset(os, "OptArg\n");
176 os << "collapsed true\n";
177 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, textclass);
181 parse_text(p, os, FLAG_ITEM, outer, textclass, layout);
186 } // anonymous namespace
189 void parse_environment(Parser & p, ostream & os, bool outer,
190 LyXTextClass const & textclass, LyXLayout_ptr layout)
192 LyXLayout_ptr newlayout;
193 string const name = p.getArg('{', '}');
194 const bool is_starred = suffixIs(name, '*');
195 string const unstarred_name = rtrim(name, "*");
196 active_environments.push_back(name);
197 if (is_math_env(name)) {
198 check_layout(os, layout);
199 begin_inset(os, "Formula ");
200 os << "\\begin{" << name << "}";
201 parse_math(p, os, FLAG_END, MATH_MODE);
202 os << "\\end{" << name << "}";
204 } else if (name == "tabular") {
205 check_layout(os, layout);
206 begin_inset(os, "Tabular ");
207 handle_tabular(p, os, textclass);
209 } else if (textclass.floats().typeExist(unstarred_name)) {
210 check_layout(os, layout);
211 begin_inset(os, "Float " + unstarred_name + "\n");
212 if (p.next_token().asInput() == "[") {
213 os << "placement " << p.getArg('[', ']') << '\n';
215 os << "wide " << tostr(is_starred)
216 << "\ncollapsed false\n";
217 parse_text_in_inset(p, os, FLAG_END, outer, textclass);
219 } else if (name == "center") {
220 parse_text(p, os, FLAG_END, outer, textclass);
221 // The single '=' is meant here.
222 } else if ((newlayout = findLayout(textclass, name)).get() &&
223 newlayout->isEnvironment()) {
224 size_t const n = active_environments.size();
225 string const s = active_environments[n - 2];
226 bool const deeper = s == "enumerate" || s == "itemize"
229 os << "\n\\begin_deeper";
230 switch (newlayout->latextype) {
231 case LATEX_LIST_ENVIRONMENT:
232 extra_stuff = "\\labelwidthstring "
233 + p.verbatim_item() + '\n';
235 case LATEX_BIB_ENVIRONMENT:
236 p.verbatim_item(); // swallow next arg
242 parse_text(p, os, FLAG_END, outer, textclass, newlayout);
243 check_end_layout(os);
245 os << "\n\\end_deeper\n";
248 cerr << "why are we here?" << endl;
249 parse_text(p, os, FLAG_END, outer, textclass);
254 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
255 LyXTextClass const & textclass, LyXLayout_ptr layout)
258 layout = textclass.defaultLayout();
259 LyXLayout_ptr newlayout;
261 Token const & t = p.get_token();
264 cerr << "t: " << t << " flags: " << flags << "\n";
267 if (flags & FLAG_ITEM) {
268 if (t.cat() == catSpace)
272 if (t.cat() == catBegin) {
273 // skip the brace and collect everything to the next matching
275 flags |= FLAG_BRACE_LAST;
279 // handle only this single token, leave the loop if done
283 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
289 if (t.cat() == catMath) {
290 // we are inside some text mode thingy, so opening new math is allowed
291 check_layout(os, layout);
292 begin_inset(os, "Formula ");
293 Token const & n = p.get_token();
294 if (n.cat() == catMath && outer) {
295 // TeX's $$...$$ syntax for displayed math
297 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
299 p.get_token(); // skip the second '$' token
301 // simple $...$ stuff
304 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
310 else if (t.cat() == catSuper || t.cat() == catSub)
311 cerr << "catcode " << t << " illegal in text mode\n";
313 // Basic support for english quotes. This should be
314 // extended to other quotes, but is not so easy (a
315 // left english quote is the same as a right german
317 else if (t.asInput() == "`"
318 && p.next_token().asInput() == "`") {
319 check_layout(os, layout);
320 begin_inset(os, "Quotes ");
326 else if (t.asInput() == "'"
327 && p.next_token().asInput() == "'") {
328 check_layout(os, layout);
329 begin_inset(os, "Quotes ");
337 else if (t.cat() == catLetter ||
338 t.cat() == catSpace ||
339 t.cat() == catOther ||
340 t.cat() == catAlign ||
341 t.cat() == catParameter) {
342 check_layout(os, layout);
346 else if (t.cat() == catNewline) {
347 if (p.next_token().cat() == catNewline) {
351 os << " "; // note the space
355 else if (t.cat() == catActive) {
356 check_layout(os, layout);
357 if (t.character() == '~') {
358 if (layout->free_spacing)
361 os << "\\InsetSpace ~\n";
366 else if (t.cat() == catBegin) {
368 // special handling of size changes
369 check_layout(os, layout);
370 bool const is_size = is_known(p.next_token().cs(), known_sizes);
371 need_end_layout = false;
372 string const s = parse_text(p, FLAG_BRACE_LAST, outer, textclass, layout);
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 == "*")
385 else if (t.cat() == catEnd) {
386 if (flags & FLAG_BRACE_LAST) {
387 check_end_layout(os);
390 cerr << "stray '}' in text\n";
394 else if (t.cat() == catComment)
401 else if (t.cs() == "(") {
402 check_layout(os, layout);
403 begin_inset(os, "Formula");
405 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
410 else if (t.cs() == "[") {
411 check_layout(os, layout);
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, textclass, layout);
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();
432 p.error("found 'end' unexpectedly");
435 else if (t.cs() == "item") {
436 // should be done automatically by Parser::tokenize
439 if (p.next_token().character() == '[') {
440 p.get_token(); // eat '['
441 s = parse_text(p, FLAG_BRACK_LAST, outer, textclass, layout);
444 check_layout(os, layout);
449 else if (t.cs() == "def") {
450 string name = p.get_token().cs();
451 while (p.next_token().cat() != catBegin)
452 name += p.get_token().asString();
453 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}');
456 else if (t.cs() == "par") {
459 // if (p.next_token().cs() != "\\begin")
461 //cerr << "next token: '" << p.next_token().cs() << "'\n";
464 // Must attempt to parse "Section*" before "Section".
465 else if ((p.next_token().asInput() == "*") &&
466 // The single '=' is meant here.
467 (newlayout = findLayout(textclass,
468 t.cs() + '*')).get() &&
469 newlayout->isCommand()) {
471 output_command_layout(os, newlayout, p, outer, textclass);
474 // The single '=' is meant here.
475 else if ((newlayout = findLayout(textclass, t.cs())).get() &&
476 newlayout->isCommand()) {
477 output_command_layout(os, newlayout, p, outer, textclass);
480 else if (t.cs() == "includegraphics") {
481 map<string, string> opts = split_map(p.getArg('[', ']'));
482 string name = p.verbatim_item();
484 check_layout(os, layout);
485 begin_inset(os, "Graphics ");
486 os << "\n\tfilename " << name << '\n';
487 if (opts.find("width") != opts.end())
488 os << "\twidth " << opts["width"] << '\n';
489 if (opts.find("height") != opts.end())
490 os << "\theight " << opts["height"] << '\n';
494 else if (t.cs() == "footnote") {
495 check_layout(os, layout);
496 begin_inset(os, "Foot\n");
497 os << "collapsed true\n";
498 parse_text_in_inset(p, os, FLAG_ITEM, false, textclass);
502 else if (t.cs() == "ensuremath") {
503 check_layout(os, layout);
504 string s = parse_text(p, FLAG_ITEM, false, textclass);
505 if (s == "±" || s == "³" || s == "²" || s == "µ")
508 handle_ert(os, "\\ensuremath{" + s + "}");
511 else if (t.cs() == "marginpar") {
512 check_layout(os, layout);
513 begin_inset(os, "Marginal\n");
514 os << "collapsed true\n";
516 parse_text(p, os, FLAG_ITEM, false, textclass);
518 need_end_layout = true;
521 else if (t.cs() == "hfill") {
522 check_layout(os, layout);
527 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
528 skip_braces(p); // swallow this
530 else if (t.cs() == "tableofcontents") {
531 check_layout(os, layout);
532 begin_inset(os, "LatexCommand ");
533 os << '\\' << t.cs() << "{}\n";
535 skip_braces(p); // swallow this
539 else if (t.cs() == "textrm") {
540 check_layout(os, layout);
541 os << "\n\\family roman \n";
542 parse_text(p, os, FLAG_ITEM, outer, textclass);
543 os << "\n\\family default \n";
546 else if (t.cs() == "textsf") {
547 check_layout(os, layout);
548 os << "\n\\family sans \n";
549 parse_text(p, os, FLAG_ITEM, outer, textclass);
550 os << "\n\\family default \n";
553 else if (t.cs() == "texttt") {
554 check_layout(os, layout);
555 os << "\n\\family typewriter \n";
556 parse_text(p, os, FLAG_ITEM, outer, textclass);
557 os << "\n\\family default \n";
560 else if (t.cs() == "textit") {
561 check_layout(os, layout);
562 os << "\n\\shape italic \n";
563 parse_text(p, os, FLAG_ITEM, outer, textclass);
564 os << "\n\\shape default \n";
567 else if (t.cs() == "textsc") {
568 check_layout(os, layout);
569 os << "\n\\noun on \n";
570 parse_text(p, os, FLAG_ITEM, outer, textclass);
571 os << "\n\\noun default \n";
574 else if (t.cs() == "textbf") {
575 check_layout(os, layout);
576 os << "\n\\series bold \n";
577 parse_text(p, os, FLAG_ITEM, outer, textclass);
578 os << "\n\\series default \n";
581 else if (t.cs() == "underbar") {
582 check_layout(os, layout);
583 os << "\n\\bar under \n";
584 parse_text(p, os, FLAG_ITEM, outer, textclass);
585 os << "\n\\bar default \n";
588 else if (t.cs() == "emph" || t.cs() == "noun") {
589 check_layout(os, layout);
590 os << "\n\\" << t.cs() << " on \n";
591 parse_text(p, os, FLAG_ITEM, outer, textclass);
592 os << "\n\\" << t.cs() << " default \n";
595 else if (t.cs() == "bibitem") {
596 check_layout(os, layout);
599 os << '{' << p.verbatim_item() << '}' << "\n";
602 else if (is_known(t.cs(), known_latex_commands)) {
603 check_layout(os, layout);
604 begin_inset(os, "LatexCommand ");
605 os << '\\' << t.cs();
608 os << '{' << p.verbatim_item() << "}\n";
612 else if (is_known(t.cs(), known_quotes)) {
613 char const ** where = is_known(t.cs(), known_quotes);
614 begin_inset(os, "Quotes ");
615 os << known_coded_quotes[where - known_quotes];
620 else if (is_known(t.cs(), known_sizes)) {
621 char const ** where = is_known(t.cs(), known_sizes);
622 check_layout(os, layout);
623 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
626 else if (t.cs() == "LyX" || t.cs() == "TeX"
627 || t.cs() == "LaTeX") {
628 check_layout(os, layout);
630 skip_braces(p); // eat {}
633 else if (t.cs() == "LaTeXe") {
634 check_layout(os, layout);
636 skip_braces(p); // eat {}
639 else if (t.cs() == "ldots") {
640 check_layout(os, layout);
642 os << "\\SpecialChar \\ldots{}\n";
645 else if (t.cs() == "lyxarrow") {
646 check_layout(os, layout);
647 os << "\\SpecialChar \\menuseparator\n";
651 else if (t.cs() == "ldots") {
652 check_layout(os, layout);
653 os << "\\SpecialChar \\ldots{}\n";
657 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
658 check_layout(os, layout);
659 os << "\\SpecialChar \\@.\n";
663 else if (t.cs() == "-") {
664 check_layout(os, layout);
665 os << "\\SpecialChar \\-\n";
668 else if (t.cs() == "textasciitilde") {
669 check_layout(os, layout);
674 else if (t.cs() == "textasciicircum") {
675 check_layout(os, layout);
680 else if (t.cs() == "textbackslash") {
681 check_layout(os, layout);
682 os << "\n\\backslash \n";
686 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
687 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
689 check_layout(os, layout);
693 else if (t.cs() == "char") {
694 check_layout(os, layout);
695 if (p.next_token().character() == '`') {
697 if (p.next_token().cs() == "\"") {
702 handle_ert(os, "\\char`");
705 handle_ert(os, "\\char");
709 else if (t.cs() == "\"") {
710 check_layout(os, layout);
711 string const name = p.verbatim_item();
712 if (name == "a") os << 'ä';
713 else if (name == "o") os << 'ö';
714 else if (name == "u") os << 'ü';
715 else if (name == "A") os << 'Ä';
716 else if (name == "O") os << 'Ö';
717 else if (name == "U") os << 'Ü';
718 else handle_ert(os, "\"{" + name + "}");
721 else if (t.cs() == "=" || t.cs() == "H" || t.cs() == "c"
722 || t.cs() == "^" || t.cs() == "'" || t.cs() == "~") {
723 // we need the trim as the LyX parser chokes on such spaces
724 check_layout(os, layout);
725 os << "\n\\i \\" << t.cs() << "{"
726 << trim(parse_text(p, FLAG_ITEM, outer, textclass), " ") << "}\n";
729 else if (t.cs() == "ss") {
730 check_layout(os, layout);
734 else if (t.cs() == "i" || t.cs() == "j") {
735 check_layout(os, layout);
736 os << "\\" << t.cs() << ' ';
739 else if (t.cs() == "\\") {
740 check_layout(os, layout);
741 os << "\n\\newline \n";
744 else if (t.cs() == "input") {
745 check_layout(os, layout);
746 handle_ert(os, "\\input{" + p.verbatim_item() + "}\n");
748 else if (t.cs() == "fancyhead") {
749 check_layout(os, layout);
753 ss << '{' << p.verbatim_item() << "}\n";
754 handle_ert(os, ss.str());
758 //cerr << "#: " << t << " mode: " << mode << endl;
759 // heuristic: read up to next non-nested space
761 string s = t.asInput();
762 string z = p.verbatim_item();
763 while (p.good() && z != " " && z.size()) {
764 //cerr << "read: " << z << endl;
766 z = p.verbatim_item();
768 cerr << "found ERT: " << s << endl;
769 handle_ert(os, s + ' ');
771 check_layout(os, layout);
772 handle_ert(os, t.asInput() + ' ');
775 if (flags & FLAG_LEAVE) {
776 flags &= ~FLAG_LEAVE;
783 string parse_text(Parser & p, unsigned flags, const bool outer,
784 LyXTextClass const & textclass,
785 LyXLayout_ptr layout)
788 parse_text(p, os, flags, outer, textclass, layout);
792 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
793 LyXTextClass const & textclass, LyXLayout_ptr layout)
796 need_end_layout = false;
797 parse_text(p, os, flags, outer, textclass, layout);
798 check_end_layout(os);
799 need_end_layout = true;