3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
18 #include "lyxtextclass.h"
20 #include "support/filetools.h"
29 using std::istringstream;
31 using std::ostringstream;
37 using lyx::support::LibFileSearch;
39 // special columntypes
40 extern std::map<char, int> special_columns;
42 std::map<string, vector<string> > used_packages;
46 const char * const known_languages[] = { "austrian", "babel", "bahasa",
47 "basque", "breton", "british", "bulgarian", "catalan", "croatian", "czech",
48 "danish", "dutch", "english", "esperanto", "estonian", "finnish", "francais",
49 "frenchb", "galician", "german", "germanb", "greek", "hebcal", "hebfont",
50 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
51 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
52 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
53 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
54 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
55 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
56 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
57 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
59 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
62 ostringstream h_preamble;
63 string h_textclass = "article";
64 string h_options = string();
65 string h_language = "english";
66 string h_inputencoding = "latin1";
67 string h_fontscheme = "default";
68 string h_graphics = "default";
69 string h_paperfontsize = "default";
70 string h_spacing = "single";
71 // Match the entry in ../src/tex-strings.C. Why not "default"?
72 string h_papersize = "Default";
73 string h_paperpackage = "none";
74 string h_use_geometry = "0";
75 string h_use_amsmath = "0";
76 string h_cite_engine = "basic";
77 string h_use_bibtopic = "0";
78 string h_paperorientation = "portrait";
79 string h_secnumdepth = "3";
80 string h_tocdepth = "3";
81 string h_paragraph_separation = "indent";
82 string h_defskip = "medskip";
83 string h_quotes_language = "english";
84 string h_quotes_times = "2";
85 string h_papercolumns = "1";
86 string h_papersides = string();
87 string h_paperpagestyle = "default";
88 string h_tracking_changes = "0";
91 void handle_opt(vector<string> & opts, char const * const * what, string & target)
96 for ( ; *what; ++what) {
97 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
98 if (it != opts.end()) {
99 //cerr << "### found option '" << *what << "'\n";
109 * Split a package options string (keyval format) into a vector.
111 * authorformat=smallcaps,
113 * titleformat=colonsep,
114 * bibformat={tabular,ibidem,numbered}
116 vector<string> split_options(string const & input)
118 vector<string> options;
122 Token const & t = p.get_token();
123 if (t.asInput() == ",") {
124 options.push_back(option);
126 } else if (t.asInput() == "=") {
129 if (p.next_token().asInput() == "{")
130 option += '{' + p.getArg('{', '}') + '}';
131 } else if (t.cat() != catSpace)
132 option += t.asInput();
136 options.push_back(option);
143 * Add package \p name with options \p options to used_packages.
144 * Remove options from \p options that we don't want to output.
146 void add_package(string const & name, vector<string> & options)
148 // every package inherits the global options
149 if (used_packages.find(name) == used_packages.end())
150 used_packages[name] = split_options(h_options);
152 vector<string> & v = used_packages[name];
153 v.insert(v.end(), options.begin(), options.end());
154 if (name == "jurabib") {
155 // Don't output the order argument (see the cite command
156 // handling code in text.C).
157 vector<string>::iterator end =
158 remove(options.begin(), options.end(), "natbiborder");
159 end = remove(options.begin(), end, "jurabiborder");
160 options.erase(end, options.end());
165 void handle_package(string const & name, string const & opts)
167 vector<string> options = split_options(opts);
168 add_package(name, options);
170 //cerr << "handle_package: '" << name << "'\n";
171 if (name == "a4wide") {
172 h_papersize = "a4paper";
173 h_paperpackage = "widemarginsa4";
174 } else if (name == "ae")
176 else if (name == "aecompl")
178 else if (name == "amsmath")
180 else if (name == "amssymb")
182 else if (name == "babel")
184 else if (name == "fontenc")
186 else if (name == "inputenc") {
187 h_inputencoding = opts;
189 } else if (name == "makeidx")
191 else if (name == "verbatim")
193 else if (is_known(name, known_languages)) {
195 h_quotes_language = name;
196 } else if (name == "natbib") {
197 h_cite_engine = "natbib_authoryear";
198 vector<string>::iterator it =
199 find(options.begin(), options.end(), "authoryear");
200 if (it != options.end())
203 it = find(options.begin(), options.end(), "numbers");
204 if (it != options.end()) {
205 h_cite_engine = "natbib_numerical";
209 } else if (name == "jurabib") {
210 h_cite_engine = "jurabib";
211 } else if (options.empty())
212 h_preamble << "\\usepackage{" << name << "}\n";
214 h_preamble << "\\usepackage[" << opts << "]{" << name << "}\n";
218 // We need to do something with the options...
219 if (!options.empty())
220 cerr << "Ignoring options '" << join(options, ",")
221 << "' of package " << name << '.' << endl;
226 void end_preamble(ostream & os, LyXTextClass const & /*textclass*/)
228 os << "#LyX file created by tex2lyx 0.1.2 \n"
229 << "\\lyxformat 235\n"
230 << "\\textclass " << h_textclass << "\n"
231 << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
232 if (!h_options.empty())
233 os << "\\options " << h_options << "\n";
234 os << "\\language " << h_language << "\n"
235 << "\\inputencoding " << h_inputencoding << "\n"
236 << "\\fontscheme " << h_fontscheme << "\n"
237 << "\\graphics " << h_graphics << "\n"
238 << "\\paperfontsize " << h_paperfontsize << "\n"
239 << "\\spacing " << h_spacing << "\n"
240 << "\\papersize " << h_papersize << "\n"
241 << "\\paperpackage " << h_paperpackage << "\n"
242 << "\\use_geometry " << h_use_geometry << "\n"
243 << "\\use_amsmath " << h_use_amsmath << "\n"
244 << "\\cite_engine " << h_cite_engine << "\n"
245 << "\\use_bibtopic " << h_use_bibtopic << "\n"
246 << "\\paperorientation " << h_paperorientation << "\n"
247 << "\\secnumdepth " << h_secnumdepth << "\n"
248 << "\\tocdepth " << h_tocdepth << "\n"
249 << "\\paragraph_separation " << h_paragraph_separation << "\n"
250 << "\\defskip " << h_defskip << "\n"
251 << "\\quotes_language " << h_quotes_language << "\n"
252 << "\\quotes_times " << h_quotes_times << "\n"
253 << "\\papercolumns " << h_papercolumns << "\n"
254 << "\\papersides " << h_papersides << "\n"
255 << "\\paperpagestyle " << h_paperpagestyle << "\n"
256 << "\\tracking_changes " << h_tracking_changes << "\n"
258 // clear preamble for subdocuments
262 } // anonymous namespace
264 LyXTextClass const parse_preamble(Parser & p, ostream & os, string const & forceclass)
266 // initialize fixed types
267 special_columns['D'] = 3;
268 bool is_full_document = false;
270 // determine whether this is a full document or a fragment for inclusion
272 Token const & t = p.get_token();
274 if (t.cat() == catEscape && t.cs() == "documentclass") {
275 is_full_document = true;
281 while (is_full_document && p.good()) {
282 Token const & t = p.get_token();
285 cerr << "t: " << t << "\n";
291 if (t.cat() == catLetter ||
292 t.cat() == catSuper ||
294 t.cat() == catOther ||
295 t.cat() == catMath ||
296 t.cat() == catActive ||
297 t.cat() == catBegin ||
299 t.cat() == catAlign ||
300 t.cat() == catParameter)
301 h_preamble << t.character();
303 else if (t.cat() == catSpace || t.cat() == catNewline)
304 h_preamble << t.asInput();
306 else if (t.cat() == catComment)
307 h_preamble << t.asInput();
309 else if (t.cs() == "pagestyle")
310 h_paperpagestyle = p.verbatim_item();
312 else if (t.cs() == "makeatletter") {
313 p.setCatCode('@', catLetter);
314 h_preamble << "\\makeatletter";
317 else if (t.cs() == "makeatother") {
318 p.setCatCode('@', catOther);
319 h_preamble << "\\makeatother";
322 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
323 || t.cs() == "providecommand") {
325 if (p.next_token().character() == '*') {
329 string const name = p.verbatim_item();
330 string const opts = p.getOpt();
331 string const body = p.verbatim_item();
332 // only non-lyxspecific stuff
333 if ( name != "\\noun"
334 && name != "\\tabularnewline"
336 && name != "\\lyxline"
337 && name != "\\lyxaddress"
338 && name != "\\lyxrightaddress"
339 && name != "\\lyxdot"
340 && name != "\\boldsymbol"
341 && name != "\\lyxarrow") {
343 ss << '\\' << t.cs();
346 ss << '{' << name << '}' << opts << '{' << body << "}";
347 h_preamble << ss.str();
349 ostream & out = in_preamble ? h_preamble : os;
350 out << "\\" << t.cs() << "{" << name << "}"
351 << opts << "{" << body << "}";
356 else if (t.cs() == "documentclass") {
358 split(p.getArg('[', ']'), opts, ',');
359 handle_opt(opts, known_languages, h_language);
360 handle_opt(opts, known_fontsizes, h_paperfontsize);
361 h_quotes_language = h_language;
362 h_options = join(opts, ",");
363 h_textclass = p.getArg('{', '}');
366 else if (t.cs() == "usepackage") {
367 string const options = p.getArg('[', ']');
368 string const name = p.getArg('{', '}');
369 if (options.empty() && name.find(',')) {
370 vector<string> vecnames;
371 split(name, vecnames, ',');
372 vector<string>::const_iterator it = vecnames.begin();
373 vector<string>::const_iterator end = vecnames.end();
374 for (; it != end; ++it)
375 handle_package(trim(*it), string());
377 handle_package(name, options);
381 else if (t.cs() == "newenvironment") {
382 string const name = p.getArg('{', '}');
384 ss << "\\newenvironment{" << name << "}";
387 ss << '{' << p.verbatim_item() << '}';
388 ss << '{' << p.verbatim_item() << '}';
389 if (name != "lyxcode" && name != "lyxlist"
390 && name != "lyxrightadress" && name != "lyxaddress")
391 h_preamble << ss.str();
394 else if (t.cs() == "def") {
395 string name = p.get_token().cs();
396 while (p.next_token().cat() != catBegin)
397 name += p.get_token().asString();
398 h_preamble << "\\def\\" << name << '{' << p.verbatim_item() << "}";
401 else if (t.cs() == "newcolumntype") {
402 string const name = p.getArg('{', '}');
405 string opts = p.getOpt();
407 istringstream is(string(opts, 1));
408 //cerr << "opt: " << is.str() << "\n";
411 special_columns[name[0]] = nargs;
412 h_preamble << "\\newcolumntype{" << name << "}";
414 h_preamble << "[" << nargs << "]";
415 h_preamble << "{" << p.verbatim_item() << "}";
418 else if (t.cs() == "setcounter") {
419 string const name = p.getArg('{', '}');
420 string const content = p.getArg('{', '}');
421 if (name == "secnumdepth")
422 h_secnumdepth = content;
423 else if (name == "tocdepth")
424 h_tocdepth = content;
426 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
429 else if (t.cs() == "setlength") {
430 string const name = p.verbatim_item();
431 string const content = p.verbatim_item();
433 if (name == "parskip")
434 h_paragraph_separation = "skip";
435 else if (name == "parindent")
436 h_paragraph_separation = "skip";
438 h_preamble << "\\setlength{" << name << "}{" << content << "}";
441 else if (t.cs() == "begin") {
442 string const name = p.getArg('{', '}');
443 if (name == "document")
445 h_preamble << "\\begin{" << name << "}";
448 else if (t.cs() == "jurabibsetup") {
449 vector<string> jurabibsetup =
450 split_options(p.getArg('{', '}'));
451 // add jurabibsetup to the jurabib package options
452 add_package("jurabib", jurabibsetup);
453 if (!jurabibsetup.empty()) {
454 h_preamble << "\\jurabibsetup{"
455 << join(jurabibsetup, ",") << '}';
459 else if (!t.cs().empty())
460 h_preamble << '\\' << t.cs();
464 // Force textclass if the user wanted it
465 if (!forceclass.empty()) {
466 h_textclass = forceclass;
468 string layoutfilename = LibFileSearch("layouts", h_textclass, "layout");
469 if (layoutfilename.empty()) {
470 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
473 LyXTextClass textclass;
474 textclass.Read(layoutfilename);
475 if (h_papersides.empty()) {
477 ss << textclass.sides();
478 h_papersides = ss.str();
480 end_preamble(os, textclass);