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