3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
18 #include "lyxtextclass.h"
20 #include "support/filetools.h"
29 using std::istringstream;
31 using std::ostringstream;
37 using lyx::support::LibFileSearch;
39 // special columntypes
40 extern std::map<char, int> special_columns;
44 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
45 "breton", "british", "bulgarian", "catalan", "croatian", "czech", "danish",
46 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
47 "frenchb", "galician", "german", "germanb", "greek", "hebcal", "hebfont",
48 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
49 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
50 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
51 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
52 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
53 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
54 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
55 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
57 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
60 ostringstream h_preamble;
61 string h_textclass = "article";
62 string h_options = string();
63 string h_language = "english";
64 string h_inputencoding = "latin1";
65 string h_fontscheme = "default";
66 string h_graphics = "default";
67 string h_paperfontsize = "default";
68 string h_spacing = "single";
69 // Match the entry in ../src/tex-strings.C. Why not "default"?
70 string h_papersize = "Default";
71 string h_paperpackage = "default";
72 string h_use_geometry = "0";
73 string h_use_amsmath = "0";
74 string h_use_natbib = "0";
75 string h_use_numerical_citations = "0";
76 string h_paperorientation = "portrait";
77 string h_secnumdepth = "3";
78 string h_tocdepth = "3";
79 string h_paragraph_separation = "indent";
80 string h_defskip = "medskip";
81 string h_quotes_language = "english";
82 string h_quotes_times = "2";
83 string h_papercolumns = "1";
84 string h_papersides = string();
85 string h_paperpagestyle = "default";
86 string h_tracking_changes = "0";
89 void handle_opt(vector<string> & opts, char const ** what, string & target)
94 for ( ; *what; ++what) {
95 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
96 if (it != opts.end()) {
97 //cerr << "### found option '" << *what << "'\n";
106 void handle_package(string const & name, string const & options)
108 //cerr << "handle_package: '" << name << "'\n";
109 if (name == "a4wide") {
110 h_papersize = "a4paper";
111 h_paperpackage = "widemarginsa4";
112 } else if (name == "ae")
114 else if (name == "aecompl")
116 else if (name == "amsmath")
118 else if (name == "amssymb")
120 else if (name == "babel")
122 else if (name == "fontenc")
124 else if (name == "inputenc")
125 h_inputencoding = options;
126 else if (name == "makeidx")
128 else if (name == "verbatim")
130 else if (is_known(name, known_languages)) {
132 h_quotes_language = name;
135 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
137 h_preamble << "\\usepackage{" << name << "}\n";
143 void end_preamble(ostream & os, LyXTextClass const & /*textclass*/)
145 os << "#LyX file created by tex2lyx 0.1.2 \n"
146 << "\\lyxformat 225\n"
147 << "\\textclass " << h_textclass << "\n"
148 << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
149 if (h_options.size())
150 os << "\\options " << h_options << "\n";
151 os << "\\language " << h_language << "\n"
152 << "\\inputencoding " << h_inputencoding << "\n"
153 << "\\fontscheme " << h_fontscheme << "\n"
154 << "\\graphics " << h_graphics << "\n"
155 << "\\paperfontsize " << h_paperfontsize << "\n"
156 << "\\spacing " << h_spacing << "\n"
157 << "\\papersize " << h_papersize << "\n"
158 << "\\paperpackage " << h_paperpackage << "\n"
159 << "\\use_geometry " << h_use_geometry << "\n"
160 << "\\use_amsmath " << h_use_amsmath << "\n"
161 << "\\use_natbib " << h_use_natbib << "\n"
162 << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
163 << "\\paperorientation " << h_paperorientation << "\n"
164 << "\\secnumdepth " << h_secnumdepth << "\n"
165 << "\\tocdepth " << h_tocdepth << "\n"
166 << "\\paragraph_separation " << h_paragraph_separation << "\n"
167 << "\\defskip " << h_defskip << "\n"
168 << "\\quotes_language " << h_quotes_language << "\n"
169 << "\\quotes_times " << h_quotes_times << "\n"
170 << "\\papercolumns " << h_papercolumns << "\n"
171 << "\\papersides " << h_papersides << "\n"
172 << "\\paperpagestyle " << h_paperpagestyle << "\n"
173 << "\\tracking_changes " << h_tracking_changes << "\n"
175 // clear preamble for subdocuments
179 } // anonymous namespace
181 LyXTextClass const parse_preamble(Parser & p, ostream & os, string const & forceclass)
183 // initialize fixed types
184 special_columns['D'] = 3;
185 bool is_full_document = false;
187 // determine wether this is a full document or a fragment for inclusion
189 Token const & t = p.get_token();
191 if (t.cs() == "documentclass") {
192 is_full_document = true;
198 while (is_full_document && p.good()) {
199 Token const & t = p.get_token();
202 cerr << "t: " << t << "\n";
208 if (t.cat() == catLetter ||
209 t.cat() == catSpace ||
210 t.cat() == catSuper ||
212 t.cat() == catOther ||
213 t.cat() == catMath ||
214 t.cat() == catActive ||
215 t.cat() == catBegin ||
217 t.cat() == catAlign ||
218 t.cat() == catNewline ||
219 t.cat() == catParameter)
220 h_preamble << t.character();
222 else if (t.cat() == catComment)
225 else if (t.cs() == "pagestyle")
226 h_paperpagestyle = p.verbatim_item();
228 else if (t.cs() == "makeatletter") {
229 p.setCatCode('@', catLetter);
230 h_preamble << "\\makeatletter\n";
233 else if (t.cs() == "makeatother") {
234 p.setCatCode('@', catOther);
235 h_preamble << "\\makeatother\n";
238 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
239 || t.cs() == "providecommand") {
241 if (p.next_token().character() == '*') {
245 string const name = p.verbatim_item();
246 string const opts = p.getOpt();
247 string const body = p.verbatim_item();
248 // only non-lyxspecific stuff
249 if (name != "\\noun "
250 && name != "\\tabularnewline "
252 && name != "\\lyxline "
253 && name != "\\lyxaddress "
254 && name != "\\lyxrightaddress "
255 && name != "\\boldsymbol "
256 && name != "\\lyxarrow ") {
258 ss << '\\' << t.cs();
261 ss << '{' << name << '}' << opts << '{' << body << "}\n";
262 h_preamble << ss.str();
264 ostream & out = in_preamble ? h_preamble : os;
265 out << "\\" << t.cs() << "{" << name << "}"
266 << opts << "{" << body << "}\n";
271 else if (t.cs() == "documentclass") {
273 split(p.getArg('[', ']'), opts, ',');
274 handle_opt(opts, known_languages, h_language);
275 handle_opt(opts, known_fontsizes, h_paperfontsize);
276 h_quotes_language = h_language;
277 h_options = join(opts, ",");
278 h_textclass = p.getArg('{', '}');
281 else if (t.cs() == "usepackage") {
282 string const options = p.getArg('[', ']');
283 string const name = p.getArg('{', '}');
284 if (options.empty() && name.find(',')) {
285 vector<string> vecnames;
286 split(name, vecnames, ',');
287 vector<string>::const_iterator it = vecnames.begin();
288 vector<string>::const_iterator end = vecnames.end();
289 for (; it != end; ++it)
290 handle_package(trim(*it), string());
292 handle_package(name, options);
296 else if (t.cs() == "newenvironment") {
297 string const name = p.getArg('{', '}');
299 ss << "\\newenvironment{" << name << "}";
302 ss << '{' << p.verbatim_item() << '}';
303 ss << '{' << p.verbatim_item() << '}';
305 if (name != "lyxcode" && name != "lyxlist"
306 && name != "lyxrightadress" && name != "lyxaddress")
307 h_preamble << ss.str();
310 else if (t.cs() == "def") {
311 string name = p.get_token().cs();
312 while (p.next_token().cat() != catBegin)
313 name += p.get_token().asString();
314 h_preamble << "\\def\\" << name << '{' << p.verbatim_item() << "}\n";
317 else if (t.cs() == "newcolumntype") {
318 string const name = p.getArg('{', '}');
321 string opts = p.getOpt();
323 istringstream is(string(opts, 1));
324 //cerr << "opt: " << is.str() << "\n";
327 special_columns[name[0]] = nargs;
328 h_preamble << "\\newcolumntype{" << name << "}";
330 h_preamble << "[" << nargs << "]";
331 h_preamble << "{" << p.verbatim_item() << "}\n";
334 else if (t.cs() == "setcounter") {
335 string const name = p.getArg('{', '}');
336 string const content = p.getArg('{', '}');
337 if (name == "secnumdepth")
338 h_secnumdepth = content;
339 else if (name == "tocdepth")
340 h_tocdepth = content;
342 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
345 else if (t.cs() == "setlength") {
346 string const name = p.verbatim_item();
347 string const content = p.verbatim_item();
348 if (name == "parskip")
349 h_paragraph_separation = "skip";
350 else if (name == "parindent")
351 h_paragraph_separation = "skip";
353 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
356 else if (t.cs() == "par")
359 else if (t.cs() == "begin") {
360 string const name = p.getArg('{', '}');
361 if (name == "document")
363 h_preamble << "\\begin{" << name << "}";
366 else if (t.cs().size())
367 h_preamble << '\\' << t.cs() << ' ';
370 // Force textclass if the user wanted it
371 if (forceclass.size()) {
372 h_textclass = forceclass;
374 string layoutfilename = LibFileSearch("layouts", h_textclass, "layout");
375 if (layoutfilename.empty()) {
376 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
379 LyXTextClass textclass;
380 textclass.Read(layoutfilename);
381 if (! h_papersides.size()) {
383 ss << textclass.sides();
384 h_papersides = ss.str();
386 end_preamble(os, textclass);