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