1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
21 using std::istringstream;
23 using std::ostringstream;
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};
42 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
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";
73 void handle_opt(vector<string> & opts, char const ** what, string & target)
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";
90 void handle_package(string const & name, string const & options)
92 //cerr << "handle_package: '" << name << "'\n";
93 if (name == "a4wide") {
94 h_papersize = "a4paper";
95 h_paperpackage = "widemarginsa4";
96 } else if (name == "ae")
98 else if (name == "aecompl")
100 else if (name == "amsmath")
102 else if (name == "amssymb")
104 else if (name == "babel")
106 else if (name == "fontenc")
108 else if (name == "inputenc")
109 h_inputencoding = options;
110 else if (name == "makeidx")
112 else if (name == "verbatim")
114 else if (is_known(name, known_languages)) {
116 h_quotes_language = name;
119 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
121 h_preamble << "\\usepackage{" << name << "}\n";
127 void end_preamble(ostream & os)
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";
162 } // anonymous namespace
164 void parse_preamble(Parser & p, ostream & os)
167 Token const & t = p.get_token();
170 cerr << "t: " << t << " flags: " << flags << "\n";
177 if (t.cat() == catLetter ||
178 t.cat() == catSpace ||
179 t.cat() == catSuper ||
181 t.cat() == catOther ||
182 t.cat() == catMath ||
183 t.cat() == catActive ||
184 t.cat() == catBegin ||
186 t.cat() == catAlign ||
187 t.cat() == catNewline ||
188 t.cat() == catParameter)
189 h_preamble << t.character();
191 else if (t.cat() == catComment)
194 else if (t.cs() == "pagestyle")
195 h_paperpagestyle == p.verbatim_item();
197 else if (t.cs() == "makeatletter") {
198 p.setCatCode('@', catLetter);
199 h_preamble << "\\makeatletter\n";
202 else if (t.cs() == "makeatother") {
203 p.setCatCode('@', catOther);
204 h_preamble << "\\makeatother\n";
207 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
208 || t.cs() == "providecommand") {
210 if (p.next_token().character() == '*') {
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 "
221 && name != "\\lyxline "
222 && name != "\\lyxaddress "
223 && name != "\\lyxrightaddress "
224 && name != "\\boldsymbol "
225 && name != "\\lyxarrow ") {
227 ss << '\\' << t.cs();
230 ss << '{' << name << '}' << opts << '{' << body << "}\n";
231 h_preamble << ss.str();
233 ostream & out = in_preamble ? h_preamble : os;
234 out << "\\" << t.cs() << "{" << name << "}"
235 << opts << "{" << body << "}\n";
240 else if (t.cs() == "documentclass") {
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('{', '}');
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());
261 handle_package(name, options);
265 else if (t.cs() == "newenvironment") {
266 string const name = p.getArg('{', '}');
268 ss << "\\newenvironment{" << name << "}";
271 ss << '{' << p.verbatim_item() << '}';
272 ss << '{' << p.verbatim_item() << '}';
274 if (name != "lyxcode" && name != "lyxlist"
275 && name != "lyxrightadress" && name != "lyxaddress")
276 h_preamble << ss.str();
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";
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;
294 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
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";
305 h_preamble << "\\setlength{" + name + "}{" + content + "}\n";
308 else if (t.cs() == "par")
311 else if (t.cs() == "begin") {
312 string const name = p.getArg('{', '}');
313 if (name == "document") {
317 h_preamble << "\\begin{" << name << "}";
320 else if (t.cs().size())
321 h_preamble << '\\' << t.cs() << ' ';