]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/preamble.C
handle natbib and jurabib packages and citation commands
[lyx.git] / src / tex2lyx / preamble.C
1 /**
2  * \file preamble.C
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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 // {[(
12
13 #include <config.h>
14
15 #include "tex2lyx.h"
16
17 #include "layout.h"
18 #include "lyxtextclass.h"
19 #include "lyxlex.h"
20 #include "support/filetools.h"
21
22 #include <algorithm>
23 #include <iostream>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 #include <map>
28
29 using std::istringstream;
30 using std::ostream;
31 using std::ostringstream;
32 using std::string;
33 using std::vector;
34 using std::cerr;
35 using std::endl;
36
37 using lyx::support::LibFileSearch;
38
39 // special columntypes
40 extern std::map<char, int> special_columns;
41
42 std::map<string, vector<string> > used_packages;
43
44 namespace {
45
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};
58
59 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
60
61 // some ugly stuff
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";
89
90
91 void handle_opt(vector<string> & opts, char const * const * what, string & target)
92 {
93         if (opts.empty())
94                 return;
95
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";
100                         target = *what;
101                         opts.erase(it);
102                         return;
103                 }
104         }
105 }
106
107
108 /*!
109  * Split a package options string (keyval format) into a vector.
110  * Example input:
111  *   authorformat=smallcaps,
112  *   commabeforerest,
113  *   titleformat=colonsep,
114  *   bibformat={tabular,ibidem,numbered}
115  */
116 vector<string> split_options(string const & input)
117 {
118         vector<string> options;
119         string option;
120         Parser p(input);
121         while (p.good()) {
122                 Token const & t = p.get_token();
123                 if (t.asInput() == ",") {
124                         options.push_back(option);
125                         option.erase();
126                 } else if (t.asInput() == "=") {
127                         option += '=';
128                         p.skip_spaces(true);
129                         if (p.next_token().asInput() == "{")
130                                 option += '{' + p.getArg('{', '}') + '}';
131                 } else if (t.cat() != catSpace)
132                         option += t.asInput();
133         }
134         
135         if (!option.empty())
136                 options.push_back(option);
137
138         return options;
139 }
140
141
142 /*!
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.
145  */
146 void add_package(string const & name, vector<string> & options)
147 {
148         // every package inherits the global options
149         if (used_packages.find(name) == used_packages.end())
150                 used_packages[name] = split_options(h_options);
151
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());
161         }
162 }
163
164
165 void handle_package(string const & name, string const & opts)
166 {
167         vector<string> options = split_options(opts);
168         add_package(name, options);
169
170         //cerr << "handle_package: '" << name << "'\n";
171         if (name == "a4wide") {
172                 h_papersize = "a4paper";
173                 h_paperpackage = "widemarginsa4";
174         } else if (name == "ae")
175                 h_fontscheme = "ae";
176         else if (name == "aecompl")
177                 h_fontscheme = "ae";
178         else if (name == "amsmath")
179                 h_use_amsmath = "1";
180         else if (name == "amssymb")
181                 h_use_amsmath = "1";
182         else if (name == "babel")
183                 ; // ignore this
184         else if (name == "fontenc")
185                 ; // ignore this
186         else if (name == "inputenc") {
187                 h_inputencoding = opts;
188                 options.clear();
189         } else if (name == "makeidx")
190                 ; // ignore this
191         else if (name == "verbatim")
192                 ; // ignore this
193         else if (is_known(name, known_languages)) {
194                 h_language = name;
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())
201                         options.erase(it);
202                 else {
203                         it = find(options.begin(), options.end(), "numbers");
204                         if (it != options.end()) {
205                                 h_cite_engine = "natbib_numerical";
206                                 options.erase(it);
207                         }
208                 }
209         } else if (name == "jurabib") {
210                 h_cite_engine = "jurabib";
211         } else if (options.empty())
212                 h_preamble << "\\usepackage{" << name << "}\n";
213         else {
214                 h_preamble << "\\usepackage[" << opts << "]{" << name << "}\n";
215                 options.clear();
216         }
217
218         // We need to do something with the options...
219         if (!options.empty())
220                 cerr << "Ignoring options '" << join(options, ",")
221                      << "' of package " << name << '.' << endl;
222 }
223
224
225
226 void end_preamble(ostream & os, LyXTextClass const & /*textclass*/)
227 {
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"
257            << "\\end_header\n";
258         // clear preamble for subdocuments
259         h_preamble.str("");
260 }
261
262 } // anonymous namespace
263
264 LyXTextClass const parse_preamble(Parser & p, ostream & os, string const & forceclass)
265 {
266         // initialize fixed types
267         special_columns['D'] = 3;
268         bool is_full_document = false;
269
270         // determine whether this is a full document or a fragment for inclusion
271         while (p.good()) {
272                 Token const & t = p.get_token();
273
274                 if (t.cat() == catEscape && t.cs() == "documentclass") {
275                         is_full_document = true;
276                         break;
277                 }
278         }
279         p.reset();
280
281         while (is_full_document && p.good()) {
282                 Token const & t = p.get_token();
283
284 #ifdef FILEDEBUG
285                 cerr << "t: " << t << "\n";
286 #endif
287
288                 //
289                 // cat codes
290                 //
291                 if (t.cat() == catLetter ||
292                           t.cat() == catSuper ||
293                           t.cat() == catSub ||
294                           t.cat() == catOther ||
295                           t.cat() == catMath ||
296                           t.cat() == catActive ||
297                           t.cat() == catBegin ||
298                           t.cat() == catEnd ||
299                           t.cat() == catAlign ||
300                           t.cat() == catParameter)
301                 h_preamble << t.character();
302
303                 else if (t.cat() == catSpace || t.cat() == catNewline)
304                         h_preamble << t.asInput();
305
306                 else if (t.cat() == catComment)
307                         h_preamble << t.asInput();
308
309                 else if (t.cs() == "pagestyle")
310                         h_paperpagestyle = p.verbatim_item();
311
312                 else if (t.cs() == "makeatletter") {
313                         p.setCatCode('@', catLetter);
314                         h_preamble << "\\makeatletter";
315                 }
316
317                 else if (t.cs() == "makeatother") {
318                         p.setCatCode('@', catOther);
319                         h_preamble << "\\makeatother";
320                 }
321
322                 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
323                             || t.cs() == "providecommand") {
324                         bool star = false;
325                         if (p.next_token().character() == '*') {
326                                 p.get_token();
327                                 star = true;
328                         }
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"
335                             && name != "\\LyX"
336                             && name != "\\lyxline"
337                             && name != "\\lyxaddress"
338                             && name != "\\lyxrightaddress"
339                             && name != "\\lyxdot"
340                             && name != "\\boldsymbol"
341                             && name != "\\lyxarrow") {
342                                 ostringstream ss;
343                                 ss << '\\' << t.cs();
344                                 if (star)
345                                         ss << '*';
346                                 ss << '{' << name << '}' << opts << '{' << body << "}";
347                                 h_preamble << ss.str();
348 /*
349                                 ostream & out = in_preamble ? h_preamble : os;
350                                 out << "\\" << t.cs() << "{" << name << "}"
351                                     << opts << "{" << body << "}";
352 */
353                         }
354                 }
355
356                 else if (t.cs() == "documentclass") {
357                         vector<string> opts;
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('{', '}');
364                 }
365
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());
376                         } else {
377                                 handle_package(name, options);
378                         }
379                 }
380
381                 else if (t.cs() == "newenvironment") {
382                         string const name = p.getArg('{', '}');
383                         ostringstream ss;
384                         ss << "\\newenvironment{" << name << "}";
385                         ss << p.getOpt();
386                         ss << p.getOpt();
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();
392                 }
393
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() << "}";
399                 }
400
401                 else if (t.cs() == "newcolumntype") {
402                         string const name = p.getArg('{', '}');
403                         trim(name);
404                         int nargs = 0;
405                         string opts = p.getOpt();
406                         if (!opts.empty()) {
407                                 istringstream is(string(opts, 1));
408                                 //cerr << "opt: " << is.str() << "\n";
409                                 is >> nargs;
410                         }
411                         special_columns[name[0]] = nargs;
412                         h_preamble << "\\newcolumntype{" << name << "}";
413                         if (nargs)
414                                 h_preamble << "[" << nargs << "]";
415                         h_preamble << "{" << p.verbatim_item() << "}";
416                 }
417
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;
425                         else
426                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
427                 }
428
429                 else if (t.cs() == "setlength") {
430                         string const name = p.verbatim_item();
431                         string const content = p.verbatim_item();
432                         // Is this correct?
433                         if (name == "parskip")
434                                 h_paragraph_separation = "skip";
435                         else if (name == "parindent")
436                                 h_paragraph_separation = "skip";
437                         else
438                                 h_preamble << "\\setlength{" << name << "}{" << content << "}";
439                 }
440
441                 else if (t.cs() == "begin") {
442                         string const name = p.getArg('{', '}');
443                         if (name == "document")
444                                 break;
445                         h_preamble << "\\begin{" << name << "}";
446                 }
447
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, ",") << '}';
456                         }
457                 }
458
459                 else if (!t.cs().empty())
460                         h_preamble << '\\' << t.cs();
461         }
462         p.skip_spaces();
463
464         // Force textclass if the user wanted it
465         if (!forceclass.empty()) {
466                 h_textclass = forceclass;
467         }
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;
471                 exit(1);
472         }
473         LyXTextClass textclass;
474         textclass.Read(layoutfilename);
475         if (h_papersides.empty()) {
476                 ostringstream ss;
477                 ss << textclass.sides();
478                 h_papersides = ss.str();
479         }
480         end_preamble(os, textclass);
481         return textclass;
482 }
483
484 // }])