]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/preamble.cpp
preamble.cpp: cleanup the list of supported languages
[lyx.git] / src / tex2lyx / preamble.cpp
1 /**
2  * \file preamble.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 // {[(
13
14 #include <config.h>
15
16 #include "tex2lyx.h"
17
18 #include "Layout.h"
19 #include "Lexer.h"
20 #include "TextClass.h"
21 #include "support/convert.h"
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
24
25 #include <algorithm>
26 #include <iostream>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 #include <map>
31
32
33 namespace lyx {
34
35 using std::istringstream;
36 using std::ostream;
37 using std::ostringstream;
38 using std::string;
39 using std::vector;
40 using std::cerr;
41 using std::endl;
42 using std::find;
43
44 using support::FileName;
45 using support::libFileSearch;
46 using support::isStrDbl;
47
48 // special columntypes
49 extern std::map<char, int> special_columns;
50
51 std::map<string, vector<string> > used_packages;
52
53 namespace {
54
55 const char * const known_languages[] = { "afrikaans", "american", "arabic",
56 "austrian", "bahasa", "basque", "belarusian", "brazil", "breton", "british",
57 "bulgarian", "canadian", "canadien", "catalan", "croatian", "czech", "danish",
58 "dutch", "english", "esperanto", "estonian", "finnish", "francais", "french",
59 "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
60 "hebrew", "icelandic", "irish", "italian", "lsorbian", "magyar", "naustrian",
61 "ngerman", "ngermanb", "norsk", "nynorsk", "polish", "portuges", "romanian",
62 "russian", "russianb", "scottish", "serbian", "slovak", "slovene", "spanish",
63 "swedish", "thai", "turkish", "ukraineb", "ukrainian", "usorbian", "welsh", 0};
64
65 //note this when updating to lyxformat 305:
66 //bahasai, indonesian, and indon = equal to bahasa
67 //malay, and meyalu = equal to bahasam
68
69 const char * const known_french_languages[] = {"french", "frenchb", "francais",
70                                                 "frenchle", "frenchpro", 0};
71 const char * const known_german_languages[] = {"german", "germanb",     "ngerman",
72                                                 "ngermanb", 0};
73 const char * const known_russian_languages[] = {"russian", "russianb", 0};
74 const char * const known_ukrainian_languages[] = {"ukrainian", "ukraineb", 0};
75
76 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
77
78 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
79 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
80
81 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
82 "helvet", "lmss", 0};
83
84 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
85 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
86 "newcent", 0};
87
88 // some ugly stuff
89 ostringstream h_preamble;
90 string h_textclass               = "article";
91 string h_options                 = string();
92 string h_language                = "english";
93 string h_inputencoding           = "auto";
94 string h_font_roman              = "default";
95 string h_font_sans               = "default";
96 string h_font_typewriter         = "default";
97 string h_font_default_family     = "default";
98 string h_font_sc                 = "false";
99 string h_font_osf                = "false";
100 string h_font_sf_scale           = "100";
101 string h_font_tt_scale           = "100";
102 string h_graphics                = "default";
103 string h_paperfontsize           = "default";
104 string h_spacing                 = "single";
105 string h_papersize               = "default";
106 string h_use_geometry            = "false";
107 string h_use_amsmath             = "0";
108 string h_cite_engine             = "basic";
109 string h_use_bibtopic            = "false";
110 string h_paperorientation        = "portrait";
111 string h_secnumdepth             = "3";
112 string h_tocdepth                = "3";
113 string h_paragraph_separation    = "indent";
114 string h_defskip                 = "medskip";
115 string h_quotes_language         = "english";
116 string h_papercolumns            = "1";
117 string h_papersides              = string();
118 string h_paperpagestyle          = "default";
119 string h_tracking_changes        = "false";
120 string h_output_changes          = "false";
121
122
123 void handle_opt(vector<string> & opts, char const * const * what, string & target)
124 {
125         if (opts.empty())
126                 return;
127
128         // the last language option is the document language (for babel and LyX)
129         // the last size option is the document font size
130         vector<string>::iterator it;
131         vector<string>::iterator position = opts.begin();
132         for (; *what; ++what) {
133                 it = find(opts.begin(), opts.end(), *what);
134                 if (it != opts.end()) {
135                         if (it >= position) {
136                                 target = *what;
137                                 position = it;
138                         }
139                         // remove found options from the list
140                         opts.erase(it);
141                 }
142         }
143 }
144
145
146 /*!
147  * Split a package options string (keyval format) into a vector.
148  * Example input:
149  *   authorformat=smallcaps,
150  *   commabeforerest,
151  *   titleformat=colonsep,
152  *   bibformat={tabular,ibidem,numbered}
153  */
154 vector<string> split_options(string const & input)
155 {
156         vector<string> options;
157         string option;
158         Parser p(input);
159         while (p.good()) {
160                 Token const & t = p.get_token();
161                 if (t.asInput() == ",") {
162                         options.push_back(trim(option));
163                         option.erase();
164                 } else if (t.asInput() == "=") {
165                         option += '=';
166                         p.skip_spaces(true);
167                         if (p.next_token().asInput() == "{")
168                                 option += '{' + p.getArg('{', '}') + '}';
169                 } else if (t.cat() != catSpace)
170                         option += t.asInput();
171         }
172
173         if (!option.empty())
174                 options.push_back(trim(option));
175
176         return options;
177 }
178
179
180 /*!
181  * Add package \p name with options \p options to used_packages.
182  * Remove options from \p options that we don't want to output.
183  */
184 void add_package(string const & name, vector<string> & options)
185 {
186         // every package inherits the global options
187         if (used_packages.find(name) == used_packages.end())
188                 used_packages[name] = split_options(h_options);
189
190         vector<string> & v = used_packages[name];
191         v.insert(v.end(), options.begin(), options.end());
192         if (name == "jurabib") {
193                 // Don't output the order argument (see the cite command
194                 // handling code in text.cpp).
195                 vector<string>::iterator end =
196                         remove(options.begin(), options.end(), "natbiborder");
197                 end = remove(options.begin(), end, "jurabiborder");
198                 options.erase(end, options.end());
199         }
200 }
201
202
203 void handle_package(string const & name, string const & opts)
204 {
205         vector<string> options = split_options(opts);
206         add_package(name, options);
207         size_t pos;
208         string scale;
209
210         // roman fonts
211         if (is_known(name, known_roman_fonts))
212                 h_font_roman = name;
213         if (name == "fourier") {
214                 h_font_roman = "utopia";
215                 // when font uses real small capitals
216                 if (opts == "expert")
217                         h_font_sc = "true";
218         }
219         if (name == "mathpazo")
220                 h_font_roman = "palatino";
221         if (name == "mathptmx")
222                 h_font_roman = "times";
223         // sansserif fonts
224         if (is_known(name, known_sans_fonts)) {
225                 h_font_sans = name;
226                 if (!opts.empty()) {
227                         scale = opts;
228                         // the option is in the form "scaled=0.9"
229                         // therefore cut of before the "="
230                         pos = scale.find("=");
231                         if (pos != string::npos) { 
232                                 scale.erase(0, pos + 1);
233                                 if (isStrDbl(scale)) {
234                                         // LyX needs the scale as integer, therfore multiply by 100
235                                         scale = convert<string>(100 * convert<double>(scale));
236                                         h_font_sf_scale = scale;
237                                 }
238                         }
239                 }
240         }
241         // typewriter fonts
242         if (is_known(name, known_typewriter_fonts)) {
243                 h_font_typewriter = name;
244                 if (!opts.empty()) {
245                         scale = opts;
246                         // the option is in the form "scaled=0.9"
247                         // therefore cut of before the "="
248                         pos = scale.find("=");
249                         if (pos != string::npos) { 
250                                 scale.erase(0, pos + 1);
251                                 if (isStrDbl(scale)) {
252                                         // LyX needs the scale as integer, therfore multiply by 100
253                                         scale = convert<string>(100 * convert<double>(scale));
254                                         h_font_tt_scale = scale;
255                                 }
256                         }
257                 }
258         }
259         // font uses old-style figure
260         if (name == "eco")
261                 h_font_osf = "true";
262
263         else if (name == "amsmath" || name == "amssymb")
264                 h_use_amsmath = "1";
265         else if (name == "babel")
266                 ; // ignore this
267         else if (name == "fontenc")
268                 ; // ignore this
269         else if (name == "inputenc") {
270                 // only set when there is not more than one inputenc option
271                 // therefore check for the "," character
272                 if (opts.find(",") == string::npos)
273                         h_inputencoding = opts;
274                 options.clear();
275         } else if (name == "makeidx")
276                 ; // ignore this
277         else if (name == "verbatim")
278                 ; // ignore this
279         else if (name == "graphicx")
280                 ; // ignore this
281         else if (is_known(name, known_languages)) {
282                 if (is_known(name, known_french_languages))
283                         h_language = "french";
284                 else if (is_known(name, known_german_languages))
285                         h_language = "ngerman";
286                 else if (is_known(name, known_russian_languages))
287                         h_language = "russian";
288                 else if (is_known(name, known_ukrainian_languages))
289                         h_language = "ukrainian";
290                 else
291                         h_language = name;
292                 h_quotes_language = h_language;
293
294         } else if (name == "natbib") {
295                 h_cite_engine = "natbib_authoryear";
296                 vector<string>::iterator it =
297                         find(options.begin(), options.end(), "authoryear");
298                 if (it != options.end())
299                         options.erase(it);
300                 else {
301                         it = find(options.begin(), options.end(), "numbers");
302                         if (it != options.end()) {
303                                 h_cite_engine = "natbib_numerical";
304                                 options.erase(it);
305                         }
306                 }
307         } else if (name == "jurabib") {
308                 h_cite_engine = "jurabib";
309         } else if (options.empty())
310                 h_preamble << "\\usepackage{" << name << "}\n";
311         else {
312                 h_preamble << "\\usepackage[" << opts << "]{" << name << "}\n";
313                 options.clear();
314         }
315
316         // We need to do something with the options...
317         if (!options.empty())
318                 cerr << "Ignoring options '" << join(options, ",")
319                      << "' of package " << name << '.' << endl;
320 }
321
322
323
324 void end_preamble(ostream & os, TextClass const & /*textclass*/)
325 {
326         os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
327            << "\\lyxformat 247\n"
328            << "\\begin_document\n"
329            << "\\begin_header\n"
330            << "\\textclass " << h_textclass << "\n";
331         if (!h_preamble.str().empty())
332                 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
333         if (!h_options.empty())
334                 os << "\\options " << h_options << "\n";
335         os << "\\language " << h_language << "\n"
336            << "\\inputencoding " << h_inputencoding << "\n"
337            << "\\font_roman " << h_font_roman << "\n"
338            << "\\font_sans " << h_font_sans << "\n"
339            << "\\font_typewriter " << h_font_typewriter << "\n"
340            << "\\font_default_family " << h_font_default_family << "\n"
341            << "\\font_sc " << h_font_sc << "\n"
342            << "\\font_osf " << h_font_osf << "\n"
343            << "\\font_sf_scale " << h_font_sf_scale << "\n"
344            << "\\font_tt_scale " << h_font_tt_scale << "\n"
345            << "\\graphics " << h_graphics << "\n"
346            << "\\paperfontsize " << h_paperfontsize << "\n"
347            << "\\spacing " << h_spacing << "\n"
348            << "\\papersize " << h_papersize << "\n"
349            << "\\use_geometry " << h_use_geometry << "\n"
350            << "\\use_amsmath " << h_use_amsmath << "\n"
351            << "\\cite_engine " << h_cite_engine << "\n"
352            << "\\use_bibtopic " << h_use_bibtopic << "\n"
353            << "\\paperorientation " << h_paperorientation << "\n"
354            << "\\secnumdepth " << h_secnumdepth << "\n"
355            << "\\tocdepth " << h_tocdepth << "\n"
356            << "\\paragraph_separation " << h_paragraph_separation << "\n"
357            << "\\defskip " << h_defskip << "\n"
358            << "\\quotes_language " << h_quotes_language << "\n"
359            << "\\papercolumns " << h_papercolumns << "\n"
360            << "\\papersides " << h_papersides << "\n"
361            << "\\paperpagestyle " << h_paperpagestyle << "\n"
362            << "\\tracking_changes " << h_tracking_changes << "\n"
363            << "\\output_changes " << h_output_changes << "\n"
364            << "\\end_header\n\n"
365            << "\\begin_body\n";
366         // clear preamble for subdocuments
367         h_preamble.str("");
368 }
369
370 } // anonymous namespace
371
372 TextClass const parse_preamble(Parser & p, ostream & os, string const & forceclass)
373 {
374         // initialize fixed types
375         special_columns['D'] = 3;
376         bool is_full_document = false;
377
378         // determine whether this is a full document or a fragment for inclusion
379         while (p.good()) {
380                 Token const & t = p.get_token();
381
382                 if (t.cat() == catEscape && t.cs() == "documentclass") {
383                         is_full_document = true;
384                         break;
385                 }
386         }
387         p.reset();
388
389         while (is_full_document && p.good()) {
390                 Token const & t = p.get_token();
391
392 #ifdef FILEDEBUG
393                 cerr << "t: " << t << "\n";
394 #endif
395
396                 //
397                 // cat codes
398                 //
399                 if (t.cat() == catLetter ||
400                           t.cat() == catSuper ||
401                           t.cat() == catSub ||
402                           t.cat() == catOther ||
403                           t.cat() == catMath ||
404                           t.cat() == catActive ||
405                           t.cat() == catBegin ||
406                           t.cat() == catEnd ||
407                           t.cat() == catAlign ||
408                           t.cat() == catParameter)
409                 h_preamble << t.character();
410
411                 else if (t.cat() == catSpace || t.cat() == catNewline)
412                         h_preamble << t.asInput();
413
414                 else if (t.cat() == catComment)
415                         h_preamble << t.asInput();
416
417                 else if (t.cs() == "pagestyle")
418                         h_paperpagestyle = p.verbatim_item();
419
420                 else if (t.cs() == "makeatletter") {
421                         p.setCatCode('@', catLetter);
422                         h_preamble << "\\makeatletter";
423                 }
424
425                 else if (t.cs() == "makeatother") {
426                         p.setCatCode('@', catOther);
427                         h_preamble << "\\makeatother";
428                 }
429
430                 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
431                             || t.cs() == "providecommand") {
432                         bool star = false;
433                         if (p.next_token().character() == '*') {
434                                 p.get_token();
435                                 star = true;
436                         }
437                         string const name = p.verbatim_item();
438                         string const opt1 = p.getOpt();
439                         string const opt2 = p.getFullOpt();
440                         string const body = p.verbatim_item();
441                         // font settings
442                         if (name == "\\rmdefault")
443                                 if (is_known(body, known_roman_fonts))
444                                         h_font_roman = body;
445
446                         if (name == "\\sfdefault")
447                                 if (is_known(body, known_sans_fonts))
448                                         h_font_sans = body;
449
450                         if (name == "\\ttdefault")
451                                 if (is_known(body, known_typewriter_fonts))
452                                         h_font_typewriter = body;
453
454                         if (name == "\\familydefault") {
455                                 string family = body;
456                                 // remove leading "\"
457                                 h_font_default_family = family.erase(0,1);
458                         }
459                         // only non-lyxspecific stuff
460                         if (   name != "\\noun"
461                             && name != "\\tabularnewline"
462                             && name != "\\LyX"
463                             && name != "\\lyxline"
464                             && name != "\\lyxaddress"
465                             && name != "\\lyxrightaddress"
466                             && name != "\\lyxdot"
467                             && name != "\\boldsymbol"
468                             && name != "\\lyxarrow"
469                             && name != "\\rmdefault"
470                             && name != "\\sfdefault"
471                             && name != "\\ttdefault"
472                             && name != "\\familydefault") {
473                                 ostringstream ss;
474                                 ss << '\\' << t.cs();
475                                 if (star)
476                                         ss << '*';
477                                 ss << '{' << name << '}' << opt1 << opt2
478                                    << '{' << body << "}";
479                                 h_preamble << ss.str();
480
481                                 // Add the command to the known commands
482                                 add_known_command(name, opt1, !opt2.empty());
483 /*
484                                 ostream & out = in_preamble ? h_preamble : os;
485                                 out << "\\" << t.cs() << "{" << name << "}"
486                                     << opts << "{" << body << "}";
487 */
488                         }
489                 }
490
491                 else if (t.cs() == "documentclass") {
492                         vector<string> opts = split_options(p.getArg('[', ']'));
493                         handle_opt(opts, known_languages, h_language);
494                         if (is_known(h_language, known_french_languages))
495                                 h_language = "french";
496                         else if (is_known(h_language, known_german_languages))
497                                 h_language = "ngerman";
498                         else if (is_known(h_language, known_russian_languages))
499                                 h_language = "russian";
500                         else if (is_known(h_language, known_ukrainian_languages))
501                                 h_language = "ukrainian";
502                         handle_opt(opts, known_fontsizes, h_paperfontsize);
503                         // delete "pt" at the end
504                         string::size_type i = h_paperfontsize.find("pt");
505                         if (i != string::npos)
506                                 h_paperfontsize.erase(i);
507                         h_quotes_language = h_language;
508                         h_options = join(opts, ",");
509                         h_textclass = p.getArg('{', '}');
510                 }
511
512                 else if (t.cs() == "usepackage") {
513                         string const options = p.getArg('[', ']');
514                         string const name = p.getArg('{', '}');
515                         if (options.empty() && name.find(',')) {
516                                 vector<string> vecnames;
517                                 split(name, vecnames, ',');
518                                 vector<string>::const_iterator it  = vecnames.begin();
519                                 vector<string>::const_iterator end = vecnames.end();
520                                 for (; it != end; ++it)
521                                         handle_package(trim(*it), string());
522                         } else {
523                                 handle_package(name, options);
524                         }
525                 }
526
527                 else if (t.cs() == "newenvironment") {
528                         string const name = p.getArg('{', '}');
529                         ostringstream ss;
530                         ss << "\\newenvironment{" << name << "}";
531                         ss << p.getOpt();
532                         ss << p.getOpt();
533                         ss << '{' << p.verbatim_item() << '}';
534                         ss << '{' << p.verbatim_item() << '}';
535                         if (name != "lyxcode" && name != "lyxlist" &&
536                             name != "lyxrightadress" &&
537                             name != "lyxaddress" && name != "lyxgreyedout")
538                                 h_preamble << ss.str();
539                 }
540
541                 else if (t.cs() == "def") {
542                         string name = p.get_token().cs();
543                         while (p.next_token().cat() != catBegin)
544                                 name += p.get_token().asString();
545                         h_preamble << "\\def\\" << name << '{'
546                                    << p.verbatim_item() << "}";
547                 }
548
549                 else if (t.cs() == "newcolumntype") {
550                         string const name = p.getArg('{', '}');
551                         trim(name);
552                         int nargs = 0;
553                         string opts = p.getOpt();
554                         if (!opts.empty()) {
555                                 istringstream is(string(opts, 1));
556                                 //cerr << "opt: " << is.str() << "\n";
557                                 is >> nargs;
558                         }
559                         special_columns[name[0]] = nargs;
560                         h_preamble << "\\newcolumntype{" << name << "}";
561                         if (nargs)
562                                 h_preamble << "[" << nargs << "]";
563                         h_preamble << "{" << p.verbatim_item() << "}";
564                 }
565
566                 else if (t.cs() == "setcounter") {
567                         string const name = p.getArg('{', '}');
568                         string const content = p.getArg('{', '}');
569                         if (name == "secnumdepth")
570                                 h_secnumdepth = content;
571                         else if (name == "tocdepth")
572                                 h_tocdepth = content;
573                         else
574                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
575                 }
576
577                 else if (t.cs() == "setlength") {
578                         string const name = p.verbatim_item();
579                         string const content = p.verbatim_item();
580                         // Is this correct?
581                         if (name == "parskip")
582                                 h_paragraph_separation = "skip";
583                         else if (name == "parindent")
584                                 h_paragraph_separation = "skip";
585                         else
586                                 h_preamble << "\\setlength{" << name << "}{" << content << "}";
587                 }
588
589                 else if (t.cs() == "begin") {
590                         string const name = p.getArg('{', '}');
591                         if (name == "document")
592                                 break;
593                         h_preamble << "\\begin{" << name << "}";
594                 }
595
596                 else if (t.cs() == "jurabibsetup") {
597                         vector<string> jurabibsetup =
598                                 split_options(p.getArg('{', '}'));
599                         // add jurabibsetup to the jurabib package options
600                         add_package("jurabib", jurabibsetup);
601                         if (!jurabibsetup.empty()) {
602                                 h_preamble << "\\jurabibsetup{"
603                                            << join(jurabibsetup, ",") << '}';
604                         }
605                 }
606
607                 else if (!t.cs().empty())
608                         h_preamble << '\\' << t.cs();
609         }
610         p.skip_spaces();
611
612         // Force textclass if the user wanted it
613         if (!forceclass.empty())
614                 h_textclass = forceclass;
615         if (noweb_mode && !lyx::support::prefixIs(h_textclass, "literate-"))
616                 h_textclass.insert(0, "literate-");
617         FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
618         if (layoutfilename.empty()) {
619                 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
620                 exit(1);
621         }
622         TextClass textclass;
623         textclass.read(layoutfilename);
624         if (h_papersides.empty()) {
625                 ostringstream ss;
626                 ss << textclass.sides();
627                 h_papersides = ss.str();
628         }
629         end_preamble(os, textclass);
630         return textclass;
631 }
632
633 // }])
634
635
636 } // namespace lyx