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))
242 if (name == "fourier") {
243 h_font_roman = "utopia";
244 // when font uses real small capitals
245 if (opts == "expert")
248 if (name == "mathpazo")
249 h_font_roman = "palatino";
251 if (name == "mathptmx")
252 h_font_roman = "times";
255 if (is_known(name, known_sans_fonts)) {
259 h_font_sf_scale = scale_as_percentage(scale);
263 if (is_known(name, known_typewriter_fonts)) {
264 h_font_typewriter = name;
267 h_font_tt_scale = scale_as_percentage(scale);
270 // font uses old-style figure
274 else if (name == "amsmath" || name == "amssymb")
277 else if (name == "babel" && !opts.empty()) {
278 // check if more than one option was used - used later for inputenc
279 // in case inputenc is parsed before babel, set the encoding to auto
280 if (options.begin() != options.end() - 1) {
281 one_language = false;
282 h_inputencoding = "auto";
284 // only set the document language when there was not already one set
285 // via the documentclass options
286 // babel takes the the last language given in the documentclass options
287 // as document language. If there is no such language option, the last
288 // option of its \usepackage call is used.
289 if (documentclass_language == false) {
290 handle_opt(options, known_languages, h_language);
291 delete_opt(options, known_languages);
292 if (is_known(h_language, known_french_languages))
293 h_language = "french";
294 else if (is_known(h_language, known_german_languages))
295 h_language = "german";
296 else if (is_known(h_language, known_ngerman_languages))
297 h_language = "ngerman";
298 else if (is_known(h_language, known_russian_languages))
299 h_language = "russian";
300 else if (is_known(h_language, known_ukrainian_languages))
301 h_language = "ukrainian";
302 h_quotes_language = h_language;
306 else if (name == "fontenc")
309 else if (name == "inputenc") {
310 // only set when there is not more than one inputenc option
311 // therefore check for the "," character
312 // also only set when there is not more then one babel language option
313 if (opts.find(",") == string::npos && one_language == true)
315 //change ascii to auto to be in the unicode range, see
316 //http://bugzilla.lyx.org/show_bug.cgi?id=4719
317 h_inputencoding = "auto";
319 h_inputencoding = opts;
322 } else if (name == "makeidx")
325 else if (name == "verbatim")
328 else if (name == "color")
329 // with the following command this package is only loaded when needed for
330 // undefined colors, since we only support the predefined colors
331 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
333 else if (name == "graphicx")
335 else if (name == "setspace")
337 else if (is_known(name, known_languages)) {
338 if (is_known(name, known_french_languages))
339 h_language = "french";
340 else if (is_known(name, known_german_languages))
341 h_language = "german";
342 else if (is_known(name, known_ngerman_languages))
343 h_language = "ngerman";
344 else if (is_known(name, known_russian_languages))
345 h_language = "russian";
346 else if (is_known(name, known_ukrainian_languages))
347 h_language = "ukrainian";
350 h_quotes_language = h_language;
352 } else if (name == "natbib") {
353 h_cite_engine = "natbib_authoryear";
354 vector<string>::iterator it =
355 find(options.begin(), options.end(), "authoryear");
356 if (it != options.end())
359 it = find(options.begin(), options.end(), "numbers");
360 if (it != options.end()) {
361 h_cite_engine = "natbib_numerical";
365 } else if (name == "jurabib") {
366 h_cite_engine = "jurabib";
368 } else if (options.empty())
369 h_preamble << "\\usepackage{" << name << "}\n";
371 h_preamble << "\\usepackage[" << opts << "]{" << name << "}\n";
375 // We need to do something with the options...
376 if (!options.empty())
377 cerr << "Ignoring options '" << join(options, ",")
378 << "' of package " << name << '.' << endl;
383 void end_preamble(ostream & os, TextClass const & /*textclass*/)
385 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
386 << "\\lyxformat 247\n"
387 << "\\begin_document\n"
388 << "\\begin_header\n"
389 << "\\textclass " << h_textclass << "\n";
390 if (!h_preamble.str().empty())
391 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
392 if (!h_options.empty())
393 os << "\\options " << h_options << "\n";
394 os << "\\language " << h_language << "\n"
395 << "\\inputencoding " << h_inputencoding << "\n"
396 << "\\font_roman " << h_font_roman << "\n"
397 << "\\font_sans " << h_font_sans << "\n"
398 << "\\font_typewriter " << h_font_typewriter << "\n"
399 << "\\font_default_family " << h_font_default_family << "\n"
400 << "\\font_sc " << h_font_sc << "\n"
401 << "\\font_osf " << h_font_osf << "\n"
402 << "\\font_sf_scale " << h_font_sf_scale << "\n"
403 << "\\font_tt_scale " << h_font_tt_scale << "\n"
404 << "\\graphics " << h_graphics << "\n"
405 << "\\paperfontsize " << h_paperfontsize << "\n"
406 << "\\spacing " << h_spacing << "\n"
407 << "\\papersize " << h_papersize << "\n"
408 << "\\use_geometry " << h_use_geometry << "\n"
409 << "\\use_amsmath " << h_use_amsmath << "\n"
410 << "\\cite_engine " << h_cite_engine << "\n"
411 << "\\use_bibtopic " << h_use_bibtopic << "\n"
412 << "\\paperorientation " << h_paperorientation << "\n"
413 << "\\secnumdepth " << h_secnumdepth << "\n"
414 << "\\tocdepth " << h_tocdepth << "\n"
415 << "\\paragraph_separation " << h_paragraph_separation << "\n"
416 << "\\defskip " << h_defskip << "\n"
417 << "\\quotes_language " << h_quotes_language << "\n"
418 << "\\papercolumns " << h_papercolumns << "\n"
419 << "\\papersides " << h_papersides << "\n"
420 << "\\paperpagestyle " << h_paperpagestyle << "\n"
421 << "\\tracking_changes " << h_tracking_changes << "\n"
422 << "\\output_changes " << h_output_changes << "\n"
423 << "\\end_header\n\n"
425 // clear preamble for subdocuments
429 } // anonymous namespace
431 void parse_preamble(Parser & p, ostream & os,
432 string const & forceclass, TeX2LyXDocClass & tc)
434 // initialize fixed types
435 special_columns['D'] = 3;
436 bool is_full_document = false;
438 // determine whether this is a full document or a fragment for inclusion
440 Token const & t = p.get_token();
442 if (t.cat() == catEscape && t.cs() == "documentclass") {
443 is_full_document = true;
449 while (is_full_document && p.good()) {
450 Token const & t = p.get_token();
453 cerr << "t: " << t << "\n";
459 if (t.cat() == catLetter ||
460 t.cat() == catSuper ||
462 t.cat() == catOther ||
463 t.cat() == catMath ||
464 t.cat() == catActive ||
465 t.cat() == catBegin ||
467 t.cat() == catAlign ||
468 t.cat() == catParameter)
469 h_preamble << t.character();
471 else if (t.cat() == catSpace || t.cat() == catNewline)
472 h_preamble << t.asInput();
474 else if (t.cat() == catComment)
475 h_preamble << t.asInput();
477 else if (t.cs() == "pagestyle")
478 h_paperpagestyle = p.verbatim_item();
480 else if (t.cs() == "makeatletter") {
481 p.setCatCode('@', catLetter);
484 else if (t.cs() == "makeatother") {
485 p.setCatCode('@', catOther);
488 else if (t.cs() == "newcommand"
489 || t.cs() == "renewcommand"
490 || t.cs() == "providecommand"
491 || t.cs() == "newlyxcommand") {
493 if (p.next_token().character() == '*') {
497 string const name = p.verbatim_item();
498 string const opt1 = p.getOpt();
499 string const opt2 = p.getFullOpt();
500 string const body = p.verbatim_item();
502 if (name == "\\rmdefault")
503 if (is_known(body, known_roman_fonts))
506 if (name == "\\sfdefault")
507 if (is_known(body, known_sans_fonts))
510 if (name == "\\ttdefault")
511 if (is_known(body, known_typewriter_fonts))
512 h_font_typewriter = body;
514 if (name == "\\familydefault") {
515 string family = body;
516 // remove leading "\"
517 h_font_default_family = family.erase(0,1);
519 // only non-lyxspecific stuff
520 if ( name != "\\noun"
521 && name != "\\tabularnewline"
523 && name != "\\lyxline"
524 && name != "\\lyxaddress"
525 && name != "\\lyxrightaddress"
526 && name != "\\lyxdot"
527 && name != "\\boldsymbol"
528 && name != "\\lyxarrow"
529 && name != "\\rmdefault"
530 && name != "\\sfdefault"
531 && name != "\\ttdefault"
532 && name != "\\familydefault") {
534 ss << '\\' << t.cs();
537 ss << '{' << name << '}' << opt1 << opt2
538 << '{' << body << "}";
539 h_preamble << ss.str();
541 // Add the command to the known commands
542 add_known_command(name, opt1, !opt2.empty());
544 ostream & out = in_preamble ? h_preamble : os;
545 out << "\\" << t.cs() << "{" << name << "}"
546 << opts << "{" << body << "}";
551 else if (t.cs() == "documentclass") {
552 vector<string> opts = split_options(p.getArg('[', ']'));
553 handle_opt(opts, known_fontsizes, h_paperfontsize);
554 delete_opt(opts, known_fontsizes);
555 // delete "pt" at the end
556 string::size_type i = h_paperfontsize.find("pt");
557 if (i != string::npos)
558 h_paperfontsize.erase(i);
559 // to avoid that the babel options overwrite the documentclass options
560 documentclass_language = false;
561 handle_opt(opts, known_languages, h_language);
562 delete_opt(opts, known_languages);
563 if (is_known(h_language, known_french_languages))
564 h_language = "french";
565 else if (is_known(h_language, known_german_languages))
566 h_language = "german";
567 else if (is_known(h_language, known_ngerman_languages))
568 h_language = "ngerman";
569 else if (is_known(h_language, known_russian_languages))
570 h_language = "russian";
571 else if (is_known(h_language, known_ukrainian_languages))
572 h_language = "ukrainian";
573 h_quotes_language = h_language;
574 h_options = join(opts, ",");
575 h_textclass = p.getArg('{', '}');
578 else if (t.cs() == "usepackage") {
579 string const options = p.getArg('[', ']');
580 string const name = p.getArg('{', '}');
581 if (options.empty() && name.find(',')) {
582 vector<string> vecnames;
583 split(name, vecnames, ',');
584 vector<string>::const_iterator it = vecnames.begin();
585 vector<string>::const_iterator end = vecnames.end();
586 for (; it != end; ++it)
587 handle_package(trim(*it), string());
589 handle_package(name, options);
593 else if (t.cs() == "newenvironment") {
594 string const name = p.getArg('{', '}');
596 ss << "\\newenvironment{" << name << "}";
599 ss << '{' << p.verbatim_item() << '}';
600 ss << '{' << p.verbatim_item() << '}';
601 if (name != "lyxcode" && name != "lyxlist" &&
602 name != "lyxrightadress" &&
603 name != "lyxaddress" && name != "lyxgreyedout")
604 h_preamble << ss.str();
607 else if (t.cs() == "def") {
608 string name = p.get_token().cs();
609 while (p.next_token().cat() != catBegin)
610 name += p.get_token().asString();
611 h_preamble << "\\def\\" << name << '{'
612 << p.verbatim_item() << "}";
615 else if (t.cs() == "newcolumntype") {
616 string const name = p.getArg('{', '}');
619 string opts = p.getOpt();
621 istringstream is(string(opts, 1));
624 special_columns[name[0]] = nargs;
625 h_preamble << "\\newcolumntype{" << name << "}";
627 h_preamble << "[" << nargs << "]";
628 h_preamble << "{" << p.verbatim_item() << "}";
631 else if (t.cs() == "setcounter") {
632 string const name = p.getArg('{', '}');
633 string const content = p.getArg('{', '}');
634 if (name == "secnumdepth")
635 h_secnumdepth = content;
636 else if (name == "tocdepth")
637 h_tocdepth = content;
639 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
642 else if (t.cs() == "setlength") {
643 string const name = p.verbatim_item();
644 string const content = p.verbatim_item();
645 // the paragraphs are only not indented when \parindent is set to zero
646 if (name == "\\parindent" && content != "") {
647 if (content[0] == '0')
648 h_paragraph_separation = "skip";
649 } else if (name == "\\parskip") {
650 if (content == "\\smallskipamount")
651 h_defskip = "smallskip";
652 else if (content == "\\medskipamount")
653 h_defskip = "medskip";
654 else if (content == "\\bigskipamount")
655 h_defskip = "bigskip";
659 h_preamble << "\\setlength{" << name << "}{" << content << "}";
662 else if (t.cs() =="onehalfspacing")
663 h_spacing = "onehalf";
665 else if (t.cs() =="doublespacing")
666 h_spacing = "double";
668 else if (t.cs() =="setstretch")
669 h_spacing = "other " + p.verbatim_item();
671 else if (t.cs() == "begin") {
672 string const name = p.getArg('{', '}');
673 if (name == "document")
675 h_preamble << "\\begin{" << name << "}";
678 else if (t.cs() == "jurabibsetup") {
679 vector<string> jurabibsetup =
680 split_options(p.getArg('{', '}'));
681 // add jurabibsetup to the jurabib package options
682 add_package("jurabib", jurabibsetup);
683 if (!jurabibsetup.empty()) {
684 h_preamble << "\\jurabibsetup{"
685 << join(jurabibsetup, ",") << '}';
689 else if (!t.cs().empty())
690 h_preamble << '\\' << t.cs();
694 // Force textclass if the user wanted it
695 if (!forceclass.empty())
696 h_textclass = forceclass;
697 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
698 h_textclass.insert(0, "literate-");
699 FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
700 if (layoutfilename.empty()) {
701 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
704 tc.read(layoutfilename);
705 if (h_papersides.empty()) {
708 h_papersides = ss.str();
710 end_preamble(os, tc);