]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
*** empty log message ***
[lyx.git] / src / tex2lyx / tex2lyx.C
1 /** The .tex to .lyx converter
2     \author André Pönitz (2003)
3  */
4
5 // {[(
6
7 #include <config.h>
8
9 #include <algorithm>
10 #include <cctype>
11 #include <fstream>
12 #include <iostream>
13 #include <sstream>
14 #include <stack>
15 #include <string>
16 #include <vector>
17
18 #include "texparser.h"
19
20 using std::cout;
21 using std::cerr;
22 using std::endl;
23 using std::fill;
24 using std::getline;
25 using std::ios;
26 using std::ifstream;
27 using std::istream;
28 using std::istringstream;
29 using std::ostream;
30 using std::ostringstream;
31 using std::stack;
32 using std::string;
33 using std::vector;
34
35
36 namespace {
37
38 char const OPEN = '<';
39 char const CLOSE = '>';
40
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};
53
54 const char * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
55
56
57 const char * known_math_envs[] = {"equation", "eqnarray", "eqnarray*",
58 "align", "align*", 0};
59
60
61 // some ugly stuff
62 ostringstream h_preamble;
63 string h_textclass               = "FIXME";
64 string h_options                 = "FIXME";
65 string h_language                = "FIXME";
66 string h_inputencoding           = "FIXME";
67 string h_fontscheme              = "FIXME";
68 string h_graphics                = "default";
69 string h_paperfontsize           = "FIXME";
70 string h_spacing                 = "single";
71 string h_papersize               = "FIXME";
72 string h_paperpackage            = "FIXME";
73 string h_use_geometry            = "0";
74 string h_use_amsmath             = "0";
75 string h_use_natbib              = "0";
76 string h_use_numerical_citations = "0";
77 string h_paperorientation        = "portrait";
78 string h_secnumdepth             = "3";
79 string h_tocdepth                = "3";
80 string h_paragraph_separation    = "indent";
81 string h_defskip                 = "medskip";
82 string h_quotes_language         = "FIXME";
83 string h_quotes_times            = "1";
84 string h_papercolumns            = "1";
85 string h_papersides              = "1";
86 string h_paperpagestyle          = "default";
87 string h_tracking_changes        = "0";
88
89 // indicates whether we are in the preamble
90 bool in_preamble = true;
91
92 // current stack of nested environments
93 stack<string> active_environments;
94
95
96 string cap(string s)
97 {
98         if (s.size())
99                 s[0] = toupper(s[0]);
100         return s;
101 }
102
103
104 string const trim(string const & a, char const * p = " ")
105 {
106         // lyx::Assert(p);
107
108         if (a.empty() || !*p)
109                 return a;
110
111         string::size_type r = a.find_last_not_of(p);
112         string::size_type l = a.find_first_not_of(p);
113
114         // Is this the minimal test? (lgb)
115         if (r == string::npos && l == string::npos)
116                 return string();
117
118         return a.substr(l, r - l + 1);
119 }
120
121
122 void split(string const & s, vector<string> & result, char delim)
123 {
124         istringstream is(s);    
125         string t;
126         while (getline(is, t, delim))
127                 result.push_back(t);
128 }
129
130
131 string join(vector<string> const & input, char delim)
132 {
133         ostringstream os;
134         for (size_t i = 0; i != input.size(); ++i) {
135                 if (i)
136                         os << delim;    
137                 os << input[i]; 
138         }
139         return os.str();
140 }
141
142
143 void handle_opt(vector<string> & opts, char const ** what, string & target)
144 {
145         if (opts.empty())
146                 return;
147
148         for ( ; what; ++what) {
149                 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
150                 if (it != opts.end()) {
151                         //cerr << "### found option '" << *what << "'\n";
152                         target = *what;
153                         opts.erase(it);
154                         return;
155                 }
156         }
157 }
158
159
160 bool is_math_env(string const & name)
161 {
162         for (char const ** what = known_math_envs; *what; ++what)
163                 if (*what == name)
164                         return true;
165         return false;
166 }
167
168
169 bool is_heading(string const & name)
170 {
171         return
172                 name == "caption" ||
173                 name == "title" ||
174                 name == "author" ||
175                 name == "paragraph" ||
176                 name == "chapter" ||
177                 name == "chapter*" ||
178                 name == "section" ||
179                 name == "section*" ||
180                 name == "subsection" ||
181                 name == "subsection*" ||
182                 name == "subsubsection" ||
183                 name == "subsubsection*";
184 }
185
186
187 bool is_latex_command(string const & name)
188 {
189         return
190                 name == "ref" ||
191                 name == "cite" ||
192                 name == "label" ||
193                 name == "index" ||
194                 name == "printindex";
195 }
196
197
198 void begin_inset(ostream & os, string const & name)
199 {
200         os << "\n\\begin_inset " << name;
201 }
202
203
204 void end_inset(ostream & os)
205 {
206         os << "\n\\end_inset\n";
207 }
208
209
210 string curr_env()
211 {
212         return active_environments.empty() ? string() : active_environments.top();
213 }
214
215
216 void handle_ert(ostream & os, string const & s)
217 {
218         begin_inset(os, "ERT");
219         os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
220         for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
221                 if (*it == '\\')
222                         os << "\n\\backslash\n";
223                 else
224                         os << *it;
225         }
226         end_inset(os);
227 }
228
229
230 void handle_par(ostream & os)
231 {
232         if (active_environments.empty())
233                 return;
234         os << "\n\\layout ";
235         string s = curr_env();
236         if (s == "document") {
237                 os << "Standard\n\n";
238                 return;
239         }
240         if (s == "lyxcode") {
241                 os << "LyX-Code\n\n";
242                 return;
243         }
244         os << cap(s) << "\n\n";
245 }
246
247
248 void handle_package(string const & name, string const & options)
249 {
250         if (name == "a4wide") {
251                 h_papersize = "a4paper";
252                 h_paperpackage = "widemarginsa4";
253         } else if (name == "ae") 
254                 h_fontscheme = "ae";
255         else if (name == "aecompl") 
256                 h_fontscheme = "ae";
257         else if (name == "amsmath") 
258                 h_use_amsmath = "1";
259         else if (name == "amssymb") 
260                 h_use_amsmath = "1";
261         else if (name == "babel") 
262                 ; // ignore this
263         else if (name == "fontenc") 
264                 ; // ignore this
265         else if (name == "inputenc") 
266                 h_inputencoding = options;
267         else if (name == "makeidx") 
268                 ; // ignore this
269         else if (name == "verbatim") 
270                 ; // ignore this
271         else {
272                 if (options.size())
273                         h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
274                 else
275                         h_preamble << "\\usepackage{" << name << "}\n";
276         }
277 }
278
279
280 void handle_table(Parser &, ostream &)
281 {
282         // \begin{table} has been read
283         //parse(end
284 }
285
286
287 string wrap(string const & cmd, string const & str)
288 {
289         return OPEN + cmd + ' ' + str + CLOSE;
290 }
291
292
293 void end_preamble(ostream & os)
294 {
295         in_preamble = false;
296         os << "# tex2lyx 0.0.2 created this file\n"
297            << "\\lyxformat 222\n"
298            << "\\textclass " << h_textclass << "\n"
299            << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n"
300            << "\\options " << h_options << "\n"
301            << "\\language " << h_language << "\n"
302            << "\\inputencoding " << h_inputencoding << "\n"
303            << "\\fontscheme " << h_fontscheme << "\n"
304            << "\\graphics " << h_graphics << "\n"
305            << "\\paperfontsize " << h_paperfontsize << "\n"
306            << "\\spacing " << h_spacing << "\n"
307            << "\\papersize " << h_papersize << "\n"
308            << "\\paperpackage " << h_paperpackage << "\n"
309            << "\\use_geometry " << h_use_geometry << "\n"
310            << "\\use_amsmath " << h_use_amsmath << "\n"
311            << "\\use_natbib " << h_use_natbib << "\n"
312            << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
313            << "\\paperorientation " << h_paperorientation << "\n"
314            << "\\secnumdepth " << h_secnumdepth << "\n"
315            << "\\tocdepth " << h_tocdepth << "\n"
316            << "\\paragraph_separation " << h_paragraph_separation << "\n"
317            << "\\defskip " << h_defskip << "\n"
318            << "\\quotes_language " << h_quotes_language << "\n"
319            << "\\quotes_times " << h_quotes_times << "\n"
320            << "\\papercolumns " << h_papercolumns << "\n"
321            << "\\papersides " << h_papersides << "\n"
322            << "\\paperpagestyle " << h_paperpagestyle << "\n"
323            << "\\tracking_changes " << h_tracking_changes << "\n";
324 }
325
326
327 void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
328 {
329         while (p.good()) {
330                 Token const & t = p.getToken();
331
332 #ifdef FILEDEBUG
333                 cerr << "t: " << t << " flags: " << flags << "\n";
334                 //cell->dump();
335 #endif
336
337                 if (flags & FLAG_ITEM) {
338                         if (t.cat() == catSpace)
339                                 continue;
340
341                         flags &= ~FLAG_ITEM;
342                         if (t.cat() == catBegin) {
343                                 // skip the brace and collect everything to the next matching
344                                 // closing brace
345                                 flags |= FLAG_BRACE_LAST;
346                                 continue;
347                         }
348
349                         // handle only this single token, leave the loop if done
350                         flags |= FLAG_LEAVE;
351                 }
352
353
354                 if (flags & FLAG_BRACED) {
355                         if (t.cat() == catSpace)
356                                 continue;
357
358                         if (t.cat() != catBegin) {
359                                 p.error("opening brace expected");
360                                 return;
361                         }
362
363                         // skip the brace and collect everything to the next matching
364                         // closing brace
365                         flags = FLAG_BRACE_LAST;
366                 }
367
368
369                 if (flags & FLAG_OPTION) {
370                         if (t.cat() == catOther && t.character() == '[') {
371                                 parse(p, os, FLAG_BRACK_LAST, mode);
372                         } else {
373                                 // no option found, put back token and we are done
374                                 p.putback();
375                         }
376                         return;
377                 }
378
379                 //
380                 // cat codes
381                 //
382                 if (t.cat() == catMath) {
383                         if (mode == TEXT_MODE || mode == MATHTEXT_MODE) {
384                                 // we are inside some text mode thingy, so opening new math is allowed
385                                 if (mode == TEXT_MODE)
386                                         begin_inset(os, "Formula ");
387                                 Token const & n = p.getToken();
388                                 if (n.cat() == catMath) {
389                                         // TeX's $$...$$ syntax for displayed math
390                                         os << "\\[";
391                                         parse(p, os, FLAG_SIMPLE, MATH_MODE);
392                                         os << "\\]";
393                                         p.getToken(); // skip the second '$' token
394                                 } else {
395                                         // simple $...$  stuff
396                                         p.putback();
397                                         os << '$';
398                                         parse(p, os, FLAG_SIMPLE, MATH_MODE);
399                                         os << '$';
400                                 }
401                                 if (mode == TEXT_MODE)
402                                         end_inset(os);
403                         }
404
405                         else if (flags & FLAG_SIMPLE) {
406                                 // this is the end of the formula
407                                 return;
408                         }
409
410                         else {
411                                 cerr << "mode: " << mode << endl;
412                                 p.error("something strange in the parser\n");
413                                 break;
414                         }
415                 }
416
417                 else if (t.cat() == catLetter)
418                         os << t.character();
419
420                 else if (t.cat() == catSpace) 
421                         os << t.character();
422
423                 else if (t.cat() == catNewline)
424                         os << ' ';
425
426                 else if (t.cat() == catSuper)
427                         os << t.character();
428
429                 else if (t.cat() == catSub)
430                         os << t.character();
431
432                 else if (t.cat() == catParameter) {
433                         Token const & n = p.getToken();
434                         os << wrap("macroarg", string(1, n.character()));
435                 }
436
437                 else if (t.cat() == catActive) {
438                         if (t.character() == '~')
439                                 os << (curr_env() == "lyxcode" ? ' ' : '~');
440                         else
441                                 os << t.asInput();
442                 }
443
444                 else if (t.cat() == catBegin) {
445                         os << '{';
446                         parse(p, os, FLAG_BRACE_LAST, mode);    
447                         os << '}';
448                 }
449
450                 else if (t.cat() == catEnd) {
451                         if (flags & FLAG_BRACE_LAST)
452                                 return;
453                         p.error("found '}' unexpectedly");
454                         //lyx::Assert(0);
455                         //add(cell, '}', LM_TC_TEX);
456                 }
457
458                 else if (t.cat() == catAlign) 
459                         os << t.character();
460
461 /*
462                         ++cellcol;
463                         //cerr << " column now " << cellcol << " max: " << grid.ncols() << "\n";
464                         if (cellcol == grid.ncols()) {
465                                 //cerr << "adding column " << cellcol << "\n";
466                                 grid.addCol(cellcol - 1);
467                         }
468                         cell = &grid.cell(grid.index(cellrow, cellcol));
469                 }
470 */
471
472                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
473                         //cerr << "finished reading option\n";
474                         return;
475                 }
476
477                 else if (t.cat() == catOther)
478                         os << string(1, t.character());
479
480                 else if (t.cat() == catComment) {
481                         string s;
482                         while (p.good()) {
483                                 Token const & t = p.getToken();
484                                 if (t.cat() == catNewline)
485                                         break;
486                                 s += t.asString();
487                         }
488                         //os << wrap("comment", s);
489                         p.skipSpaces();
490                 }
491
492                 //
493                 // control sequences
494                 //
495
496                 else if (t.cs() == "lyxlock") {
497                         // ignored;
498                 }
499
500                 else if (t.cs() == "newcommand" || t.cs() == "providecommand") {
501                         string const name = p.verbatimItem();
502                         string const opts = p.getArg('[', ']');
503                         string const body = p.verbatimItem();
504                         // only non-lyxspecific stuff
505                         if (name != "noun" && name != "tabularnewline") {
506                                 ostream & out = in_preamble ? h_preamble : os;
507                                 if (!in_preamble)
508                                         begin_inset(os, "FormulaMacro\n");
509                                 out << "\\" << t.cs() << "{" << name << "}";
510                                 if (opts.size()) 
511                                         out << "[" << opts << "]";
512                                 out << "{" << body << "}";
513                                 if (!in_preamble)
514                                         end_inset(os);
515                         }
516                 }
517
518                 else if (t.cs() == "(") {
519                         begin_inset(os, "Formula");
520                         os << " \\(";
521                         parse(p, os, FLAG_SIMPLE2, MATH_MODE);
522                         os << "\\)";
523                         end_inset(os);
524                 }
525
526                 else if (t.cs() == "[") {
527                         begin_inset(os, "Formula");
528                         os << " \\[";
529                         parse(p, os, FLAG_EQUATION, MATH_MODE);
530                         os << "\\]";
531                         end_inset(os);
532                 }
533
534                 else if (t.cs() == "protect")
535                         // ignore \\protect, will hopefully be re-added during output
536                         ;
537
538                 else if (t.cs() == "begin") {
539                         string const name = p.getArg('{', '}');
540                         active_environments.push(name);
541                         if (name == "document") {
542                                 end_preamble(os);
543                                 parse(p, os, FLAG_END, mode);
544                         } else if (name == "abstract") {
545                                 handle_par(os);
546                                 parse(p, os, FLAG_END, mode);
547                         } else if (is_math_env(name)) {
548                                 begin_inset(os, "Formula ");    
549                                 os << "\\begin{" << name << "}";
550                                 parse(p, os, FLAG_END, MATH_MODE);
551                         } else if (name == "table") {
552                                 handle_table(p, os);
553                                 parse(p, os, FLAG_END, mode);
554                         } else if (name == "thebibliography") {
555                                 p.verbatimItem(); // swallow next arg
556                                 handle_table(p, os);
557                                 parse(p, os, FLAG_END, mode);
558                         } else if (mode == MATH_MODE) {
559                                 os << "\\begin{" << name << "}";
560                                 parse(p, os, FLAG_END, mode);
561                         } else {
562                                 parse(p, os, FLAG_END, mode);
563                         }
564                 }
565
566                 else if (t.cs() == "end") {
567                         if (flags & FLAG_END) {
568                                 // eat environment name
569                                 string const name = p.getArg('{', '}');
570                                 if (name != curr_env())
571                                         p.error("\\end{" + name + "} does not match \\begin{"
572                                                 + curr_env() + "}");
573                                 active_environments.pop();
574                                 if (is_math_env(name)) {
575                                         end_inset(os);  
576                                         os << "\\end{" << name << "}";
577                                 } else if (mode == MATH_MODE) {
578                                         os << "\\end{" << name << "}";
579                                 } else
580                                         ;
581                                 return;
582                         }
583                         p.error("found 'end' unexpectedly");
584                 }
585
586                 else if (t.cs() == "item")
587                         handle_par(os);
588
589                 else if (t.cs() == ")") {
590                         if (flags & FLAG_SIMPLE2)
591                                 return;
592                         p.error("found '\\)' unexpectedly");
593                 }
594
595                 else if (t.cs() == "]") {
596                         if (flags & FLAG_EQUATION)
597                                 return;
598                         p.error("found '\\]' unexpectedly");
599                 }
600
601                 else if (t.cs() == "documentclass") {
602                         vector<string> opts;
603                         split(p.getArg('[', ']'), opts, ',');
604                         handle_opt(opts, known_languages, h_language); 
605                         handle_opt(opts, known_fontsizes, h_paperfontsize); 
606                         h_quotes_language = h_language;
607                         h_options = join(opts, ',');
608                         h_textclass = p.getArg('{', '}');
609                 }
610
611                 else if (t.cs() == "usepackage") {
612                         string const options = p.getArg('[', ']');
613                         string const name = p.getArg('{', '}');
614                         if (options.empty() && name.find(',')) {
615                                 vector<string> vecnames;
616                                 split(name, vecnames, ',');
617                                 vector<string>::const_iterator it  = vecnames.begin();
618                                 vector<string>::const_iterator end = vecnames.end();
619                                 for (; it != end; ++it)
620                                         handle_package(trim(*it), string());
621                         } else {
622                                 handle_package(name, options);
623                         }
624                 }
625
626                 else if (t.cs() == "newenvironment") {
627                         string const name = p.getArg('{', '}');
628                         p.skipSpaces();
629                         string const begin = p.verbatimItem();
630                         p.skipSpaces();
631                         string const end = p.verbatimItem();
632                         // ignore out mess
633                         if (name != "lyxcode") 
634                                 os << wrap("newenvironment", begin + end); 
635                 }
636
637                 else if (t.cs() == "def") {
638                         string name = p.getToken().cs();
639                         while (p.nextToken().cat() != catBegin)
640                                 name += p.getToken().asString();
641                         handle_ert(os, "\\def\\" + name + '{' + p.verbatimItem() + '}');
642                 }
643
644                 else if (t.cs() == "setcounter") {
645                         string const name = p.getArg('{', '}');
646                         string const content = p.getArg('{', '}');
647                         if (name == "secnumdepth") 
648                                 h_secnumdepth = content;
649                         else if (name == "tocdepth") 
650                                 h_tocdepth = content;
651                         else
652                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
653                 }
654
655                 else if (t.cs() == "setlength") {
656                         string const name = p.getToken().cs();
657                         string const content = p.getArg('{', '}');
658                         if (name == "parskip")
659                                 h_paragraph_separation = "skip";
660                         else if (name == "parindent")
661                                 h_paragraph_separation = "skip";
662                         else
663                                 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
664                 }
665         
666                 else if (t.cs() == "par")
667                         handle_par(os);
668
669                 else if (is_heading(t.cs())) {
670                         os << "\\layout " << cap(t.cs()) << "\n\n";
671                         parse(p, os, FLAG_ITEM, mode);
672                 }
673
674                 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
675                         ; // swallow this
676
677                 else if (t.cs() == "tableofcontents")
678                         p.verbatimItem(); // swallow this
679
680                 else if (t.cs() == "textrm") {
681                         os << '\\' << t.cs() << '{';
682                         parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
683                         os << '}';
684                 }
685
686                 else if ((t.cs() == "emph" || t.cs() == "noun") && mode == TEXT_MODE) {
687                         os << "\n\\" << t.cs() << " on\n";
688                         parse(p, os, FLAG_ITEM, mode);
689                         os << "\n\\" << t.cs() << " default\n";
690                 }
691
692                 else if (is_latex_command(t.cs()) && mode == TEXT_MODE) {
693                         begin_inset(os, "LatexCommand ");
694                         os << '\\' << t.cs() << '{';
695                         parse(p, os, FLAG_ITEM, TEXT_MODE);
696                         os << '}';
697                         end_inset(os);
698                 }
699
700                 else if (t.cs() == "bibitem") {
701                         os << "\n\\layout Bibliography\n\\bibitem ";
702                         string opt = p.getArg('[',']');
703                         if (opt.size())
704                                 os << '[' << opt << ']';
705                         os << '{' << p.getArg('{','}') << '}' << "\n\n";
706                 }
707
708                 else if (t.cs() == "textasciitilde")
709                         os << '~';
710
711                 else if (t.cs() == "_" && mode == TEXT_MODE)
712                         os << '_';
713
714                 else if (t.cs() == "&" && mode == TEXT_MODE)
715                         os << '&';
716
717                 else {
718                         if (mode == MATH_MODE)
719                                 os << t.asInput();
720                         else if (in_preamble)
721                                 h_preamble << t.asInput();
722                         else {
723                                 // heuristic: read up to next non-nested space
724                                 string s = t.asInput();
725                                 string z = p.verbatimItem();
726                                 while (p.good() && z != " " && z.size()) {
727                                         //cerr << "read: " << z << endl;
728                                         s += z;
729                                         z = p.verbatimItem();
730                                 }
731                                 cerr << "found ERT: " << s << endl;
732                                 handle_ert(os, s + ' ');
733                         }
734                 }
735
736                 if (flags & FLAG_LEAVE) {
737                         flags &= ~FLAG_LEAVE;
738                         break;
739                 }
740         }
741 }
742
743
744
745 } // anonymous namespace
746
747
748 int main(int argc, char * argv[])
749 {
750         if (argc <= 1) {
751                 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
752                 return 2;
753         }
754
755         ifstream is(argv[1]);
756         Parser p(is);
757         parse(p, cout, 0, TEXT_MODE);
758         cout << "\n\\the_end";
759
760         return 0;       
761 }       
762
763 // }])