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