1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
21 using std::istringstream;
23 using std::ostringstream;
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};
43 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
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";
74 void handle_opt(vector<string> & opts, char const ** what, string & target)
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";
91 void handle_package(string const & name, string const & options)
93 //cerr << "handle_package: '" << name << "'\n";
94 if (name == "a4wide") {
95 h_papersize = "a4paper";
96 h_paperpackage = "widemarginsa4";
97 } else if (name == "ae")
99 else if (name == "aecompl")
101 else if (name == "amsmath")
103 else if (name == "amssymb")
105 else if (name == "babel")
107 else if (name == "fontenc")
109 else if (name == "inputenc")
110 h_inputencoding = options;
111 else if (name == "makeidx")
113 else if (name == "verbatim")
115 else if (is_known(name, known_languages)) {
117 h_quotes_language = name;
120 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
122 h_preamble << "\\usepackage{" << name << "}\n";
128 void end_preamble(ostream & os)
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";
161 } // anonymous namespace
163 void parse_preamble(Parser & p, ostream & os)
166 Token const & t = p.getToken();
169 cerr << "t: " << t << " flags: " << flags << "\n";
176 if (t.cat() == catLetter ||
177 t.cat() == catSpace ||
178 t.cat() == catSuper ||
180 t.cat() == catOther ||
181 t.cat() == catMath ||
182 t.cat() == catActive ||
183 t.cat() == catBegin ||
185 t.cat() == catAlign ||
186 t.cat() == catNewline ||
187 t.cat() == catParameter)
188 h_preamble << t.character();
190 else if (t.cat() == catComment)
193 else if (t.cs() == "pagestyle")
194 h_paperpagestyle == p.verbatimItem();
196 else if (t.cs() == "makeatletter") {
197 p.setCatCode('@', catLetter);
198 h_preamble << "\\makeatletter\n";
201 else if (t.cs() == "makeatother") {
202 p.setCatCode('@', catOther);
203 h_preamble << "\\makeatother\n";
206 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
207 || t.cs() == "providecommand") {
209 if (p.nextToken().character() == '*') {
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 "
220 && name != "\\lyxline "
221 && name != "\\lyxaddress "
222 && name != "\\lyxrightaddress "
223 && name != "\\boldsymbol "
224 && name != "\\lyxarrow ") {
226 ss << '\\' << t.cs();
229 ss << '{' << name << '}' << opts << '{' << body << "}\n";
230 h_preamble << ss.str();
232 ostream & out = in_preamble ? h_preamble : os;
233 out << "\\" << t.cs() << "{" << name << "}"
234 << opts << "{" << body << "}\n";
239 else if (t.cs() == "documentclass") {
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('{', '}');
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());
260 handle_package(name, options);
264 else if (t.cs() == "newenvironment") {
265 string const name = p.getArg('{', '}');
267 ss << "\\newenvironment{" << name << "}";
270 ss << '{' << p.verbatimItem() << '}';
271 ss << '{' << p.verbatimItem() << '}';
273 if (name != "lyxcode" && name != "lyxlist"
274 && name != "lyxrightadress" && name != "lyxaddress")
275 h_preamble << ss.str();
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";
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;
293 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
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";
304 h_preamble << "\\setlength{" + name + "}{" + content + "}\n";
307 else if (t.cs() == "par")
310 else if (t.cs() == "begin") {
311 string const name = p.getArg('{', '}');
312 if (name == "document") {
314 os << "\n\n\\layout Standard\n\n";
317 h_preamble << "\\begin{" << name << "}";
320 else if (t.cs().size())
321 h_preamble << '\\' << t.cs() << ' ';