1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
18 #include "texparser.h"
28 using std::istringstream;
30 using std::ostringstream;
38 char const OPEN = '<';
39 char const CLOSE = '>';
41 const char * known_languages[] = { "austrian", "babel", "bahasa",
42 "basque", "breton", "bulgarian", "catalan", "croatian", "czech", "danish",
43 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
44 "frenchb", "galician", "germanb", "greek", "hebcal", "hebfont", "hebrew",
45 "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen", "icelandic",
46 "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss", "lgrcmtt",
47 "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr", "lhecmss",
48 "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
49 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
50 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
51 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
52 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
54 const char * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
58 ostringstream h_preamble;
59 string h_textclass = "FIXME";
60 string h_options = "FIXME";
61 string h_language = "FIXME";
62 string h_inputencoding = "FIXME";
63 string h_fontscheme = "FIXME";
64 string h_graphics = "default";
65 string h_paperfontsize = "FIXME";
66 string h_spacing = "single";
67 string h_papersize = "FIXME";
68 string h_paperpackage = "FIXME";
69 string h_use_geometry = "0";
70 string h_use_amsmath = "0";
71 string h_use_natbib = "0";
72 string h_use_numerical_citations = "0";
73 string h_paperorientation = "portrait";
74 string h_secnumdepth = "3";
75 string h_tocdepth = "3";
76 string h_paragraph_separation = "indent";
77 string h_defskip = "medskip";
78 string h_quotes_language = "FIXME";
79 string h_quotes_times = "1";
80 string h_papercolumns = "1";
81 string h_papersides = "1";
82 string h_paperpagestyle = "default";
83 string h_tracking_changes = "0";
85 // indicates whether we are in the preamble
86 bool in_preamble = true;
88 // current stack of nested environments
89 stack<string> active_environments;
93 string const trim(string const & a, char const * p = " ")
100 string::size_type r = a.find_last_not_of(p);
101 string::size_type l = a.find_first_not_of(p);
103 // Is this the minimal test? (lgb)
104 if (r == string::npos && l == string::npos)
107 return a.substr(l, r - l + 1);
111 void split(string const & s, vector<string> & result, char delim)
115 while (getline(is, t, delim))
120 string join(vector<string> const & input, char delim)
123 for (size_t i = 0; i != input.size(); ++i) {
132 void handle_opt(vector<string> & opts, char const ** what, string & target)
137 for ( ; what; ++what) {
138 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
139 if (it != opts.end()) {
140 //cerr << "### found option '" << *what << "'\n";
149 void inset_header(ostream & os, string const & name)
151 os << "\n\\begin_inset " << name;
155 void inset_footer(ostream & os)
157 os << "\n\\end_inset\n";
161 void handle_ert(ostream & os, string const & s)
163 inset_header(os, "ERT");
164 os << "\nstatus Collapsed\n\n\\layout Standard\n\n";
165 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
167 os << "\n\\backslash\n";
175 void handle_par(ostream & os)
177 if (active_environments.empty())
180 string s = active_environments.top();
181 if (s == "document") {
182 os << "Standard\n\n";
186 s[0] = toupper(s[0]);
191 void handle_package(string const & name, string const & options)
193 if (name == "a4wide") {
194 h_papersize = "a4paper";
195 h_paperpackage = "widemarginsa4";
196 } else if (name == "ae")
198 else if (name == "aecompl")
200 else if (name == "amsmath")
202 else if (name == "amssymb")
204 else if (name == "babel")
206 else if (name == "fontenc")
208 else if (name == "inputenc")
209 h_inputencoding = options;
210 else if (name == "makeidx")
212 else if (name == "verbatim")
216 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
218 h_preamble << "\\usepackage{" << name << "}\n";
223 string wrap(string const & cmd, string const & str)
225 return OPEN + cmd + ' ' + str + CLOSE;
229 void end_preamble(ostream & os)
232 os << "# tex2lyx 0.0.2 created this file\n"
233 << "\\lyxformat 222\n"
234 << "\\textclass " << h_textclass << "\n"
235 << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n"
236 << "\\options " << h_options << "\n"
237 << "\\language " << h_language << "\n"
238 << "\\inputencoding " << h_inputencoding << "\n"
239 << "\\fontscheme " << h_fontscheme << "\n"
240 << "\\graphics " << h_graphics << "\n"
241 << "\\paperfontsize " << h_paperfontsize << "\n"
242 << "\\spacing " << h_spacing << "\n"
243 << "\\papersize " << h_papersize << "\n"
244 << "\\paperpackage " << h_paperpackage << "\n"
245 << "\\use_geometry " << h_use_geometry << "\n"
246 << "\\use_amsmath " << h_use_amsmath << "\n"
247 << "\\use_natbib " << h_use_natbib << "\n"
248 << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
249 << "\\paperorientation " << h_paperorientation << "\n"
250 << "\\secnumdepth " << h_secnumdepth << "\n"
251 << "\\tocdepth " << h_tocdepth << "\n"
252 << "\\paragraph_separation " << h_paragraph_separation << "\n"
253 << "\\defskip " << h_defskip << "\n"
254 << "\\quotes_language " << h_quotes_language << "\n"
255 << "\\quotes_times " << h_quotes_times << "\n"
256 << "\\papercolumns " << h_papercolumns << "\n"
257 << "\\papersides " << h_papersides << "\n"
258 << "\\paperpagestyle " << h_paperpagestyle << "\n"
259 << "\\tracking_changes " << h_tracking_changes << "\n"
260 << h_preamble.str() << "\n";
264 void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
267 Token const & t = p.getToken();
270 cerr << "t: " << t << " flags: " << flags << "\n";
274 if (flags & FLAG_ITEM) {
275 if (t.cat() == catSpace)
279 if (t.cat() == catBegin) {
280 // skip the brace and collect everything to the next matching
282 flags |= FLAG_BRACE_LAST;
286 // handle only this single token, leave the loop if done
291 if (flags & FLAG_BRACED) {
292 if (t.cat() == catSpace)
295 if (t.cat() != catBegin) {
296 p.error("opening brace expected");
300 // skip the brace and collect everything to the next matching
302 flags = FLAG_BRACE_LAST;
306 if (flags & FLAG_OPTION) {
307 if (t.cat() == catOther && t.character() == '[') {
308 parse(p, os, FLAG_BRACK_LAST, mode);
310 // no option found, put back token and we are done
319 if (t.cat() == catMath) {
320 if (mode == TEXT_MODE || mode == MATHTEXT_MODE || mode == UNDECIDED_MODE){
321 // we are inside some text mode thingy, so opening new math is allowed
322 if (mode == TEXT_MODE)
323 inset_header(os, "Formula ");
324 Token const & n = p.getToken();
325 if (n.cat() == catMath) {
326 // TeX's $$...$$ syntax for displayed math
328 parse(p, os, FLAG_SIMPLE, MATH_MODE);
330 p.getToken(); // skip the second '$' token
332 // simple $...$ stuff
335 parse(p, os, FLAG_SIMPLE, MATH_MODE);
338 if (mode == TEXT_MODE)
342 else if (flags & FLAG_SIMPLE) {
343 // this is the end of the formula
348 cerr << "mode: " << mode << endl;
349 p.error("something strange in the parser\n");
354 else if (t.cat() == catLetter)
357 else if (t.cat() == catSpace)
360 else if (t.cat() == catNewline)
363 else if (t.cat() == catParameter) {
364 Token const & n = p.getToken();
365 os << wrap("macroarg", string(1, n.character()));
368 else if (t.cat() == catActive)
369 os << wrap("active", string(1, t.character()));
371 else if (t.cat() == catBegin) {
373 parse(p, os, FLAG_BRACE_LAST, mode);
377 else if (t.cat() == catEnd) {
378 if (flags & FLAG_BRACE_LAST)
380 p.error("found '}' unexpectedly");
382 //add(cell, '}', LM_TC_TEX);
385 else if (t.cat() == catAlign)
390 //cerr << " column now " << cellcol << " max: " << grid.ncols() << "\n";
391 if (cellcol == grid.ncols()) {
392 //cerr << "adding column " << cellcol << "\n";
393 grid.addCol(cellcol - 1);
395 cell = &grid.cell(grid.index(cellrow, cellcol));
399 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
400 //cerr << "finished reading option\n";
404 else if (t.cat() == catOther)
405 os << string(1, t.character());
407 else if (t.cat() == catComment) {
410 Token const & t = p.getToken();
411 if (t.cat() == catNewline)
415 //os << wrap("comment", s);
423 else if (t.cs() == "lyxlock") {
427 else if (t.cs() == "newcommand" || t.cs() == "providecommand") {
428 string const name = p.verbatimItem();
429 string const opts = p.getArg('[', ']');
430 string const body = p.verbatimItem();
431 // only non-lyxspecific stuff
432 if (name != "noun" && name != "tabularnewline") {
433 h_preamble << "\\" << t.cs() << "{" << name << "}";
435 h_preamble << "[" << opts << "]";
436 h_preamble << "{" << body << "}\n";
440 else if (t.cs() == "(") {
441 inset_header(os, "Formula");
443 parse(p, os, FLAG_SIMPLE2, MATH_MODE);
448 else if (t.cs() == "[") {
449 inset_header(os, "Formula");
451 parse(p, os, FLAG_EQUATION, MATH_MODE);
456 else if (t.cs() == "protect")
457 // ignore \\protect, will hopefully be re-added during output
460 else if (t.cs() == "begin") {
461 string const name = p.getArg('{', '}');
462 active_environments.push(name);
463 if (name == "document")
465 else if (name == "abstract")
468 os << "\\begin{" << name << "}";
469 parse(p, os, FLAG_END, mode);
472 else if (t.cs() == "end") {
473 if (flags & FLAG_END) {
474 // eat environment name
475 string const name = p.getArg('{', '}');
476 if (name != active_environments.top())
477 p.error("\\end{" + name + "} does not match \\begin{"
478 + active_environments.top() + "}");
479 active_environments.pop();
480 if (name == "document" || name == "abstract")
483 os << "\\end{" << name << "}";
486 p.error("found 'end' unexpectedly");
489 else if (t.cs() == ")") {
490 if (flags & FLAG_SIMPLE2)
492 p.error("found '\\)' unexpectedly");
495 else if (t.cs() == "]") {
496 if (flags & FLAG_EQUATION)
498 p.error("found '\\]' unexpectedly");
501 else if (t.cs() == "documentclass") {
503 split(p.getArg('[', ']'), opts, ',');
504 handle_opt(opts, known_languages, h_language);
505 handle_opt(opts, known_fontsizes, h_paperfontsize);
506 h_quotes_language = h_language;
507 h_options = join(opts, ',');
508 h_textclass = p.getArg('{', '}');
511 else if (t.cs() == "usepackage") {
512 string const options = p.getArg('[', ']');
513 string const name = p.getArg('{', '}');
514 if (options.empty() && name.find(',')) {
515 vector<string> vecnames;
516 split(name, vecnames, ',');
517 vector<string>::const_iterator it = vecnames.begin();
518 vector<string>::const_iterator end = vecnames.end();
519 for (; it != end; ++it)
520 handle_package(trim(*it), string());
522 handle_package(name, options);
526 else if (t.cs() == "newenvironment") {
527 string const name = p.getArg('{', '}');
529 string const begin = p.verbatimItem();
531 string const end = p.verbatimItem();
533 if (name != "lyxcode")
534 os << wrap("newenvironment", begin + end);
537 else if (t.cs() == "def") {
538 string name = p.getToken().cs();
539 while (p.nextToken().cat() != catBegin)
540 name += p.getToken().asString();
541 handle_ert(os, "\\def\\" + name + '{' + p.verbatimItem() + '}');
544 else if (t.cs() == "setcounter") {
545 string const name = p.getArg('{', '}');
546 string const content = p.getArg('{', '}');
547 if (name == "secnumdepth")
548 h_secnumdepth = content;
549 else if (name == "tocdepth")
550 h_tocdepth = content;
552 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
555 else if (t.cs() == "setlength") {
556 string const name = p.getToken().cs();
557 string const content = p.getArg('{', '}');
558 if (name == "parskip")
559 h_paragraph_separation = "skip";
560 else if (name == "parindent")
561 h_paragraph_separation = "skip";
563 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
566 else if (t.cs() == "par")
569 else if (t.cs() == "title")
570 os << "\\layout Title\n\n" + p.verbatimItem();
572 else if (t.cs() == "author")
573 os << "\\layout Author\n\n" + p.verbatimItem();
575 else if (t.cs() == "makeindex" || t.cs() == "maketitle")
578 else if (t.cs() == "textrm") {
579 os << '\\' << t.cs() << '{';
580 parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
585 (in_preamble ? h_preamble : os) << '\\' << t.asString();
587 if (flags & FLAG_LEAVE) {
588 flags &= ~FLAG_LEAVE;
596 } // anonymous namespace
599 int main(int argc, char * argv[])
602 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
606 ifstream is(argv[1]);
608 parse(p, cout, 0, UNDECIDED_MODE);
609 cout << "\n\\the_end";