]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
751 to go
[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 // some ugly stuff
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";
84
85 // indicates whether we are in the preamble
86 bool in_preamble = true;
87
88 // current stack of nested environments
89 stack<string> active_environments;
90
91
92
93 string const trim(string const & a, char const * p = " ")
94 {
95         // lyx::Assert(p);
96
97         if (a.empty() || !*p)
98                 return a;
99
100         string::size_type r = a.find_last_not_of(p);
101         string::size_type l = a.find_first_not_of(p);
102
103         // Is this the minimal test? (lgb)
104         if (r == string::npos && l == string::npos)
105                 return string();
106
107         return a.substr(l, r - l + 1);
108 }
109
110
111 void split(string const & s, vector<string> & result, char delim)
112 {
113         istringstream is(s);    
114         string t;
115         while (getline(is, t, delim))
116                 result.push_back(t);
117 }
118
119
120 string join(vector<string> const & input, char delim)
121 {
122         ostringstream os;
123         for (size_t i = 0; i != input.size(); ++i) {
124                 if (i)
125                         os << delim;    
126                 os << input[i]; 
127         }
128         return os.str();
129 }
130
131
132 void handle_opt(vector<string> & opts, char const ** what, string & target)
133 {
134         if (opts.empty())
135                 return;
136
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";
141                         target = *what;
142                         opts.erase(it);
143                         return;
144                 }
145         }
146 }
147
148
149 void begin_inset(ostream & os, string const & name)
150 {
151         os << "\n\\begin_inset " << name;
152 }
153
154
155 void end_inset(ostream & os)
156 {
157         os << "\n\\end_inset\n";
158 }
159
160
161 void handle_ert(ostream & os, string const & s)
162 {
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) {
166                 if (*it == '\\')
167                         os << "\n\\backslash\n";
168                 else
169                         os << *it;
170         }
171         end_inset(os);
172 }
173
174
175 void handle_par(ostream & os)
176 {
177         if (active_environments.empty())
178                 return;
179         os << "\n\\layout ";
180         string s = active_environments.top();
181         if (s == "document") {
182                 os << "Standard\n\n";
183                 return;
184         }
185         if (s == "lyxcode") {
186                 os << "LyX-Code\n\n";
187                 return;
188         }
189         if (s.size()) 
190                 s[0] = toupper(s[0]);   
191         os << s << "\n\n";
192 }
193
194
195 void handle_package(string const & name, string const & options)
196 {
197         if (name == "a4wide") {
198                 h_papersize = "a4paper";
199                 h_paperpackage = "widemarginsa4";
200         } else if (name == "ae") 
201                 h_fontscheme = "ae";
202         else if (name == "aecompl") 
203                 h_fontscheme = "ae";
204         else if (name == "amsmath") 
205                 h_use_amsmath = "1";
206         else if (name == "amssymb") 
207                 h_use_amsmath = "1";
208         else if (name == "babel") 
209                 ; // ignore this
210         else if (name == "fontenc") 
211                 ; // ignore this
212         else if (name == "inputenc") 
213                 h_inputencoding = options;
214         else if (name == "makeidx") 
215                 ; // ignore this
216         else if (name == "verbatim") 
217                 ; // ignore this
218         else {
219                 if (options.size())
220                         h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
221                 else
222                         h_preamble << "\\usepackage{" << name << "}\n";
223         }
224 }
225
226
227 string wrap(string const & cmd, string const & str)
228 {
229         return OPEN + cmd + ' ' + str + CLOSE;
230 }
231
232
233 void end_preamble(ostream & os)
234 {
235         in_preamble = false;
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";
265 }
266
267
268 void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
269 {
270         while (p.good()) {
271                 Token const & t = p.getToken();
272
273 #ifdef FILEDEBUG
274                 cerr << "t: " << t << " flags: " << flags << "\n";
275                 //cell->dump();
276 #endif
277
278                 if (flags & FLAG_ITEM) {
279                         if (t.cat() == catSpace)
280                                 continue;
281
282                         flags &= ~FLAG_ITEM;
283                         if (t.cat() == catBegin) {
284                                 // skip the brace and collect everything to the next matching
285                                 // closing brace
286                                 flags |= FLAG_BRACE_LAST;
287                                 continue;
288                         }
289
290                         // handle only this single token, leave the loop if done
291                         flags |= FLAG_LEAVE;
292                 }
293
294
295                 if (flags & FLAG_BRACED) {
296                         if (t.cat() == catSpace)
297                                 continue;
298
299                         if (t.cat() != catBegin) {
300                                 p.error("opening brace expected");
301                                 return;
302                         }
303
304                         // skip the brace and collect everything to the next matching
305                         // closing brace
306                         flags = FLAG_BRACE_LAST;
307                 }
308
309
310                 if (flags & FLAG_OPTION) {
311                         if (t.cat() == catOther && t.character() == '[') {
312                                 parse(p, os, FLAG_BRACK_LAST, mode);
313                         } else {
314                                 // no option found, put back token and we are done
315                                 p.putback();
316                         }
317                         return;
318                 }
319
320                 //
321                 // cat codes
322                 //
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
331                                         os << "\\[";
332                                         parse(p, os, FLAG_SIMPLE, MATH_MODE);
333                                         os << "\\]";
334                                         p.getToken(); // skip the second '$' token
335                                 } else {
336                                         // simple $...$  stuff
337                                         p.putback();
338                                         os << '$';
339                                         parse(p, os, FLAG_SIMPLE, MATH_MODE);
340                                         os << '$';
341                                 }
342                                 if (mode == TEXT_MODE)
343                                         end_inset(os);
344                         }
345
346                         else if (flags & FLAG_SIMPLE) {
347                                 // this is the end of the formula
348                                 return;
349                         }
350
351                         else {
352                                 cerr << "mode: " << mode << endl;
353                                 p.error("something strange in the parser\n");
354                                 break;
355                         }
356                 }
357
358                 else if (t.cat() == catLetter)
359                         os << t.character();
360
361                 else if (t.cat() == catSpace) 
362                         os << t.character();
363
364                 else if (t.cat() == catNewline)
365                         os << t.character();
366
367                 else if (t.cat() == catParameter) {
368                         Token const & n = p.getToken();
369                         os << wrap("macroarg", string(1, n.character()));
370                 }
371
372                 else if (t.cat() == catActive)
373                         os << wrap("active", string(1, t.character()));
374
375                 else if (t.cat() == catBegin) {
376                         os << '{';
377                         parse(p, os, FLAG_BRACE_LAST, mode);    
378                         os << '}';
379                 }
380
381                 else if (t.cat() == catEnd) {
382                         if (flags & FLAG_BRACE_LAST)
383                                 return;
384                         p.error("found '}' unexpectedly");
385                         //lyx::Assert(0);
386                         //add(cell, '}', LM_TC_TEX);
387                 }
388
389                 else if (t.cat() == catAlign) 
390                         os << t.character();
391
392 /*
393                         ++cellcol;
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);
398                         }
399                         cell = &grid.cell(grid.index(cellrow, cellcol));
400                 }
401 */
402
403                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
404                         //cerr << "finished reading option\n";
405                         return;
406                 }
407
408                 else if (t.cat() == catOther)
409                         os << string(1, t.character());
410
411                 else if (t.cat() == catComment) {
412                         string s;
413                         while (p.good()) {
414                                 Token const & t = p.getToken();
415                                 if (t.cat() == catNewline)
416                                         break;
417                                 s += t.asString();
418                         }
419                         //os << wrap("comment", s);
420                         p.skipSpaces();
421                 }
422
423                 //
424                 // control sequences
425                 //
426
427                 else if (t.cs() == "lyxlock") {
428                         // ignored;
429                 }
430
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 << "}";
438                                 if (opts.size()) 
439                                         h_preamble << "[" << opts << "]";
440                                 h_preamble << "{" << body << "}\n";
441                         }
442                 }
443
444                 else if (t.cs() == "(") {
445                         begin_inset(os, "Formula");
446                         os << " \\(";
447                         parse(p, os, FLAG_SIMPLE2, MATH_MODE);
448                         os << "\\)";
449                         end_inset(os);
450                 }
451
452                 else if (t.cs() == "[") {
453                         begin_inset(os, "Formula");
454                         os << " \\[";
455                         parse(p, os, FLAG_EQUATION, MATH_MODE);
456                         os << "\\]";
457                         end_inset(os);
458                 }
459
460                 else if (t.cs() == "protect")
461                         // ignore \\protect, will hopefully be re-added during output
462                         ;
463
464                 else if (t.cs() == "begin") {
465                         string const name = p.getArg('{', '}');
466                         active_environments.push(name);
467                         if (name == "document") 
468                                 end_preamble(os);
469                         else if (name == "abstract")
470                                 handle_par(os);
471                         else
472                                 os << "\\begin{" << name << "}";
473                         parse(p, os, FLAG_END, mode);
474                 }
475
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")
485                                         ;
486                                 else
487                                         os << "\\end{" << name << "}";
488                                 return;
489                         }
490                         p.error("found 'end' unexpectedly");
491                 }
492
493                 else if (t.cs() == ")") {
494                         if (flags & FLAG_SIMPLE2)
495                                 return;
496                         p.error("found '\\)' unexpectedly");
497                 }
498
499                 else if (t.cs() == "]") {
500                         if (flags & FLAG_EQUATION)
501                                 return;
502                         p.error("found '\\]' unexpectedly");
503                 }
504
505                 else if (t.cs() == "documentclass") {
506                         vector<string> opts;
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('{', '}');
513                 }
514
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());
525                         } else {
526                                 handle_package(name, options);
527                         }
528                 }
529
530                 else if (t.cs() == "newenvironment") {
531                         string const name = p.getArg('{', '}');
532                         p.skipSpaces();
533                         string const begin = p.verbatimItem();
534                         p.skipSpaces();
535                         string const end = p.verbatimItem();
536                         // ignore out mess
537                         if (name != "lyxcode") 
538                                 os << wrap("newenvironment", begin + end); 
539                 }
540
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() + '}');
546                 }
547
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;
555                         else
556                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
557                 }
558
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";
566                         else
567                                 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
568                 }
569         
570                 else if (t.cs() == "par")
571                         handle_par(os);
572
573                 else if (t.cs() == "title")
574                         os << "\\layout Title\n\n" + p.verbatimItem();
575
576                 else if (t.cs() == "author")
577                         os << "\\layout Author\n\n" + p.verbatimItem();
578
579                 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
580                         ; // swallow this
581
582                 else if (t.cs() == "tableofcontents")
583                         p.verbatimItem(); // swallow this
584
585                 else if (t.cs() == "textrm") {
586                         os << '\\' << t.cs() << '{';
587                         parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
588                         os << '}';
589                 }
590
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";
595                 }
596
597                 else if (t.cs() == "index") {
598                         begin_inset(os, "LatexCommand");
599                         parse(p, os, FLAG_ITEM, TEXT_MODE);
600                         end_inset(os);
601                 }
602
603                 else 
604                         (in_preamble ? h_preamble : os) << '\\' << t.asString();
605
606                 if (flags & FLAG_LEAVE) {
607                         flags &= ~FLAG_LEAVE;
608                         break;
609                 }
610         }
611 }
612
613
614
615 } // anonymous namespace
616
617
618 int main(int argc, char * argv[])
619 {
620         if (argc <= 1) {
621                 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
622                 return 2;
623         }
624
625         ifstream is(argv[1]);
626         Parser p(is);
627         parse(p, cout, 0, TEXT_MODE);
628         cout << "\n\\the_end";
629
630         return 0;       
631 }       
632
633 // }])