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"
33 using std::istringstream;
35 using std::ostringstream;
39 using lyx::support::LibFileSearch;
41 // special columntypes
42 extern std::map<char, int> special_columns;
46 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
47 "breton", "british", "bulgarian", "catalan", "croatian", "czech", "danish",
48 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
49 "frenchb", "galician", "german", "germanb", "greek", "hebcal", "hebfont",
50 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
51 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
52 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
53 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
54 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
55 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
56 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
57 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
59 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
62 ostringstream h_preamble;
63 string h_textclass = "article";
64 string h_options = string();
65 string h_language = "english";
66 string h_inputencoding = "latin1";
67 string h_fontscheme = "default";
68 string h_graphics = "default";
69 string h_paperfontsize = "default";
70 string h_spacing = "single";
71 string h_papersize = "default";
72 string h_paperpackage = "default";
73 string h_use_geometry = "0";
74 string h_use_amsmath = "0";
75 string h_use_natbib = "0";
76 string h_use_numerical_citations = "0";
77 string h_paperorientation = "portrait";
78 string h_secnumdepth = "3";
79 string h_tocdepth = "3";
80 string h_paragraph_separation = "indent";
81 string h_defskip = "medskip";
82 string h_quotes_language = "english";
83 string h_quotes_times = "2";
84 string h_papercolumns = "1";
85 string h_papersides = "1";
86 string h_paperpagestyle = "default";
87 string h_tracking_changes = "0";
90 void handle_opt(vector<string> & opts, char const ** what, string & target)
95 for ( ; *what; ++what) {
96 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
97 if (it != opts.end()) {
98 //cerr << "### found option '" << *what << "'\n";
107 void handle_package(string const & name, string const & options)
109 //cerr << "handle_package: '" << name << "'\n";
110 if (name == "a4wide") {
111 h_papersize = "a4paper";
112 h_paperpackage = "widemarginsa4";
113 } else if (name == "ae")
115 else if (name == "aecompl")
117 else if (name == "amsmath")
119 else if (name == "amssymb")
121 else if (name == "babel")
123 else if (name == "fontenc")
125 else if (name == "inputenc")
126 h_inputencoding = options;
127 else if (name == "makeidx")
129 else if (name == "verbatim")
131 else if (is_known(name, known_languages)) {
133 h_quotes_language = name;
136 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
138 h_preamble << "\\usepackage{" << name << "}\n";
144 void end_preamble(ostream & os, LyXTextClass const & /*textclass*/)
146 os << "#LyX file created by tex2lyx 0.1.2 \n"
147 << "\\lyxformat 225\n"
148 << "\\textclass " << h_textclass << "\n"
149 << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
150 if (h_options.size())
151 os << "\\options " << h_options << "\n";
152 os << "\\language " << h_language << "\n"
153 << "\\inputencoding " << h_inputencoding << "\n"
154 << "\\fontscheme " << h_fontscheme << "\n"
155 << "\\graphics " << h_graphics << "\n"
156 << "\\paperfontsize " << h_paperfontsize << "\n"
157 << "\\spacing " << h_spacing << "\n"
158 << "\\papersize " << h_papersize << "\n"
159 << "\\paperpackage " << h_paperpackage << "\n"
160 << "\\use_geometry " << h_use_geometry << "\n"
161 << "\\use_amsmath " << h_use_amsmath << "\n"
162 << "\\use_natbib " << h_use_natbib << "\n"
163 << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
164 << "\\paperorientation " << h_paperorientation << "\n"
165 << "\\secnumdepth " << h_secnumdepth << "\n"
166 << "\\tocdepth " << h_tocdepth << "\n"
167 << "\\paragraph_separation " << h_paragraph_separation << "\n"
168 << "\\defskip " << h_defskip << "\n"
169 << "\\quotes_language " << h_quotes_language << "\n"
170 << "\\quotes_times " << h_quotes_times << "\n"
171 << "\\papercolumns " << h_papercolumns << "\n"
172 << "\\papersides " << h_papersides << "\n"
173 << "\\paperpagestyle " << h_paperpagestyle << "\n"
174 << "\\tracking_changes " << h_tracking_changes << "\n"
178 } // anonymous namespace
180 LyXTextClass const parse_preamble(Parser & p, ostream & os)
182 // initialize fixed types
183 special_columns['D'] = 3;
186 Token const & t = p.get_token();
189 cerr << "t: " << t << " flags: " << flags << "\n";
196 if (t.cat() == catLetter ||
197 t.cat() == catSpace ||
198 t.cat() == catSuper ||
200 t.cat() == catOther ||
201 t.cat() == catMath ||
202 t.cat() == catActive ||
203 t.cat() == catBegin ||
205 t.cat() == catAlign ||
206 t.cat() == catNewline ||
207 t.cat() == catParameter)
208 h_preamble << t.character();
210 else if (t.cat() == catComment)
213 else if (t.cs() == "pagestyle")
214 h_paperpagestyle == p.verbatim_item();
216 else if (t.cs() == "makeatletter") {
217 p.setCatCode('@', catLetter);
218 h_preamble << "\\makeatletter\n";
221 else if (t.cs() == "makeatother") {
222 p.setCatCode('@', catOther);
223 h_preamble << "\\makeatother\n";
226 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
227 || t.cs() == "providecommand") {
229 if (p.next_token().character() == '*') {
233 string const name = p.verbatim_item();
234 string const opts = p.getOpt();
235 string const body = p.verbatim_item();
236 // only non-lyxspecific stuff
237 if (name != "\\noun "
238 && name != "\\tabularnewline "
240 && name != "\\lyxline "
241 && name != "\\lyxaddress "
242 && name != "\\lyxrightaddress "
243 && name != "\\boldsymbol "
244 && name != "\\lyxarrow ") {
246 ss << '\\' << t.cs();
249 ss << '{' << name << '}' << opts << '{' << body << "}\n";
250 h_preamble << ss.str();
252 ostream & out = in_preamble ? h_preamble : os;
253 out << "\\" << t.cs() << "{" << name << "}"
254 << opts << "{" << body << "}\n";
259 else if (t.cs() == "documentclass") {
261 split(p.getArg('[', ']'), opts, ',');
262 handle_opt(opts, known_languages, h_language);
263 handle_opt(opts, known_fontsizes, h_paperfontsize);
264 h_quotes_language = h_language;
265 h_options = join(opts, ",");
266 h_textclass = p.getArg('{', '}');
269 else if (t.cs() == "usepackage") {
270 string const options = p.getArg('[', ']');
271 string const name = p.getArg('{', '}');
272 if (options.empty() && name.find(',')) {
273 vector<string> vecnames;
274 split(name, vecnames, ',');
275 vector<string>::const_iterator it = vecnames.begin();
276 vector<string>::const_iterator end = vecnames.end();
277 for (; it != end; ++it)
278 handle_package(trim(*it), string());
280 handle_package(name, options);
284 else if (t.cs() == "newenvironment") {
285 string const name = p.getArg('{', '}');
287 ss << "\\newenvironment{" << name << "}";
290 ss << '{' << p.verbatim_item() << '}';
291 ss << '{' << p.verbatim_item() << '}';
293 if (name != "lyxcode" && name != "lyxlist"
294 && name != "lyxrightadress" && name != "lyxaddress")
295 h_preamble << ss.str();
298 else if (t.cs() == "def") {
299 string name = p.get_token().cs();
300 while (p.next_token().cat() != catBegin)
301 name += p.get_token().asString();
302 h_preamble << "\\def\\" << name << '{' << p.verbatim_item() << "}\n";
305 else if (t.cs() == "newcolumntype") {
306 string const name = p.getArg('{', '}');
309 string opts = p.getOpt();
311 istringstream is(string(opts, 1));
312 //cerr << "opt: " << is.str() << "\n";
315 special_columns[name[0]] = nargs;
316 h_preamble << "\\newcolumntype{" << name << "}";
318 h_preamble << "[" << nargs << "]";
319 h_preamble << "{" << p.verbatim_item() << "}\n";
322 else if (t.cs() == "setcounter") {
323 string const name = p.getArg('{', '}');
324 string const content = p.getArg('{', '}');
325 if (name == "secnumdepth")
326 h_secnumdepth = content;
327 else if (name == "tocdepth")
328 h_tocdepth = content;
330 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
333 else if (t.cs() == "setlength") {
334 string const name = p.verbatim_item();
335 string const content = p.verbatim_item();
336 if (name == "parskip")
337 h_paragraph_separation = "skip";
338 else if (name == "parindent")
339 h_paragraph_separation = "skip";
341 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
344 else if (t.cs() == "par")
347 else if (t.cs() == "begin") {
348 string const name = p.getArg('{', '}');
349 if (name == "document")
351 h_preamble << "\\begin{" << name << "}";
354 else if (t.cs().size())
355 h_preamble << '\\' << t.cs() << ' ';
358 LyXTextClass textclass;
359 textclass.Read(LibFileSearch("layouts", h_textclass, "layout"));
360 end_preamble(os, textclass);