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;
35 using lyx::support::LibFileSearch;
37 // special columntypes
38 extern std::map<char, int> special_columns;
42 const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
43 "breton", "british", "bulgarian", "catalan", "croatian", "czech", "danish",
44 "dutch", "english", "esperanto", "estonian", "finnish", "francais",
45 "frenchb", "galician", "german", "germanb", "greek", "hebcal", "hebfont",
46 "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
47 "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
48 "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
49 "lhecmss", "lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
50 "lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
51 "ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
52 "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
53 "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
55 char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
58 ostringstream h_preamble;
59 string h_textclass = "article";
60 string h_options = string();
61 string h_language = "english";
62 string h_inputencoding = "latin1";
63 string h_fontscheme = "default";
64 string h_graphics = "default";
65 string h_paperfontsize = "default";
66 string h_spacing = "single";
67 string h_papersize = "default";
68 string h_paperpackage = "default";
69 string h_use_geometry = "0";
70 string h_use_amsmath = "0";
71 string h_use_natbib = "0";
72 string h_use_numerical_citations = "0";
73 string h_paperorientation = "portrait";
74 string h_secnumdepth = "3";
75 string h_tocdepth = "3";
76 string h_paragraph_separation = "indent";
77 string h_defskip = "medskip";
78 string h_quotes_language = "english";
79 string h_quotes_times = "2";
80 string h_papercolumns = "1";
81 string h_papersides = "1";
82 string h_paperpagestyle = "default";
83 string h_tracking_changes = "0";
86 void handle_opt(vector<string> & opts, char const ** what, string & target)
91 for ( ; *what; ++what) {
92 vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
93 if (it != opts.end()) {
94 //cerr << "### found option '" << *what << "'\n";
103 void handle_package(string const & name, string const & options)
105 //cerr << "handle_package: '" << name << "'\n";
106 if (name == "a4wide") {
107 h_papersize = "a4paper";
108 h_paperpackage = "widemarginsa4";
109 } else if (name == "ae")
111 else if (name == "aecompl")
113 else if (name == "amsmath")
115 else if (name == "amssymb")
117 else if (name == "babel")
119 else if (name == "fontenc")
121 else if (name == "inputenc")
122 h_inputencoding = options;
123 else if (name == "makeidx")
125 else if (name == "verbatim")
127 else if (is_known(name, known_languages)) {
129 h_quotes_language = name;
132 h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
134 h_preamble << "\\usepackage{" << name << "}\n";
140 void end_preamble(ostream & os, LyXTextClass const & /*textclass*/)
142 os << "#LyX file created by tex2lyx 0.1.2 \n"
143 << "\\lyxformat 225\n"
144 << "\\textclass " << h_textclass << "\n"
145 << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
146 if (h_options.size())
147 os << "\\options " << h_options << "\n";
148 os << "\\language " << h_language << "\n"
149 << "\\inputencoding " << h_inputencoding << "\n"
150 << "\\fontscheme " << h_fontscheme << "\n"
151 << "\\graphics " << h_graphics << "\n"
152 << "\\paperfontsize " << h_paperfontsize << "\n"
153 << "\\spacing " << h_spacing << "\n"
154 << "\\papersize " << h_papersize << "\n"
155 << "\\paperpackage " << h_paperpackage << "\n"
156 << "\\use_geometry " << h_use_geometry << "\n"
157 << "\\use_amsmath " << h_use_amsmath << "\n"
158 << "\\use_natbib " << h_use_natbib << "\n"
159 << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
160 << "\\paperorientation " << h_paperorientation << "\n"
161 << "\\secnumdepth " << h_secnumdepth << "\n"
162 << "\\tocdepth " << h_tocdepth << "\n"
163 << "\\paragraph_separation " << h_paragraph_separation << "\n"
164 << "\\defskip " << h_defskip << "\n"
165 << "\\quotes_language " << h_quotes_language << "\n"
166 << "\\quotes_times " << h_quotes_times << "\n"
167 << "\\papercolumns " << h_papercolumns << "\n"
168 << "\\papersides " << h_papersides << "\n"
169 << "\\paperpagestyle " << h_paperpagestyle << "\n"
170 << "\\tracking_changes " << h_tracking_changes << "\n"
174 } // anonymous namespace
176 LyXTextClass const parse_preamble(Parser & p, ostream & os)
178 // initialize fixed types
179 special_columns['D'] = 3;
182 Token const & t = p.get_token();
185 cerr << "t: " << t << " flags: " << flags << "\n";
192 if (t.cat() == catLetter ||
193 t.cat() == catSpace ||
194 t.cat() == catSuper ||
196 t.cat() == catOther ||
197 t.cat() == catMath ||
198 t.cat() == catActive ||
199 t.cat() == catBegin ||
201 t.cat() == catAlign ||
202 t.cat() == catNewline ||
203 t.cat() == catParameter)
204 h_preamble << t.character();
206 else if (t.cat() == catComment)
209 else if (t.cs() == "pagestyle")
210 h_paperpagestyle == p.verbatim_item();
212 else if (t.cs() == "makeatletter") {
213 p.setCatCode('@', catLetter);
214 h_preamble << "\\makeatletter\n";
217 else if (t.cs() == "makeatother") {
218 p.setCatCode('@', catOther);
219 h_preamble << "\\makeatother\n";
222 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
223 || t.cs() == "providecommand") {
225 if (p.next_token().character() == '*') {
229 string const name = p.verbatim_item();
230 string const opts = p.getOpt();
231 string const body = p.verbatim_item();
232 // only non-lyxspecific stuff
233 if (name != "\\noun "
234 && name != "\\tabularnewline "
236 && name != "\\lyxline "
237 && name != "\\lyxaddress "
238 && name != "\\lyxrightaddress "
239 && name != "\\boldsymbol "
240 && name != "\\lyxarrow ") {
242 ss << '\\' << t.cs();
245 ss << '{' << name << '}' << opts << '{' << body << "}\n";
246 h_preamble << ss.str();
248 ostream & out = in_preamble ? h_preamble : os;
249 out << "\\" << t.cs() << "{" << name << "}"
250 << opts << "{" << body << "}\n";
255 else if (t.cs() == "documentclass") {
257 split(p.getArg('[', ']'), opts, ',');
258 handle_opt(opts, known_languages, h_language);
259 handle_opt(opts, known_fontsizes, h_paperfontsize);
260 h_quotes_language = h_language;
261 h_options = join(opts, ",");
262 h_textclass = p.getArg('{', '}');
265 else if (t.cs() == "usepackage") {
266 string const options = p.getArg('[', ']');
267 string const name = p.getArg('{', '}');
268 if (options.empty() && name.find(',')) {
269 vector<string> vecnames;
270 split(name, vecnames, ',');
271 vector<string>::const_iterator it = vecnames.begin();
272 vector<string>::const_iterator end = vecnames.end();
273 for (; it != end; ++it)
274 handle_package(trim(*it), string());
276 handle_package(name, options);
280 else if (t.cs() == "newenvironment") {
281 string const name = p.getArg('{', '}');
283 ss << "\\newenvironment{" << name << "}";
286 ss << '{' << p.verbatim_item() << '}';
287 ss << '{' << p.verbatim_item() << '}';
289 if (name != "lyxcode" && name != "lyxlist"
290 && name != "lyxrightadress" && name != "lyxaddress")
291 h_preamble << ss.str();
294 else if (t.cs() == "def") {
295 string name = p.get_token().cs();
296 while (p.next_token().cat() != catBegin)
297 name += p.get_token().asString();
298 h_preamble << "\\def\\" << name << '{' << p.verbatim_item() << "}\n";
301 else if (t.cs() == "newcolumntype") {
302 string const name = p.getArg('{', '}');
305 string opts = p.getOpt();
307 istringstream is(string(opts, 1));
308 //cerr << "opt: " << is.str() << "\n";
311 special_columns[name[0]] = nargs;
312 h_preamble << "\\newcolumntype{" << name << "}";
314 h_preamble << "[" << nargs << "]";
315 h_preamble << "{" << p.verbatim_item() << "}\n";
318 else if (t.cs() == "setcounter") {
319 string const name = p.getArg('{', '}');
320 string const content = p.getArg('{', '}');
321 if (name == "secnumdepth")
322 h_secnumdepth = content;
323 else if (name == "tocdepth")
324 h_tocdepth = content;
326 h_preamble << "\\setcounter{" << name << "}{" << content << "}\n";
329 else if (t.cs() == "setlength") {
330 string const name = p.verbatim_item();
331 string const content = p.verbatim_item();
332 if (name == "parskip")
333 h_paragraph_separation = "skip";
334 else if (name == "parindent")
335 h_paragraph_separation = "skip";
337 h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
340 else if (t.cs() == "par")
343 else if (t.cs() == "begin") {
344 string const name = p.getArg('{', '}');
345 if (name == "document")
347 h_preamble << "\\begin{" << name << "}";
350 else if (t.cs().size())
351 h_preamble << '\\' << t.cs() << ' ';
354 LyXTextClass textclass;
355 textclass.Read(LibFileSearch("layouts", h_textclass, "layout"));
356 end_preamble(os, textclass);