1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
18 #include "texparser.h"
28 using std::istringstream;
30 using std::ostringstream;
38 char const OPEN = '<';
39 char const CLOSE = '>';
41 const char * known_languages[] = { "austrian", "babel", "bahasa",
42 "basque", "breton", "bulgarian", "catalan", "croatian", "czech", "danish",
43 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
44 "frenchb", "galician", "germanb", "greek", "hebcal", "hebfont", "hebrew",
45 "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen", "icelandic",
46 "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss", "lgrcmtt",
47 "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr", "lhecmss",
48 "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
49 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
50 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
51 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
52 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
54 const char * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
58 ostringstream h_preamble;
59 string h_textclass = "FIXME";
60 string h_options = "FIXME";
61 string h_language = "FIXME";
62 string h_inputencoding = "FIXME";
63 string h_fontscheme = "FIXME";
64 string h_graphics = "default";
65 string h_paperfontsize = "FIXME";
66 string h_spacing = "single";
67 string h_papersize = "FIXME";
68 string h_paperpackage = "FIXME";
69 string h_use_geometry = "0";
70 string h_use_amsmath = "0";
71 string h_use_natbib = "0";
72 string h_use_numerical_citations = "0";
73 string h_paperorientation = "portrait";
74 string h_secnumdepth = "3";
75 string h_tocdepth = "3";
76 string h_paragraph_separation = "indent";
77 string h_defskip = "medskip";
78 string h_quotes_language = "FIXME";
79 string h_quotes_times = "1";
80 string h_papercolumns = "1";
81 string h_papersides = "1";
82 string h_paperpagestyle = "default";
83 string h_tracking_changes = "0";
85 // indicates whether we are in the preamble
86 bool in_preamble = true;
88 // current stack of nested environments
89 stack<string> active_environments;
93 string const trim(string const & a, char const * p = " ")
100 string::size_type r = a.find_last_not_of(p);
101 string::size_type l = a.find_first_not_of(p);
103 // Is this the minimal test? (lgb)
104 if (r == string::npos && l == string::npos)
107 return a.substr(l, r - l + 1);
111 void split(string const & s, vector<string> & result, char delim)
115 while (getline(is, t, delim))
120 string join(vector<string> const & input, char delim)
123 for (size_t i = 0; i != input.size(); ++i) {
132 void handle_opt(vector<string> & opts, char const ** what, string & target)
137 for ( ; what; ++what) {
138 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
139 if (it != opts.end()) {
140 //cerr << "### found option '" << *what << "'\n";
149 void begin_inset(ostream & os, string const & name)
151 os << "\n\\begin_inset " << name;
155 void end_inset(ostream & os)
157 os << "\n\\end_inset\n";
161 void handle_ert(ostream & os, string const & s)
163 begin_inset(os, "ERT");
164 os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
165 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
167 os << "\n\\backslash\n";
175 void handle_par(ostream & os)
177 if (active_environments.empty())
180 string s = active_environments.top();
181 if (s == "document") {
182 os << "Standard\n\n";
185 if (s == "lyxcode") {
186 os << "LyX-Code\n\n";
190 s[0] = toupper(s[0]);
195 void handle_package(string const & name, string const & options)
197 if (name == "a4wide") {
198 h_papersize = "a4paper";
199 h_paperpackage = "widemarginsa4";
200 } else if (name == "ae")
202 else if (name == "aecompl")
204 else if (name == "amsmath")
206 else if (name == "amssymb")
208 else if (name == "babel")
210 else if (name == "fontenc")
212 else if (name == "inputenc")
213 h_inputencoding = options;
214 else if (name == "makeidx")
216 else if (name == "verbatim")
220 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
222 h_preamble << "\\usepackage{" << name << "}\n";
227 string wrap(string const & cmd, string const & str)
229 return OPEN + cmd + ' ' + str + CLOSE;
233 void end_preamble(ostream & os)
236 os << "# tex2lyx 0.0.2 created this file\n"
237 << "\\lyxformat 222\n"
238 << "\\textclass " << h_textclass << "\n"
239 << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n"
240 << "\\options " << h_options << "\n"
241 << "\\language " << h_language << "\n"
242 << "\\inputencoding " << h_inputencoding << "\n"
243 << "\\fontscheme " << h_fontscheme << "\n"
244 << "\\graphics " << h_graphics << "\n"
245 << "\\paperfontsize " << h_paperfontsize << "\n"
246 << "\\spacing " << h_spacing << "\n"
247 << "\\papersize " << h_papersize << "\n"
248 << "\\paperpackage " << h_paperpackage << "\n"
249 << "\\use_geometry " << h_use_geometry << "\n"
250 << "\\use_amsmath " << h_use_amsmath << "\n"
251 << "\\use_natbib " << h_use_natbib << "\n"
252 << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
253 << "\\paperorientation " << h_paperorientation << "\n"
254 << "\\secnumdepth " << h_secnumdepth << "\n"
255 << "\\tocdepth " << h_tocdepth << "\n"
256 << "\\paragraph_separation " << h_paragraph_separation << "\n"
257 << "\\defskip " << h_defskip << "\n"
258 << "\\quotes_language " << h_quotes_language << "\n"
259 << "\\quotes_times " << h_quotes_times << "\n"
260 << "\\papercolumns " << h_papercolumns << "\n"
261 << "\\papersides " << h_papersides << "\n"
262 << "\\paperpagestyle " << h_paperpagestyle << "\n"
263 << "\\tracking_changes " << h_tracking_changes << "\n"
264 << h_preamble.str() << "\n";
268 void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
271 Token const & t = p.getToken();
274 cerr << "t: " << t << " flags: " << flags << "\n";
278 if (flags & FLAG_ITEM) {
279 if (t.cat() == catSpace)
283 if (t.cat() == catBegin) {
284 // skip the brace and collect everything to the next matching
286 flags |= FLAG_BRACE_LAST;
290 // handle only this single token, leave the loop if done
295 if (flags & FLAG_BRACED) {
296 if (t.cat() == catSpace)
299 if (t.cat() != catBegin) {
300 p.error("opening brace expected");
304 // skip the brace and collect everything to the next matching
306 flags = FLAG_BRACE_LAST;
310 if (flags & FLAG_OPTION) {
311 if (t.cat() == catOther && t.character() == '[') {
312 parse(p, os, FLAG_BRACK_LAST, mode);
314 // no option found, put back token and we are done
323 if (t.cat() == catMath) {
324 if (mode == TEXT_MODE || mode == MATHTEXT_MODE) {
325 // we are inside some text mode thingy, so opening new math is allowed
326 if (mode == TEXT_MODE)
327 begin_inset(os, "Formula ");
328 Token const & n = p.getToken();
329 if (n.cat() == catMath) {
330 // TeX's $$...$$ syntax for displayed math
332 parse(p, os, FLAG_SIMPLE, MATH_MODE);
334 p.getToken(); // skip the second '$' token
336 // simple $...$ stuff
339 parse(p, os, FLAG_SIMPLE, MATH_MODE);
342 if (mode == TEXT_MODE)
346 else if (flags & FLAG_SIMPLE) {
347 // this is the end of the formula
352 cerr << "mode: " << mode << endl;
353 p.error("something strange in the parser\n");
358 else if (t.cat() == catLetter)
361 else if (t.cat() == catSpace)
364 else if (t.cat() == catNewline)
367 else if (t.cat() == catParameter) {
368 Token const & n = p.getToken();
369 os << wrap("macroarg", string(1, n.character()));
372 else if (t.cat() == catActive)
373 os << wrap("active", string(1, t.character()));
375 else if (t.cat() == catBegin) {
377 parse(p, os, FLAG_BRACE_LAST, mode);
381 else if (t.cat() == catEnd) {
382 if (flags & FLAG_BRACE_LAST)
384 p.error("found '}' unexpectedly");
386 //add(cell, '}', LM_TC_TEX);
389 else if (t.cat() == catAlign)
394 //cerr << " column now " << cellcol << " max: " << grid.ncols() << "\n";
395 if (cellcol == grid.ncols()) {
396 //cerr << "adding column " << cellcol << "\n";
397 grid.addCol(cellcol - 1);
399 cell = &grid.cell(grid.index(cellrow, cellcol));
403 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
404 //cerr << "finished reading option\n";
408 else if (t.cat() == catOther)
409 os << string(1, t.character());
411 else if (t.cat() == catComment) {
414 Token const & t = p.getToken();
415 if (t.cat() == catNewline)
419 //os << wrap("comment", s);
427 else if (t.cs() == "lyxlock") {
431 else if (t.cs() == "newcommand" || t.cs() == "providecommand") {
432 string const name = p.verbatimItem();
433 string const opts = p.getArg('[', ']');
434 string const body = p.verbatimItem();
435 // only non-lyxspecific stuff
436 if (name != "noun" && name != "tabularnewline") {
437 h_preamble << "\\" << t.cs() << "{" << name << "}";
439 h_preamble << "[" << opts << "]";
440 h_preamble << "{" << body << "}\n";
444 else if (t.cs() == "(") {
445 begin_inset(os, "Formula");
447 parse(p, os, FLAG_SIMPLE2, MATH_MODE);
452 else if (t.cs() == "[") {
453 begin_inset(os, "Formula");
455 parse(p, os, FLAG_EQUATION, MATH_MODE);
460 else if (t.cs() == "protect")
461 // ignore \\protect, will hopefully be re-added during output
464 else if (t.cs() == "begin") {
465 string const name = p.getArg('{', '}');
466 active_environments.push(name);
467 if (name == "document")
469 else if (name == "abstract")
472 os << "\\begin{" << name << "}";
473 parse(p, os, FLAG_END, mode);
476 else if (t.cs() == "end") {
477 if (flags & FLAG_END) {
478 // eat environment name
479 string const name = p.getArg('{', '}');
480 if (name != active_environments.top())
481 p.error("\\end{" + name + "} does not match \\begin{"
482 + active_environments.top() + "}");
483 active_environments.pop();
484 if (name == "document" || name == "abstract")
487 os << "\\end{" << name << "}";
490 p.error("found 'end' unexpectedly");
493 else if (t.cs() == ")") {
494 if (flags & FLAG_SIMPLE2)
496 p.error("found '\\)' unexpectedly");
499 else if (t.cs() == "]") {
500 if (flags & FLAG_EQUATION)
502 p.error("found '\\]' unexpectedly");
505 else if (t.cs() == "documentclass") {
507 split(p.getArg('[', ']'), opts, ',');
508 handle_opt(opts, known_languages, h_language);
509 handle_opt(opts, known_fontsizes, h_paperfontsize);
510 h_quotes_language = h_language;
511 h_options = join(opts, ',');
512 h_textclass = p.getArg('{', '}');
515 else if (t.cs() == "usepackage") {
516 string const options = p.getArg('[', ']');
517 string const name = p.getArg('{', '}');
518 if (options.empty() && name.find(',')) {
519 vector<string> vecnames;
520 split(name, vecnames, ',');
521 vector<string>::const_iterator it = vecnames.begin();
522 vector<string>::const_iterator end = vecnames.end();
523 for (; it != end; ++it)
524 handle_package(trim(*it), string());
526 handle_package(name, options);
530 else if (t.cs() == "newenvironment") {
531 string const name = p.getArg('{', '}');
533 string const begin = p.verbatimItem();
535 string const end = p.verbatimItem();
537 if (name != "lyxcode")
538 os << wrap("newenvironment", begin + end);
541 else if (t.cs() == "def") {
542 string name = p.getToken().cs();
543 while (p.nextToken().cat() != catBegin)
544 name += p.getToken().asString();
545 handle_ert(os, "\\def\\" + name + '{' + p.verbatimItem() + '}');
548 else if (t.cs() == "setcounter") {
549 string const name = p.getArg('{', '}');
550 string const content = p.getArg('{', '}');
551 if (name == "secnumdepth")
552 h_secnumdepth = content;
553 else if (name == "tocdepth")
554 h_tocdepth = content;
556 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
559 else if (t.cs() == "setlength") {
560 string const name = p.getToken().cs();
561 string const content = p.getArg('{', '}');
562 if (name == "parskip")
563 h_paragraph_separation = "skip";
564 else if (name == "parindent")
565 h_paragraph_separation = "skip";
567 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
570 else if (t.cs() == "par")
573 else if (t.cs() == "title")
574 os << "\\layout Title\n\n" + p.verbatimItem();
576 else if (t.cs() == "author")
577 os << "\\layout Author\n\n" + p.verbatimItem();
579 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
582 else if (t.cs() == "tableofcontents")
583 p.verbatimItem(); // swallow this
585 else if (t.cs() == "textrm") {
586 os << '\\' << t.cs() << '{';
587 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
591 else if (t.cs() == "emph" && mode == TEXT_MODE) {
592 os << "\n\\emph on\n";
593 parse(p, os, FLAG_ITEM, mode);
594 os << "\n\\emph default\n";
597 else if (t.cs() == "index") {
598 begin_inset(os, "LatexCommand");
599 parse(p, os, FLAG_ITEM, TEXT_MODE);
604 (in_preamble ? h_preamble : os) << '\\' << t.asString();
606 if (flags & FLAG_LEAVE) {
607 flags &= ~FLAG_LEAVE;
615 } // anonymous namespace
618 int main(int argc, char * argv[])
621 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
625 ifstream is(argv[1]);
627 parse(p, cout, 0, TEXT_MODE);
628 cout << "\n\\the_end";