]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
small parser fixes
[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 <map>
14 #include <stack>
15 #include <string>
16 #include <vector>
17
18 #include "Lsstream.h"
19
20 #include "texparser.h"
21
22 using std::count_if;
23 using std::cout;
24 using std::cerr;
25 using std::endl;
26 using std::fill;
27 using std::getline;
28 using std::ios;
29 using std::ifstream;
30 using std::istream;
31 using std::istringstream;
32 using std::map;
33 using std::ostream;
34 using std::ostringstream;
35 using std::stack;
36 using std::string;
37 using std::vector;
38
39
40 namespace {
41
42 void parse(Parser & p, ostream & os, unsigned flags, const mode_type mode);
43
44 char const OPEN = '<';
45 char const CLOSE = '>';
46
47 // rather brutish way to code table structure in a string:
48 //
49 //  \begin{tabular}{ccc}
50 //    1 & 2 & 3\\ 
51 //    \multicolumn{2}{c}{4} & 5\\ 
52 //    6 & 7 \\ 
53 //  \end{tabular}
54 //
55 // gets "translated" to:
56 //  
57 //  1 TAB 2 TAB 3 LINE
58 //  2 MULT c MULT 4 TAB 5 LINE
59 //  5 TAB 7 LINE
60
61 char const TAB  = '\001';
62 char const LINE = '\002';
63 char const MULT = '\003';
64
65 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
66 "breton", "british", "bulgarian", "catalan", "croatian", "czech", "danish",
67 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
68 "frenchb", "galician", "german", "germanb", "greek", "hebcal", "hebfont",
69 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
70 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
71 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
72 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
73 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
74 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
75 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
76 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
77
78 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
79
80 char const * known_headings[] = { "caption", "title", "author", "date",
81 "paragraph", "chapter", "section", "subsection", "subsubsection", 0 };
82
83 char const * known_math_envs[] = { "equation", "equation*",
84 "eqnarray", "eqnarray*", "align", "align*", 0};
85
86 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
87 "printindex", 0 };
88
89 // LaTeX names for quotes
90 char const * known_quotes[] = { "glqq", "grqq", 0};
91
92 // the same as known_quotes with .lyx names
93 char const * known_coded_quotes[] = { "gld", "grd", 0};
94
95
96
97 // some ugly stuff
98 ostringstream h_preamble;
99 string h_textclass               = "article";
100 string h_options                 = "";
101 string h_language                = "english";
102 string h_inputencoding           = "latin1";
103 string h_fontscheme              = "default";
104 string h_graphics                = "default";
105 string h_paperfontsize           = "default";
106 string h_spacing                 = "single";
107 string h_papersize               = "default";
108 string h_paperpackage            = "default";
109 string h_use_geometry            = "0";
110 string h_use_amsmath             = "0";
111 string h_use_natbib              = "0";
112 string h_use_numerical_citations = "0";
113 string h_paperorientation        = "portrait";
114 string h_secnumdepth             = "3";
115 string h_tocdepth                = "3";
116 string h_paragraph_separation    = "indent";
117 string h_defskip                 = "medskip";
118 string h_quotes_language         = "english";
119 string h_quotes_times            = "1";
120 string h_papercolumns            = "1";
121 string h_papersides              = "1";
122 string h_paperpagestyle          = "default";
123 string h_tracking_changes        = "0";
124
125 // indicates whether we are in the preamble
126 bool in_preamble = true;
127
128 // current stack of nested environments
129 stack<string> active_environments;
130
131
132 string cap(string s)
133 {
134         if (s.size())
135                 s[0] = toupper(s[0]);
136         return s;
137 }
138
139
140 string const trim(string const & a, char const * p = " \t\n\r")
141 {
142         // lyx::Assert(p);
143
144         if (a.empty() || !*p)
145                 return a;
146
147         string::size_type r = a.find_last_not_of(p);
148         string::size_type l = a.find_first_not_of(p);
149
150         // Is this the minimal test? (lgb)
151         if (r == string::npos && l == string::npos)
152                 return string();
153
154         return a.substr(l, r - l + 1);
155 }
156
157
158 void split(string const & s, vector<string> & result, char delim = ',')
159 {
160         istringstream is(s);    
161         string t;
162         while (getline(is, t, delim))
163                 result.push_back(t);
164 }
165
166
167 // splits "x=z, y=b" into a map
168 map<string, string> split_map(string const & s)
169 {
170         map<string, string> res;
171         vector<string> v;
172         split(s, v);
173         for (size_t i = 0; i < v.size(); ++i) {
174                 size_t const pos   = v[i].find('=');
175                 string const index = v[i].substr(0, pos);
176                 string const value = v[i].substr(pos + 1, string::npos);        
177                 res[trim(index)] = trim(value);
178         }
179         return res;
180 }
181
182
183 string join(vector<string> const & input, char delim)
184 {
185         ostringstream os;
186         for (size_t i = 0; i != input.size(); ++i) {
187                 if (i)
188                         os << delim;    
189                 os << input[i]; 
190         }
191         return os.str();
192 }
193
194
195 char const ** is_known(string const & str, char const ** what)
196 {
197         for ( ; *what; ++what)
198                 if (str == *what)
199                         return what;
200         return 0;
201 }
202
203
204 void handle_opt(vector<string> & opts, char const ** what, string & target)
205 {
206         if (opts.empty())
207                 return;
208
209         for ( ; *what; ++what) {
210                 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
211                 if (it != opts.end()) {
212                         //cerr << "### found option '" << *what << "'\n";
213                         target = *what;
214                         opts.erase(it);
215                         return;
216                 }
217         }
218 }
219
220
221 bool is_math_env(string const & name)
222 {
223         for (char const ** what = known_math_envs; *what; ++what)
224                 if (*what == name)
225                         return true;
226         return false;
227 }
228
229
230 void begin_inset(ostream & os, string const & name)
231 {
232         os << "\n\\begin_inset " << name;
233 }
234
235
236 void end_inset(ostream & os)
237 {
238         os << "\n\\end_inset\n\n";
239 }
240
241
242 string curr_env()
243 {
244         return active_environments.empty() ? string() : active_environments.top();
245 }
246
247
248 void handle_ert(ostream & os, string const & s)
249 {
250         begin_inset(os, "ERT");
251         os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
252         for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
253                 if (*it == '\\')
254                         os << "\n\\backslash\n";
255                 else
256                         os << *it;
257         }
258         end_inset(os);
259 }
260
261
262 void handle_tex(ostream & os, string const & s)
263 {
264         //os << "handle_tex(" << s << ")\n";
265         if (in_preamble)
266                 h_preamble << s;
267         else
268                 handle_ert(os, s);
269 }
270                 
271
272 void handle_par(ostream & os)
273 {
274         if (active_environments.empty())
275                 return;
276         os << "\n\\layout ";
277         string s = curr_env();
278         if (s == "document" || s == "table") {
279                 os << "Standard\n\n";
280                 return;
281         }
282         if (s == "lyxcode") {
283                 os << "LyX-Code\n\n";
284                 return;
285         }
286         os << cap(s) << "\n\n";
287 }
288
289
290 void handle_package(string const & name, string const & options)
291 {
292         //cerr << "handle_package: '" << name << "'\n";
293         if (name == "a4wide") {
294                 h_papersize = "a4paper";
295                 h_paperpackage = "widemarginsa4";
296         } else if (name == "ae") 
297                 h_fontscheme = "ae";
298         else if (name == "aecompl") 
299                 h_fontscheme = "ae";
300         else if (name == "amsmath") 
301                 h_use_amsmath = "1";
302         else if (name == "amssymb") 
303                 h_use_amsmath = "1";
304         else if (name == "babel") 
305                 ; // ignore this
306         else if (name == "fontenc") 
307                 ; // ignore this
308         else if (name == "inputenc") 
309                 h_inputencoding = options;
310         else if (name == "makeidx") 
311                 ; // ignore this
312         else if (name == "verbatim") 
313                 ; // ignore this
314         else if (is_known(name, known_languages)) {
315                 h_language = name;
316                 h_quotes_language = name;
317         } else {
318                 if (options.size())
319                         h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
320                 else
321                         h_preamble << "\\usepackage{" << name << "}\n";
322         }
323 }
324
325
326 vector<string> extract_col_align(string const & s)
327 {
328         vector<string> res;
329         for (size_t i = 0; i < s.size(); ++i) {
330                 switch (s[i]) {
331                         case 'c': res.push_back("center"); break;
332                         case 'l': res.push_back("left");   break;
333                         case 'r': res.push_back("right");  break;
334                         default : res.push_back("right");  break;
335                 }
336         }
337         return res;
338 }
339
340
341 void handle_tabular(Parser & p, ostream & os, mode_type mode)
342 {
343         begin_inset(os, "Tabular \n");
344         string colopts = p.verbatimItem();
345         vector<string> colalign = extract_col_align(colopts);
346         ostringstream ss;
347         parse(p, ss, FLAG_END, mode);
348         vector<string> lines;
349         split(ss.str(), lines, LINE);
350         const size_t cols = colalign.size();
351         const size_t rows = lines.size();
352         os << "<lyxtabular version=\"3\" rows=\"" << rows
353                  << "\" columns=\"" << cols << "\">\n"
354                  << "<features>\n";
355         for (size_t c = 0; c < cols; ++c)
356                 os << "<column alignment=\"" << colalign[c] << "\""
357                          << " valignment=\"top\""
358                          << " width=\"0pt\""
359                          << ">\n";
360         for (size_t r = 0; r < rows; ++r) {
361                 vector<string> cells;
362                 split(lines[r], cells, TAB);
363                 while (cells.size() < cols)
364                         cells.push_back(string());
365                 //os << "<row bottomline=\"true\">\n";
366                 os << "<row>\n";
367                 for (size_t c = 0; c < cols; ++c) {
368                         os << "<cell";
369                         string alignment = "center";
370                         vector<string> parts;
371                         split(cells[c], parts, MULT);
372                         if (parts.size() > 2) {
373                                 os << " multicolumn=\"" << parts[0] << "\"";
374                                 alignment = parts[1];
375                         }
376                         os << " alignment=\"" << alignment << "\""
377                                  << " valignment=\"top\""
378                                  << " topline=\"true\""
379                                  << " leftline=\"true\""
380                                  << " usebox=\"none\""
381                                  << ">";
382                         begin_inset(os, "Text");
383                         os << "\n\n\\layout Standard\n\n";
384                         if (parts.size())
385                                 os << parts.back();
386                         end_inset(os);
387                         os << "</cell>\n";
388                 }
389                 os << "</row>\n";
390         }
391         os << "</lyxtabular>\n";
392         end_inset(os);  
393 }
394
395
396 string wrap(string const & cmd, string const & str)
397 {
398         return OPEN + cmd + ' ' + str + CLOSE;
399 }
400
401
402 void end_preamble(ostream & os)
403 {
404         in_preamble = false;
405         os << "# tex2lyx 0.0.2 created this file\n"
406            << "\\lyxformat 222\n"
407            << "\\textclass " << h_textclass << "\n"
408            << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n"
409            << "\\options " << h_options << "\n"
410            << "\\language " << h_language << "\n"
411            << "\\inputencoding " << h_inputencoding << "\n"
412            << "\\fontscheme " << h_fontscheme << "\n"
413            << "\\graphics " << h_graphics << "\n"
414            << "\\paperfontsize " << h_paperfontsize << "\n"
415            << "\\spacing " << h_spacing << "\n"
416            << "\\papersize " << h_papersize << "\n"
417            << "\\paperpackage " << h_paperpackage << "\n"
418            << "\\use_geometry " << h_use_geometry << "\n"
419            << "\\use_amsmath " << h_use_amsmath << "\n"
420            << "\\use_natbib " << h_use_natbib << "\n"
421            << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
422            << "\\paperorientation " << h_paperorientation << "\n"
423            << "\\secnumdepth " << h_secnumdepth << "\n"
424            << "\\tocdepth " << h_tocdepth << "\n"
425            << "\\paragraph_separation " << h_paragraph_separation << "\n"
426            << "\\defskip " << h_defskip << "\n"
427            << "\\quotes_language " << h_quotes_language << "\n"
428            << "\\quotes_times " << h_quotes_times << "\n"
429            << "\\papercolumns " << h_papercolumns << "\n"
430            << "\\papersides " << h_papersides << "\n"
431            << "\\paperpagestyle " << h_paperpagestyle << "\n"
432            << "\\tracking_changes " << h_tracking_changes << "\n";
433 }
434
435
436 void parse(Parser & p, ostream & os, unsigned flags, const mode_type mode)
437 {
438         while (p.good()) {
439                 Token const & t = p.getToken();
440
441 #ifdef FILEDEBUG
442                 cerr << "t: " << t << " flags: " << flags << "\n";
443                 //cell->dump();
444 #endif
445
446                 if (flags & FLAG_ITEM) {
447                         if (t.cat() == catSpace)
448                                 continue;
449
450                         flags &= ~FLAG_ITEM;
451                         if (t.cat() == catBegin) {
452                                 // skip the brace and collect everything to the next matching
453                                 // closing brace
454                                 flags |= FLAG_BRACE_LAST;
455                                 continue;
456                         }
457
458                         // handle only this single token, leave the loop if done
459                         flags |= FLAG_LEAVE;
460                 }
461
462
463                 if (flags & FLAG_BRACED) {
464                         if (t.cat() == catSpace)
465                                 continue;
466
467                         if (t.cat() != catBegin) {
468                                 p.error("opening brace expected");
469                                 return;
470                         }
471
472                         // skip the brace and collect everything to the next matching
473                         // closing brace
474                         flags = FLAG_BRACE_LAST;
475                 }
476
477
478                 if (flags & FLAG_OPTION) {
479                         if (t.cat() == catOther && t.character() == '[') {
480                                 parse(p, os, FLAG_BRACK_LAST, mode);
481                         } else {
482                                 // no option found, put back token and we are done
483                                 p.putback();
484                         }
485                         return;
486                 }
487
488                 //
489                 // cat codes
490                 //
491                 if (t.cat() == catMath) {
492                         if (mode == TEXT_MODE || mode == MATHTEXT_MODE) {
493                                 // we are inside some text mode thingy, so opening new math is allowed
494                                 if (mode == TEXT_MODE)
495                                         begin_inset(os, "Formula ");
496                                 Token const & n = p.getToken();
497                                 if (n.cat() == catMath) {
498                                         // TeX's $$...$$ syntax for displayed math
499                                         os << "\\[";
500                                         parse(p, os, FLAG_SIMPLE, MATH_MODE);
501                                         os << "\\]";
502                                         p.getToken(); // skip the second '$' token
503                                 } else {
504                                         // simple $...$  stuff
505                                         p.putback();
506                                         os << '$';
507                                         parse(p, os, FLAG_SIMPLE, MATH_MODE);
508                                         os << '$';
509                                 }
510                                 if (mode == TEXT_MODE)
511                                         end_inset(os);
512                         }
513
514                         else if (flags & FLAG_SIMPLE) {
515                                 // this is the end of the formula
516                                 return;
517                         }
518
519                         else {
520                                 cerr << "mode: " << mode << endl;
521                                 p.error("something strange in the parser\n");
522                                 break;
523                         }
524                 }
525
526                 else if (t.cat() == catLetter)
527                         os << t.character();
528
529                 else if (t.cat() == catSpace) 
530                         os << t.character();
531
532                 else if (t.cat() == catNewline)
533                         os << ' ';
534
535                 else if (t.cat() == catSuper)
536                         os << t.character();
537
538                 else if (t.cat() == catSub)
539                         os << t.character();
540
541                 else if (t.cat() == catParameter) {
542                         Token const & n = p.getToken();
543                         os << wrap("macroarg", string(1, n.character()));
544                 }
545
546                 else if (t.cat() == catActive) {
547                         if (t.character() == '~')
548                                 os << (curr_env() == "lyxcode" ? ' ' : '~');
549                         else
550                                 os << t.asInput();
551                 }
552
553                 else if (t.cat() == catBegin) {
554                         if (mode == TEXT_MODE)
555                                 handle_tex(os, "{");
556                         else
557                                 os << '{';
558                 }
559
560                 else if (t.cat() == catEnd) {
561                         if (flags & FLAG_BRACE_LAST)
562                                 return;
563                         if (mode == TEXT_MODE)
564                                 handle_tex(os, "}");
565                         else
566                                 os << '}';
567                 }
568
569                 else if (t.cat() == catAlign) {
570                         if (mode == TEXT_MODE)
571                                 os << TAB;
572                         else
573                                 os << t.character();
574                 }
575
576                 else if (t.cs() == "tabularnewline") {
577                         if (mode == TEXT_MODE)
578                                 os << LINE;
579                         else
580                                 os << t.asInput();
581                 }
582
583                 else if (t.cs() == "\\" && mode == MATH_MODE)
584                         os << t.asInput();
585
586                 else if (t.cs() == "\\" && mode == TEXT_MODE && curr_env() == "tabular")
587                         os << LINE;
588
589                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
590                         //cerr << "finished reading option\n";
591                         return;
592                 }
593
594                 else if (t.cat() == catOther)
595                         os << string(1, t.character());
596
597                 else if (t.cat() == catComment) {
598                         string s;
599                         while (p.good()) {
600                                 Token const & t = p.getToken();
601                                 if (t.cat() == catNewline)
602                                         break;
603                                 s += t.asString();
604                         }
605                         //os << wrap("comment", s);
606                         p.skipSpaces();
607                 }
608
609                 //
610                 // control sequences
611                 //
612
613                 else if (t.cs() == "lyxlock")
614                         ; // ignored
615
616                 else if (t.cs() == "makeatletter") {
617                         p.setCatCode('@', catLetter);
618                         handle_tex(os, "\\makeatletter\n");
619                 }
620                         
621                 else if (t.cs() == "makeatother") {
622                         p.setCatCode('@', catOther);
623                         handle_tex(os, "\\makeatother\n");
624                 }
625
626                 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
627                             || t.cs() == "providecommand") {
628                         string const name = p.verbatimItem();
629                         string const opts = p.getOpt();
630                         string const body = p.verbatimItem();
631                         // only non-lyxspecific stuff
632                         if (name != "\\noun " && name != "\\tabularnewline ") {
633                                 ostringstream ss;
634                                 ss << '\\' << t.cs() << '{' << name << '}'
635                                         << opts << '{' << body << "}\n";
636                                 handle_tex(os, ss.str());
637 /*
638                                 ostream & out = in_preamble ? h_preamble : os;
639                                 if (!in_preamble)
640                                         begin_inset(os, "FormulaMacro\n");
641                                 out << "\\" << t.cs() << "{" << name << "}"
642                                     << opts << "{" << body << "}\n";
643                                 if (!in_preamble)
644                                         end_inset(os);
645 */
646                         }
647                 }
648
649                 else if (t.cs() == "newtheorem") {
650                         ostringstream ss;
651                         ss << "\\newtheorem";
652                         ss << '{' << p.verbatimItem() << '}';
653                         ss << p.getOpt();
654                         ss << '{' << p.verbatimItem() << '}';
655                         ss << p.getOpt();
656                         ss << '\n';
657                         handle_tex(os, ss.str());
658                 }
659
660                 else if (t.cs() == "(") {
661                         begin_inset(os, "Formula");
662                         os << " \\(";
663                         parse(p, os, FLAG_SIMPLE2, MATH_MODE);
664                         os << "\\)";
665                         end_inset(os);
666                 }
667
668                 else if (t.cs() == "[") {
669                         begin_inset(os, "Formula");
670                         os << " \\[";
671                         parse(p, os, FLAG_EQUATION, MATH_MODE);
672                         os << "\\]";
673                         end_inset(os);
674                 }
675
676                 else if (t.cs() == "protect")
677                         // ignore \\protect, will hopefully be re-added during output
678                         ;
679
680                 else if (t.cs() == "begin") {
681                         string const name = p.getArg('{', '}');
682                         active_environments.push(name);
683                         if (name == "document") {
684                                 end_preamble(os);
685                                 os << "\n\n\\layout Standard\n\n";
686                                 parse(p, os, FLAG_END, mode);
687                         } else if (name == "abstract") {
688                                 handle_par(os);
689                                 parse(p, os, FLAG_END, mode);
690                         } else if (is_math_env(name)) {
691                                 begin_inset(os, "Formula ");    
692                                 os << "\\begin{" << name << "}";
693                                 parse(p, os, FLAG_END, MATH_MODE);
694                                 os << "\\end{" << name << "}";
695                                 end_inset(os);  
696                         } else if (name == "tabular") {
697                                 if (mode == TEXT_MODE) 
698                                         handle_tabular(p, os, mode);
699                                 else {
700                                         os << "\\begin{" << name << "}";
701                                         parse(p, os, FLAG_END, MATHTEXT_MODE);
702                                         os << "\\end{" << name << "}";
703                                 }
704                         } else if (name == "table" || name == "figure") {
705                                 string opts = p.getOpt();
706                                 begin_inset(os, "Float " + name + "\n");
707                                 if (opts.size())
708                                         os << "placement " << opts << '\n';
709                                 os << "wide false\n"
710                                    << "collapsed false\n"
711                                    << "\n"
712                                    << "\\layout Standard\n";
713                                 parse(p, os, FLAG_END, mode);
714                                 end_inset(os);  
715                         } else if (name == "thebibliography") {
716                                 p.verbatimItem(); // swallow next arg
717                                 parse(p, os, FLAG_END, mode);
718                                 os << "\n\\layout Standard\n\n";
719                         } else if (mode == MATH_MODE || mode == MATHTEXT_MODE) {
720                                 os << "\\begin{" << name << "}";
721                                 parse(p, os, FLAG_END, mode);
722                                 os << "\\end{" << name << "}";
723                         } else {
724                                 parse(p, os, FLAG_END, mode);
725                         }
726                 }
727
728                 else if (t.cs() == "end") {
729                         if (flags & FLAG_END) {
730                                 // eat environment name
731                                 string const name = p.getArg('{', '}');
732                                 if (name != curr_env())
733                                         p.error("\\end{" + name + "} does not match \\begin{"
734                                                 + curr_env() + "}");
735                                 active_environments.pop();
736                                 return;
737                         }
738                         p.error("found 'end' unexpectedly");
739                 }
740
741
742                 else if (t.cs() == "item")
743                         handle_par(os);
744
745                 else if (t.cs() == ")") {
746                         if (flags & FLAG_SIMPLE2)
747                                 return;
748                         p.error("found '\\)' unexpectedly");
749                 }
750
751                 else if (t.cs() == "]") {
752                         if (flags & FLAG_EQUATION)
753                                 return;
754                         p.error("found '\\]' unexpectedly");
755                 }
756
757                 else if (t.cs() == "documentclass") {
758                         vector<string> opts;
759                         split(p.getArg('[', ']'), opts, ',');
760                         handle_opt(opts, known_languages, h_language); 
761                         handle_opt(opts, known_fontsizes, h_paperfontsize); 
762                         h_quotes_language = h_language;
763                         h_options = join(opts, ',');
764                         h_textclass = p.getArg('{', '}');
765                 }
766
767                 else if (t.cs() == "usepackage") {
768                         string const options = p.getArg('[', ']');
769                         string const name = p.getArg('{', '}');
770                         if (options.empty() && name.find(',')) {
771                                 vector<string> vecnames;
772                                 split(name, vecnames, ',');
773                                 vector<string>::const_iterator it  = vecnames.begin();
774                                 vector<string>::const_iterator end = vecnames.end();
775                                 for (; it != end; ++it)
776                                         handle_package(trim(*it), string());
777                         } else {
778                                 handle_package(name, options);
779                         }
780                 }
781
782                 else if (t.cs() == "newenvironment") {
783                         string const name = p.getArg('{', '}');
784                         if (name != "lyxcode") {
785                                 ostringstream ss;
786                                 ss << "\\newenvironment{" << name << "}";
787                                 ss << p.getOpt();
788                                 ss << p.getOpt();
789                                 ss << '{' << p.verbatimItem() << '}';
790                                 ss << '{' << p.verbatimItem() << '}';
791                                 ss << '\n';
792                                 handle_tex(os, ss.str());
793                         }
794                 }
795
796                 else if (t.cs() == "newfont") {
797                         ostringstream ss;
798                         ss << "\\newfont";
799                         ss << '{' << p.verbatimItem() << '}';
800                         ss << '{' << p.verbatimItem() << '}';
801                         ss << '\n';
802                         handle_tex(os, ss.str());
803                 }
804
805                 else if (t.cs() == "newcounter") {
806                         ostringstream ss;
807                         ss << "\\newcounter";
808                         ss << '{' << p.verbatimItem() << '}';
809                         ss << p.getOpt();
810                         ss << '\n';
811                         handle_tex(os, ss.str());
812                 }
813
814                 else if (t.cs() == "def") {
815                         string name = p.getToken().cs();
816                         while (p.nextToken().cat() != catBegin)
817                                 name += p.getToken().asString();
818                         handle_ert(os, "\\def\\" + name + '{' + p.verbatimItem() + '}');
819                 }
820
821                 else if (t.cs() == "setcounter") {
822                         string const name = p.getArg('{', '}');
823                         string const content = p.getArg('{', '}');
824                         if (name == "secnumdepth") 
825                                 h_secnumdepth = content;
826                         else if (name == "tocdepth") 
827                                 h_tocdepth = content;
828                         else
829                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
830                 }
831
832                 else if (t.cs() == "setlength") {
833                         string const name = p.verbatimItem();
834                         string const content = p.verbatimItem();
835                         if (in_preamble && name == "parskip")
836                                 h_paragraph_separation = "skip";
837                         else if (in_preamble && name == "parindent")
838                                 h_paragraph_separation = "skip";
839                         else 
840                                 handle_tex(os, "\\setlength{" + name + "}{" + content + "}\n");
841                 }
842         
843                 else if (t.cs() == "par")
844                         handle_par(os);
845
846                 else if (is_known(t.cs(), known_headings)) {
847                         string name = t.cs();
848                         if (p.nextToken().asInput() == "*") {
849                                 p.getToken();
850                                 name += "*";
851                         }
852                         os << "\n\n\\layout " << cap(name) << "\n\n";
853                         parse(p, os, FLAG_ITEM, mode);
854                         os << "\n\n\\layout Standard\n\n";
855                 }
856
857                 else if (t.cs() == "includegraphics") {
858                         map<string, string> opts = split_map(p.getArg('[', ']'));
859                         string name = p.verbatimItem();
860                         begin_inset(os, "Graphics ");
861                         os << "\n\tfilename " << name << '\n';
862                         if (opts.find("width") != opts.end())
863                                 os << "\twidth " << opts["width"] << '\n';
864                         if (opts.find("height") != opts.end())
865                                 os << "\theight " << opts["height"] << '\n';
866                         end_inset(os);
867                 }
868
869                 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
870                         ; // swallow this
871
872                 else if (t.cs() == "tableofcontents")
873                         p.verbatimItem(); // swallow this
874
875                 else if (t.cs() == "multicolumn" && mode == TEXT_MODE) {
876                         // brutish...
877                         parse(p, os, FLAG_ITEM, mode); 
878                         os << MULT;
879                         parse(p, os, FLAG_ITEM, mode); 
880                         os << MULT;
881                         parse(p, os, FLAG_ITEM, mode); 
882                 }
883
884                 else if (t.cs() == "textrm") {
885                         os << '\\' << t.cs() << '{';
886                         parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
887                         os << '}';
888                 }
889
890                 else if ((t.cs() == "emph" || t.cs() == "noun") && mode == TEXT_MODE) {
891                         os << "\n\\" << t.cs() << " on\n";
892                         parse(p, os, FLAG_ITEM, mode);
893                         os << "\n\\" << t.cs() << " default\n";
894                 }
895
896                 else if (is_known(t.cs(), known_latex_commands) && mode == TEXT_MODE) {
897                         begin_inset(os, "LatexCommand ");
898                         os << '\\' << t.cs() << '{';
899                         parse(p, os, FLAG_ITEM, TEXT_MODE);
900                         os << '}';
901                         end_inset(os);
902                 }
903
904                 else if (t.cs() == "bibitem") {
905                         os << "\n\\layout Bibliography\n\\bibitem ";
906                         string opt = p.getArg('[',']');
907                         if (opt.size())
908                                 os << '[' << opt << ']';
909                         os << '{' << p.getArg('{','}') << '}' << "\n\n";
910                 }
911
912                 else if (char const ** where = is_known(t.cs(), known_quotes)) {
913                         begin_inset(os, "Quotes ");
914                         os << known_coded_quotes[where - known_quotes];
915                         end_inset(os);
916                 }
917
918                 else if (t.cs() == "textasciitilde")
919                         os << '~';
920
921                 else if (t.cs() == "_" && mode == TEXT_MODE)
922                         os << '_';
923
924                 else if (t.cs() == "&" && mode == TEXT_MODE)
925                         os << '&';
926
927                 else if (t.cs() == "\"") {
928                         string const name = p.verbatimItem();
929                              if (name == "a") os << 'ä';
930                         else if (name == "o") os << 'ö';
931                         else if (name == "u") os << 'ü';
932                         else if (name == "A") os << 'Ä';
933                         else if (name == "O") os << 'Ö';
934                         else if (name == "U") os << 'Ü';
935                         else handle_ert(os, "\"{" + name + "}");
936                 }
937
938                 else if (t.cs() == "ss") 
939                         os << "ß";
940
941                 else if (t.cs() == "input")
942                         handle_tex(os, "\\input{" + p.verbatimItem() + "}\n");
943
944                 else if (t.cs() == "pagestyle" && in_preamble)
945                         h_paperpagestyle == p.getArg('{','}');
946
947                 else if (t.cs() == "fancyhead") {
948                         ostringstream ss;
949                         ss << "\\fancyhead";
950                         ss << p.getOpt();
951                         ss << '{' << p.verbatimItem() << "}\n";
952                         handle_tex(os, ss.str());
953                 }
954
955                 else {
956                         //cerr << "#: " << t << " mode: " << mode << endl;
957                         if (mode == MATH_MODE || mode == MATHTEXT_MODE) {
958                                 os << t.asInput();
959                                 //cerr << "#: writing: '" << t.asInput() << "'\n";
960                         }
961                         else if (in_preamble)
962                                 h_preamble << t.asInput();
963                         else {
964                                 // heuristic: read up to next non-nested space
965                                 /*
966                                 string s = t.asInput();
967                                 string z = p.verbatimItem();
968                                 while (p.good() && z != " " && z.size()) {
969                                         //cerr << "read: " << z << endl;
970                                         s += z;
971                                         z = p.verbatimItem();
972                                 }
973                                 cerr << "found ERT: " << s << endl;
974                                 handle_ert(os, s + ' ');
975                                 */
976                                 handle_ert(os, t.asInput() + ' ');
977                         }
978                 }
979
980                 if (flags & FLAG_LEAVE) {
981                         flags &= ~FLAG_LEAVE;
982                         break;
983                 }
984         }
985 }
986
987
988
989 } // anonymous namespace
990
991
992 int main(int argc, char * argv[])
993 {
994         if (argc <= 1) {
995                 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
996                 return 2;
997         }
998
999         ifstream is(argv[1]);
1000         Parser p(is);
1001         parse(p, cout, 0, TEXT_MODE);
1002         cout << "\n\\the_end";
1003
1004         return 0;       
1005 }       
1006
1007 // }])