]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
finish reader/writer split
[lyx.git] / src / tex2lyx / tex2lyx.C
1 /** The .tex to .lyx converter
2     \author André Pönitz (2003)
3  */
4
5 #include <config.h>
6
7 #include <algorithm>
8 #include <cctype>
9 #include <fstream>
10 #include <iostream>
11 #include <sstream>
12 #include <stack>
13 #include <string>
14 #include <vector>
15
16 #include "texparser.h"
17
18 using std::cout;
19 using std::cerr;
20 using std::endl;
21 using std::fill;
22 using std::getline;
23 using std::ios;
24 using std::ifstream;
25 using std::istream;
26 using std::istringstream;
27 using std::ostream;
28 using std::ostringstream;
29 using std::stack;
30 using std::string;
31 using std::vector;
32
33
34 namespace {
35
36 char const OPEN = '<';
37 char const CLOSE = '>';
38
39 const char * known_languages[] = { "austrian", "babel", "bahasa",
40 "basque", "breton", "bulgarian", "catalan", "croatian", "czech", "danish",
41 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
42 "frenchb", "galician", "germanb", "greek", "hebcal", "hebfont", "hebrew",
43 "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen", "icelandic",
44 "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss", "lgrcmtt",
45 "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr", "lhecmss",
46 "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
47 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
48 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
49 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
50 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
51
52 const char * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
53
54
55 // some ugly stuff
56 string h_preamble;
57 string h_textclass               = "FIXME";
58 string h_options                 = "FIXME";
59 string h_language                = "FIXME";
60 string h_inputencoding           = "FIXME";
61 string h_fontscheme              = "FIXME";
62 string h_graphics                = "default";
63 string h_paperfontsize           = "FIXME";
64 string h_spacing                 = "single";
65 string h_papersize               = "FIXME";
66 string h_paperpackage            = "FIXME";
67 string h_use_geometry            = "0";
68 string h_use_amsmath             = "0";
69 string h_use_natbib              = "0";
70 string h_use_numerical_citations = "0";
71 string h_paperorientation        = "portrait";
72 string h_secnumdepth             = "3";
73 string h_tocdepth                = "3";
74 string h_paragraph_separation    = "indent";
75 string h_defskip                 = "medskip";
76 string h_quotes_language         = "2";
77 string h_quotes_times            = "1";
78 string h_papercolumns            = "1";
79 string h_papersides              = "1";
80 string h_paperpagestyle          = "default";
81 string h_tracking_changes        = "0";
82
83 // indicates whether we are in the preamble
84 bool in_preamble = true;
85
86 // current stack of nested environments
87 stack<string> active_environments;
88
89
90
91 string const trim(string const & a, char const * p = " ")
92 {
93         // lyx::Assert(p);
94
95         if (a.empty() || !*p)
96                 return a;
97
98         string::size_type r = a.find_last_not_of(p);
99         string::size_type l = a.find_first_not_of(p);
100
101         // Is this the minimal test? (lgb)
102         if (r == string::npos && l == string::npos)
103                 return string();
104
105         return a.substr(l, r - l + 1);
106 }
107
108
109 void split(string const & s, vector<string> & result, char delim)
110 {
111         istringstream is(s);    
112         string t;
113         while (getline(is, t, delim))
114                 result.push_back(t);
115 }
116
117
118 string join(vector<string> const & input, char delim)
119 {
120         ostringstream os;
121         for (size_t i = 0; i != input.size(); ++i) {
122                 if (i)
123                         os << delim;    
124                 os << input[i]; 
125         }
126         return os.str();
127 }
128
129
130 void handle_opt(vector<string> & opts, char const ** what, string & target)
131 {
132         if (opts.empty())
133                 return;
134
135         for ( ; what; ++what) {
136                 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
137                 if (it != opts.end()) {
138                         //cerr << "### found option '" << *what << "'\n";
139                         target = *what;
140                         opts.erase(it);
141                         return;
142                 }
143         }
144 }
145
146
147 void handle_ert(ostream & os, string const & s)
148 {
149         os << "\n\\begin_inset ERT\nstatus Collapsed\n\n\\layout Standard\n\n";
150         os << s;
151         os << "\n\\end_inset\n";
152 }
153
154
155 void handle_par(ostream & os)
156 {
157         if (active_environments.empty())
158                 return;
159         os << "\n\\layout ";
160         string s = active_environments.top();
161         if (s == "document")
162                 os << "Standard";
163         else
164                 os << s;
165         os << "\n\n";
166 }
167
168
169 void handle_package(string const & name, string const & options)
170 {
171         if (name == "a4wide") {
172                 h_papersize = "a4";
173                 h_paperpackage = "widemarginsa4";
174         } else if (name == "ae") 
175                 h_fontscheme = "ae";
176         else if (name == "aecompl") 
177                 h_fontscheme = "ae";
178         else if (name == "amsmath") 
179                 h_use_amsmath = "1";
180         else if (name == "amssymb") 
181                 h_use_amsmath = "1";
182         else if (name == "babel") 
183                 ; // ignore this
184         else if (name == "fontenc") 
185                 ; // ignore this
186         else if (name == "inputenc") 
187                 h_inputencoding = options;
188         else if (name == "makeidx") 
189                 ; // ignore this
190         else if (name == "verbatim") 
191                 ; // ignore this
192         else {
193                 if (options.size())
194                         h_preamble += "\\usepackage[" + options + "]{" + name + "}\n";
195                 else
196                         h_preamble += "\\usepackage{" + name + "}\n";
197         }
198 }
199
200
201 string wrap(string const & cmd, string const & str)
202 {
203         return OPEN + cmd + ' ' + str + CLOSE;
204 }
205
206
207 string wrap(string const & cmd, string const & str, string const & str2)
208 {
209         return OPEN + cmd + ' ' + str + ' ' + str2 + CLOSE;
210 }
211
212
213 string parse(Parser & p, unsigned flags, mode_type mode)
214 {
215         //int limits = 0;
216
217         ostringstream result;
218         while (p.good()) {
219                 Token const & t = p.getToken();
220
221 #ifdef FILEDEBUG
222                 cerr << "t: " << t << " flags: " << flags << "\n";
223                 //cell->dump();
224                 cerr << "\n";
225 #endif
226
227                 if (flags & FLAG_ITEM) {
228                         if (t.cat() == catSpace)
229                                 continue;
230
231                         flags &= ~FLAG_ITEM;
232                         if (t.cat() == catBegin) {
233                                 // skip the brace and collect everything to the next matching
234                                 // closing brace
235                                 flags |= FLAG_BRACE_LAST;
236                                 continue;
237                         }
238
239                         // handle only this single token, leave the loop if done
240                         flags |= FLAG_LEAVE;
241                 }
242
243
244                 if (flags & FLAG_BRACED) {
245                         if (t.cat() == catSpace)
246                                 continue;
247
248                         if (t.cat() != catBegin) {
249                                 p.error("opening brace expected");
250                                 return result.str();
251                         }
252
253                         // skip the brace and collect everything to the next matching
254                         // closing brace
255                         flags = FLAG_BRACE_LAST;
256                 }
257
258
259                 if (flags & FLAG_OPTION) {
260                         if (t.cat() == catOther && t.character() == '[') {
261                                 result << parse(p, FLAG_BRACK_LAST, mode);
262                         } else {
263                                 // no option found, put back token and we are done
264                                 p.putback();
265                         }
266                         return result.str();
267                 }
268
269                 //
270                 // cat codes
271                 //
272                 if (t.cat() == catMath) {
273                         if (mode != MATH_MODE) {
274                                 // we are inside some text mode thingy, so opening new math is allowed
275                                 Token const & n = p.getToken();
276                                 if (n.cat() == catMath) {
277                                         // TeX's $$...$$ syntax for displayed math
278                                         result << wrap("equation", parse(p, FLAG_SIMPLE, MATH_MODE));
279                                         p.getToken(); // skip the second '$' token
280                                 } else {
281                                         // simple $...$  stuff
282                                         p.putback();
283                                         result << wrap("simple", parse(p, FLAG_SIMPLE, MATH_MODE));
284                                 }
285                         }
286
287                         else if (flags & FLAG_SIMPLE) {
288                                 // this is the end of the formula
289                                 return result.str();
290                         }
291
292                         else {
293                                 p.error("something strange in the parser\n");
294                                 break;
295                         }
296                 }
297
298                 else if (t.cat() == catLetter)
299                         result << t.character();
300
301                 else if (t.cat() == catSpace && mode != MATH_MODE) {
302                         //if (result.empty() || result[result.size() - 1] != ' ')
303                                 result << t.character();
304                 }
305
306                 else if (t.cat() == catNewline && mode != MATH_MODE)
307                         result << t.character();
308
309                 else if (t.cat() == catParameter) {
310                         Token const & n = p.getToken();
311                         result << wrap("macroarg", string(1, n.character()));
312                 }
313
314                 else if (t.cat() == catActive)
315                         result << wrap("active", string(1, t.character()));
316
317                 else if (t.cat() == catBegin)
318                         result << wrap("braced", parse(p, FLAG_BRACE_LAST, mode));
319
320                 else if (t.cat() == catEnd) {
321                         if (flags & FLAG_BRACE_LAST)
322                                 return result.str();
323                         p.error("found '}' unexpectedly");
324                         //lyx::Assert(0);
325                         //add(cell, '}', LM_TC_TEX);
326                 }
327
328 /*
329                 else if (t.cat() == catAlign) {
330                         ++cellcol;
331                         //cerr << " column now " << cellcol << " max: " << grid.ncols() << "\n";
332                         if (cellcol == grid.ncols()) {
333                                 //cerr << "adding column " << cellcol << "\n";
334                                 grid.addCol(cellcol - 1);
335                         }
336                         cell = &grid.cell(grid.index(cellrow, cellcol));
337                 }
338 */
339
340                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
341                         //cerr << "finished reading option\n";
342                         return result.str();
343                 }
344
345                 else if (t.cat() == catOther)
346                         result << string(1, t.character());
347
348                 else if (t.cat() == catComment) {
349                         string s;
350                         while (p.good()) {
351                                 Token const & t = p.getToken();
352                                 if (t.cat() == catNewline)
353                                         break;
354                                 s += t.asString();
355                         }
356                         //result << wrap("comment", s);
357                         p.skipSpaces();
358                 }
359
360                 //
361                 // control sequences
362                 //
363
364                 else if (t.cs() == "lyxlock") {
365                         // ignored;
366                 }
367
368                 else if (t.cs() == "newcommand" || t.cs() == "providecommand") {
369                         string const name = p.verbatimItem();
370                         string const opts = p.getArg('[', ']');
371                         string const body = p.verbatimItem();
372                         // only non-lyxspecific stuff
373                         if (name != "noun" && name != "tabularnewline") {
374                                 h_preamble += "\\" + t.cs() + "{" + name + "}";
375                                 if (opts.size()) 
376                                         h_preamble += "[" + opts + "]";
377                                 h_preamble += "{" + body + "}\n";
378                         }
379                 }
380
381                 else if (t.cs() == "(") 
382                         result << wrap("simple", parse(p, FLAG_SIMPLE2, MATH_MODE));
383
384                 else if (t.cs() == "[")
385                         result << wrap("equation", parse(p, FLAG_EQUATION, MATH_MODE));
386
387                 else if (t.cs() == "protect")
388                         // ignore \\protect, will hopefully be re-added during output
389                         ;
390
391                 else if (t.cs() == "end") {
392                         if (flags & FLAG_END) {
393                                 // eat environment name
394                                 string const name = p.getArg('{', '}');
395                                 if (name != active_environments.top())
396                                         p.error("\\end{" + name + "} does not match \\begin{"
397                                                 + active_environments.top() + "}");
398                                 active_environments.pop();
399                                 return result.str();
400                         }
401                         p.error("found 'end' unexpectedly");
402                 }
403
404                 else if (t.cs() == ")") {
405                         if (flags & FLAG_SIMPLE2)
406                                 return result.str();
407                         p.error("found '\\)' unexpectedly");
408                 }
409
410                 else if (t.cs() == "]") {
411                         if (flags & FLAG_EQUATION)
412                                 return result.str();
413                         p.error("found '\\]' unexpectedly");
414                 }
415
416 /*
417                 else if (t.cs() == "\\") {
418                         grid.vcrskip(LyXLength(getArg('[', ']')), cellrow);
419                         ++cellrow;
420                         cellcol = 0;
421                         if (cellrow == grid.nrows())
422                                 grid.addRow(cellrow - 1);
423                         if (grid.asHullstring())
424                                 grid.asHullstring()->numbered(cellrow, numbered);
425                         cell = &grid.cell(grid.index(cellrow, cellcol));
426                 }
427 */
428                 else if (t.cs() == "documentclass") {
429                         vector<string> opts;
430                         split(p.getArg('[', ']'), opts, ',');
431                         handle_opt(opts, known_languages, h_language); 
432                         handle_opt(opts, known_fontsizes, h_paperfontsize); 
433                         h_options = join(opts, ',');
434                         h_textclass = p.getArg('{', '}');
435                 }
436
437                 else if (t.cs() == "usepackage") {
438                         string const options = p.getArg('[', ']');
439                         string const name = p.getArg('{', '}');
440                         if (options.empty() && name.find(',')) {
441                                 vector<string> vecnames;
442                                 split(name, vecnames, ',');
443                                 vector<string>::const_iterator it  = vecnames.begin();
444                                 vector<string>::const_iterator end = vecnames.end();
445                                 for (; it != end; ++it) {
446                                         handle_package(trim(*it), string());
447                                 }
448                         } else {
449                                 handle_package(name, options);
450                         }
451                 }
452
453                 else if (t.cs() == "newenvironment") {
454                         string const name = p.getArg('{', '}');
455                         p.skipSpaces();
456                         string const begin = p.verbatimItem();
457                         p.skipSpaces();
458                         string const end = p.verbatimItem();
459                         // ignore out mess
460                         if (name != "lyxcode") 
461                                 result << wrap("newenvironment", begin + end); 
462                 }
463
464                 else if (t.cs() == "def") {
465                         string const name = p.getToken().cs();
466                         string res;
467                         while (p.nextToken().cat() != catBegin)
468                                 res += p.getToken().asString();
469                         handle_ert(result, "\\def" + res + '{' + p.verbatimItem() + '}');
470                 }
471
472                 else if (t.cs() == "setcounter") {
473                         string const name = p.getArg('{', '}');
474                         string const content = p.getArg('{', '}');
475                         if (name == "secnumdepth") 
476                                 h_secnumdepth = content;
477                         else if (name == "tocdepth") 
478                                 h_tocdepth = content;
479                         else
480                                 h_preamble += "\\setcounter{" + name + "}{" + content + "}\n";
481                 }
482
483                 else if (t.cs() == "setlength") {
484                         string const name = p.getToken().cs();
485                         string const content = p.getArg('{', '}');
486                         if (name == "parskip")
487                                 h_paragraph_separation = "skip";
488                         else if (name == "parindent")
489                                 h_paragraph_separation = "skip";
490                         else
491                                 h_preamble += "\\setcounter{" + name + "}{" + content + "}\n";
492                 }
493         
494                 else if (t.cs() == "par")
495                         handle_par(result);
496
497                 else if (t.cs() == "title")
498                         result << "\\layout Title\n\n" + p.verbatimItem();
499
500                 else if (t.cs() == "author")
501                         result << "\\layout Author\n\n" + p.verbatimItem();
502
503                 else if (t.cs() == "abstract")
504                         result << "\\layout Abstract\n\n" + p.verbatimItem();
505
506                 else if (t.cs() == "begin") {
507                         string const name = p.getArg('{', '}');
508                         active_environments.push(name);
509                         result << parse(p, FLAG_END, mode);
510                 }
511
512                 if (flags & FLAG_LEAVE) {
513                         flags &= ~FLAG_LEAVE;
514                         break;
515                 }
516         }
517
518         return result.str();
519 }
520
521
522 } // anonymous namespace
523
524
525 int main(int argc, char * argv[])
526 {
527         if (argc <= 1) {
528                 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
529                 return 2;
530         }
531
532         ifstream is(argv[1]);
533         Parser p(is);
534         string s = parse(p, 0, UNDECIDED_MODE);
535         cout << "# tex2lyx 0.0.2 created this file\n"
536              << "\\lyxformat 222\n"
537              << "\\textclass " << h_textclass << "\n"
538              << "\\begin_preamble\n" << h_preamble << "\\end_preamble\n"
539              << "\\options " << h_options << "\n"
540              << "\\language " << h_language << "\n"
541              << "\\inputencoding " << h_inputencoding << "\n"
542              << "\\fontscheme " << h_fontscheme << "\n"
543              << "\\graphics " << h_graphics << "\n"
544              << "\\paperfontsize " << h_paperfontsize << "\n"
545              << "\\spacing " << h_spacing << "\n"
546              << "\\papersize " << h_papersize << "\n"
547              << "\\paperpackage " << h_paperpackage << "\n"
548              << "\\use_geometry " << h_use_geometry << "\n"
549              << "\\use_amsmath " << h_use_amsmath << "\n"
550              << "\\use_natbib " << h_use_natbib << "\n"
551              << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
552              << "\\paperorientation " << h_paperorientation << "\n"
553              << "\\secnumdepth " << h_secnumdepth << "\n"
554              << "\\tocdepth " << h_tocdepth << "\n"
555              << "\\paragraph_separation " << h_paragraph_separation << "\n"
556              << "\\defskip " << h_defskip << "\n"
557              << "\\quotes_language " << h_quotes_language << "\n"
558              << "\\quotes_times " << h_quotes_times << "\n"
559              << "\\papercolumns " << h_papercolumns << "\n"
560              << "\\papersides " << h_papersides << "\n"
561              << "\\paperpagestyle " << h_paperpagestyle << "\n"
562              << "\\tracking_changes " << h_tracking_changes << "\n"
563              << s << "\n"
564              << "\\the_end";
565
566         return 0;       
567 }