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/filetools.h"
22 #include "support/lstrings.h"
34 using std::istringstream;
36 using std::ostringstream;
43 using support::FileName;
44 using support::libFileSearch;
46 // special columntypes
47 extern std::map<char, int> special_columns;
49 std::map<string, vector<string> > used_packages;
53 const char * const known_languages[] = { "austrian", "babel", "bahasa",
54 "basque", "breton", "british", "bulgarian", "catalan", "croatian", "czech",
55 "danish", "dutch", "english", "esperanto", "estonian", "finnish",
56 "francais", "french", "frenchb", "frenchle", "frenchpro",
57 "galician", "german", "germanb", "greek", "hebcal", "hebfont",
58 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
59 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
60 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
61 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
62 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
63 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
64 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
65 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
67 const char * const known_french_languages[] = {"french", "frenchb", "francais",
68 "frenchle", "frenchpro", 0};
69 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
72 ostringstream h_preamble;
73 string h_textclass = "article";
74 string h_options = string();
75 string h_language = "english";
76 string h_inputencoding = "auto";
77 string h_fontscheme = "default";
78 string h_graphics = "default";
79 string h_paperfontsize = "default";
80 string h_spacing = "single";
81 string h_papersize = "default";
82 string h_use_geometry = "false";
83 string h_use_amsmath = "0";
84 string h_cite_engine = "basic";
85 string h_use_bibtopic = "false";
86 string h_paperorientation = "portrait";
87 string h_secnumdepth = "3";
88 string h_tocdepth = "3";
89 string h_paragraph_separation = "indent";
90 string h_defskip = "medskip";
91 string h_quotes_language = "english";
92 string h_papercolumns = "1";
93 string h_papersides = string();
94 string h_paperpagestyle = "default";
95 string h_tracking_changes = "false";
96 string h_output_changes = "false";
99 void handle_opt(vector<string> & opts, char const * const * what, string & target)
104 // the last language option is the document language (for babel and LyX)
105 // the last size option is the document font size
106 vector<string>::iterator it;
107 vector<string>::iterator position = opts.begin();
108 for (; *what; ++what) {
109 it = find(opts.begin(), opts.end(), *what);
110 if (it != opts.end()) {
111 if (it >= position) {
115 // remove found options from the list
123 * Split a package options string (keyval format) into a vector.
125 * authorformat=smallcaps,
127 * titleformat=colonsep,
128 * bibformat={tabular,ibidem,numbered}
130 vector<string> split_options(string const & input)
132 vector<string> options;
136 Token const & t = p.get_token();
137 if (t.asInput() == ",") {
138 options.push_back(trim(option));
140 } else if (t.asInput() == "=") {
143 if (p.next_token().asInput() == "{")
144 option += '{' + p.getArg('{', '}') + '}';
145 } else if (t.cat() != catSpace)
146 option += t.asInput();
150 options.push_back(trim(option));
157 * Add package \p name with options \p options to used_packages.
158 * Remove options from \p options that we don't want to output.
160 void add_package(string const & name, vector<string> & options)
162 // every package inherits the global options
163 if (used_packages.find(name) == used_packages.end())
164 used_packages[name] = split_options(h_options);
166 vector<string> & v = used_packages[name];
167 v.insert(v.end(), options.begin(), options.end());
168 if (name == "jurabib") {
169 // Don't output the order argument (see the cite command
170 // handling code in text.cpp).
171 vector<string>::iterator end =
172 remove(options.begin(), options.end(), "natbiborder");
173 end = remove(options.begin(), end, "jurabiborder");
174 options.erase(end, options.end());
179 void handle_package(string const & name, string const & opts)
181 vector<string> options = split_options(opts);
182 add_package(name, options);
184 //cerr << "handle_package: '" << name << "'\n";
187 else if (name == "aecompl")
189 else if (name == "amsmath")
191 else if (name == "amssymb")
193 else if (name == "babel")
195 else if (name == "fontenc")
197 else if (name == "inputenc") {
198 // only set when there is not more than one inputenc option
199 // therefore check for the "," character
200 if (opts.find(",", 0) == string::npos)
201 h_inputencoding = opts;
203 } else if (name == "makeidx")
205 else if (name == "verbatim")
207 else if (name == "graphicx")
209 else if (is_known(name, known_languages)) {
210 if (is_known(name, known_french_languages)) {
211 h_language = "french";
212 h_quotes_language = "french";
215 h_quotes_language = name;
218 } else if (name == "natbib") {
219 h_cite_engine = "natbib_authoryear";
220 vector<string>::iterator it =
221 find(options.begin(), options.end(), "authoryear");
222 if (it != options.end())
225 it = find(options.begin(), options.end(), "numbers");
226 if (it != options.end()) {
227 h_cite_engine = "natbib_numerical";
231 } else if (name == "jurabib") {
232 h_cite_engine = "jurabib";
233 } else if (options.empty())
234 h_preamble << "\\usepackage{" << name << "}\n";
236 h_preamble << "\\usepackage[" << opts << "]{" << name << "}\n";
240 // We need to do something with the options...
241 if (!options.empty())
242 cerr << "Ignoring options '" << join(options, ",")
243 << "' of package " << name << '.' << endl;
248 void end_preamble(ostream & os, TextClass const & /*textclass*/)
250 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
251 << "\\lyxformat 246\n"
252 << "\\begin_document\n"
253 << "\\begin_header\n"
254 << "\\textclass " << h_textclass << "\n";
255 if (!h_preamble.str().empty())
256 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
257 if (!h_options.empty())
258 os << "\\options " << h_options << "\n";
259 os << "\\language " << h_language << "\n"
260 << "\\inputencoding " << h_inputencoding << "\n"
261 << "\\fontscheme " << h_fontscheme << "\n"
262 << "\\graphics " << h_graphics << "\n"
263 << "\\paperfontsize " << h_paperfontsize << "\n"
264 << "\\spacing " << h_spacing << "\n"
265 << "\\papersize " << h_papersize << "\n"
266 << "\\use_geometry " << h_use_geometry << "\n"
267 << "\\use_amsmath " << h_use_amsmath << "\n"
268 << "\\cite_engine " << h_cite_engine << "\n"
269 << "\\use_bibtopic " << h_use_bibtopic << "\n"
270 << "\\paperorientation " << h_paperorientation << "\n"
271 << "\\secnumdepth " << h_secnumdepth << "\n"
272 << "\\tocdepth " << h_tocdepth << "\n"
273 << "\\paragraph_separation " << h_paragraph_separation << "\n"
274 << "\\defskip " << h_defskip << "\n"
275 << "\\quotes_language " << h_quotes_language << "\n"
276 << "\\papercolumns " << h_papercolumns << "\n"
277 << "\\papersides " << h_papersides << "\n"
278 << "\\paperpagestyle " << h_paperpagestyle << "\n"
279 << "\\tracking_changes " << h_tracking_changes << "\n"
280 << "\\output_changes " << h_output_changes << "\n"
281 << "\\end_header\n\n"
283 // clear preamble for subdocuments
287 } // anonymous namespace
289 TextClass const parse_preamble(Parser & p, ostream & os, string const & forceclass)
291 // initialize fixed types
292 special_columns['D'] = 3;
293 bool is_full_document = false;
295 // determine whether this is a full document or a fragment for inclusion
297 Token const & t = p.get_token();
299 if (t.cat() == catEscape && t.cs() == "documentclass") {
300 is_full_document = true;
306 while (is_full_document && p.good()) {
307 Token const & t = p.get_token();
310 cerr << "t: " << t << "\n";
316 if (t.cat() == catLetter ||
317 t.cat() == catSuper ||
319 t.cat() == catOther ||
320 t.cat() == catMath ||
321 t.cat() == catActive ||
322 t.cat() == catBegin ||
324 t.cat() == catAlign ||
325 t.cat() == catParameter)
326 h_preamble << t.character();
328 else if (t.cat() == catSpace || t.cat() == catNewline)
329 h_preamble << t.asInput();
331 else if (t.cat() == catComment)
332 h_preamble << t.asInput();
334 else if (t.cs() == "pagestyle")
335 h_paperpagestyle = p.verbatim_item();
337 else if (t.cs() == "makeatletter") {
338 p.setCatCode('@', catLetter);
339 h_preamble << "\\makeatletter";
342 else if (t.cs() == "makeatother") {
343 p.setCatCode('@', catOther);
344 h_preamble << "\\makeatother";
347 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
348 || t.cs() == "providecommand") {
350 if (p.next_token().character() == '*') {
354 string const name = p.verbatim_item();
355 string const opt1 = p.getOpt();
356 string const opt2 = p.getFullOpt();
357 string const body = p.verbatim_item();
358 // only non-lyxspecific stuff
359 if ( name != "\\noun"
360 && name != "\\tabularnewline"
362 && name != "\\lyxline"
363 && name != "\\lyxaddress"
364 && name != "\\lyxrightaddress"
365 && name != "\\lyxdot"
366 && name != "\\boldsymbol"
367 && name != "\\lyxarrow") {
369 ss << '\\' << t.cs();
372 ss << '{' << name << '}' << opt1 << opt2
373 << '{' << body << "}";
374 h_preamble << ss.str();
376 // Add the command to the known commands
377 add_known_command(name, opt1, !opt2.empty());
379 ostream & out = in_preamble ? h_preamble : os;
380 out << "\\" << t.cs() << "{" << name << "}"
381 << opts << "{" << body << "}";
386 else if (t.cs() == "documentclass") {
387 vector<string> opts = split_options(p.getArg('[', ']'));
388 handle_opt(opts, known_languages, h_language);
389 if (is_known(h_language, known_french_languages))
390 h_language = "french";
391 handle_opt(opts, known_fontsizes, h_paperfontsize);
392 // delete "pt" at the end
393 string::size_type i = h_paperfontsize.find("pt");
394 if (i != string::npos)
395 h_paperfontsize.erase(i);
396 h_quotes_language = h_language;
397 h_options = join(opts, ",");
398 h_textclass = p.getArg('{', '}');
401 else if (t.cs() == "usepackage") {
402 string const options = p.getArg('[', ']');
403 string const name = p.getArg('{', '}');
404 if (options.empty() && name.find(',')) {
405 vector<string> vecnames;
406 split(name, vecnames, ',');
407 vector<string>::const_iterator it = vecnames.begin();
408 vector<string>::const_iterator end = vecnames.end();
409 for (; it != end; ++it)
410 handle_package(trim(*it), string());
412 handle_package(name, options);
416 else if (t.cs() == "newenvironment") {
417 string const name = p.getArg('{', '}');
419 ss << "\\newenvironment{" << name << "}";
422 ss << '{' << p.verbatim_item() << '}';
423 ss << '{' << p.verbatim_item() << '}';
424 if (name != "lyxcode" && name != "lyxlist" &&
425 name != "lyxrightadress" &&
426 name != "lyxaddress" && name != "lyxgreyedout")
427 h_preamble << ss.str();
430 else if (t.cs() == "def") {
431 string name = p.get_token().cs();
432 while (p.next_token().cat() != catBegin)
433 name += p.get_token().asString();
434 h_preamble << "\\def\\" << name << '{'
435 << p.verbatim_item() << "}";
438 else if (t.cs() == "newcolumntype") {
439 string const name = p.getArg('{', '}');
442 string opts = p.getOpt();
444 istringstream is(string(opts, 1));
445 //cerr << "opt: " << is.str() << "\n";
448 special_columns[name[0]] = nargs;
449 h_preamble << "\\newcolumntype{" << name << "}";
451 h_preamble << "[" << nargs << "]";
452 h_preamble << "{" << p.verbatim_item() << "}";
455 else if (t.cs() == "setcounter") {
456 string const name = p.getArg('{', '}');
457 string const content = p.getArg('{', '}');
458 if (name == "secnumdepth")
459 h_secnumdepth = content;
460 else if (name == "tocdepth")
461 h_tocdepth = content;
463 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
466 else if (t.cs() == "setlength") {
467 string const name = p.verbatim_item();
468 string const content = p.verbatim_item();
470 if (name == "parskip")
471 h_paragraph_separation = "skip";
472 else if (name == "parindent")
473 h_paragraph_separation = "skip";
475 h_preamble << "\\setlength{" << name << "}{" << content << "}";
478 else if (t.cs() == "begin") {
479 string const name = p.getArg('{', '}');
480 if (name == "document")
482 h_preamble << "\\begin{" << name << "}";
485 else if (t.cs() == "jurabibsetup") {
486 vector<string> jurabibsetup =
487 split_options(p.getArg('{', '}'));
488 // add jurabibsetup to the jurabib package options
489 add_package("jurabib", jurabibsetup);
490 if (!jurabibsetup.empty()) {
491 h_preamble << "\\jurabibsetup{"
492 << join(jurabibsetup, ",") << '}';
496 else if (!t.cs().empty())
497 h_preamble << '\\' << t.cs();
501 // Force textclass if the user wanted it
502 if (!forceclass.empty())
503 h_textclass = forceclass;
504 if (noweb_mode && !lyx::support::prefixIs(h_textclass, "literate-"))
505 h_textclass.insert(0, "literate-");
506 FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
507 if (layoutfilename.empty()) {
508 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
512 textclass.read(layoutfilename);
513 if (h_papersides.empty()) {
515 ss << textclass.sides();
516 h_papersides = ss.str();
518 end_preamble(os, textclass);