1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
22 using std::istringstream;
24 using std::ostringstream;
28 // special columntypes
29 extern std::map<char, int> special_columns;
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};
46 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
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";
77 void handle_opt(vector<string> & opts, char const ** what, string & target)
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";
94 void handle_package(string const & name, string const & options)
96 //cerr << "handle_package: '" << name << "'\n";
97 if (name == "a4wide") {
98 h_papersize = "a4paper";
99 h_paperpackage = "widemarginsa4";
100 } else if (name == "ae")
102 else if (name == "aecompl")
104 else if (name == "amsmath")
106 else if (name == "amssymb")
108 else if (name == "babel")
110 else if (name == "fontenc")
112 else if (name == "inputenc")
113 h_inputencoding = options;
114 else if (name == "makeidx")
116 else if (name == "verbatim")
118 else if (is_known(name, known_languages)) {
120 h_quotes_language = name;
123 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
125 h_preamble << "\\usepackage{" << name << "}\n";
131 void end_preamble(ostream & os)
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";
166 } // anonymous namespace
168 void parse_preamble(Parser & p, ostream & os)
170 // initialize fixed types
171 special_columns['D'] = 3;
174 Token const & t = p.get_token();
177 cerr << "t: " << t << " flags: " << flags << "\n";
184 if (t.cat() == catLetter ||
185 t.cat() == catSpace ||
186 t.cat() == catSuper ||
188 t.cat() == catOther ||
189 t.cat() == catMath ||
190 t.cat() == catActive ||
191 t.cat() == catBegin ||
193 t.cat() == catAlign ||
194 t.cat() == catNewline ||
195 t.cat() == catParameter)
196 h_preamble << t.character();
198 else if (t.cat() == catComment)
201 else if (t.cs() == "pagestyle")
202 h_paperpagestyle == p.verbatim_item();
204 else if (t.cs() == "makeatletter") {
205 p.setCatCode('@', catLetter);
206 h_preamble << "\\makeatletter\n";
209 else if (t.cs() == "makeatother") {
210 p.setCatCode('@', catOther);
211 h_preamble << "\\makeatother\n";
214 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
215 || t.cs() == "providecommand") {
217 if (p.next_token().character() == '*') {
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 "
228 && name != "\\lyxline "
229 && name != "\\lyxaddress "
230 && name != "\\lyxrightaddress "
231 && name != "\\boldsymbol "
232 && name != "\\lyxarrow ") {
234 ss << '\\' << t.cs();
237 ss << '{' << name << '}' << opts << '{' << body << "}\n";
238 h_preamble << ss.str();
240 ostream & out = in_preamble ? h_preamble : os;
241 out << "\\" << t.cs() << "{" << name << "}"
242 << opts << "{" << body << "}\n";
247 else if (t.cs() == "documentclass") {
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('{', '}');
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());
268 handle_package(name, options);
272 else if (t.cs() == "newenvironment") {
273 string const name = p.getArg('{', '}');
275 ss << "\\newenvironment{" << name << "}";
278 ss << '{' << p.verbatim_item() << '}';
279 ss << '{' << p.verbatim_item() << '}';
281 if (name != "lyxcode" && name != "lyxlist"
282 && name != "lyxrightadress" && name != "lyxaddress")
283 h_preamble << ss.str();
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";
293 else if (t.cs() == "newcolumntype") {
294 string const name = p.getArg('{', '}');
297 string opts = p.getOpt();
299 istringstream is(string(opts, 1));
300 //cerr << "opt: " << is.str() << "\n";
303 special_columns[name[0]] = nargs;
304 h_preamble << "\\newcolumntype{" << name << "}";
306 h_preamble << "[" << nargs << "]";
307 h_preamble << "{" << p.verbatim_item() << "}\n";
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;
318 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
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";
329 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
332 else if (t.cs() == "par")
335 else if (t.cs() == "begin") {
336 string const name = p.getArg('{', '}');
337 if (name == "document") {
341 h_preamble << "\\begin{" << name << "}";
344 else if (t.cs().size())
345 h_preamble << '\\' << t.cs() << ' ';