1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
16 #include "texparser.h"
26 using std::istringstream;
28 using std::ostringstream;
36 char const OPEN = '<';
37 char const CLOSE = '>';
39 const char * known_languages[] = { "austrian", "babel", "bahasa",
40 "basque", "breton", "bulgarian", "catalan", "croatian", "czech", "danish",
41 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
42 "frenchb", "galician", "germanb", "greek", "hebcal", "hebfont", "hebrew",
43 "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen", "icelandic",
44 "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss", "lgrcmtt",
45 "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr", "lhecmss",
46 "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
47 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
48 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
49 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
50 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
52 const char * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
57 string h_textclass = "FIXME";
58 string h_options = "FIXME";
59 string h_language = "FIXME";
60 string h_inputencoding = "FIXME";
61 string h_fontscheme = "FIXME";
62 string h_graphics = "default";
63 string h_paperfontsize = "FIXME";
64 string h_spacing = "single";
65 string h_papersize = "FIXME";
66 string h_paperpackage = "FIXME";
67 string h_use_geometry = "0";
68 string h_use_amsmath = "0";
69 string h_use_natbib = "0";
70 string h_use_numerical_citations = "0";
71 string h_paperorientation = "portrait";
72 string h_secnumdepth = "3";
73 string h_tocdepth = "3";
74 string h_paragraph_separation = "indent";
75 string h_defskip = "medskip";
76 string h_quotes_language = "2";
77 string h_quotes_times = "1";
78 string h_papercolumns = "1";
79 string h_papersides = "1";
80 string h_paperpagestyle = "default";
81 string h_tracking_changes = "0";
83 // indicates whether we are in the preamble
84 bool in_preamble = true;
86 // current stack of nested environments
87 stack<string> active_environments;
91 string const trim(string const & a, char const * p = " ")
98 string::size_type r = a.find_last_not_of(p);
99 string::size_type l = a.find_first_not_of(p);
101 // Is this the minimal test? (lgb)
102 if (r == string::npos && l == string::npos)
105 return a.substr(l, r - l + 1);
109 void split(string const & s, vector<string> & result, char delim)
113 while (getline(is, t, delim))
118 string join(vector<string> const & input, char delim)
121 for (size_t i = 0; i != input.size(); ++i) {
130 void handle_opt(vector<string> & opts, char const ** what, string & target)
135 for ( ; what; ++what) {
136 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
137 if (it != opts.end()) {
138 //cerr << "### found option '" << *what << "'\n";
147 void handle_ert(ostream & os, string const & s)
149 os << "\n\\begin_inset ERT\nstatus Collapsed\n\n\\layout Standard\n\n";
151 os << "\n\\end_inset\n";
155 void handle_par(ostream & os)
157 if (active_environments.empty())
160 string s = active_environments.top();
169 void handle_package(string const & name, string const & options)
171 if (name == "a4wide") {
173 h_paperpackage = "widemarginsa4";
174 } else if (name == "ae")
176 else if (name == "aecompl")
178 else if (name == "amsmath")
180 else if (name == "amssymb")
182 else if (name == "babel")
184 else if (name == "fontenc")
186 else if (name == "inputenc")
187 h_inputencoding = options;
188 else if (name == "makeidx")
190 else if (name == "verbatim")
194 h_preamble += "\\usepackage[" + options + "]{" + name + "}\n";
196 h_preamble += "\\usepackage{" + name + "}\n";
201 string wrap(string const & cmd, string const & str)
203 return OPEN + cmd + ' ' + str + CLOSE;
207 string wrap(string const & cmd, string const & str, string const & str2)
209 return OPEN + cmd + ' ' + str + ' ' + str2 + CLOSE;
213 string parse(Parser & p, unsigned flags, mode_type mode)
217 ostringstream result;
219 Token const & t = p.getToken();
222 cerr << "t: " << t << " flags: " << flags << "\n";
227 if (flags & FLAG_ITEM) {
228 if (t.cat() == catSpace)
232 if (t.cat() == catBegin) {
233 // skip the brace and collect everything to the next matching
235 flags |= FLAG_BRACE_LAST;
239 // handle only this single token, leave the loop if done
244 if (flags & FLAG_BRACED) {
245 if (t.cat() == catSpace)
248 if (t.cat() != catBegin) {
249 p.error("opening brace expected");
253 // skip the brace and collect everything to the next matching
255 flags = FLAG_BRACE_LAST;
259 if (flags & FLAG_OPTION) {
260 if (t.cat() == catOther && t.character() == '[') {
261 result << parse(p, FLAG_BRACK_LAST, mode);
263 // no option found, put back token and we are done
272 if (t.cat() == catMath) {
273 if (mode != MATH_MODE) {
274 // we are inside some text mode thingy, so opening new math is allowed
275 Token const & n = p.getToken();
276 if (n.cat() == catMath) {
277 // TeX's $$...$$ syntax for displayed math
278 result << wrap("equation", parse(p, FLAG_SIMPLE, MATH_MODE));
279 p.getToken(); // skip the second '$' token
281 // simple $...$ stuff
283 result << wrap("simple", parse(p, FLAG_SIMPLE, MATH_MODE));
287 else if (flags & FLAG_SIMPLE) {
288 // this is the end of the formula
293 p.error("something strange in the parser\n");
298 else if (t.cat() == catLetter)
299 result << t.character();
301 else if (t.cat() == catSpace && mode != MATH_MODE) {
302 //if (result.empty() || result[result.size() - 1] != ' ')
303 result << t.character();
306 else if (t.cat() == catNewline && mode != MATH_MODE)
307 result << t.character();
309 else if (t.cat() == catParameter) {
310 Token const & n = p.getToken();
311 result << wrap("macroarg", string(1, n.character()));
314 else if (t.cat() == catActive)
315 result << wrap("active", string(1, t.character()));
317 else if (t.cat() == catBegin)
318 result << wrap("braced", parse(p, FLAG_BRACE_LAST, mode));
320 else if (t.cat() == catEnd) {
321 if (flags & FLAG_BRACE_LAST)
323 p.error("found '}' unexpectedly");
325 //add(cell, '}', LM_TC_TEX);
329 else if (t.cat() == catAlign) {
331 //cerr << " column now " << cellcol << " max: " << grid.ncols() << "\n";
332 if (cellcol == grid.ncols()) {
333 //cerr << "adding column " << cellcol << "\n";
334 grid.addCol(cellcol - 1);
336 cell = &grid.cell(grid.index(cellrow, cellcol));
340 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
341 //cerr << "finished reading option\n";
345 else if (t.cat() == catOther)
346 result << string(1, t.character());
348 else if (t.cat() == catComment) {
351 Token const & t = p.getToken();
352 if (t.cat() == catNewline)
356 //result << wrap("comment", s);
364 else if (t.cs() == "lyxlock") {
368 else if (t.cs() == "newcommand" || t.cs() == "providecommand") {
369 string const name = p.verbatimItem();
370 string const opts = p.getArg('[', ']');
371 string const body = p.verbatimItem();
372 // only non-lyxspecific stuff
373 if (name != "noun" && name != "tabularnewline") {
374 h_preamble += "\\" + t.cs() + "{" + name + "}";
376 h_preamble += "[" + opts + "]";
377 h_preamble += "{" + body + "}\n";
381 else if (t.cs() == "(")
382 result << wrap("simple", parse(p, FLAG_SIMPLE2, MATH_MODE));
384 else if (t.cs() == "[")
385 result << wrap("equation", parse(p, FLAG_EQUATION, MATH_MODE));
387 else if (t.cs() == "protect")
388 // ignore \\protect, will hopefully be re-added during output
391 else if (t.cs() == "end") {
392 if (flags & FLAG_END) {
393 // eat environment name
394 string const name = p.getArg('{', '}');
395 if (name != active_environments.top())
396 p.error("\\end{" + name + "} does not match \\begin{"
397 + active_environments.top() + "}");
398 active_environments.pop();
401 p.error("found 'end' unexpectedly");
404 else if (t.cs() == ")") {
405 if (flags & FLAG_SIMPLE2)
407 p.error("found '\\)' unexpectedly");
410 else if (t.cs() == "]") {
411 if (flags & FLAG_EQUATION)
413 p.error("found '\\]' unexpectedly");
417 else if (t.cs() == "\\") {
418 grid.vcrskip(LyXLength(getArg('[', ']')), cellrow);
421 if (cellrow == grid.nrows())
422 grid.addRow(cellrow - 1);
423 if (grid.asHullstring())
424 grid.asHullstring()->numbered(cellrow, numbered);
425 cell = &grid.cell(grid.index(cellrow, cellcol));
428 else if (t.cs() == "documentclass") {
430 split(p.getArg('[', ']'), opts, ',');
431 handle_opt(opts, known_languages, h_language);
432 handle_opt(opts, known_fontsizes, h_paperfontsize);
433 h_options = join(opts, ',');
434 h_textclass = p.getArg('{', '}');
437 else if (t.cs() == "usepackage") {
438 string const options = p.getArg('[', ']');
439 string const name = p.getArg('{', '}');
440 if (options.empty() && name.find(',')) {
441 vector<string> vecnames;
442 split(name, vecnames, ',');
443 vector<string>::const_iterator it = vecnames.begin();
444 vector<string>::const_iterator end = vecnames.end();
445 for (; it != end; ++it) {
446 handle_package(trim(*it), string());
449 handle_package(name, options);
453 else if (t.cs() == "newenvironment") {
454 string const name = p.getArg('{', '}');
456 string const begin = p.verbatimItem();
458 string const end = p.verbatimItem();
460 if (name != "lyxcode")
461 result << wrap("newenvironment", begin + end);
464 else if (t.cs() == "def") {
465 string const name = p.getToken().cs();
467 while (p.nextToken().cat() != catBegin)
468 res += p.getToken().asString();
469 handle_ert(result, "\\def" + res + '{' + p.verbatimItem() + '}');
472 else if (t.cs() == "setcounter") {
473 string const name = p.getArg('{', '}');
474 string const content = p.getArg('{', '}');
475 if (name == "secnumdepth")
476 h_secnumdepth = content;
477 else if (name == "tocdepth")
478 h_tocdepth = content;
480 h_preamble += "\\setcounter{" + name + "}{" + content + "}\n";
483 else if (t.cs() == "setlength") {
484 string const name = p.getToken().cs();
485 string const content = p.getArg('{', '}');
486 if (name == "parskip")
487 h_paragraph_separation = "skip";
488 else if (name == "parindent")
489 h_paragraph_separation = "skip";
491 h_preamble += "\\setcounter{" + name + "}{" + content + "}\n";
494 else if (t.cs() == "par")
497 else if (t.cs() == "title")
498 result << "\\layout Title\n\n" + p.verbatimItem();
500 else if (t.cs() == "author")
501 result << "\\layout Author\n\n" + p.verbatimItem();
503 else if (t.cs() == "abstract")
504 result << "\\layout Abstract\n\n" + p.verbatimItem();
506 else if (t.cs() == "begin") {
507 string const name = p.getArg('{', '}');
508 active_environments.push(name);
509 result << parse(p, FLAG_END, mode);
512 if (flags & FLAG_LEAVE) {
513 flags &= ~FLAG_LEAVE;
522 } // anonymous namespace
525 int main(int argc, char * argv[])
528 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
532 ifstream is(argv[1]);
534 string s = parse(p, 0, UNDECIDED_MODE);
535 cout << "# tex2lyx 0.0.2 created this file\n"
536 << "\\lyxformat 222\n"
537 << "\\textclass " << h_textclass << "\n"
538 << "\\begin_preamble\n" << h_preamble << "\\end_preamble\n"
539 << "\\options " << h_options << "\n"
540 << "\\language " << h_language << "\n"
541 << "\\inputencoding " << h_inputencoding << "\n"
542 << "\\fontscheme " << h_fontscheme << "\n"
543 << "\\graphics " << h_graphics << "\n"
544 << "\\paperfontsize " << h_paperfontsize << "\n"
545 << "\\spacing " << h_spacing << "\n"
546 << "\\papersize " << h_papersize << "\n"
547 << "\\paperpackage " << h_paperpackage << "\n"
548 << "\\use_geometry " << h_use_geometry << "\n"
549 << "\\use_amsmath " << h_use_amsmath << "\n"
550 << "\\use_natbib " << h_use_natbib << "\n"
551 << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
552 << "\\paperorientation " << h_paperorientation << "\n"
553 << "\\secnumdepth " << h_secnumdepth << "\n"
554 << "\\tocdepth " << h_tocdepth << "\n"
555 << "\\paragraph_separation " << h_paragraph_separation << "\n"
556 << "\\defskip " << h_defskip << "\n"
557 << "\\quotes_language " << h_quotes_language << "\n"
558 << "\\quotes_times " << h_quotes_times << "\n"
559 << "\\papercolumns " << h_papercolumns << "\n"
560 << "\\papersides " << h_papersides << "\n"
561 << "\\paperpagestyle " << h_paperpagestyle << "\n"
562 << "\\tracking_changes " << h_tracking_changes << "\n"