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.
20 #include "TextClass.h"
21 #include "support/convert.h"
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
33 using namespace lyx::support;
37 // special columntypes
38 extern map<char, int> special_columns;
40 map<string, vector<string> > used_packages;
42 // needed to handle encodings with babel
43 bool one_language = true;
45 // to avoid that the babel options overwrite the documentclass options
46 bool documentclass_language;
50 const char * const known_languages[] = { "afrikaans", "american", "arabic",
51 "austrian", "bahasa", "basque", "belarusian", "brazil", "breton", "british",
52 "bulgarian", "canadian", "canadien", "catalan", "croatian", "czech", "danish",
53 "dutch", "english", "esperanto", "estonian", "finnish", "francais", "french",
54 "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
55 "hebrew", "icelandic", "irish", "italian", "lsorbian", "magyar", "naustrian",
56 "ngerman", "ngermanb", "norsk", "nynorsk", "polish", "portuges", "romanian",
57 "russian", "russianb", "scottish", "serbian", "slovak", "slovene", "spanish",
58 "swedish", "thai", "turkish", "ukraineb", "ukrainian", "usorbian", "welsh", 0};
60 //note this when updating to lyxformat 305:
61 //bahasai, indonesian, and indon = equal to bahasa
62 //malay, and meyalu = equal to bahasam
64 const char * const known_french_languages[] = {"french", "frenchb", "francais",
65 "frenchle", "frenchpro", 0};
66 const char * const known_german_languages[] = {"german", "germanb", 0};
67 const char * const known_ngerman_languages[] = {"ngerman", "ngermanb", 0};
68 const char * const known_russian_languages[] = {"russian", "russianb", 0};
69 const char * const known_ukrainian_languages[] = {"ukrainian", "ukraineb", 0};
71 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
73 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
74 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
76 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
79 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
80 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
84 ostringstream h_preamble;
85 string h_textclass = "article";
86 string h_options = string();
87 string h_language = "english";
88 string h_inputencoding = "auto";
89 string h_font_roman = "default";
90 string h_font_sans = "default";
91 string h_font_typewriter = "default";
92 string h_font_default_family = "default";
93 string h_font_sc = "false";
94 string h_font_osf = "false";
95 string h_font_sf_scale = "100";
96 string h_font_tt_scale = "100";
97 string h_graphics = "default";
98 string h_paperfontsize = "default";
99 string h_spacing = "single";
100 string h_papersize = "default";
101 string h_use_geometry = "false";
102 string h_use_amsmath = "0";
103 string h_cite_engine = "basic";
104 string h_use_bibtopic = "false";
105 string h_paperorientation = "portrait";
106 string h_secnumdepth = "3";
107 string h_tocdepth = "3";
108 string h_paragraph_separation = "indent";
109 string h_defskip = "medskip";
110 string h_quotes_language = "english";
111 string h_papercolumns = "1";
112 string h_papersides = string();
113 string h_paperpagestyle = "default";
114 string h_tracking_changes = "false";
115 string h_output_changes = "false";
118 void handle_opt(vector<string> & opts, char const * const * what, string & target)
123 // the last language option is the document language (for babel and LyX)
124 // the last size option is the document font size
125 vector<string>::iterator it;
126 vector<string>::iterator position = opts.begin();
127 for (; *what; ++what) {
128 it = find(opts.begin(), opts.end(), *what);
129 if (it != opts.end()) {
130 documentclass_language = true;
131 if (it >= position) {
140 void delete_opt(vector<string> & opts, char const * const * what)
145 // remove found options from the list
146 // do this after handle_opt to avoid potential memory leaks and to be able
147 // to find in every case the last language option
148 vector<string>::iterator it;
149 for (; *what; ++what) {
150 it = find(opts.begin(), opts.end(), *what);
151 if (it != opts.end())
158 * Split a package options string (keyval format) into a vector.
160 * authorformat=smallcaps,
162 * titleformat=colonsep,
163 * bibformat={tabular,ibidem,numbered}
165 vector<string> split_options(string const & input)
167 vector<string> options;
171 Token const & t = p.get_token();
172 if (t.asInput() == ",") {
173 options.push_back(trim(option));
175 } else if (t.asInput() == "=") {
178 if (p.next_token().asInput() == "{")
179 option += '{' + p.getArg('{', '}') + '}';
180 } else if (t.cat() != catSpace)
181 option += t.asInput();
185 options.push_back(trim(option));
192 * Add package \p name with options \p options to used_packages.
193 * Remove options from \p options that we don't want to output.
195 void add_package(string const & name, vector<string> & options)
197 // every package inherits the global options
198 if (used_packages.find(name) == used_packages.end())
199 used_packages[name] = split_options(h_options);
201 vector<string> & v = used_packages[name];
202 v.insert(v.end(), options.begin(), options.end());
203 if (name == "jurabib") {
204 // Don't output the order argument (see the cite command
205 // handling code in text.cpp).
206 vector<string>::iterator end =
207 remove(options.begin(), options.end(), "natbiborder");
208 end = remove(options.begin(), end, "jurabiborder");
209 options.erase(end, options.end());
214 // Given is a string like "scaled=0.9", return 0.9 * 100
215 string const scale_as_percentage(string const & scale)
217 string::size_type pos = scale.find('=');
218 if (pos != string::npos) {
219 string value = scale.substr(pos + 1);
221 return convert<string>(100 * convert<double>(value));
223 // If the input string didn't match our expectations.
224 // return the default value "100"
229 void handle_package(string const & name, string const & opts)
231 vector<string> options = split_options(opts);
232 add_package(name, options);
236 if (is_known(name, known_roman_fonts))
238 if (name == "fourier") {
239 h_font_roman = "utopia";
240 // when font uses real small capitals
241 if (opts == "expert")
244 if (name == "mathpazo")
245 h_font_roman = "palatino";
246 if (name == "mathptmx")
247 h_font_roman = "times";
249 if (is_known(name, known_sans_fonts)) {
253 h_font_sf_scale = scale_as_percentage(scale);
257 if (is_known(name, known_typewriter_fonts)) {
258 h_font_typewriter = name;
261 h_font_tt_scale = scale_as_percentage(scale);
264 // font uses old-style figure
268 else if (name == "amsmath" || name == "amssymb")
270 else if (name == "babel" && !opts.empty()) {
271 // check if more than one option was used - used later for inputenc
272 // in case inputenc is parsed before babel, set the encoding to auto
273 if (options.begin() != options.end() - 1) {
274 one_language = false;
275 h_inputencoding = "auto";
277 // only set the document language when there was not already one set
278 // via the documentclass options
279 // babel takes the the last language given in the documentclass options
280 // as document language. If there is no such language option, the last
281 // option of its \usepackage call is used.
282 if (documentclass_language == false) {
283 handle_opt(options, known_languages, h_language);
284 delete_opt(options, known_languages);
285 if (is_known(h_language, known_french_languages))
286 h_language = "french";
287 else if (is_known(h_language, known_german_languages))
288 h_language = "german";
289 else if (is_known(h_language, known_ngerman_languages))
290 h_language = "ngerman";
291 else if (is_known(h_language, known_russian_languages))
292 h_language = "russian";
293 else if (is_known(h_language, known_ukrainian_languages))
294 h_language = "ukrainian";
295 h_quotes_language = h_language;
298 else if (name == "fontenc")
300 else if (name == "inputenc") {
301 // only set when there is not more than one inputenc option
302 // therefore check for the "," character
303 // also only set when there is not more then one babel language option
304 if (opts.find(",") == string::npos && one_language == true)
305 h_inputencoding = opts;
307 } else if (name == "makeidx")
309 else if (name == "verbatim")
311 else if (name == "graphicx")
313 else if (is_known(name, known_languages)) {
314 if (is_known(name, known_french_languages))
315 h_language = "french";
316 else if (is_known(name, known_german_languages))
317 h_language = "german";
318 else if (is_known(name, known_ngerman_languages))
319 h_language = "ngerman";
320 else if (is_known(name, known_russian_languages))
321 h_language = "russian";
322 else if (is_known(name, known_ukrainian_languages))
323 h_language = "ukrainian";
326 h_quotes_language = h_language;
328 } else if (name == "natbib") {
329 h_cite_engine = "natbib_authoryear";
330 vector<string>::iterator it =
331 find(options.begin(), options.end(), "authoryear");
332 if (it != options.end())
335 it = find(options.begin(), options.end(), "numbers");
336 if (it != options.end()) {
337 h_cite_engine = "natbib_numerical";
341 } else if (name == "jurabib") {
342 h_cite_engine = "jurabib";
343 } else if (options.empty())
344 h_preamble << "\\usepackage{" << name << "}\n";
346 h_preamble << "\\usepackage[" << opts << "]{" << name << "}\n";
350 // We need to do something with the options...
351 if (!options.empty())
352 cerr << "Ignoring options '" << join(options, ",")
353 << "' of package " << name << '.' << endl;
358 void end_preamble(ostream & os, TextClass const & /*textclass*/)
360 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
361 << "\\lyxformat 247\n"
362 << "\\begin_document\n"
363 << "\\begin_header\n"
364 << "\\textclass " << h_textclass << "\n";
365 if (!h_preamble.str().empty())
366 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
367 if (!h_options.empty())
368 os << "\\options " << h_options << "\n";
369 os << "\\language " << h_language << "\n"
370 << "\\inputencoding " << h_inputencoding << "\n"
371 << "\\font_roman " << h_font_roman << "\n"
372 << "\\font_sans " << h_font_sans << "\n"
373 << "\\font_typewriter " << h_font_typewriter << "\n"
374 << "\\font_default_family " << h_font_default_family << "\n"
375 << "\\font_sc " << h_font_sc << "\n"
376 << "\\font_osf " << h_font_osf << "\n"
377 << "\\font_sf_scale " << h_font_sf_scale << "\n"
378 << "\\font_tt_scale " << h_font_tt_scale << "\n"
379 << "\\graphics " << h_graphics << "\n"
380 << "\\paperfontsize " << h_paperfontsize << "\n"
381 << "\\spacing " << h_spacing << "\n"
382 << "\\papersize " << h_papersize << "\n"
383 << "\\use_geometry " << h_use_geometry << "\n"
384 << "\\use_amsmath " << h_use_amsmath << "\n"
385 << "\\cite_engine " << h_cite_engine << "\n"
386 << "\\use_bibtopic " << h_use_bibtopic << "\n"
387 << "\\paperorientation " << h_paperorientation << "\n"
388 << "\\secnumdepth " << h_secnumdepth << "\n"
389 << "\\tocdepth " << h_tocdepth << "\n"
390 << "\\paragraph_separation " << h_paragraph_separation << "\n"
391 << "\\defskip " << h_defskip << "\n"
392 << "\\quotes_language " << h_quotes_language << "\n"
393 << "\\papercolumns " << h_papercolumns << "\n"
394 << "\\papersides " << h_papersides << "\n"
395 << "\\paperpagestyle " << h_paperpagestyle << "\n"
396 << "\\tracking_changes " << h_tracking_changes << "\n"
397 << "\\output_changes " << h_output_changes << "\n"
398 << "\\end_header\n\n"
400 // clear preamble for subdocuments
404 } // anonymous namespace
406 TextClass const parse_preamble(Parser & p, ostream & os, string const & forceclass)
408 // initialize fixed types
409 special_columns['D'] = 3;
410 bool is_full_document = false;
412 // determine whether this is a full document or a fragment for inclusion
414 Token const & t = p.get_token();
416 if (t.cat() == catEscape && t.cs() == "documentclass") {
417 is_full_document = true;
423 while (is_full_document && p.good()) {
424 Token const & t = p.get_token();
427 cerr << "t: " << t << "\n";
433 if (t.cat() == catLetter ||
434 t.cat() == catSuper ||
436 t.cat() == catOther ||
437 t.cat() == catMath ||
438 t.cat() == catActive ||
439 t.cat() == catBegin ||
441 t.cat() == catAlign ||
442 t.cat() == catParameter)
443 h_preamble << t.character();
445 else if (t.cat() == catSpace || t.cat() == catNewline)
446 h_preamble << t.asInput();
448 else if (t.cat() == catComment)
449 h_preamble << t.asInput();
451 else if (t.cs() == "pagestyle")
452 h_paperpagestyle = p.verbatim_item();
454 else if (t.cs() == "makeatletter") {
455 p.setCatCode('@', catLetter);
458 else if (t.cs() == "makeatother") {
459 p.setCatCode('@', catOther);
462 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
463 || t.cs() == "providecommand") {
465 if (p.next_token().character() == '*') {
469 string const name = p.verbatim_item();
470 string const opt1 = p.getOpt();
471 string const opt2 = p.getFullOpt();
472 string const body = p.verbatim_item();
474 if (name == "\\rmdefault")
475 if (is_known(body, known_roman_fonts))
478 if (name == "\\sfdefault")
479 if (is_known(body, known_sans_fonts))
482 if (name == "\\ttdefault")
483 if (is_known(body, known_typewriter_fonts))
484 h_font_typewriter = body;
486 if (name == "\\familydefault") {
487 string family = body;
488 // remove leading "\"
489 h_font_default_family = family.erase(0,1);
491 // only non-lyxspecific stuff
492 if ( name != "\\noun"
493 && name != "\\tabularnewline"
495 && name != "\\lyxline"
496 && name != "\\lyxaddress"
497 && name != "\\lyxrightaddress"
498 && name != "\\lyxdot"
499 && name != "\\boldsymbol"
500 && name != "\\lyxarrow"
501 && name != "\\rmdefault"
502 && name != "\\sfdefault"
503 && name != "\\ttdefault"
504 && name != "\\familydefault") {
506 ss << '\\' << t.cs();
509 ss << '{' << name << '}' << opt1 << opt2
510 << '{' << body << "}";
511 h_preamble << ss.str();
513 // Add the command to the known commands
514 add_known_command(name, opt1, !opt2.empty());
516 ostream & out = in_preamble ? h_preamble : os;
517 out << "\\" << t.cs() << "{" << name << "}"
518 << opts << "{" << body << "}";
523 else if (t.cs() == "documentclass") {
524 vector<string> opts = split_options(p.getArg('[', ']'));
525 handle_opt(opts, known_fontsizes, h_paperfontsize);
526 delete_opt(opts, known_fontsizes);
527 // delete "pt" at the end
528 string::size_type i = h_paperfontsize.find("pt");
529 if (i != string::npos)
530 h_paperfontsize.erase(i);
531 // to avoid that the babel options overwrite the documentclass options
532 documentclass_language = false;
533 handle_opt(opts, known_languages, h_language);
534 delete_opt(opts, known_languages);
535 if (is_known(h_language, known_french_languages))
536 h_language = "french";
537 else if (is_known(h_language, known_german_languages))
538 h_language = "german";
539 else if (is_known(h_language, known_ngerman_languages))
540 h_language = "ngerman";
541 else if (is_known(h_language, known_russian_languages))
542 h_language = "russian";
543 else if (is_known(h_language, known_ukrainian_languages))
544 h_language = "ukrainian";
545 h_quotes_language = h_language;
546 h_options = join(opts, ",");
547 h_textclass = p.getArg('{', '}');
550 else if (t.cs() == "usepackage") {
551 string const options = p.getArg('[', ']');
552 string const name = p.getArg('{', '}');
553 if (options.empty() && name.find(',')) {
554 vector<string> vecnames;
555 split(name, vecnames, ',');
556 vector<string>::const_iterator it = vecnames.begin();
557 vector<string>::const_iterator end = vecnames.end();
558 for (; it != end; ++it)
559 handle_package(trim(*it), string());
561 handle_package(name, options);
565 else if (t.cs() == "newenvironment") {
566 string const name = p.getArg('{', '}');
568 ss << "\\newenvironment{" << name << "}";
571 ss << '{' << p.verbatim_item() << '}';
572 ss << '{' << p.verbatim_item() << '}';
573 if (name != "lyxcode" && name != "lyxlist" &&
574 name != "lyxrightadress" &&
575 name != "lyxaddress" && name != "lyxgreyedout")
576 h_preamble << ss.str();
579 else if (t.cs() == "def") {
580 string name = p.get_token().cs();
581 while (p.next_token().cat() != catBegin)
582 name += p.get_token().asString();
583 h_preamble << "\\def\\" << name << '{'
584 << p.verbatim_item() << "}";
587 else if (t.cs() == "newcolumntype") {
588 string const name = p.getArg('{', '}');
591 string opts = p.getOpt();
593 istringstream is(string(opts, 1));
596 special_columns[name[0]] = nargs;
597 h_preamble << "\\newcolumntype{" << name << "}";
599 h_preamble << "[" << nargs << "]";
600 h_preamble << "{" << p.verbatim_item() << "}";
603 else if (t.cs() == "setcounter") {
604 string const name = p.getArg('{', '}');
605 string const content = p.getArg('{', '}');
606 if (name == "secnumdepth")
607 h_secnumdepth = content;
608 else if (name == "tocdepth")
609 h_tocdepth = content;
611 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
614 else if (t.cs() == "setlength") {
615 string const name = p.verbatim_item();
616 string const content = p.verbatim_item();
618 if (name == "parskip")
619 h_paragraph_separation = "skip";
620 else if (name == "parindent")
621 h_paragraph_separation = "skip";
623 h_preamble << "\\setlength{" << name << "}{" << content << "}";
626 else if (t.cs() == "begin") {
627 string const name = p.getArg('{', '}');
628 if (name == "document")
630 h_preamble << "\\begin{" << name << "}";
633 else if (t.cs() == "jurabibsetup") {
634 vector<string> jurabibsetup =
635 split_options(p.getArg('{', '}'));
636 // add jurabibsetup to the jurabib package options
637 add_package("jurabib", jurabibsetup);
638 if (!jurabibsetup.empty()) {
639 h_preamble << "\\jurabibsetup{"
640 << join(jurabibsetup, ",") << '}';
644 else if (!t.cs().empty())
645 h_preamble << '\\' << t.cs();
649 // Force textclass if the user wanted it
650 if (!forceclass.empty())
651 h_textclass = forceclass;
652 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
653 h_textclass.insert(0, "literate-");
654 FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
655 if (layoutfilename.empty()) {
656 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
660 textclass.read(layoutfilename);
661 if (h_papersides.empty()) {
663 ss << textclass.sides();
664 h_papersides = ss.str();
666 end_preamble(os, textclass);