]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/preamble.C
If I ever see another licence blurb again, it'll be too soon...
[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::cerr;
30 using std::endl;
31 using std::getline;
32 using std::istream;
33 using std::istringstream;
34 using std::ostream;
35 using std::ostringstream;
36 using std::string;
37 using std::vector;
38
39 using lyx::support::LibFileSearch;
40
41 // special columntypes
42 extern std::map<char, int> special_columns;
43
44 namespace {
45
46 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
47 "breton", "british", "bulgarian", "catalan", "croatian", "czech", "danish",
48 "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 * 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 string h_papersize               = "default";
72 string h_paperpackage            = "default";
73 string h_use_geometry            = "0";
74 string h_use_amsmath             = "0";
75 string h_use_natbib              = "0";
76 string h_use_numerical_citations = "0";
77 string h_paperorientation        = "portrait";
78 string h_secnumdepth             = "3";
79 string h_tocdepth                = "3";
80 string h_paragraph_separation    = "indent";
81 string h_defskip                 = "medskip";
82 string h_quotes_language         = "english";
83 string h_quotes_times            = "2";
84 string h_papercolumns            = "1";
85 string h_papersides              = "1";
86 string h_paperpagestyle          = "default";
87 string h_tracking_changes        = "0";
88
89
90 void handle_opt(vector<string> & opts, char const ** what, string & target)
91 {
92         if (opts.empty())
93                 return;
94
95         for ( ; *what; ++what) {
96                 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
97                 if (it != opts.end()) {
98                         //cerr << "### found option '" << *what << "'\n";
99                         target = *what;
100                         opts.erase(it);
101                         return;
102                 }
103         }
104 }
105
106
107 void handle_package(string const & name, string const & options)
108 {
109         //cerr << "handle_package: '" << name << "'\n";
110         if (name == "a4wide") {
111                 h_papersize = "a4paper";
112                 h_paperpackage = "widemarginsa4";
113         } else if (name == "ae")
114                 h_fontscheme = "ae";
115         else if (name == "aecompl")
116                 h_fontscheme = "ae";
117         else if (name == "amsmath")
118                 h_use_amsmath = "1";
119         else if (name == "amssymb")
120                 h_use_amsmath = "1";
121         else if (name == "babel")
122                 ; // ignore this
123         else if (name == "fontenc")
124                 ; // ignore this
125         else if (name == "inputenc")
126                 h_inputencoding = options;
127         else if (name == "makeidx")
128                 ; // ignore this
129         else if (name == "verbatim")
130                 ; // ignore this
131         else if (is_known(name, known_languages)) {
132                 h_language = name;
133                 h_quotes_language = name;
134         } else {
135                 if (options.size())
136                         h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
137                 else
138                         h_preamble << "\\usepackage{" << name << "}\n";
139         }
140 }
141
142
143
144 void end_preamble(ostream & os, LyXTextClass const & /*textclass*/)
145 {
146         os << "#LyX file created by  tex2lyx 0.1.2 \n"
147            << "\\lyxformat 225\n"
148            << "\\textclass " << h_textclass << "\n"
149            << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
150         if (h_options.size())
151            os << "\\options " << h_options << "\n";
152         os << "\\language " << h_language << "\n"
153            << "\\inputencoding " << h_inputencoding << "\n"
154            << "\\fontscheme " << h_fontscheme << "\n"
155            << "\\graphics " << h_graphics << "\n"
156            << "\\paperfontsize " << h_paperfontsize << "\n"
157            << "\\spacing " << h_spacing << "\n"
158            << "\\papersize " << h_papersize << "\n"
159            << "\\paperpackage " << h_paperpackage << "\n"
160            << "\\use_geometry " << h_use_geometry << "\n"
161            << "\\use_amsmath " << h_use_amsmath << "\n"
162            << "\\use_natbib " << h_use_natbib << "\n"
163            << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
164            << "\\paperorientation " << h_paperorientation << "\n"
165            << "\\secnumdepth " << h_secnumdepth << "\n"
166            << "\\tocdepth " << h_tocdepth << "\n"
167            << "\\paragraph_separation " << h_paragraph_separation << "\n"
168            << "\\defskip " << h_defskip << "\n"
169            << "\\quotes_language " << h_quotes_language << "\n"
170            << "\\quotes_times " << h_quotes_times << "\n"
171            << "\\papercolumns " << h_papercolumns << "\n"
172            << "\\papersides " << h_papersides << "\n"
173            << "\\paperpagestyle " << h_paperpagestyle << "\n"
174            << "\\tracking_changes " << h_tracking_changes << "\n"
175            << "\\end_header\n";
176 }
177
178 } // anonymous namespace
179
180 LyXTextClass const parse_preamble(Parser & p, ostream & os)
181 {
182         // initialize fixed types
183         special_columns['D'] = 3;
184
185         while (p.good()) {
186                 Token const & t = p.get_token();
187
188 #ifdef FILEDEBUG
189                 cerr << "t: " << t << " flags: " << flags << "\n";
190                 //cell->dump();
191 #endif
192
193                 //
194                 // cat codes
195                 //
196                 if (t.cat() == catLetter ||
197                           t.cat() == catSpace ||
198                           t.cat() == catSuper ||
199                           t.cat() == catSub ||
200                           t.cat() == catOther ||
201                           t.cat() == catMath ||
202                           t.cat() == catActive ||
203                           t.cat() == catBegin ||
204                           t.cat() == catEnd ||
205                           t.cat() == catAlign ||
206                           t.cat() == catNewline ||
207                           t.cat() == catParameter)
208                 h_preamble << t.character();
209
210                 else if (t.cat() == catComment)
211                         handle_comment(p);
212
213                 else if (t.cs() == "pagestyle")
214                         h_paperpagestyle == p.verbatim_item();
215
216                 else if (t.cs() == "makeatletter") {
217                         p.setCatCode('@', catLetter);
218                         h_preamble << "\\makeatletter\n";
219                 }
220
221                 else if (t.cs() == "makeatother") {
222                         p.setCatCode('@', catOther);
223                         h_preamble << "\\makeatother\n";
224                 }
225
226                 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
227                             || t.cs() == "providecommand") {
228                         bool star = false;
229                         if (p.next_token().character() == '*') {
230                                 p.get_token();
231                                 star = true;
232                         }
233                         string const name = p.verbatim_item();
234                         string const opts = p.getOpt();
235                         string const body = p.verbatim_item();
236                         // only non-lyxspecific stuff
237                         if (name != "\\noun "
238                                   && name != "\\tabularnewline "
239                             && name != "\\LyX "
240                                   && name != "\\lyxline "
241                                   && name != "\\lyxaddress "
242                                   && name != "\\lyxrightaddress "
243                                   && name != "\\boldsymbol "
244                                   && name != "\\lyxarrow ") {
245                                 ostringstream ss;
246                                 ss << '\\' << t.cs();
247                                 if (star)
248                                         ss << '*';
249                                 ss << '{' << name << '}' << opts << '{' << body << "}\n";
250                                 h_preamble << ss.str();
251 /*
252                                 ostream & out = in_preamble ? h_preamble : os;
253                                 out << "\\" << t.cs() << "{" << name << "}"
254                                     << opts << "{" << body << "}\n";
255 */
256                         }
257                 }
258
259                 else if (t.cs() == "documentclass") {
260                         vector<string> opts;
261                         split(p.getArg('[', ']'), opts, ',');
262                         handle_opt(opts, known_languages, h_language);
263                         handle_opt(opts, known_fontsizes, h_paperfontsize);
264                         h_quotes_language = h_language;
265                         h_options = join(opts, ",");
266                         h_textclass = p.getArg('{', '}');
267                 }
268
269                 else if (t.cs() == "usepackage") {
270                         string const options = p.getArg('[', ']');
271                         string const name = p.getArg('{', '}');
272                         if (options.empty() && name.find(',')) {
273                                 vector<string> vecnames;
274                                 split(name, vecnames, ',');
275                                 vector<string>::const_iterator it  = vecnames.begin();
276                                 vector<string>::const_iterator end = vecnames.end();
277                                 for (; it != end; ++it)
278                                         handle_package(trim(*it), string());
279                         } else {
280                                 handle_package(name, options);
281                         }
282                 }
283
284                 else if (t.cs() == "newenvironment") {
285                         string const name = p.getArg('{', '}');
286                         ostringstream ss;
287                         ss << "\\newenvironment{" << name << "}";
288                         ss << p.getOpt();
289                         ss << p.getOpt();
290                         ss << '{' << p.verbatim_item() << '}';
291                         ss << '{' << p.verbatim_item() << '}';
292                         ss << '\n';
293                         if (name != "lyxcode" && name != "lyxlist"
294                                         && name != "lyxrightadress" && name != "lyxaddress")
295                                 h_preamble << ss.str();
296                 }
297
298                 else if (t.cs() == "def") {
299                         string name = p.get_token().cs();
300                         while (p.next_token().cat() != catBegin)
301                                 name += p.get_token().asString();
302                         h_preamble << "\\def\\" << name << '{' << p.verbatim_item() << "}\n";
303                 }
304
305                 else if (t.cs() == "newcolumntype") {
306                         string const name = p.getArg('{', '}');
307                         trim(name);
308                         int nargs = 0;
309                         string opts = p.getOpt();
310                         if (opts.size()) {
311                                 istringstream is(string(opts, 1));
312                                 //cerr << "opt: " << is.str() << "\n";
313                                 is >> nargs;
314                         }
315                         special_columns[name[0]] = nargs;
316                         h_preamble << "\\newcolumntype{" << name << "}";
317                         if (nargs)
318                                 h_preamble << "[" << nargs << "]";
319                         h_preamble << "{" << p.verbatim_item() << "}\n";
320                 }
321
322                 else if (t.cs() == "setcounter") {
323                         string const name = p.getArg('{', '}');
324                         string const content = p.getArg('{', '}');
325                         if (name == "secnumdepth")
326                                 h_secnumdepth = content;
327                         else if (name == "tocdepth")
328                                 h_tocdepth = content;
329                         else
330                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
331                 }
332
333                 else if (t.cs() == "setlength") {
334                         string const name = p.verbatim_item();
335                         string const content = p.verbatim_item();
336                         if (name == "parskip")
337                                 h_paragraph_separation = "skip";
338                         else if (name == "parindent")
339                                 h_paragraph_separation = "skip";
340                         else
341                                 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
342                 }
343
344                 else if (t.cs() == "par")
345                         h_preamble << '\n';
346
347                 else if (t.cs() == "begin") {
348                         string const name = p.getArg('{', '}');
349                         if (name == "document")
350                                 break;
351                         h_preamble << "\\begin{" << name << "}";
352                 }
353
354                 else if (t.cs().size())
355                         h_preamble << '\\' << t.cs() << ' ';
356         }
357
358         LyXTextClass textclass;
359         textclass.Read(LibFileSearch("layouts", h_textclass, "layout"));
360         end_preamble(os, textclass);
361         return textclass;
362 }
363
364 // }])