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"
35 using std::istringstream;
37 using std::ostringstream;
44 using support::FileName;
45 using support::libFileSearch;
46 using support::isStrDbl;
48 // special columntypes
49 extern std::map<char, int> special_columns;
51 std::map<string, vector<string> > used_packages;
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};
65 //note this when updating to lyxformat 305:
66 //bahasai, indonesian, and indon = equal to bahasa
67 //malay, and meyalu = equal to bahasam
69 const char * const known_french_languages[] = {"french", "frenchb", "francais",
70 "frenchle", "frenchpro", 0};
71 const char * const known_german_languages[] = {"german", "germanb", 0};
72 const char * const known_ngerman_languages[] = {"ngerman", "ngermanb", 0};
73 const char * const known_russian_languages[] = {"russian", "russianb", 0};
74 const char * const known_ukrainian_languages[] = {"ukrainian", "ukraineb", 0};
76 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
78 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
79 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
81 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
84 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
85 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
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";
123 void handle_opt(vector<string> & opts, char const * const * what, string & target)
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) {
139 // remove found options from the list
147 * Split a package options string (keyval format) into a vector.
149 * authorformat=smallcaps,
151 * titleformat=colonsep,
152 * bibformat={tabular,ibidem,numbered}
154 vector<string> split_options(string const & input)
156 vector<string> options;
160 Token const & t = p.get_token();
161 if (t.asInput() == ",") {
162 options.push_back(trim(option));
164 } else if (t.asInput() == "=") {
167 if (p.next_token().asInput() == "{")
168 option += '{' + p.getArg('{', '}') + '}';
169 } else if (t.cat() != catSpace)
170 option += t.asInput();
174 options.push_back(trim(option));
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.
184 void add_package(string const & name, vector<string> & options)
186 // every package inherits the global options
187 if (used_packages.find(name) == used_packages.end())
188 used_packages[name] = split_options(h_options);
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());
203 void handle_package(string const & name, string const & opts)
205 vector<string> options = split_options(opts);
206 add_package(name, options);
211 if (is_known(name, known_roman_fonts))
213 if (name == "fourier") {
214 h_font_roman = "utopia";
215 // when font uses real small capitals
216 if (opts == "expert")
219 if (name == "mathpazo")
220 h_font_roman = "palatino";
221 if (name == "mathptmx")
222 h_font_roman = "times";
224 if (is_known(name, known_sans_fonts)) {
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;
242 if (is_known(name, known_typewriter_fonts)) {
243 h_font_typewriter = name;
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;
259 // font uses old-style figure
263 else if (name == "amsmath" || name == "amssymb")
265 else if (name == "babel")
267 else if (name == "fontenc")
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;
275 } else if (name == "makeidx")
277 else if (name == "verbatim")
279 else if (name == "graphicx")
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 = "german";
286 else if (is_known(name, known_ngerman_languages))
287 h_language = "ngerman";
288 else if (is_known(name, known_russian_languages))
289 h_language = "russian";
290 else if (is_known(name, known_ukrainian_languages))
291 h_language = "ukrainian";
294 h_quotes_language = h_language;
296 } else if (name == "natbib") {
297 h_cite_engine = "natbib_authoryear";
298 vector<string>::iterator it =
299 find(options.begin(), options.end(), "authoryear");
300 if (it != options.end())
303 it = find(options.begin(), options.end(), "numbers");
304 if (it != options.end()) {
305 h_cite_engine = "natbib_numerical";
309 } else if (name == "jurabib") {
310 h_cite_engine = "jurabib";
311 } else if (options.empty())
312 h_preamble << "\\usepackage{" << name << "}\n";
314 h_preamble << "\\usepackage[" << opts << "]{" << name << "}\n";
318 // We need to do something with the options...
319 if (!options.empty())
320 cerr << "Ignoring options '" << join(options, ",")
321 << "' of package " << name << '.' << endl;
326 void end_preamble(ostream & os, TextClass const & /*textclass*/)
328 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
329 << "\\lyxformat 247\n"
330 << "\\begin_document\n"
331 << "\\begin_header\n"
332 << "\\textclass " << h_textclass << "\n";
333 if (!h_preamble.str().empty())
334 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
335 if (!h_options.empty())
336 os << "\\options " << h_options << "\n";
337 os << "\\language " << h_language << "\n"
338 << "\\inputencoding " << h_inputencoding << "\n"
339 << "\\font_roman " << h_font_roman << "\n"
340 << "\\font_sans " << h_font_sans << "\n"
341 << "\\font_typewriter " << h_font_typewriter << "\n"
342 << "\\font_default_family " << h_font_default_family << "\n"
343 << "\\font_sc " << h_font_sc << "\n"
344 << "\\font_osf " << h_font_osf << "\n"
345 << "\\font_sf_scale " << h_font_sf_scale << "\n"
346 << "\\font_tt_scale " << h_font_tt_scale << "\n"
347 << "\\graphics " << h_graphics << "\n"
348 << "\\paperfontsize " << h_paperfontsize << "\n"
349 << "\\spacing " << h_spacing << "\n"
350 << "\\papersize " << h_papersize << "\n"
351 << "\\use_geometry " << h_use_geometry << "\n"
352 << "\\use_amsmath " << h_use_amsmath << "\n"
353 << "\\cite_engine " << h_cite_engine << "\n"
354 << "\\use_bibtopic " << h_use_bibtopic << "\n"
355 << "\\paperorientation " << h_paperorientation << "\n"
356 << "\\secnumdepth " << h_secnumdepth << "\n"
357 << "\\tocdepth " << h_tocdepth << "\n"
358 << "\\paragraph_separation " << h_paragraph_separation << "\n"
359 << "\\defskip " << h_defskip << "\n"
360 << "\\quotes_language " << h_quotes_language << "\n"
361 << "\\papercolumns " << h_papercolumns << "\n"
362 << "\\papersides " << h_papersides << "\n"
363 << "\\paperpagestyle " << h_paperpagestyle << "\n"
364 << "\\tracking_changes " << h_tracking_changes << "\n"
365 << "\\output_changes " << h_output_changes << "\n"
366 << "\\end_header\n\n"
368 // clear preamble for subdocuments
372 } // anonymous namespace
374 TextClass const parse_preamble(Parser & p, ostream & os, string const & forceclass)
376 // initialize fixed types
377 special_columns['D'] = 3;
378 bool is_full_document = false;
380 // determine whether this is a full document or a fragment for inclusion
382 Token const & t = p.get_token();
384 if (t.cat() == catEscape && t.cs() == "documentclass") {
385 is_full_document = true;
391 while (is_full_document && p.good()) {
392 Token const & t = p.get_token();
395 cerr << "t: " << t << "\n";
401 if (t.cat() == catLetter ||
402 t.cat() == catSuper ||
404 t.cat() == catOther ||
405 t.cat() == catMath ||
406 t.cat() == catActive ||
407 t.cat() == catBegin ||
409 t.cat() == catAlign ||
410 t.cat() == catParameter)
411 h_preamble << t.character();
413 else if (t.cat() == catSpace || t.cat() == catNewline)
414 h_preamble << t.asInput();
416 else if (t.cat() == catComment)
417 h_preamble << t.asInput();
419 else if (t.cs() == "pagestyle")
420 h_paperpagestyle = p.verbatim_item();
422 else if (t.cs() == "makeatletter") {
423 p.setCatCode('@', catLetter);
424 h_preamble << "\\makeatletter";
427 else if (t.cs() == "makeatother") {
428 p.setCatCode('@', catOther);
429 h_preamble << "\\makeatother";
432 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
433 || t.cs() == "providecommand") {
435 if (p.next_token().character() == '*') {
439 string const name = p.verbatim_item();
440 string const opt1 = p.getOpt();
441 string const opt2 = p.getFullOpt();
442 string const body = p.verbatim_item();
444 if (name == "\\rmdefault")
445 if (is_known(body, known_roman_fonts))
448 if (name == "\\sfdefault")
449 if (is_known(body, known_sans_fonts))
452 if (name == "\\ttdefault")
453 if (is_known(body, known_typewriter_fonts))
454 h_font_typewriter = body;
456 if (name == "\\familydefault") {
457 string family = body;
458 // remove leading "\"
459 h_font_default_family = family.erase(0,1);
461 // only non-lyxspecific stuff
462 if ( name != "\\noun"
463 && name != "\\tabularnewline"
465 && name != "\\lyxline"
466 && name != "\\lyxaddress"
467 && name != "\\lyxrightaddress"
468 && name != "\\lyxdot"
469 && name != "\\boldsymbol"
470 && name != "\\lyxarrow"
471 && name != "\\rmdefault"
472 && name != "\\sfdefault"
473 && name != "\\ttdefault"
474 && name != "\\familydefault") {
476 ss << '\\' << t.cs();
479 ss << '{' << name << '}' << opt1 << opt2
480 << '{' << body << "}";
481 h_preamble << ss.str();
483 // Add the command to the known commands
484 add_known_command(name, opt1, !opt2.empty());
486 ostream & out = in_preamble ? h_preamble : os;
487 out << "\\" << t.cs() << "{" << name << "}"
488 << opts << "{" << body << "}";
493 else if (t.cs() == "documentclass") {
494 vector<string> opts = split_options(p.getArg('[', ']'));
495 handle_opt(opts, known_languages, h_language);
496 if (is_known(h_language, known_french_languages))
497 h_language = "french";
498 else if (is_known(h_language, known_german_languages))
499 h_language = "german";
500 else if (is_known(h_language, known_ngerman_languages))
501 h_language = "ngerman";
502 else if (is_known(h_language, known_russian_languages))
503 h_language = "russian";
504 else if (is_known(h_language, known_ukrainian_languages))
505 h_language = "ukrainian";
506 handle_opt(opts, known_fontsizes, h_paperfontsize);
507 // delete "pt" at the end
508 string::size_type i = h_paperfontsize.find("pt");
509 if (i != string::npos)
510 h_paperfontsize.erase(i);
511 h_quotes_language = h_language;
512 h_options = join(opts, ",");
513 h_textclass = p.getArg('{', '}');
516 else if (t.cs() == "usepackage") {
517 string const options = p.getArg('[', ']');
518 string const name = p.getArg('{', '}');
519 if (options.empty() && name.find(',')) {
520 vector<string> vecnames;
521 split(name, vecnames, ',');
522 vector<string>::const_iterator it = vecnames.begin();
523 vector<string>::const_iterator end = vecnames.end();
524 for (; it != end; ++it)
525 handle_package(trim(*it), string());
527 handle_package(name, options);
531 else if (t.cs() == "newenvironment") {
532 string const name = p.getArg('{', '}');
534 ss << "\\newenvironment{" << name << "}";
537 ss << '{' << p.verbatim_item() << '}';
538 ss << '{' << p.verbatim_item() << '}';
539 if (name != "lyxcode" && name != "lyxlist" &&
540 name != "lyxrightadress" &&
541 name != "lyxaddress" && name != "lyxgreyedout")
542 h_preamble << ss.str();
545 else if (t.cs() == "def") {
546 string name = p.get_token().cs();
547 while (p.next_token().cat() != catBegin)
548 name += p.get_token().asString();
549 h_preamble << "\\def\\" << name << '{'
550 << p.verbatim_item() << "}";
553 else if (t.cs() == "newcolumntype") {
554 string const name = p.getArg('{', '}');
557 string opts = p.getOpt();
559 istringstream is(string(opts, 1));
560 //cerr << "opt: " << is.str() << "\n";
563 special_columns[name[0]] = nargs;
564 h_preamble << "\\newcolumntype{" << name << "}";
566 h_preamble << "[" << nargs << "]";
567 h_preamble << "{" << p.verbatim_item() << "}";
570 else if (t.cs() == "setcounter") {
571 string const name = p.getArg('{', '}');
572 string const content = p.getArg('{', '}');
573 if (name == "secnumdepth")
574 h_secnumdepth = content;
575 else if (name == "tocdepth")
576 h_tocdepth = content;
578 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
581 else if (t.cs() == "setlength") {
582 string const name = p.verbatim_item();
583 string const content = p.verbatim_item();
585 if (name == "parskip")
586 h_paragraph_separation = "skip";
587 else if (name == "parindent")
588 h_paragraph_separation = "skip";
590 h_preamble << "\\setlength{" << name << "}{" << content << "}";
593 else if (t.cs() == "begin") {
594 string const name = p.getArg('{', '}');
595 if (name == "document")
597 h_preamble << "\\begin{" << name << "}";
600 else if (t.cs() == "jurabibsetup") {
601 vector<string> jurabibsetup =
602 split_options(p.getArg('{', '}'));
603 // add jurabibsetup to the jurabib package options
604 add_package("jurabib", jurabibsetup);
605 if (!jurabibsetup.empty()) {
606 h_preamble << "\\jurabibsetup{"
607 << join(jurabibsetup, ",") << '}';
611 else if (!t.cs().empty())
612 h_preamble << '\\' << t.cs();
616 // Force textclass if the user wanted it
617 if (!forceclass.empty())
618 h_textclass = forceclass;
619 if (noweb_mode && !lyx::support::prefixIs(h_textclass, "literate-"))
620 h_textclass.insert(0, "literate-");
621 FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
622 if (layoutfilename.empty()) {
623 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
627 textclass.read(layoutfilename);
628 if (h_papersides.empty()) {
630 ss << textclass.sides();
631 h_papersides = ss.str();
633 end_preamble(os, textclass);