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.0.3 created this file\n"
141 << "\\lyxformat 224\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"
169 << "\\end_header\n\n\\layout Standard\n";
173 } // anonymous namespace
175 LyXTextClass const parse_preamble(Parser & p, ostream & os)
177 // initialize fixed types
178 special_columns['D'] = 3;
181 Token const & t = p.get_token();
184 cerr << "t: " << t << " flags: " << flags << "\n";
191 if (t.cat() == catLetter ||
192 t.cat() == catSpace ||
193 t.cat() == catSuper ||
195 t.cat() == catOther ||
196 t.cat() == catMath ||
197 t.cat() == catActive ||
198 t.cat() == catBegin ||
200 t.cat() == catAlign ||
201 t.cat() == catNewline ||
202 t.cat() == catParameter)
203 h_preamble << t.character();
205 else if (t.cat() == catComment)
208 else if (t.cs() == "pagestyle")
209 h_paperpagestyle == p.verbatim_item();
211 else if (t.cs() == "makeatletter") {
212 p.setCatCode('@', catLetter);
213 h_preamble << "\\makeatletter\n";
216 else if (t.cs() == "makeatother") {
217 p.setCatCode('@', catOther);
218 h_preamble << "\\makeatother\n";
221 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
222 || t.cs() == "providecommand") {
224 if (p.next_token().character() == '*') {
228 string const name = p.verbatim_item();
229 string const opts = p.getOpt();
230 string const body = p.verbatim_item();
231 // only non-lyxspecific stuff
232 if (name != "\\noun "
233 && name != "\\tabularnewline "
235 && name != "\\lyxline "
236 && name != "\\lyxaddress "
237 && name != "\\lyxrightaddress "
238 && name != "\\boldsymbol "
239 && name != "\\lyxarrow ") {
241 ss << '\\' << t.cs();
244 ss << '{' << name << '}' << opts << '{' << body << "}\n";
245 h_preamble << ss.str();
247 ostream & out = in_preamble ? h_preamble : os;
248 out << "\\" << t.cs() << "{" << name << "}"
249 << opts << "{" << body << "}\n";
254 else if (t.cs() == "documentclass") {
256 split(p.getArg('[', ']'), opts, ',');
257 handle_opt(opts, known_languages, h_language);
258 handle_opt(opts, known_fontsizes, h_paperfontsize);
259 h_quotes_language = h_language;
260 h_options = join(opts, ",");
261 h_textclass = p.getArg('{', '}');
264 else if (t.cs() == "usepackage") {
265 string const options = p.getArg('[', ']');
266 string const name = p.getArg('{', '}');
267 if (options.empty() && name.find(',')) {
268 vector<string> vecnames;
269 split(name, vecnames, ',');
270 vector<string>::const_iterator it = vecnames.begin();
271 vector<string>::const_iterator end = vecnames.end();
272 for (; it != end; ++it)
273 handle_package(trim(*it), string());
275 handle_package(name, options);
279 else if (t.cs() == "newenvironment") {
280 string const name = p.getArg('{', '}');
282 ss << "\\newenvironment{" << name << "}";
285 ss << '{' << p.verbatim_item() << '}';
286 ss << '{' << p.verbatim_item() << '}';
288 if (name != "lyxcode" && name != "lyxlist"
289 && name != "lyxrightadress" && name != "lyxaddress")
290 h_preamble << ss.str();
293 else if (t.cs() == "def") {
294 string name = p.get_token().cs();
295 while (p.next_token().cat() != catBegin)
296 name += p.get_token().asString();
297 h_preamble << "\\def\\" << name << '{' << p.verbatim_item() << "}\n";
300 else if (t.cs() == "newcolumntype") {
301 string const name = p.getArg('{', '}');
304 string opts = p.getOpt();
306 istringstream is(string(opts, 1));
307 //cerr << "opt: " << is.str() << "\n";
310 special_columns[name[0]] = nargs;
311 h_preamble << "\\newcolumntype{" << name << "}";
313 h_preamble << "[" << nargs << "]";
314 h_preamble << "{" << p.verbatim_item() << "}\n";
317 else if (t.cs() == "setcounter") {
318 string const name = p.getArg('{', '}');
319 string const content = p.getArg('{', '}');
320 if (name == "secnumdepth")
321 h_secnumdepth = content;
322 else if (name == "tocdepth")
323 h_tocdepth = content;
325 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
328 else if (t.cs() == "setlength") {
329 string const name = p.verbatim_item();
330 string const content = p.verbatim_item();
331 if (name == "parskip")
332 h_paragraph_separation = "skip";
333 else if (name == "parindent")
334 h_paragraph_separation = "skip";
336 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
339 else if (t.cs() == "par")
342 else if (t.cs() == "begin") {
343 string const name = p.getArg('{', '}');
344 if (name == "document")
346 h_preamble << "\\begin{" << name << "}";
349 else if (t.cs().size())
350 h_preamble << '\\' << t.cs() << ' ';
353 LyXTextClass textclass;
354 textclass.Read(LibFileSearch("layouts", h_textclass, "layout"));
355 end_preamble(os, textclass);