]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/preamble.C
handle special columns
[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         while (p.good()) {
171                 Token const & t = p.get_token();
172
173 #ifdef FILEDEBUG
174                 cerr << "t: " << t << " flags: " << flags << "\n";
175                 //cell->dump();
176 #endif
177
178                 //
179                 // cat codes
180                 //
181                 if (t.cat() == catLetter ||
182                           t.cat() == catSpace ||
183                           t.cat() == catSuper ||
184                           t.cat() == catSub ||
185                           t.cat() == catOther ||
186                           t.cat() == catMath ||
187                           t.cat() == catActive ||
188                           t.cat() == catBegin ||
189                           t.cat() == catEnd ||
190                           t.cat() == catAlign ||
191                           t.cat() == catNewline ||
192                           t.cat() == catParameter)
193                 h_preamble << t.character();
194
195                 else if (t.cat() == catComment)
196                         handle_comment(p);
197
198                 else if (t.cs() == "pagestyle")
199                         h_paperpagestyle == p.verbatim_item();
200
201                 else if (t.cs() == "makeatletter") {
202                         p.setCatCode('@', catLetter);
203                         h_preamble << "\\makeatletter\n";
204                 }
205
206                 else if (t.cs() == "makeatother") {
207                         p.setCatCode('@', catOther);
208                         h_preamble << "\\makeatother\n";
209                 }
210
211                 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
212                             || t.cs() == "providecommand") {
213                         bool star = false;
214                         if (p.next_token().character() == '*') {
215                                 p.get_token();
216                                 star = true;
217                         }
218                         string const name = p.verbatim_item();
219                         string const opts = p.getOpt();
220                         string const body = p.verbatim_item();
221                         // only non-lyxspecific stuff
222                         if (name != "\\noun "
223                                   && name != "\\tabularnewline "
224                             && name != "\\LyX "
225                                   && name != "\\lyxline "
226                                   && name != "\\lyxaddress "
227                                   && name != "\\lyxrightaddress "
228                                   && name != "\\boldsymbol "
229                                   && name != "\\lyxarrow ") {
230                                 ostringstream ss;
231                                 ss << '\\' << t.cs();
232                                 if (star)
233                                         ss << '*';
234                                 ss << '{' << name << '}' << opts << '{' << body << "}\n";
235                                 h_preamble << ss.str();
236 /*
237                                 ostream & out = in_preamble ? h_preamble : os;
238                                 out << "\\" << t.cs() << "{" << name << "}"
239                                     << opts << "{" << body << "}\n";
240 */
241                         }
242                 }
243
244                 else if (t.cs() == "documentclass") {
245                         vector<string> opts;
246                         split(p.getArg('[', ']'), opts, ',');
247                         handle_opt(opts, known_languages, h_language);
248                         handle_opt(opts, known_fontsizes, h_paperfontsize);
249                         h_quotes_language = h_language;
250                         h_options = join(opts, ",");
251                         h_textclass = p.getArg('{', '}');
252                 }
253
254                 else if (t.cs() == "usepackage") {
255                         string const options = p.getArg('[', ']');
256                         string const name = p.getArg('{', '}');
257                         if (options.empty() && name.find(',')) {
258                                 vector<string> vecnames;
259                                 split(name, vecnames, ',');
260                                 vector<string>::const_iterator it  = vecnames.begin();
261                                 vector<string>::const_iterator end = vecnames.end();
262                                 for (; it != end; ++it)
263                                         handle_package(trim(*it), string());
264                         } else {
265                                 handle_package(name, options);
266                         }
267                 }
268
269                 else if (t.cs() == "newenvironment") {
270                         string const name = p.getArg('{', '}');
271                         ostringstream ss;
272                         ss << "\\newenvironment{" << name << "}";
273                         ss << p.getOpt();
274                         ss << p.getOpt();
275                         ss << '{' << p.verbatim_item() << '}';
276                         ss << '{' << p.verbatim_item() << '}';
277                         ss << '\n';
278                         if (name != "lyxcode" && name != "lyxlist"
279                                         && name != "lyxrightadress" && name != "lyxaddress")
280                                 h_preamble << ss.str();
281                 }
282
283                 else if (t.cs() == "def") {
284                         string name = p.get_token().cs();
285                         while (p.next_token().cat() != catBegin)
286                                 name += p.get_token().asString();
287                         h_preamble << "\\def\\" << name << '{' << p.verbatim_item() << "}\n";
288                 }
289
290                 else if (t.cs() == "newcolumntype") {
291                         string const name = p.getArg('{', '}');
292                         trim(name);
293                         int nargs = 0;
294                         string opts = p.getOpt();
295                         if (opts.size()) {
296                                 istringstream is(string(opts, 1));
297                                 //cerr << "opt: " << is.str() << "\n";
298                                 is >> nargs;
299                         }
300                         special_columns[name[0]] = nargs;
301                         h_preamble << "\\newcolumntype{" << name << "}";
302                         if (nargs)
303                                 h_preamble << "[" << nargs << "]";
304                         h_preamble << "{" << p.verbatim_item() << "}\n";
305                 }
306
307                 else if (t.cs() == "setcounter") {
308                         string const name = p.getArg('{', '}');
309                         string const content = p.getArg('{', '}');
310                         if (name == "secnumdepth")
311                                 h_secnumdepth = content;
312                         else if (name == "tocdepth")
313                                 h_tocdepth = content;
314                         else
315                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
316                 }
317
318                 else if (t.cs() == "setlength") {
319                         string const name = p.verbatim_item();
320                         string const content = p.verbatim_item();
321                         if (name == "parskip")
322                                 h_paragraph_separation = "skip";
323                         else if (name == "parindent")
324                                 h_paragraph_separation = "skip";
325                         else
326                                 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
327                 }
328
329                 else if (t.cs() == "par")
330                         h_preamble << '\n';
331
332                 else if (t.cs() == "begin") {
333                         string const name = p.getArg('{', '}');
334                         if (name == "document") {
335                                 end_preamble(os);
336                                 return;
337                         }
338                         h_preamble << "\\begin{" << name << "}";
339                 }
340
341                 else if (t.cs().size())
342                         h_preamble << '\\' << t.cs() << ' ';
343         }
344 }
345
346
347 // }])