]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
Swap <sstream> for "Lsstream.h".
[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 <stack>
14 #include <string>
15 #include <vector>
16
17 #include "Lsstream.h"
18
19 #include "texparser.h"
20
21 using std::count_if;
22 using std::cout;
23 using std::cerr;
24 using std::endl;
25 using std::fill;
26 using std::getline;
27 using std::ios;
28 using std::ifstream;
29 using std::istream;
30 using std::istringstream;
31 using std::ostream;
32 using std::ostringstream;
33 using std::stack;
34 using std::string;
35 using std::vector;
36
37
38 namespace {
39
40 char const OPEN = '<';
41 char const CLOSE = '>';
42 char const TAB = '\001';
43 char const LINE = '\002';
44
45 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
46 "breton", "british", "bulgarian", "catalan", "croatian", "czech",
47 "danish", "dutch", "english", "esperanto", "estonian", "finnish",
48 "francais", "frenchb", "galician", "germanb", "greek", "hebcal", "hebfont",
49 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
50 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
51 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
52 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
53 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
54 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
55 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
56 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
57
58 const char * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
59
60
61 const char * known_math_envs[] = {"equation", "eqnarray", "eqnarray*",
62 "align", "align*", 0};
63
64
65 // some ugly stuff
66 ostringstream h_preamble;
67 string h_textclass               = "article";
68 string h_options                 = "";
69 string h_language                = "english";
70 string h_inputencoding           = "latin1";
71 string h_fontscheme              = "default";
72 string h_graphics                = "default";
73 string h_paperfontsize           = "default";
74 string h_spacing                 = "single";
75 string h_papersize               = "default";
76 string h_paperpackage            = "default";
77 string h_use_geometry            = "0";
78 string h_use_amsmath             = "0";
79 string h_use_natbib              = "0";
80 string h_use_numerical_citations = "0";
81 string h_paperorientation        = "portrait";
82 string h_secnumdepth             = "3";
83 string h_tocdepth                = "3";
84 string h_paragraph_separation    = "indent";
85 string h_defskip                 = "medskip";
86 string h_quotes_language         = "english";
87 string h_quotes_times            = "1";
88 string h_papercolumns            = "1";
89 string h_papersides              = "1";
90 string h_paperpagestyle          = "default";
91 string h_tracking_changes        = "0";
92
93 // indicates whether we are in the preamble
94 bool in_preamble = true;
95
96 // current stack of nested environments
97 stack<string> active_environments;
98
99
100 string cap(string s)
101 {
102         if (s.size())
103                 s[0] = toupper(s[0]);
104         return s;
105 }
106
107
108 string const trim(string const & a, char const * p = " ")
109 {
110         // lyx::Assert(p);
111
112         if (a.empty() || !*p)
113                 return a;
114
115         string::size_type r = a.find_last_not_of(p);
116         string::size_type l = a.find_first_not_of(p);
117
118         // Is this the minimal test? (lgb)
119         if (r == string::npos && l == string::npos)
120                 return string();
121
122         return a.substr(l, r - l + 1);
123 }
124
125
126 void split(string const & s, vector<string> & result, char delim)
127 {
128         istringstream is(s);    
129         string t;
130         while (getline(is, t, delim))
131                 result.push_back(t);
132 }
133
134
135 string join(vector<string> const & input, char delim)
136 {
137         ostringstream os;
138         for (size_t i = 0; i != input.size(); ++i) {
139                 if (i)
140                         os << delim;    
141                 os << input[i]; 
142         }
143         return os.str();
144 }
145
146
147 void handle_opt(vector<string> & opts, char const ** what, string & target)
148 {
149         if (opts.empty())
150                 return;
151
152         for ( ; *what; ++what) {
153                 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
154                 if (it != opts.end()) {
155                         //cerr << "### found option '" << *what << "'\n";
156                         target = *what;
157                         opts.erase(it);
158                         return;
159                 }
160         }
161 }
162
163
164 bool is_math_env(string const & name)
165 {
166         for (char const ** what = known_math_envs; *what; ++what)
167                 if (*what == name)
168                         return true;
169         return false;
170 }
171
172
173 bool is_heading(string const & name)
174 {
175         return
176                 name == "caption" ||
177                 name == "title" ||
178                 name == "author" ||
179                 name == "paragraph" ||
180                 name == "chapter" ||
181                 name == "section" ||
182                 name == "subsection" ||
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" || s == "table") {
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 vector<string> extract_col_align(string const & s)
294 {
295         vector<string> res;
296         for (size_t i = 0; i < s.size(); ++i) {
297                 switch (s[i]) {
298                         case 'c':
299                                 res.push_back("center");
300                                 break;
301                         case 'l':
302                                 res.push_back("left");
303                                 break;
304                         case 'r':
305                                 res.push_back("right");
306                                 break;
307                         default:
308                                 res.push_back("right");
309                                 break;
310                 }
311         }
312         return res;
313 }
314
315
316 void end_preamble(ostream & os)
317 {
318         in_preamble = false;
319         os << "# tex2lyx 0.0.2 created this file\n"
320            << "\\lyxformat 222\n"
321            << "\\textclass " << h_textclass << "\n"
322            << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n"
323            << "\\options " << h_options << "\n"
324            << "\\language " << h_language << "\n"
325            << "\\inputencoding " << h_inputencoding << "\n"
326            << "\\fontscheme " << h_fontscheme << "\n"
327            << "\\graphics " << h_graphics << "\n"
328            << "\\paperfontsize " << h_paperfontsize << "\n"
329            << "\\spacing " << h_spacing << "\n"
330            << "\\papersize " << h_papersize << "\n"
331            << "\\paperpackage " << h_paperpackage << "\n"
332            << "\\use_geometry " << h_use_geometry << "\n"
333            << "\\use_amsmath " << h_use_amsmath << "\n"
334            << "\\use_natbib " << h_use_natbib << "\n"
335            << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
336            << "\\paperorientation " << h_paperorientation << "\n"
337            << "\\secnumdepth " << h_secnumdepth << "\n"
338            << "\\tocdepth " << h_tocdepth << "\n"
339            << "\\paragraph_separation " << h_paragraph_separation << "\n"
340            << "\\defskip " << h_defskip << "\n"
341            << "\\quotes_language " << h_quotes_language << "\n"
342            << "\\quotes_times " << h_quotes_times << "\n"
343            << "\\papercolumns " << h_papercolumns << "\n"
344            << "\\papersides " << h_papersides << "\n"
345            << "\\paperpagestyle " << h_paperpagestyle << "\n"
346            << "\\tracking_changes " << h_tracking_changes << "\n";
347 }
348
349
350 void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
351 {
352         while (p.good()) {
353                 Token const & t = p.getToken();
354
355 #ifdef FILEDEBUG
356                 cerr << "t: " << t << " flags: " << flags << "\n";
357                 //cell->dump();
358 #endif
359
360                 if (flags & FLAG_ITEM) {
361                         if (t.cat() == catSpace)
362                                 continue;
363
364                         flags &= ~FLAG_ITEM;
365                         if (t.cat() == catBegin) {
366                                 // skip the brace and collect everything to the next matching
367                                 // closing brace
368                                 flags |= FLAG_BRACE_LAST;
369                                 continue;
370                         }
371
372                         // handle only this single token, leave the loop if done
373                         flags |= FLAG_LEAVE;
374                 }
375
376
377                 if (flags & FLAG_BRACED) {
378                         if (t.cat() == catSpace)
379                                 continue;
380
381                         if (t.cat() != catBegin) {
382                                 p.error("opening brace expected");
383                                 return;
384                         }
385
386                         // skip the brace and collect everything to the next matching
387                         // closing brace
388                         flags = FLAG_BRACE_LAST;
389                 }
390
391
392                 if (flags & FLAG_OPTION) {
393                         if (t.cat() == catOther && t.character() == '[') {
394                                 parse(p, os, FLAG_BRACK_LAST, mode);
395                         } else {
396                                 // no option found, put back token and we are done
397                                 p.putback();
398                         }
399                         return;
400                 }
401
402                 //
403                 // cat codes
404                 //
405                 if (t.cat() == catMath) {
406                         if (mode == TEXT_MODE || mode == MATHTEXT_MODE) {
407                                 // we are inside some text mode thingy, so opening new math is allowed
408                                 if (mode == TEXT_MODE)
409                                         begin_inset(os, "Formula ");
410                                 Token const & n = p.getToken();
411                                 if (n.cat() == catMath) {
412                                         // TeX's $$...$$ syntax for displayed math
413                                         os << "\\[";
414                                         parse(p, os, FLAG_SIMPLE, MATH_MODE);
415                                         os << "\\]";
416                                         p.getToken(); // skip the second '$' token
417                                 } else {
418                                         // simple $...$  stuff
419                                         p.putback();
420                                         os << '$';
421                                         parse(p, os, FLAG_SIMPLE, MATH_MODE);
422                                         os << '$';
423                                 }
424                                 if (mode == TEXT_MODE)
425                                         end_inset(os);
426                         }
427
428                         else if (flags & FLAG_SIMPLE) {
429                                 // this is the end of the formula
430                                 return;
431                         }
432
433                         else {
434                                 cerr << "mode: " << mode << endl;
435                                 p.error("something strange in the parser\n");
436                                 break;
437                         }
438                 }
439
440                 else if (t.cat() == catLetter)
441                         os << t.character();
442
443                 else if (t.cat() == catSpace) 
444                         os << t.character();
445
446                 else if (t.cat() == catNewline)
447                         os << ' ';
448
449                 else if (t.cat() == catSuper)
450                         os << t.character();
451
452                 else if (t.cat() == catSub)
453                         os << t.character();
454
455                 else if (t.cat() == catParameter) {
456                         Token const & n = p.getToken();
457                         os << wrap("macroarg", string(1, n.character()));
458                 }
459
460                 else if (t.cat() == catActive) {
461                         if (t.character() == '~')
462                                 os << (curr_env() == "lyxcode" ? ' ' : '~');
463                         else
464                                 os << t.asInput();
465                 }
466
467                 else if (t.cat() == catBegin) {
468                         if (mode == MATH_MODE)
469                                 os << '{';
470                         else
471                                 handle_ert(os, "{");
472                 }
473
474                 else if (t.cat() == catEnd) {
475                         if (flags & FLAG_BRACE_LAST)
476                                 return;
477                         if (mode == MATH_MODE)
478                                 os << '}';
479                         else
480                                 handle_ert(os, "}");
481                 }
482
483                 else if (t.cat() == catAlign) {
484                         if (mode == MATH_MODE)
485                                 os << t.character();
486                         else
487                                 os << TAB;
488                 }
489
490                 else if (t.cs() == "tabularnewline") {
491                         if (mode == MATH_MODE)
492                                 os << t.asInput();
493                         else
494                                 os << LINE;
495                 }
496
497                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
498                         //cerr << "finished reading option\n";
499                         return;
500                 }
501
502                 else if (t.cat() == catOther)
503                         os << string(1, t.character());
504
505                 else if (t.cat() == catComment) {
506                         string s;
507                         while (p.good()) {
508                                 Token const & t = p.getToken();
509                                 if (t.cat() == catNewline)
510                                         break;
511                                 s += t.asString();
512                         }
513                         //os << wrap("comment", s);
514                         p.skipSpaces();
515                 }
516
517                 //
518                 // control sequences
519                 //
520
521                 else if (t.cs() == "lyxlock")
522                         ; // ignored
523
524                 else if (t.cs() == "newcommand" || t.cs() == "providecommand") {
525                         string const name = p.verbatimItem();
526                         string const opts = p.getArg('[', ']');
527                         string const body = p.verbatimItem();
528                         // only non-lyxspecific stuff
529                         if (name != "\\noun " && name != "\\tabularnewline ") {
530                                 ostream & out = in_preamble ? h_preamble : os;
531                                 if (!in_preamble)
532                                         begin_inset(os, "FormulaMacro\n");
533                                 out << "\\" << t.cs() << "{" << name << "}";
534                                 if (opts.size()) 
535                                         out << "[" << opts << "]";
536                                 out << "{" << body << "}";
537                                 if (!in_preamble)
538                                         end_inset(os);
539                         }
540                 }
541
542                 else if (t.cs() == "(") {
543                         begin_inset(os, "Formula");
544                         os << " \\(";
545                         parse(p, os, FLAG_SIMPLE2, MATH_MODE);
546                         os << "\\)";
547                         end_inset(os);
548                 }
549
550                 else if (t.cs() == "[") {
551                         begin_inset(os, "Formula");
552                         os << " \\[";
553                         parse(p, os, FLAG_EQUATION, MATH_MODE);
554                         os << "\\]";
555                         end_inset(os);
556                 }
557
558                 else if (t.cs() == "protect")
559                         // ignore \\protect, will hopefully be re-added during output
560                         ;
561
562                 else if (t.cs() == "begin") {
563                         string const name = p.getArg('{', '}');
564                         active_environments.push(name);
565                         if (name == "document") {
566                                 end_preamble(os);
567                                 parse(p, os, FLAG_END, mode);
568                         } else if (name == "abstract") {
569                                 handle_par(os);
570                                 parse(p, os, FLAG_END, mode);
571                         } else if (is_math_env(name)) {
572                                 begin_inset(os, "Formula ");    
573                                 os << "\\begin{" << name << "}";
574                                 parse(p, os, FLAG_END, MATH_MODE);
575                                 os << "\\end{" << name << "}";
576                                 end_inset(os);  
577                         } else if (name == "tabular") {
578                                 begin_inset(os, "Tabular \n");
579                                 string colopts = p.verbatimItem();
580                                 vector<string> colalign = extract_col_align(colopts);
581                                 ostringstream ss;
582                                 parse(p, ss, FLAG_END, mode);
583                                 vector<string> lines;
584                                 split(ss.str(), lines, LINE);
585                                 const size_t cols = colalign.size();
586                                 const size_t rows = lines.size();
587                                 os << "<lyxtabular version=\"3\" rows=\"" << rows
588                                    << "\" columns=\"" << cols << "\">\n"
589                                    << "<features>\n";
590                                 for (size_t c = 0; c < cols; ++c)
591                                         os << "<column alignment=\"" << colalign[c] << "\""
592                                            << " valignment=\"top\""
593                                            << " width=\"0pt\""
594                                            << ">\n";
595                                 for (size_t r = 0; r < rows; ++r) {
596                                         vector<string> cells;
597                                         split(lines[r], cells, TAB);
598                                         while (cells.size() < cols)
599                                                 cells.push_back(string());
600                                         //os << "<row bottomline=\"true\">\n";
601                                         os << "<row>\n";
602                                         for (size_t c = 0; c < cols; ++c) {
603                                                 os << "<cell alignment=\"center\""
604                                                    << " valignment=\"top\""
605                                                    << " topline=\"true\""
606                                                    << " leftline=\"true\""
607                                                    << " usebox=\"none\""
608                                                    << ">";
609                                                 begin_inset(os, "Text");
610                                                 os << "\n\n\\layout Standard\n\n";
611                                                 os << cells[c];
612                                                 end_inset(os);
613                                                 os << "</cell>\n";
614                                         }
615                                         os << "</row>\n";
616                                 }
617                                 os << "</lyxtabular>\n";
618                                 end_inset(os);  
619                         } else if (name == "table") {
620                                 begin_inset(os, "Float table\n");       
621                                 os << "wide false\n"
622                                    << "collapsed false\n"
623                                    << "\n"
624                                    << "\\layout Standard\n";
625                                 parse(p, os, FLAG_END, mode);
626                                 end_inset(os);  
627                         } else if (name == "thebibliography") {
628                                 p.verbatimItem(); // swallow next arg
629                                 parse(p, os, FLAG_END, mode);
630                                 os << "\n\\layout Standard\n\n";
631                         } else if (mode == MATH_MODE) {
632                                 os << "\\begin{" << name << "}";
633                                 parse(p, os, FLAG_END, mode);
634                                 os << "\\end{" << name << "}";
635                         } else {
636                                 parse(p, os, FLAG_END, mode);
637                         }
638                 }
639
640                 else if (t.cs() == "end") {
641                         if (flags & FLAG_END) {
642                                 // eat environment name
643                                 string const name = p.getArg('{', '}');
644                                 if (name != curr_env())
645                                         p.error("\\end{" + name + "} does not match \\begin{"
646                                                 + curr_env() + "}");
647                                 active_environments.pop();
648                                 return;
649                         }
650                         p.error("found 'end' unexpectedly");
651                 }
652
653                 else if (t.cs() == "item")
654                         handle_par(os);
655
656                 else if (t.cs() == ")") {
657                         if (flags & FLAG_SIMPLE2)
658                                 return;
659                         p.error("found '\\)' unexpectedly");
660                 }
661
662                 else if (t.cs() == "]") {
663                         if (flags & FLAG_EQUATION)
664                                 return;
665                         p.error("found '\\]' unexpectedly");
666                 }
667
668                 else if (t.cs() == "documentclass") {
669                         vector<string> opts;
670                         split(p.getArg('[', ']'), opts, ',');
671                         handle_opt(opts, known_languages, h_language); 
672                         handle_opt(opts, known_fontsizes, h_paperfontsize); 
673                         h_quotes_language = h_language;
674                         h_options = join(opts, ',');
675                         h_textclass = p.getArg('{', '}');
676                 }
677
678                 else if (t.cs() == "usepackage") {
679                         string const options = p.getArg('[', ']');
680                         string const name = p.getArg('{', '}');
681                         if (options.empty() && name.find(',')) {
682                                 vector<string> vecnames;
683                                 split(name, vecnames, ',');
684                                 vector<string>::const_iterator it  = vecnames.begin();
685                                 vector<string>::const_iterator end = vecnames.end();
686                                 for (; it != end; ++it)
687                                         handle_package(trim(*it), string());
688                         } else {
689                                 handle_package(name, options);
690                         }
691                 }
692
693                 else if (t.cs() == "newenvironment") {
694                         string const name = p.getArg('{', '}');
695                         p.skipSpaces();
696                         string const begin = p.verbatimItem();
697                         p.skipSpaces();
698                         string const end = p.verbatimItem();
699                         // ignore out mess
700                         if (name != "lyxcode") 
701                                 os << wrap("newenvironment", begin + end); 
702                 }
703
704                 else if (t.cs() == "def") {
705                         string name = p.getToken().cs();
706                         while (p.nextToken().cat() != catBegin)
707                                 name += p.getToken().asString();
708                         handle_ert(os, "\\def\\" + name + '{' + p.verbatimItem() + '}');
709                 }
710
711                 else if (t.cs() == "setcounter") {
712                         string const name = p.getArg('{', '}');
713                         string const content = p.getArg('{', '}');
714                         if (name == "secnumdepth") 
715                                 h_secnumdepth = content;
716                         else if (name == "tocdepth") 
717                                 h_tocdepth = content;
718                         else
719                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
720                 }
721
722                 else if (t.cs() == "setlength") {
723                         string const name = p.getToken().cs();
724                         string const content = p.getArg('{', '}');
725                         if (name == "parskip")
726                                 h_paragraph_separation = "skip";
727                         else if (name == "parindent")
728                                 h_paragraph_separation = "skip";
729                         else
730                                 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
731                 }
732         
733                 else if (t.cs() == "par")
734                         handle_par(os);
735
736                 else if (is_heading(t.cs())) {
737                         string name = t.cs();
738                         if (p.nextToken().asInput() == "*") {
739                                 p.getToken();
740                                 name += "*";
741                         }
742                         os << "\\layout " << cap(name) << "\n\n";
743                         parse(p, os, FLAG_ITEM, mode);
744                 }
745
746                 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
747                         ; // swallow this
748
749                 else if (t.cs() == "tableofcontents")
750                         p.verbatimItem(); // swallow this
751
752                 else if (t.cs() == "textrm") {
753                         os << '\\' << t.cs() << '{';
754                         parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
755                         os << '}';
756                 }
757
758                 else if ((t.cs() == "emph" || t.cs() == "noun") && mode == TEXT_MODE) {
759                         os << "\n\\" << t.cs() << " on\n";
760                         parse(p, os, FLAG_ITEM, mode);
761                         os << "\n\\" << t.cs() << " default\n";
762                 }
763
764                 else if (is_latex_command(t.cs()) && mode == TEXT_MODE) {
765                         begin_inset(os, "LatexCommand ");
766                         os << '\\' << t.cs() << '{';
767                         parse(p, os, FLAG_ITEM, TEXT_MODE);
768                         os << '}';
769                         end_inset(os);
770                 }
771
772                 else if (t.cs() == "bibitem") {
773                         os << "\n\\layout Bibliography\n\\bibitem ";
774                         string opt = p.getArg('[',']');
775                         if (opt.size())
776                                 os << '[' << opt << ']';
777                         os << '{' << p.getArg('{','}') << '}' << "\n\n";
778                 }
779
780                 else if (t.cs() == "textasciitilde")
781                         os << '~';
782
783                 else if (t.cs() == "_" && mode == TEXT_MODE)
784                         os << '_';
785
786                 else if (t.cs() == "&" && mode == TEXT_MODE)
787                         os << '&';
788
789                 else if (t.cs() == "pagestyle" && in_preamble)
790                         h_paperpagestyle == p.getArg('{','}');
791
792                 else {
793                         if (mode == MATH_MODE)
794                                 os << t.asInput();
795                         else if (in_preamble)
796                                 h_preamble << t.asInput();
797                         else {
798                                 // heuristic: read up to next non-nested space
799                                 /*
800                                 string s = t.asInput();
801                                 string z = p.verbatimItem();
802                                 while (p.good() && z != " " && z.size()) {
803                                         //cerr << "read: " << z << endl;
804                                         s += z;
805                                         z = p.verbatimItem();
806                                 }
807                                 cerr << "found ERT: " << s << endl;
808                                 handle_ert(os, s + ' ');
809                                 */
810                                 handle_ert(os, t.asInput() + ' ');
811                         }
812                 }
813
814                 if (flags & FLAG_LEAVE) {
815                         flags &= ~FLAG_LEAVE;
816                         break;
817                 }
818         }
819 }
820
821
822
823 } // anonymous namespace
824
825
826 int main(int argc, char * argv[])
827 {
828         if (argc <= 1) {
829                 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
830                 return 2;
831         }
832
833         ifstream is(argv[1]);
834         Parser p(is);
835         parse(p, cout, 0, TEXT_MODE);
836         cout << "\n\\the_end";
837
838         return 0;       
839 }       
840
841 // }])