]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/preamble.C
*** empty log message ***
[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                 = "";
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            << "\\options " << h_options << "\n"
135            << "\\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 }
159
160
161 } // anonymous namespace
162
163 void parse_preamble(Parser & p, ostream & os)
164 {
165         while (p.good()) {
166                 Token const & t = p.getToken();
167
168 #ifdef FILEDEBUG
169                 cerr << "t: " << t << " flags: " << flags << "\n";
170                 //cell->dump();
171 #endif
172
173                 //
174                 // cat codes
175                 //
176                 if (t.cat() == catLetter ||
177                           t.cat() == catSpace ||
178                           t.cat() == catSuper ||
179                           t.cat() == catSub ||
180                           t.cat() == catOther ||
181                           t.cat() == catMath ||
182                           t.cat() == catActive ||
183                           t.cat() == catBegin ||
184                           t.cat() == catEnd ||
185                           t.cat() == catAlign ||
186                           t.cat() == catNewline ||
187                           t.cat() == catParameter)
188                 h_preamble << t.character();
189
190                 else if (t.cat() == catComment)
191                         handle_comment(p);
192
193                 else if (t.cs() == "pagestyle")
194                         h_paperpagestyle == p.verbatimItem();
195
196                 else if (t.cs() == "makeatletter") {
197                         p.setCatCode('@', catLetter);
198                         h_preamble << "\\makeatletter\n";
199                 }
200
201                 else if (t.cs() == "makeatother") {
202                         p.setCatCode('@', catOther);
203                         h_preamble << "\\makeatother\n";
204                 }
205
206                 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
207                             || t.cs() == "providecommand") {
208                         bool star = false;
209                         if (p.nextToken().character() == '*') {
210                                 p.getToken();
211                                 star = true;
212                         }
213                         string const name = p.verbatimItem();
214                         string const opts = p.getOpt();
215                         string const body = p.verbatimItem();
216                         // only non-lyxspecific stuff
217                         if (name != "\\noun "
218                                   && name != "\\tabularnewline "
219                             && name != "\\LyX "
220                                   && name != "\\lyxline "
221                                   && name != "\\lyxaddress "
222                                   && name != "\\lyxrightaddress "
223                                   && name != "\\boldsymbol "
224                                   && name != "\\lyxarrow ") {
225                                 ostringstream ss;
226                                 ss << '\\' << t.cs();
227                                 if (star)
228                                         ss << '*';
229                                 ss << '{' << name << '}' << opts << '{' << body << "}\n";
230                                 h_preamble << ss.str();
231 /*
232                                 ostream & out = in_preamble ? h_preamble : os;
233                                 out << "\\" << t.cs() << "{" << name << "}"
234                                     << opts << "{" << body << "}\n";
235 */
236                         }
237                 }
238
239                 else if (t.cs() == "documentclass") {
240                         vector<string> opts;
241                         split(p.getArg('[', ']'), opts, ',');
242                         handle_opt(opts, known_languages, h_language);
243                         handle_opt(opts, known_fontsizes, h_paperfontsize);
244                         h_quotes_language = h_language;
245                         h_options = join(opts, ",");
246                         h_textclass = p.getArg('{', '}');
247                 }
248
249                 else if (t.cs() == "usepackage") {
250                         string const options = p.getArg('[', ']');
251                         string const name = p.getArg('{', '}');
252                         if (options.empty() && name.find(',')) {
253                                 vector<string> vecnames;
254                                 split(name, vecnames, ',');
255                                 vector<string>::const_iterator it  = vecnames.begin();
256                                 vector<string>::const_iterator end = vecnames.end();
257                                 for (; it != end; ++it)
258                                         handle_package(trim(*it), string());
259                         } else {
260                                 handle_package(name, options);
261                         }
262                 }
263
264                 else if (t.cs() == "newenvironment") {
265                         string const name = p.getArg('{', '}');
266                         ostringstream ss;
267                         ss << "\\newenvironment{" << name << "}";
268                         ss << p.getOpt();
269                         ss << p.getOpt();
270                         ss << '{' << p.verbatimItem() << '}';
271                         ss << '{' << p.verbatimItem() << '}';
272                         ss << '\n';
273                         if (name != "lyxcode" && name != "lyxlist"
274                                         && name != "lyxrightadress" && name != "lyxaddress")
275                                 h_preamble << ss.str();
276                 }
277
278                 else if (t.cs() == "def") {
279                         string name = p.getToken().cs();
280                         while (p.nextToken().cat() != catBegin)
281                                 name += p.getToken().asString();
282                         h_preamble << "\\def\\" << name << '{' << p.verbatimItem() << "}\n";
283                 }
284
285                 else if (t.cs() == "setcounter") {
286                         string const name = p.getArg('{', '}');
287                         string const content = p.getArg('{', '}');
288                         if (name == "secnumdepth")
289                                 h_secnumdepth = content;
290                         else if (name == "tocdepth")
291                                 h_tocdepth = content;
292                         else
293                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
294                 }
295
296                 else if (t.cs() == "setlength") {
297                         string const name = p.verbatimItem();
298                         string const content = p.verbatimItem();
299                         if (name == "parskip")
300                                 h_paragraph_separation = "skip";
301                         else if (name == "parindent")
302                                 h_paragraph_separation = "skip";
303                         else
304                                 h_preamble << "\\setlength{" + name + "}{" + content + "}\n";
305                 }
306
307                 else if (t.cs() == "par")
308                         h_preamble << '\n';
309
310                 else if (t.cs() == "begin") {
311                         string const name = p.getArg('{', '}');
312                         if (name == "document") {
313                                 end_preamble(os);
314                                 os << "\n\n\\layout Standard\n\n";
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 // }])