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