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