1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
20 using std::ostringstream;
26 char const * known_headings[] = { "caption", "title", "author", "date",
27 "paragraph", "chapter", "section", "subsection", "subsubsection", 0 };
29 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
30 "printindex", "pageref", "url", 0 };
32 // LaTeX names for quotes
33 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
34 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
36 // the same as known_quotes with .lyx names
37 char const * known_coded_quotes[] = { "gld", "grd", "gld",
38 "grd", "gls", "fls", "frd", 0};
40 char const * known_sizes[] = { "tiny", "scriptsize", "footnotesize",
41 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
43 char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
44 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
55 // splits "x=z, y=b" into a map
56 map<string, string> split_map(string const & s)
58 map<string, string> res;
61 for (size_t i = 0; i < v.size(); ++i) {
62 size_t const pos = v[i].find('=');
63 string const index = v[i].substr(0, pos);
64 string const value = v[i].substr(pos + 1, string::npos);
65 res[trim(index)] = trim(value);
71 void begin_inset(ostream & os, string const & name)
73 os << "\n\\begin_inset " << name;
77 void end_inset(ostream & os)
79 os << "\n\\end_inset\n\n";
83 void skip_braces(Parser & p)
85 if (p.next_token().cat() != catBegin)
88 if (p.next_token().cat() == catEnd) {
96 void handle_ert(ostream & os, string const & s)
98 begin_inset(os, "ERT");
99 os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
100 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
102 os << "\n\\backslash\n";
110 void handle_par(ostream & os)
112 if (active_environments.empty())
115 string s = active_environment();
116 if (s == "document" || s == "table")
117 os << "Standard\n\n";
118 else if (s == "center")
119 os << "Standard\n\n\\align center\n";
120 else if (s == "lyxcode")
121 os << "LyX-Code\n\n";
122 else if (s == "lyxlist")
124 else if (s == "thebibliography")
125 os << "Bibliography\n\n";
127 os << cap(s) << "\n\n";
131 } // anonymous namespace
134 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer)
137 Token const & t = p.get_token();
140 cerr << "t: " << t << " flags: " << flags << "\n";
143 if (flags & FLAG_ITEM) {
144 if (t.cat() == catSpace)
148 if (t.cat() == catBegin) {
149 // skip the brace and collect everything to the next matching
151 flags |= FLAG_BRACE_LAST;
155 // handle only this single token, leave the loop if done
159 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
165 if (t.cat() == catMath) {
166 // we are inside some text mode thingy, so opening new math is allowed
167 begin_inset(os, "Formula ");
168 Token const & n = p.get_token();
169 if (n.cat() == catMath && outer) {
170 // TeX's $$...$$ syntax for displayed math
172 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
174 p.get_token(); // skip the second '$' token
176 // simple $...$ stuff
179 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
185 else if (t.cat() == catSuper || t.cat() == catSub)
186 cerr << "catcode " << t << " illegal in text mode\n";
188 else if (t.cat() == catLetter ||
189 t.cat() == catSpace ||
190 t.cat() == catOther ||
191 t.cat() == catAlign ||
192 t.cat() == catParameter)
195 else if (t.cat() == catNewline) {
196 if (p.next_token().cat() == catNewline) {
200 os << " "; // note the space
204 else if (t.cat() == catActive) {
205 if (t.character() == '~') {
206 if (active_environment() == "lyxcode")
209 os << "\\SpecialChar ~\n";
214 else if (t.cat() == catBegin) {
215 // special handling of size changes
216 bool const is_size = is_known(p.next_token().cs(), known_sizes);
217 string const s = parse_text(p, FLAG_BRACE_LAST, outer);
218 if (s.empty() && p.next_token().character() == '`')
219 ; // ignore it in {}``
220 else if (is_size || s == "[" || s == "]" || s == "*")
229 else if (t.cat() == catEnd) {
230 if (flags & FLAG_BRACE_LAST)
232 cerr << "stray '}' in text\n";
236 else if (t.cat() == catOther)
237 os << string(1, t.character());
239 else if (t.cat() == catComment)
246 else if (t.cs() == "ldots") {
248 os << "\n\\SpecialChar \\ldots{}\n";
251 else if (t.cs() == "(") {
252 begin_inset(os, "Formula");
254 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
259 else if (t.cs() == "[") {
260 begin_inset(os, "Formula");
262 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
267 else if (t.cs() == "begin") {
268 string const name = p.getArg('{', '}');
269 active_environments.push_back(name);
270 if (is_math_env(name)) {
271 begin_inset(os, "Formula ");
272 os << "\\begin{" << name << "}";
273 parse_math(p, os, FLAG_END, MATH_MODE);
274 os << "\\end{" << name << "}";
276 } else if (name == "tabular") {
277 begin_inset(os, "Tabular ");
278 handle_tabular(p, os);
280 } else if (name == "table" || name == "figure") {
281 string opts = p.getOpt();
282 begin_inset(os, "Float " + name + "\n");
284 os << "placement " << opts << '\n';
285 os << "wide false\ncollapsed false\n\n"
286 << "\\layout Standard\n";
287 parse_text(p, os, FLAG_END, outer);
289 } else if (name == "center") {
291 parse_text(p, os, FLAG_END, outer);
292 } else if (name == "enumerate" || name == "itemize"
293 || name == "lyxlist") {
294 size_t const n = active_environments.size();
295 string const s = active_environments[n - 2];
296 bool const deeper = s == "enumerate" || s == "itemize"
299 os << "\n\\begin_deeper";
300 os << "\n\\layout " << cap(name) << "\n\n";
301 if (name == "lyxlist")
302 p.verbatim_item(); // swallow next arg
303 parse_text(p, os, FLAG_END, outer);
305 os << "\n\\end_deeper\n";
307 } else if (name == "thebibliography") {
308 p.verbatim_item(); // swallow next arg
309 parse_text(p, os, FLAG_END, outer);
310 os << "\n\\layout Bibliography\n\n";
313 parse_text(p, os, FLAG_END, outer);
317 else if (t.cs() == "end") {
318 if (flags & FLAG_END) {
319 // eat environment name
320 string const name = p.getArg('{', '}');
321 if (name != active_environment())
322 cerr << "\\end{" + name + "} does not match \\begin{"
323 + active_environment() + "}\n";
324 active_environments.pop_back();
328 p.error("found 'end' unexpectedly");
331 else if (t.cs() == "item") {
334 if (p.next_token().character() == '[') {
335 p.get_token(); // eat '['
336 s = parse_text(p, FLAG_BRACK_LAST, outer);
342 else if (t.cs() == "def") {
343 string name = p.get_token().cs();
344 while (p.next_token().cat() != catBegin)
345 name += p.get_token().asString();
346 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}');
349 else if (t.cs() == "par") {
351 if (p.next_token().cs() != "\\begin")
353 //cerr << "next token: '" << p.next_token().cs() << "'\n";
356 else if (is_known(t.cs(), known_headings)) {
357 string name = t.cs();
358 if (p.next_token().asInput() == "*") {
362 os << "\n\n\\layout " << cap(name) << "\n\n";
363 string opt = p.getOpt();
365 begin_inset(os, "OptArg\n");
366 os << "collapsed true\n\n\\layout Standard\n\n" << opt;
369 parse_text(p, os, FLAG_ITEM, outer);
370 os << "\n\n\\layout Standard\n\n";
373 else if (t.cs() == "includegraphics") {
374 map<string, string> opts = split_map(p.getArg('[', ']'));
375 string name = p.verbatim_item();
376 begin_inset(os, "Graphics ");
377 os << "\n\tfilename " << name << '\n';
378 if (opts.find("width") != opts.end())
379 os << "\twidth " << opts["width"] << '\n';
380 if (opts.find("height") != opts.end())
381 os << "\theight " << opts["height"] << '\n';
385 else if (t.cs() == "footnote") {
386 begin_inset(os, "Foot\n");
387 os << "collapsed true\n\n\\layout Standard\n\n";
388 parse_text(p, os, FLAG_ITEM, false);
392 else if (t.cs() == "ensuremath") {
393 string s = parse_text(p, FLAG_ITEM, false);
394 if (s == "±" || s == "³" || s == "²" || s == "µ")
397 handle_ert(os, "\\ensuremath{" + s + "}");
400 else if (t.cs() == "marginpar") {
401 begin_inset(os, "Marginal\n");
402 os << "collapsed true\n\n\\layout Standard\n\n";
403 parse_text(p, os, FLAG_ITEM, false);
407 else if (t.cs() == "hfill") {
412 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
413 skip_braces(p); // swallow this
415 else if (t.cs() == "tableofcontents")
416 skip_braces(p); // swallow this
418 else if (t.cs() == "textrm") {
419 os << "\n\\family roman \n";
420 parse_text(p, os, FLAG_ITEM, outer);
421 os << "\n\\family default \n";
424 else if (t.cs() == "textsf") {
425 os << "\n\\family sans \n";
426 parse_text(p, os, FLAG_ITEM, outer);
427 os << "\n\\family default \n";
430 else if (t.cs() == "texttt") {
431 os << "\n\\family typewriter \n";
432 parse_text(p, os, FLAG_ITEM, outer);
433 os << "\n\\family default \n";
436 else if (t.cs() == "textit") {
437 os << "\n\\shape italic \n";
438 parse_text(p, os, FLAG_ITEM, outer);
439 os << "\n\\shape default \n";
442 else if (t.cs() == "textsc") {
443 os << "\n\\noun on \n";
444 parse_text(p, os, FLAG_ITEM, outer);
445 os << "\n\\noun default \n";
448 else if (t.cs() == "textbf") {
449 os << "\n\\series bold \n";
450 parse_text(p, os, FLAG_ITEM, outer);
451 os << "\n\\series default \n";
454 else if (t.cs() == "underbar") {
455 os << "\n\\bar under \n";
456 parse_text(p, os, FLAG_ITEM, outer);
457 os << "\n\\bar default \n";
460 else if (t.cs() == "emph" || t.cs() == "noun") {
461 os << "\n\\" << t.cs() << " on \n";
462 parse_text(p, os, FLAG_ITEM, outer);
463 os << "\n\\" << t.cs() << " default \n";
466 else if (t.cs() == "bibitem") {
467 os << "\n\\layout Bibliography\n\\bibitem ";
469 os << '{' << p.verbatim_item() << '}' << "\n\n";
472 else if (is_known(t.cs(), known_latex_commands)) {
473 begin_inset(os, "LatexCommand ");
474 os << '\\' << t.cs();
477 os << '{' << p.verbatim_item() << '}';
481 else if (is_known(t.cs(), known_quotes)) {
482 char const ** where = is_known(t.cs(), known_quotes);
483 begin_inset(os, "Quotes ");
484 os << known_coded_quotes[where - known_quotes];
489 else if (is_known(t.cs(), known_sizes)) {
490 char const ** where = is_known(t.cs(), known_sizes);
491 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
494 else if (t.cs() == "LyX" || t.cs() == "TeX"
495 || t.cs() == "LaTeX" || t.cs() == "LaTeXe") {
497 skip_braces(p); // eat {}
500 else if (t.cs() == "lyxarrow") {
501 os << "\\SpecialChar \\menuseparator\n";
505 else if (t.cs() == "ldots") {
506 os << "\\SpecialChar \\ldots{}\n";
510 else if (t.cs() == "@") {
511 os << "\\SpecialChar \\@";
515 else if (t.cs() == "textasciitilde") {
520 else if (t.cs() == "textasciicircum") {
525 else if (t.cs() == "textbackslash") {
526 os << "\n\\backslash\n";
530 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#" || t.cs() == "$"
531 || t.cs() == "{" || t.cs() == "}" || t.cs() == "%")
534 else if (t.cs() == "char") {
535 if (p.next_token().character() == '`') {
537 if (p.next_token().cs() == "\"") {
542 handle_ert(os, "\\char`");
545 handle_ert(os, "\\char");
549 else if (t.cs() == "\"") {
550 string const name = p.verbatim_item();
551 if (name == "a") os << 'ä';
552 else if (name == "o") os << 'ö';
553 else if (name == "u") os << 'ü';
554 else if (name == "A") os << 'Ä';
555 else if (name == "O") os << 'Ö';
556 else if (name == "U") os << 'Ü';
557 else handle_ert(os, "\"{" + name + "}");
560 else if (t.cs() == "=" || t.cs() == "H" || t.cs() == "c"
561 || t.cs() == "^" || t.cs() == "'" || t.cs() == "~") {
562 // we need the trim as the LyX parser chokes on such spaces
563 os << "\n\\i \\" << t.cs() << "{"
564 << trim(parse_text(p, FLAG_ITEM, outer), " ") << "}\n";
567 else if (t.cs() == "ss")
570 else if (t.cs() == "i" || t.cs() == "j")
571 os << "\\" << t.cs() << ' ';
573 else if (t.cs() == "-")
574 os << "\\SpecialChar \\-\n";
576 else if (t.cs() == "\\")
577 os << "\n\\newline\n";
579 else if (t.cs() == "lyxrightaddress") {
580 os << "\n\\layout Right Address\n";
581 parse_text(p, os, FLAG_ITEM, outer);
582 os << "\n\\layout Standard\n";
585 else if (t.cs() == "lyxaddress") {
586 os << "\n\\layout Address\n";
587 parse_text(p, os, FLAG_ITEM, outer);
588 os << "\n\\layout Standard\n";
591 else if (t.cs() == "input")
592 handle_ert(os, "\\input{" + p.verbatim_item() + "}\n");
594 else if (t.cs() == "fancyhead") {
598 ss << '{' << p.verbatim_item() << "}\n";
599 handle_ert(os, STRCONV(ss.str()));
603 //cerr << "#: " << t << " mode: " << mode << endl;
604 // heuristic: read up to next non-nested space
606 string s = t.asInput();
607 string z = p.verbatim_item();
608 while (p.good() && z != " " && z.size()) {
609 //cerr << "read: " << z << endl;
611 z = p.verbatim_item();
613 cerr << "found ERT: " << s << endl;
614 handle_ert(os, s + ' ');
616 handle_ert(os, t.asInput() + ' ');
619 if (flags & FLAG_LEAVE) {
620 flags &= ~FLAG_LEAVE;
627 string parse_text(Parser & p, unsigned flags, const bool outer)
630 parse_text(p, os, flags, outer);
631 return STRCONV(os.str());