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