]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/preamble.cpp
preamble.cpp, text.cpp:
[lyx.git] / src / tex2lyx / preamble.cpp
1 /**
2  * \file preamble.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 // {[(
13
14 #include <config.h>
15
16 #include "tex2lyx.h"
17
18 #include "Layout.h"
19 #include "Lexer.h"
20 #include "TextClass.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23
24 #include <algorithm>
25 #include <iostream>
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 #include <map>
30
31
32 namespace lyx {
33
34 using std::istringstream;
35 using std::ostream;
36 using std::ostringstream;
37 using std::string;
38 using std::vector;
39 using std::cerr;
40 using std::endl;
41 using std::find;
42
43 using support::FileName;
44 using support::libFileSearch;
45
46 // special columntypes
47 extern std::map<char, int> special_columns;
48
49 std::map<string, vector<string> > used_packages;
50
51 namespace {
52
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};
66
67 const char * const known_french_languages[] = {"french", "frenchb", "francais",
68                                                "frenchle", "frenchpro", 0};
69 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
70
71 // some ugly stuff
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";
97
98
99 void handle_opt(vector<string> & opts, char const * const * what, string & target)
100 {
101         if (opts.empty())
102                 return;
103
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) {
112                                 target = *what;
113                                 position = it;
114                         }
115                         // remove found options from the list
116                         opts.erase(it);
117                 }
118         }
119 }
120
121
122 /*!
123  * Split a package options string (keyval format) into a vector.
124  * Example input:
125  *   authorformat=smallcaps,
126  *   commabeforerest,
127  *   titleformat=colonsep,
128  *   bibformat={tabular,ibidem,numbered}
129  */
130 vector<string> split_options(string const & input)
131 {
132         vector<string> options;
133         string option;
134         Parser p(input);
135         while (p.good()) {
136                 Token const & t = p.get_token();
137                 if (t.asInput() == ",") {
138                         options.push_back(trim(option));
139                         option.erase();
140                 } else if (t.asInput() == "=") {
141                         option += '=';
142                         p.skip_spaces(true);
143                         if (p.next_token().asInput() == "{")
144                                 option += '{' + p.getArg('{', '}') + '}';
145                 } else if (t.cat() != catSpace)
146                         option += t.asInput();
147         }
148
149         if (!option.empty())
150                 options.push_back(trim(option));
151
152         return options;
153 }
154
155
156 /*!
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.
159  */
160 void add_package(string const & name, vector<string> & options)
161 {
162         // every package inherits the global options
163         if (used_packages.find(name) == used_packages.end())
164                 used_packages[name] = split_options(h_options);
165
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());
175         }
176 }
177
178
179 void handle_package(string const & name, string const & opts)
180 {
181         vector<string> options = split_options(opts);
182         add_package(name, options);
183
184         //cerr << "handle_package: '" << name << "'\n";
185         if (name == "ae")
186                 h_fontscheme = "ae";
187         else if (name == "aecompl")
188                 h_fontscheme = "ae";
189         else if (name == "amsmath")
190                 h_use_amsmath = "1";
191         else if (name == "amssymb")
192                 h_use_amsmath = "1";
193         else if (name == "babel")
194                 ; // ignore this
195         else if (name == "fontenc")
196                 ; // ignore this
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;
202                 options.clear();
203         } else if (name == "makeidx")
204                 ; // ignore this
205         else if (name == "verbatim")
206                 ; // ignore this
207         else if (name == "graphicx")
208                 ; // ignore this
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";
213                 } else {
214                         h_language = name;
215                         h_quotes_language = name;
216                 }
217
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())
223                         options.erase(it);
224                 else {
225                         it = find(options.begin(), options.end(), "numbers");
226                         if (it != options.end()) {
227                                 h_cite_engine = "natbib_numerical";
228                                 options.erase(it);
229                         }
230                 }
231         } else if (name == "jurabib") {
232                 h_cite_engine = "jurabib";
233         } else if (options.empty())
234                 h_preamble << "\\usepackage{" << name << "}\n";
235         else {
236                 h_preamble << "\\usepackage[" << opts << "]{" << name << "}\n";
237                 options.clear();
238         }
239
240         // We need to do something with the options...
241         if (!options.empty())
242                 cerr << "Ignoring options '" << join(options, ",")
243                      << "' of package " << name << '.' << endl;
244 }
245
246
247
248 void end_preamble(ostream & os, TextClass const & /*textclass*/)
249 {
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"
282            << "\\begin_body\n";
283         // clear preamble for subdocuments
284         h_preamble.str("");
285 }
286
287 } // anonymous namespace
288
289 TextClass const parse_preamble(Parser & p, ostream & os, string const & forceclass)
290 {
291         // initialize fixed types
292         special_columns['D'] = 3;
293         bool is_full_document = false;
294
295         // determine whether this is a full document or a fragment for inclusion
296         while (p.good()) {
297                 Token const & t = p.get_token();
298
299                 if (t.cat() == catEscape && t.cs() == "documentclass") {
300                         is_full_document = true;
301                         break;
302                 }
303         }
304         p.reset();
305
306         while (is_full_document && p.good()) {
307                 Token const & t = p.get_token();
308
309 #ifdef FILEDEBUG
310                 cerr << "t: " << t << "\n";
311 #endif
312
313                 //
314                 // cat codes
315                 //
316                 if (t.cat() == catLetter ||
317                           t.cat() == catSuper ||
318                           t.cat() == catSub ||
319                           t.cat() == catOther ||
320                           t.cat() == catMath ||
321                           t.cat() == catActive ||
322                           t.cat() == catBegin ||
323                           t.cat() == catEnd ||
324                           t.cat() == catAlign ||
325                           t.cat() == catParameter)
326                 h_preamble << t.character();
327
328                 else if (t.cat() == catSpace || t.cat() == catNewline)
329                         h_preamble << t.asInput();
330
331                 else if (t.cat() == catComment)
332                         h_preamble << t.asInput();
333
334                 else if (t.cs() == "pagestyle")
335                         h_paperpagestyle = p.verbatim_item();
336
337                 else if (t.cs() == "makeatletter") {
338                         p.setCatCode('@', catLetter);
339                         h_preamble << "\\makeatletter";
340                 }
341
342                 else if (t.cs() == "makeatother") {
343                         p.setCatCode('@', catOther);
344                         h_preamble << "\\makeatother";
345                 }
346
347                 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
348                             || t.cs() == "providecommand") {
349                         bool star = false;
350                         if (p.next_token().character() == '*') {
351                                 p.get_token();
352                                 star = true;
353                         }
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"
361                             && name != "\\LyX"
362                             && name != "\\lyxline"
363                             && name != "\\lyxaddress"
364                             && name != "\\lyxrightaddress"
365                             && name != "\\lyxdot"
366                             && name != "\\boldsymbol"
367                             && name != "\\lyxarrow") {
368                                 ostringstream ss;
369                                 ss << '\\' << t.cs();
370                                 if (star)
371                                         ss << '*';
372                                 ss << '{' << name << '}' << opt1 << opt2
373                                    << '{' << body << "}";
374                                 h_preamble << ss.str();
375
376                                 // Add the command to the known commands
377                                 add_known_command(name, opt1, !opt2.empty());
378 /*
379                                 ostream & out = in_preamble ? h_preamble : os;
380                                 out << "\\" << t.cs() << "{" << name << "}"
381                                     << opts << "{" << body << "}";
382 */
383                         }
384                 }
385
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('{', '}');
399                 }
400
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());
411                         } else {
412                                 handle_package(name, options);
413                         }
414                 }
415
416                 else if (t.cs() == "newenvironment") {
417                         string const name = p.getArg('{', '}');
418                         ostringstream ss;
419                         ss << "\\newenvironment{" << name << "}";
420                         ss << p.getOpt();
421                         ss << p.getOpt();
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();
428                 }
429
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() << "}";
436                 }
437
438                 else if (t.cs() == "newcolumntype") {
439                         string const name = p.getArg('{', '}');
440                         trim(name);
441                         int nargs = 0;
442                         string opts = p.getOpt();
443                         if (!opts.empty()) {
444                                 istringstream is(string(opts, 1));
445                                 //cerr << "opt: " << is.str() << "\n";
446                                 is >> nargs;
447                         }
448                         special_columns[name[0]] = nargs;
449                         h_preamble << "\\newcolumntype{" << name << "}";
450                         if (nargs)
451                                 h_preamble << "[" << nargs << "]";
452                         h_preamble << "{" << p.verbatim_item() << "}";
453                 }
454
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;
462                         else
463                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
464                 }
465
466                 else if (t.cs() == "setlength") {
467                         string const name = p.verbatim_item();
468                         string const content = p.verbatim_item();
469                         // Is this correct?
470                         if (name == "parskip")
471                                 h_paragraph_separation = "skip";
472                         else if (name == "parindent")
473                                 h_paragraph_separation = "skip";
474                         else
475                                 h_preamble << "\\setlength{" << name << "}{" << content << "}";
476                 }
477
478                 else if (t.cs() == "begin") {
479                         string const name = p.getArg('{', '}');
480                         if (name == "document")
481                                 break;
482                         h_preamble << "\\begin{" << name << "}";
483                 }
484
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, ",") << '}';
493                         }
494                 }
495
496                 else if (!t.cs().empty())
497                         h_preamble << '\\' << t.cs();
498         }
499         p.skip_spaces();
500
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;
509                 exit(1);
510         }
511         TextClass textclass;
512         textclass.read(layoutfilename);
513         if (h_papersides.empty()) {
514                 ostringstream ss;
515                 ss << textclass.sides();
516                 h_papersides = ss.str();
517         }
518         end_preamble(os, textclass);
519         return textclass;
520 }
521
522 // }])
523
524
525 } // namespace lyx