1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
12 #include "lyxtextclass.h"
14 #include "support/filetools.h"
27 using std::istringstream;
29 using std::ostringstream;
33 using lyx::support::LibFileSearch;
35 // special columntypes
36 extern std::map<char, int> special_columns;
40 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
41 "breton", "british", "bulgarian", "catalan", "croatian", "czech", "danish",
42 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
43 "frenchb", "galician", "german", "germanb", "greek", "hebcal", "hebfont",
44 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
45 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
46 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
47 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
48 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
49 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
50 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
51 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
53 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
56 ostringstream h_preamble;
57 string h_textclass = "article";
58 string h_options = string();
59 string h_language = "english";
60 string h_inputencoding = "latin1";
61 string h_fontscheme = "default";
62 string h_graphics = "default";
63 string h_paperfontsize = "default";
64 string h_spacing = "single";
65 string h_papersize = "default";
66 string h_paperpackage = "default";
67 string h_use_geometry = "0";
68 string h_use_amsmath = "0";
69 string h_use_natbib = "0";
70 string h_use_numerical_citations = "0";
71 string h_paperorientation = "portrait";
72 string h_secnumdepth = "3";
73 string h_tocdepth = "3";
74 string h_paragraph_separation = "indent";
75 string h_defskip = "medskip";
76 string h_quotes_language = "english";
77 string h_quotes_times = "2";
78 string h_papercolumns = "1";
79 string h_papersides = "1";
80 string h_paperpagestyle = "default";
81 string h_tracking_changes = "0";
84 void handle_opt(vector<string> & opts, char const ** what, string & target)
89 for ( ; *what; ++what) {
90 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
91 if (it != opts.end()) {
92 //cerr << "### found option '" << *what << "'\n";
101 void handle_package(string const & name, string const & options)
103 //cerr << "handle_package: '" << name << "'\n";
104 if (name == "a4wide") {
105 h_papersize = "a4paper";
106 h_paperpackage = "widemarginsa4";
107 } else if (name == "ae")
109 else if (name == "aecompl")
111 else if (name == "amsmath")
113 else if (name == "amssymb")
115 else if (name == "babel")
117 else if (name == "fontenc")
119 else if (name == "inputenc")
120 h_inputencoding = options;
121 else if (name == "makeidx")
123 else if (name == "verbatim")
125 else if (is_known(name, known_languages)) {
127 h_quotes_language = name;
130 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
132 h_preamble << "\\usepackage{" << name << "}\n";
138 void end_preamble(ostream & os, LyXTextClass const & /*textclass*/)
140 os << "# tex2lyx 0.1.0 created this file\n"
141 << "\\lyxformat 225\n"
142 << "\\textclass " << h_textclass << "\n"
143 << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
144 if (h_options.size())
145 os << "\\options " << h_options << "\n";
146 os << "\\language " << h_language << "\n"
147 << "\\inputencoding " << h_inputencoding << "\n"
148 << "\\fontscheme " << h_fontscheme << "\n"
149 << "\\graphics " << h_graphics << "\n"
150 << "\\paperfontsize " << h_paperfontsize << "\n"
151 << "\\spacing " << h_spacing << "\n"
152 << "\\papersize " << h_papersize << "\n"
153 << "\\paperpackage " << h_paperpackage << "\n"
154 << "\\use_geometry " << h_use_geometry << "\n"
155 << "\\use_amsmath " << h_use_amsmath << "\n"
156 << "\\use_natbib " << h_use_natbib << "\n"
157 << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
158 << "\\paperorientation " << h_paperorientation << "\n"
159 << "\\secnumdepth " << h_secnumdepth << "\n"
160 << "\\tocdepth " << h_tocdepth << "\n"
161 << "\\paragraph_separation " << h_paragraph_separation << "\n"
162 << "\\defskip " << h_defskip << "\n"
163 << "\\quotes_language " << h_quotes_language << "\n"
164 << "\\quotes_times " << h_quotes_times << "\n"
165 << "\\papercolumns " << h_papercolumns << "\n"
166 << "\\papersides " << h_papersides << "\n"
167 << "\\paperpagestyle " << h_paperpagestyle << "\n"
168 << "\\tracking_changes " << h_tracking_changes << "\n"
172 } // anonymous namespace
174 LyXTextClass const parse_preamble(Parser & p, ostream & os)
176 // initialize fixed types
177 special_columns['D'] = 3;
180 Token const & t = p.get_token();
183 cerr << "t: " << t << " flags: " << flags << "\n";
190 if (t.cat() == catLetter ||
191 t.cat() == catSpace ||
192 t.cat() == catSuper ||
194 t.cat() == catOther ||
195 t.cat() == catMath ||
196 t.cat() == catActive ||
197 t.cat() == catBegin ||
199 t.cat() == catAlign ||
200 t.cat() == catNewline ||
201 t.cat() == catParameter)
202 h_preamble << t.character();
204 else if (t.cat() == catComment)
207 else if (t.cs() == "pagestyle")
208 h_paperpagestyle == p.verbatim_item();
210 else if (t.cs() == "makeatletter") {
211 p.setCatCode('@', catLetter);
212 h_preamble << "\\makeatletter\n";
215 else if (t.cs() == "makeatother") {
216 p.setCatCode('@', catOther);
217 h_preamble << "\\makeatother\n";
220 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
221 || t.cs() == "providecommand") {
223 if (p.next_token().character() == '*') {
227 string const name = p.verbatim_item();
228 string const opts = p.getOpt();
229 string const body = p.verbatim_item();
230 // only non-lyxspecific stuff
231 if (name != "\\noun "
232 && name != "\\tabularnewline "
234 && name != "\\lyxline "
235 && name != "\\lyxaddress "
236 && name != "\\lyxrightaddress "
237 && name != "\\boldsymbol "
238 && name != "\\lyxarrow ") {
240 ss << '\\' << t.cs();
243 ss << '{' << name << '}' << opts << '{' << body << "}\n";
244 h_preamble << ss.str();
246 ostream & out = in_preamble ? h_preamble : os;
247 out << "\\" << t.cs() << "{" << name << "}"
248 << opts << "{" << body << "}\n";
253 else if (t.cs() == "documentclass") {
255 split(p.getArg('[', ']'), opts, ',');
256 handle_opt(opts, known_languages, h_language);
257 handle_opt(opts, known_fontsizes, h_paperfontsize);
258 h_quotes_language = h_language;
259 h_options = join(opts, ",");
260 h_textclass = p.getArg('{', '}');
263 else if (t.cs() == "usepackage") {
264 string const options = p.getArg('[', ']');
265 string const name = p.getArg('{', '}');
266 if (options.empty() && name.find(',')) {
267 vector<string> vecnames;
268 split(name, vecnames, ',');
269 vector<string>::const_iterator it = vecnames.begin();
270 vector<string>::const_iterator end = vecnames.end();
271 for (; it != end; ++it)
272 handle_package(trim(*it), string());
274 handle_package(name, options);
278 else if (t.cs() == "newenvironment") {
279 string const name = p.getArg('{', '}');
281 ss << "\\newenvironment{" << name << "}";
284 ss << '{' << p.verbatim_item() << '}';
285 ss << '{' << p.verbatim_item() << '}';
287 if (name != "lyxcode" && name != "lyxlist"
288 && name != "lyxrightadress" && name != "lyxaddress")
289 h_preamble << ss.str();
292 else if (t.cs() == "def") {
293 string name = p.get_token().cs();
294 while (p.next_token().cat() != catBegin)
295 name += p.get_token().asString();
296 h_preamble << "\\def\\" << name << '{' << p.verbatim_item() << "}\n";
299 else if (t.cs() == "newcolumntype") {
300 string const name = p.getArg('{', '}');
303 string opts = p.getOpt();
305 istringstream is(string(opts, 1));
306 //cerr << "opt: " << is.str() << "\n";
309 special_columns[name[0]] = nargs;
310 h_preamble << "\\newcolumntype{" << name << "}";
312 h_preamble << "[" << nargs << "]";
313 h_preamble << "{" << p.verbatim_item() << "}\n";
316 else if (t.cs() == "setcounter") {
317 string const name = p.getArg('{', '}');
318 string const content = p.getArg('{', '}');
319 if (name == "secnumdepth")
320 h_secnumdepth = content;
321 else if (name == "tocdepth")
322 h_tocdepth = content;
324 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
327 else if (t.cs() == "setlength") {
328 string const name = p.verbatim_item();
329 string const content = p.verbatim_item();
330 if (name == "parskip")
331 h_paragraph_separation = "skip";
332 else if (name == "parindent")
333 h_paragraph_separation = "skip";
335 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
338 else if (t.cs() == "par")
341 else if (t.cs() == "begin") {
342 string const name = p.getArg('{', '}');
343 if (name == "document")
345 h_preamble << "\\begin{" << name << "}";
348 else if (t.cs().size())
349 h_preamble << '\\' << t.cs() << ' ';
352 LyXTextClass textclass;
353 textclass.Read(LibFileSearch("layouts", h_textclass, "layout"));
354 end_preamble(os, textclass);