]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/preamble.C
* text.C (check_layout): \layout -> \begin_layout
[lyx.git] / src / tex2lyx / preamble.C
1 /** The .tex to .lyx converter
2     \author André Pönitz (2003)
3  */
4
5 // {[(
6
7 #include <config.h>
8
9 #include "tex2lyx.h"
10
11 #include "layout.h"
12 #include "lyxtextclass.h"
13 #include "lyxlex.h"
14 #include "support/filetools.h"
15
16 #include <algorithm>
17 #include <iostream>
18 #include <sstream>
19 #include <string>
20 #include <vector>
21 #include <map>
22
23 using std::cerr;
24 using std::endl;
25 using std::getline;
26 using std::istream;
27 using std::istringstream;
28 using std::ostream;
29 using std::ostringstream;
30 using std::string;
31 using std::vector;
32
33 using lyx::support::LibFileSearch;
34
35 // special columntypes
36 extern std::map<char, int> special_columns;
37
38 namespace {
39
40 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
41 "breton", "british", "bulgarian", "catalan", "croatian", "czech", "danish",
42 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
43 "frenchb", "galician", "german", "germanb", "greek", "hebcal", "hebfont",
44 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
45 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
46 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
47 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
48 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
49 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
50 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
51 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
52
53 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
54
55 // some ugly stuff
56 ostringstream h_preamble;
57 string h_textclass               = "article";
58 string h_options                 = string();
59 string h_language                = "english";
60 string h_inputencoding           = "latin1";
61 string h_fontscheme              = "default";
62 string h_graphics                = "default";
63 string h_paperfontsize           = "default";
64 string h_spacing                 = "single";
65 string h_papersize               = "default";
66 string h_paperpackage            = "default";
67 string h_use_geometry            = "0";
68 string h_use_amsmath             = "0";
69 string h_use_natbib              = "0";
70 string h_use_numerical_citations = "0";
71 string h_paperorientation        = "portrait";
72 string h_secnumdepth             = "3";
73 string h_tocdepth                = "3";
74 string h_paragraph_separation    = "indent";
75 string h_defskip                 = "medskip";
76 string h_quotes_language         = "english";
77 string h_quotes_times            = "2";
78 string h_papercolumns            = "1";
79 string h_papersides              = "1";
80 string h_paperpagestyle          = "default";
81 string h_tracking_changes        = "0";
82
83
84 void handle_opt(vector<string> & opts, char const ** what, string & target)
85 {
86         if (opts.empty())
87                 return;
88
89         for ( ; *what; ++what) {
90                 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
91                 if (it != opts.end()) {
92                         //cerr << "### found option '" << *what << "'\n";
93                         target = *what;
94                         opts.erase(it);
95                         return;
96                 }
97         }
98 }
99
100
101 void handle_package(string const & name, string const & options)
102 {
103         //cerr << "handle_package: '" << name << "'\n";
104         if (name == "a4wide") {
105                 h_papersize = "a4paper";
106                 h_paperpackage = "widemarginsa4";
107         } else if (name == "ae")
108                 h_fontscheme = "ae";
109         else if (name == "aecompl")
110                 h_fontscheme = "ae";
111         else if (name == "amsmath")
112                 h_use_amsmath = "1";
113         else if (name == "amssymb")
114                 h_use_amsmath = "1";
115         else if (name == "babel")
116                 ; // ignore this
117         else if (name == "fontenc")
118                 ; // ignore this
119         else if (name == "inputenc")
120                 h_inputencoding = options;
121         else if (name == "makeidx")
122                 ; // ignore this
123         else if (name == "verbatim")
124                 ; // ignore this
125         else if (is_known(name, known_languages)) {
126                 h_language = name;
127                 h_quotes_language = name;
128         } else {
129                 if (options.size())
130                         h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
131                 else
132                         h_preamble << "\\usepackage{" << name << "}\n";
133         }
134 }
135
136
137
138 void end_preamble(ostream & os, LyXTextClass const & /*textclass*/)
139 {
140         os << "# tex2lyx 0.1.0 created this file\n"
141            << "\\lyxformat 225\n"
142            << "\\textclass " << h_textclass << "\n"
143            << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
144         if (h_options.size())
145            os << "\\options " << h_options << "\n";
146         os << "\\language " << h_language << "\n"
147            << "\\inputencoding " << h_inputencoding << "\n"
148            << "\\fontscheme " << h_fontscheme << "\n"
149            << "\\graphics " << h_graphics << "\n"
150            << "\\paperfontsize " << h_paperfontsize << "\n"
151            << "\\spacing " << h_spacing << "\n"
152            << "\\papersize " << h_papersize << "\n"
153            << "\\paperpackage " << h_paperpackage << "\n"
154            << "\\use_geometry " << h_use_geometry << "\n"
155            << "\\use_amsmath " << h_use_amsmath << "\n"
156            << "\\use_natbib " << h_use_natbib << "\n"
157            << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
158            << "\\paperorientation " << h_paperorientation << "\n"
159            << "\\secnumdepth " << h_secnumdepth << "\n"
160            << "\\tocdepth " << h_tocdepth << "\n"
161            << "\\paragraph_separation " << h_paragraph_separation << "\n"
162            << "\\defskip " << h_defskip << "\n"
163            << "\\quotes_language " << h_quotes_language << "\n"
164            << "\\quotes_times " << h_quotes_times << "\n"
165            << "\\papercolumns " << h_papercolumns << "\n"
166            << "\\papersides " << h_papersides << "\n"
167            << "\\paperpagestyle " << h_paperpagestyle << "\n"
168            << "\\tracking_changes " << h_tracking_changes << "\n"
169            << "\\end_header";
170 }
171
172 } // anonymous namespace
173
174 LyXTextClass const parse_preamble(Parser & p, ostream & os)
175 {
176         // initialize fixed types
177         special_columns['D'] = 3;
178
179         while (p.good()) {
180                 Token const & t = p.get_token();
181
182 #ifdef FILEDEBUG
183                 cerr << "t: " << t << " flags: " << flags << "\n";
184                 //cell->dump();
185 #endif
186
187                 //
188                 // cat codes
189                 //
190                 if (t.cat() == catLetter ||
191                           t.cat() == catSpace ||
192                           t.cat() == catSuper ||
193                           t.cat() == catSub ||
194                           t.cat() == catOther ||
195                           t.cat() == catMath ||
196                           t.cat() == catActive ||
197                           t.cat() == catBegin ||
198                           t.cat() == catEnd ||
199                           t.cat() == catAlign ||
200                           t.cat() == catNewline ||
201                           t.cat() == catParameter)
202                 h_preamble << t.character();
203
204                 else if (t.cat() == catComment)
205                         handle_comment(p);
206
207                 else if (t.cs() == "pagestyle")
208                         h_paperpagestyle == p.verbatim_item();
209
210                 else if (t.cs() == "makeatletter") {
211                         p.setCatCode('@', catLetter);
212                         h_preamble << "\\makeatletter\n";
213                 }
214
215                 else if (t.cs() == "makeatother") {
216                         p.setCatCode('@', catOther);
217                         h_preamble << "\\makeatother\n";
218                 }
219
220                 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
221                             || t.cs() == "providecommand") {
222                         bool star = false;
223                         if (p.next_token().character() == '*') {
224                                 p.get_token();
225                                 star = true;
226                         }
227                         string const name = p.verbatim_item();
228                         string const opts = p.getOpt();
229                         string const body = p.verbatim_item();
230                         // only non-lyxspecific stuff
231                         if (name != "\\noun "
232                                   && name != "\\tabularnewline "
233                             && name != "\\LyX "
234                                   && name != "\\lyxline "
235                                   && name != "\\lyxaddress "
236                                   && name != "\\lyxrightaddress "
237                                   && name != "\\boldsymbol "
238                                   && name != "\\lyxarrow ") {
239                                 ostringstream ss;
240                                 ss << '\\' << t.cs();
241                                 if (star)
242                                         ss << '*';
243                                 ss << '{' << name << '}' << opts << '{' << body << "}\n";
244                                 h_preamble << ss.str();
245 /*
246                                 ostream & out = in_preamble ? h_preamble : os;
247                                 out << "\\" << t.cs() << "{" << name << "}"
248                                     << opts << "{" << body << "}\n";
249 */
250                         }
251                 }
252
253                 else if (t.cs() == "documentclass") {
254                         vector<string> opts;
255                         split(p.getArg('[', ']'), opts, ',');
256                         handle_opt(opts, known_languages, h_language);
257                         handle_opt(opts, known_fontsizes, h_paperfontsize);
258                         h_quotes_language = h_language;
259                         h_options = join(opts, ",");
260                         h_textclass = p.getArg('{', '}');
261                 }
262
263                 else if (t.cs() == "usepackage") {
264                         string const options = p.getArg('[', ']');
265                         string const name = p.getArg('{', '}');
266                         if (options.empty() && name.find(',')) {
267                                 vector<string> vecnames;
268                                 split(name, vecnames, ',');
269                                 vector<string>::const_iterator it  = vecnames.begin();
270                                 vector<string>::const_iterator end = vecnames.end();
271                                 for (; it != end; ++it)
272                                         handle_package(trim(*it), string());
273                         } else {
274                                 handle_package(name, options);
275                         }
276                 }
277
278                 else if (t.cs() == "newenvironment") {
279                         string const name = p.getArg('{', '}');
280                         ostringstream ss;
281                         ss << "\\newenvironment{" << name << "}";
282                         ss << p.getOpt();
283                         ss << p.getOpt();
284                         ss << '{' << p.verbatim_item() << '}';
285                         ss << '{' << p.verbatim_item() << '}';
286                         ss << '\n';
287                         if (name != "lyxcode" && name != "lyxlist"
288                                         && name != "lyxrightadress" && name != "lyxaddress")
289                                 h_preamble << ss.str();
290                 }
291
292                 else if (t.cs() == "def") {
293                         string name = p.get_token().cs();
294                         while (p.next_token().cat() != catBegin)
295                                 name += p.get_token().asString();
296                         h_preamble << "\\def\\" << name << '{' << p.verbatim_item() << "}\n";
297                 }
298
299                 else if (t.cs() == "newcolumntype") {
300                         string const name = p.getArg('{', '}');
301                         trim(name);
302                         int nargs = 0;
303                         string opts = p.getOpt();
304                         if (opts.size()) {
305                                 istringstream is(string(opts, 1));
306                                 //cerr << "opt: " << is.str() << "\n";
307                                 is >> nargs;
308                         }
309                         special_columns[name[0]] = nargs;
310                         h_preamble << "\\newcolumntype{" << name << "}";
311                         if (nargs)
312                                 h_preamble << "[" << nargs << "]";
313                         h_preamble << "{" << p.verbatim_item() << "}\n";
314                 }
315
316                 else if (t.cs() == "setcounter") {
317                         string const name = p.getArg('{', '}');
318                         string const content = p.getArg('{', '}');
319                         if (name == "secnumdepth")
320                                 h_secnumdepth = content;
321                         else if (name == "tocdepth")
322                                 h_tocdepth = content;
323                         else
324                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
325                 }
326
327                 else if (t.cs() == "setlength") {
328                         string const name = p.verbatim_item();
329                         string const content = p.verbatim_item();
330                         if (name == "parskip")
331                                 h_paragraph_separation = "skip";
332                         else if (name == "parindent")
333                                 h_paragraph_separation = "skip";
334                         else
335                                 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
336                 }
337
338                 else if (t.cs() == "par")
339                         h_preamble << '\n';
340
341                 else if (t.cs() == "begin") {
342                         string const name = p.getArg('{', '}');
343                         if (name == "document")
344                                 break;
345                         h_preamble << "\\begin{" << name << "}";
346                 }
347
348                 else if (t.cs().size())
349                         h_preamble << '\\' << t.cs() << ' ';
350         }
351
352         LyXTextClass textclass;
353         textclass.Read(LibFileSearch("layouts", h_textclass, "layout"));
354         end_preamble(os, textclass);
355         return textclass;
356 }
357
358 // }])