3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
18 #include "LayoutFile.h"
21 #include "TextClass.h"
23 #include "support/convert.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
36 using namespace lyx::support;
40 // special columntypes
41 extern map<char, int> special_columns;
43 map<string, vector<string> > used_packages;
45 // needed to handle encodings with babel
46 bool one_language = true;
48 // to avoid that the babel options overwrite the documentclass options
49 bool documentclass_language;
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};
63 //note this when updating to lyxformat 305:
64 //bahasai, indonesian, and indon = equal to bahasa
65 //malay, and meyalu = equal to bahasam
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};
74 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
76 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
77 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
79 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
82 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
83 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
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";
121 void handle_opt(vector<string> & opts, char const * const * what, string & target)
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) {
143 void delete_opt(vector<string> & opts, char const * const * what)
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())
161 * Split a package options string (keyval format) into a vector.
163 * authorformat=smallcaps,
165 * titleformat=colonsep,
166 * bibformat={tabular,ibidem,numbered}
168 vector<string> split_options(string const & input)
170 vector<string> options;
174 Token const & t = p.get_token();
175 if (t.asInput() == ",") {
176 options.push_back(trim(option));
178 } else if (t.asInput() == "=") {
181 if (p.next_token().asInput() == "{")
182 option += '{' + p.getArg('{', '}') + '}';
183 } else if (t.cat() != catSpace)
184 option += t.asInput();
188 options.push_back(trim(option));
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.
198 void add_package(string const & name, vector<string> & options)
200 // every package inherits the global options
201 if (used_packages.find(name) == used_packages.end())
202 used_packages[name] = split_options(h_options);
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());
217 // Given is a string like "scaled=0.9", return 0.9 * 100
218 string const scale_as_percentage(string const & scale)
220 string::size_type pos = scale.find('=');
221 if (pos != string::npos) {
222 string value = scale.substr(pos + 1);
224 return convert<string>(100 * convert<double>(value));
226 // If the input string didn't match our expectations.
227 // return the default value "100"
232 void handle_package(string const & name, string const & opts)
234 vector<string> options = split_options(opts);
235 add_package(name, options);
239 if (is_known(name, known_roman_fonts))
241 if (name == "fourier") {
242 h_font_roman = "utopia";
243 // when font uses real small capitals
244 if (opts == "expert")
247 if (name == "mathpazo")
248 h_font_roman = "palatino";
249 if (name == "mathptmx")
250 h_font_roman = "times";
252 if (is_known(name, known_sans_fonts)) {
256 h_font_sf_scale = scale_as_percentage(scale);
260 if (is_known(name, known_typewriter_fonts)) {
261 h_font_typewriter = name;
264 h_font_tt_scale = scale_as_percentage(scale);
267 // font uses old-style figure
271 else if (name == "amsmath" || name == "amssymb")
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";
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;
301 else if (name == "fontenc")
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 h_inputencoding = opts;
310 } else if (name == "makeidx")
312 else if (name == "verbatim")
314 else if (name == "graphicx")
316 else if (is_known(name, known_languages)) {
317 if (is_known(name, known_french_languages))
318 h_language = "french";
319 else if (is_known(name, known_german_languages))
320 h_language = "german";
321 else if (is_known(name, known_ngerman_languages))
322 h_language = "ngerman";
323 else if (is_known(name, known_russian_languages))
324 h_language = "russian";
325 else if (is_known(name, known_ukrainian_languages))
326 h_language = "ukrainian";
329 h_quotes_language = h_language;
331 } else if (name == "natbib") {
332 h_cite_engine = "natbib_authoryear";
333 vector<string>::iterator it =
334 find(options.begin(), options.end(), "authoryear");
335 if (it != options.end())
338 it = find(options.begin(), options.end(), "numbers");
339 if (it != options.end()) {
340 h_cite_engine = "natbib_numerical";
344 } else if (name == "jurabib") {
345 h_cite_engine = "jurabib";
346 } else if (options.empty())
347 h_preamble << "\\usepackage{" << name << "}\n";
349 h_preamble << "\\usepackage[" << opts << "]{" << name << "}\n";
353 // We need to do something with the options...
354 if (!options.empty())
355 cerr << "Ignoring options '" << join(options, ",")
356 << "' of package " << name << '.' << endl;
361 void end_preamble(ostream & os, TextClass const & /*textclass*/)
363 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
364 << "\\lyxformat 247\n"
365 << "\\begin_document\n"
366 << "\\begin_header\n"
367 << "\\textclass " << h_textclass << "\n";
368 if (!h_preamble.str().empty())
369 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
370 if (!h_options.empty())
371 os << "\\options " << h_options << "\n";
372 os << "\\language " << h_language << "\n"
373 << "\\inputencoding " << h_inputencoding << "\n"
374 << "\\font_roman " << h_font_roman << "\n"
375 << "\\font_sans " << h_font_sans << "\n"
376 << "\\font_typewriter " << h_font_typewriter << "\n"
377 << "\\font_default_family " << h_font_default_family << "\n"
378 << "\\font_sc " << h_font_sc << "\n"
379 << "\\font_osf " << h_font_osf << "\n"
380 << "\\font_sf_scale " << h_font_sf_scale << "\n"
381 << "\\font_tt_scale " << h_font_tt_scale << "\n"
382 << "\\graphics " << h_graphics << "\n"
383 << "\\paperfontsize " << h_paperfontsize << "\n"
384 << "\\spacing " << h_spacing << "\n"
385 << "\\papersize " << h_papersize << "\n"
386 << "\\use_geometry " << h_use_geometry << "\n"
387 << "\\use_amsmath " << h_use_amsmath << "\n"
388 << "\\cite_engine " << h_cite_engine << "\n"
389 << "\\use_bibtopic " << h_use_bibtopic << "\n"
390 << "\\paperorientation " << h_paperorientation << "\n"
391 << "\\secnumdepth " << h_secnumdepth << "\n"
392 << "\\tocdepth " << h_tocdepth << "\n"
393 << "\\paragraph_separation " << h_paragraph_separation << "\n"
394 << "\\defskip " << h_defskip << "\n"
395 << "\\quotes_language " << h_quotes_language << "\n"
396 << "\\papercolumns " << h_papercolumns << "\n"
397 << "\\papersides " << h_papersides << "\n"
398 << "\\paperpagestyle " << h_paperpagestyle << "\n"
399 << "\\tracking_changes " << h_tracking_changes << "\n"
400 << "\\output_changes " << h_output_changes << "\n"
401 << "\\end_header\n\n"
403 // clear preamble for subdocuments
407 } // anonymous namespace
409 void parse_preamble(Parser & p, ostream & os,
410 string const & forceclass, TeX2LyXDocClass & tc)
412 // initialize fixed types
413 special_columns['D'] = 3;
414 bool is_full_document = false;
416 // determine whether this is a full document or a fragment for inclusion
418 Token const & t = p.get_token();
420 if (t.cat() == catEscape && t.cs() == "documentclass") {
421 is_full_document = true;
427 while (is_full_document && p.good()) {
428 Token const & t = p.get_token();
431 cerr << "t: " << t << "\n";
437 if (t.cat() == catLetter ||
438 t.cat() == catSuper ||
440 t.cat() == catOther ||
441 t.cat() == catMath ||
442 t.cat() == catActive ||
443 t.cat() == catBegin ||
445 t.cat() == catAlign ||
446 t.cat() == catParameter)
447 h_preamble << t.character();
449 else if (t.cat() == catSpace || t.cat() == catNewline)
450 h_preamble << t.asInput();
452 else if (t.cat() == catComment)
453 h_preamble << t.asInput();
455 else if (t.cs() == "pagestyle")
456 h_paperpagestyle = p.verbatim_item();
458 else if (t.cs() == "makeatletter") {
459 p.setCatCode('@', catLetter);
462 else if (t.cs() == "makeatother") {
463 p.setCatCode('@', catOther);
466 else if (t.cs() == "newcommand"
467 || t.cs() == "renewcommand"
468 || t.cs() == "providecommand"
469 || t.cs() == "newlyxcommand") {
471 if (p.next_token().character() == '*') {
475 string const name = p.verbatim_item();
476 string const opt1 = p.getOpt();
477 string const opt2 = p.getFullOpt();
478 string const body = p.verbatim_item();
480 if (name == "\\rmdefault")
481 if (is_known(body, known_roman_fonts))
484 if (name == "\\sfdefault")
485 if (is_known(body, known_sans_fonts))
488 if (name == "\\ttdefault")
489 if (is_known(body, known_typewriter_fonts))
490 h_font_typewriter = body;
492 if (name == "\\familydefault") {
493 string family = body;
494 // remove leading "\"
495 h_font_default_family = family.erase(0,1);
497 // only non-lyxspecific stuff
498 if ( name != "\\noun"
499 && name != "\\tabularnewline"
501 && name != "\\lyxline"
502 && name != "\\lyxaddress"
503 && name != "\\lyxrightaddress"
504 && name != "\\lyxdot"
505 && name != "\\boldsymbol"
506 && name != "\\lyxarrow"
507 && name != "\\rmdefault"
508 && name != "\\sfdefault"
509 && name != "\\ttdefault"
510 && name != "\\familydefault") {
512 ss << '\\' << t.cs();
515 ss << '{' << name << '}' << opt1 << opt2
516 << '{' << body << "}";
517 h_preamble << ss.str();
519 // Add the command to the known commands
520 add_known_command(name, opt1, !opt2.empty());
522 ostream & out = in_preamble ? h_preamble : os;
523 out << "\\" << t.cs() << "{" << name << "}"
524 << opts << "{" << body << "}";
529 else if (t.cs() == "documentclass") {
530 vector<string> opts = split_options(p.getArg('[', ']'));
531 handle_opt(opts, known_fontsizes, h_paperfontsize);
532 delete_opt(opts, known_fontsizes);
533 // delete "pt" at the end
534 string::size_type i = h_paperfontsize.find("pt");
535 if (i != string::npos)
536 h_paperfontsize.erase(i);
537 // to avoid that the babel options overwrite the documentclass options
538 documentclass_language = false;
539 handle_opt(opts, known_languages, h_language);
540 delete_opt(opts, known_languages);
541 if (is_known(h_language, known_french_languages))
542 h_language = "french";
543 else if (is_known(h_language, known_german_languages))
544 h_language = "german";
545 else if (is_known(h_language, known_ngerman_languages))
546 h_language = "ngerman";
547 else if (is_known(h_language, known_russian_languages))
548 h_language = "russian";
549 else if (is_known(h_language, known_ukrainian_languages))
550 h_language = "ukrainian";
551 h_quotes_language = h_language;
552 h_options = join(opts, ",");
553 h_textclass = p.getArg('{', '}');
556 else if (t.cs() == "usepackage") {
557 string const options = p.getArg('[', ']');
558 string const name = p.getArg('{', '}');
559 if (options.empty() && name.find(',')) {
560 vector<string> vecnames;
561 split(name, vecnames, ',');
562 vector<string>::const_iterator it = vecnames.begin();
563 vector<string>::const_iterator end = vecnames.end();
564 for (; it != end; ++it)
565 handle_package(trim(*it), string());
567 handle_package(name, options);
571 else if (t.cs() == "newenvironment") {
572 string const name = p.getArg('{', '}');
574 ss << "\\newenvironment{" << name << "}";
577 ss << '{' << p.verbatim_item() << '}';
578 ss << '{' << p.verbatim_item() << '}';
579 if (name != "lyxcode" && name != "lyxlist" &&
580 name != "lyxrightadress" &&
581 name != "lyxaddress" && name != "lyxgreyedout")
582 h_preamble << ss.str();
585 else if (t.cs() == "def") {
586 string name = p.get_token().cs();
587 while (p.next_token().cat() != catBegin)
588 name += p.get_token().asString();
589 h_preamble << "\\def\\" << name << '{'
590 << p.verbatim_item() << "}";
593 else if (t.cs() == "newcolumntype") {
594 string const name = p.getArg('{', '}');
597 string opts = p.getOpt();
599 istringstream is(string(opts, 1));
602 special_columns[name[0]] = nargs;
603 h_preamble << "\\newcolumntype{" << name << "}";
605 h_preamble << "[" << nargs << "]";
606 h_preamble << "{" << p.verbatim_item() << "}";
609 else if (t.cs() == "setcounter") {
610 string const name = p.getArg('{', '}');
611 string const content = p.getArg('{', '}');
612 if (name == "secnumdepth")
613 h_secnumdepth = content;
614 else if (name == "tocdepth")
615 h_tocdepth = content;
617 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
620 else if (t.cs() == "setlength") {
621 string const name = p.verbatim_item();
622 string const content = p.verbatim_item();
624 if (name == "parskip")
625 h_paragraph_separation = "skip";
626 else if (name == "parindent")
627 h_paragraph_separation = "skip";
629 h_preamble << "\\setlength{" << name << "}{" << content << "}";
632 else if (t.cs() == "begin") {
633 string const name = p.getArg('{', '}');
634 if (name == "document")
636 h_preamble << "\\begin{" << name << "}";
639 else if (t.cs() == "jurabibsetup") {
640 vector<string> jurabibsetup =
641 split_options(p.getArg('{', '}'));
642 // add jurabibsetup to the jurabib package options
643 add_package("jurabib", jurabibsetup);
644 if (!jurabibsetup.empty()) {
645 h_preamble << "\\jurabibsetup{"
646 << join(jurabibsetup, ",") << '}';
650 else if (!t.cs().empty())
651 h_preamble << '\\' << t.cs();
655 // Force textclass if the user wanted it
656 if (!forceclass.empty())
657 h_textclass = forceclass;
658 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
659 h_textclass.insert(0, "literate-");
660 FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
661 if (layoutfilename.empty()) {
662 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
665 tc.read(layoutfilename);
666 if (h_papersides.empty()) {
669 h_papersides = ss.str();
671 end_preamble(os, tc);