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.
19 #include "lyxtextclass.h"
21 #include "support/convert.h"
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
24 #include "support/lyxlib.h"
25 #include "support/os.h"
26 #include "support/package.h"
28 #include <boost/function.hpp>
45 using std::istringstream;
46 using std::ostringstream;
47 using std::stringstream;
52 using lyx::support::isStrUnsignedInt;
53 using lyx::support::ltrim;
54 using lyx::support::rtrim;
55 using lyx::support::IsFileReadable;
56 using lyx::support::IsFileWriteable;
58 // Hacks to allow the thing to link in the lyxlayout stuff
59 LyXErr lyxerr(std::cerr.rdbuf());
62 string const trim(string const & a, char const * p)
69 string::size_type r = a.find_last_not_of(p);
70 string::size_type l = a.find_first_not_of(p);
72 // Is this the minimal test? (lgb)
73 if (r == string::npos && l == string::npos)
76 return a.substr(l, r - l + 1);
80 void split(string const & s, vector<string> & result, char delim)
82 //cerr << "split 1: '" << s << "'\n";
85 while (getline(is, t, delim))
87 //cerr << "split 2\n";
91 string join(vector<string> const & input, char const * delim)
94 for (size_t i = 0; i < input.size(); ++i) {
103 char const * const * is_known(string const & str, char const * const * what)
105 for ( ; *what; ++what)
113 // current stack of nested environments
114 vector<string> active_environments;
117 string active_environment()
119 return active_environments.empty() ? string() : active_environments.back();
123 map<string, vector<ArgumentType> > known_commands;
126 void add_known_command(string const & command, string const & o1,
129 // We have to handle the following cases:
130 // definition o1 o2 invocation result
131 // \newcommand{\foo}{bar} "" false \foo bar
132 // \newcommand{\foo}[1]{bar #1} "[1]" false \foo{x} bar x
133 // \newcommand{\foo}[1][]{bar #1} "[1]" true \foo bar
134 // \newcommand{\foo}[1][]{bar #1} "[1]" true \foo[x] bar x
135 // \newcommand{\foo}[1][x]{bar #1} "[1]" true \foo[x] bar x
136 unsigned int nargs = 0;
137 vector<ArgumentType> arguments;
138 string const opt1 = rtrim(ltrim(o1, "["), "]");
139 if (isStrUnsignedInt(opt1)) {
140 // The command has arguments
141 nargs = convert<unsigned int>(opt1);
142 if (nargs > 0 && o2) {
143 // The first argument is optional
144 arguments.push_back(optional);
148 for (unsigned int i = 0; i < nargs; ++i)
149 arguments.push_back(required);
150 known_commands[command] = arguments;
158 * Read a list of TeX commands from a reLyX compatible syntax file.
159 * Since this list is used after all commands that have a LyX counterpart
160 * are handled, it does not matter that the "syntax.default" file from reLyX
161 * has almost all of them listed. For the same reason the reLyX-specific
162 * reLyXre environment is ignored.
164 void read_syntaxfile(string const & file_name)
166 if (!IsFileReadable(file_name)) {
167 cerr << "Could not open syntax file \"" << file_name
168 << "\" for reading." << endl;
171 ifstream is(file_name.c_str());
172 // We can use our TeX parser, since the syntax of the layout file is
173 // modeled after TeX.
174 // Unknown tokens are just silently ignored, this helps us to skip some
175 // reLyX specific things.
178 Token const & t = p.get_token();
179 if (t.cat() == catEscape) {
180 string command = t.asInput();
181 if (p.next_token().asInput() == "*") {
186 vector<ArgumentType> arguments;
187 while (p.next_token().cat() == catBegin ||
188 p.next_token().asInput() == "[") {
189 if (p.next_token().cat() == catBegin) {
190 string const arg = p.getArg('{', '}');
191 if (arg == "translate")
192 arguments.push_back(required);
194 arguments.push_back(verbatim);
197 arguments.push_back(optional);
200 known_commands[command] = arguments;
206 string documentclass;
208 bool overwrite_files = false;
211 /// return the number of arguments consumed
212 typedef boost::function<int(string const &, string const &)> cmd_helper;
215 int parse_help(string const &, string const &)
217 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
218 "Command line switches (case sensitive):\n"
219 "\t-help summarize tex2lyx usage\n"
220 "\t-f Force creation of .lyx files even if they exist already\n"
221 "\t-userdir dir try to set user directory to dir\n"
222 "\t-sysdir dir try to set system directory to dir\n"
223 "\t-c textclass declare the textclass\n"
224 "\t-s syntaxfile read additional syntax file" << endl;
229 int parse_class(string const & arg, string const &)
232 cerr << "Missing textclass string after -c switch" << endl;
240 int parse_syntaxfile(string const & arg, string const &)
243 cerr << "Missing syntaxfile string after -s switch" << endl;
251 // Filled with the command line arguments "foo" of "-sysdir foo" or
253 string cl_system_support;
254 string cl_user_support;
257 int parse_sysdir(string const & arg, string const &)
260 cerr << "Missing directory for -sysdir switch" << endl;
263 cl_system_support = arg;
268 int parse_userdir(string const & arg, string const &)
271 cerr << "Missing directory for -userdir switch" << endl;
274 cl_user_support = arg;
279 int parse_force(string const &, string const &)
281 overwrite_files = true;
286 void easyParse(int & argc, char * argv[])
288 map<string, cmd_helper> cmdmap;
290 cmdmap["-c"] = parse_class;
291 cmdmap["-f"] = parse_force;
292 cmdmap["-s"] = parse_syntaxfile;
293 cmdmap["-help"] = parse_help;
294 cmdmap["--help"] = parse_help;
295 cmdmap["-sysdir"] = parse_sysdir;
296 cmdmap["-userdir"] = parse_userdir;
298 for (int i = 1; i < argc; ++i) {
299 std::map<string, cmd_helper>::const_iterator it
300 = cmdmap.find(argv[i]);
302 // don't complain if not found - may be parsed later
303 if (it == cmdmap.end())
306 string arg((i + 1 < argc) ? argv[i + 1] : "");
307 string arg2((i + 2 < argc) ? argv[i + 2] : "");
309 int const remove = 1 + it->second(arg, arg2);
311 // Now, remove used arguments by shifting
312 // the following ones remove places down.
314 for (int j = i; j < argc; ++j)
315 argv[j] = argv[j + remove];
321 // path of the parsed file
322 string masterFilePath;
324 } // anonymous namespace
327 string getMasterFilePath()
329 return masterFilePath;
333 void tex2lyx(std::istream &is, std::ostream &os)
339 LyXTextClass textclass = parse_preamble(p, ss, documentclass);
341 active_environments.push_back("document");
342 Context context(true, textclass);
343 parse_text(p, ss, FLAG_END, true, context);
344 context.check_end_layout(ss);
345 ss << "\n\\end_body\n\\end_document\n";
346 active_environments.pop_back();
351 ofstream parsertest("parsertest.tex");
353 parsertest << p.get_token().asInput();
354 // <origfile> and parsertest.tex should now have identical content
359 bool tex2lyx(string const &infilename, string const &outfilename)
361 if (!(IsFileReadable(infilename) && IsFileWriteable(outfilename))) {
364 if (!overwrite_files && IsFileReadable(outfilename)) {
365 cerr << "Not overwriting existing file " << outfilename << "\n";
368 ifstream is(infilename.c_str());
369 ofstream os(outfilename.c_str());
371 cerr << "File: " << infilename << "\n";
378 int main(int argc, char * argv[])
380 easyParse(argc, argv);
383 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
384 "See tex2lyx -help." << endl;
388 lyx::support::os::init(argc, argv);
389 lyx::support::init_package(argv[0], cl_system_support, cl_user_support);
391 string const system_syntaxfile = lyx::support::LibFileSearch("reLyX", "syntax.default");
392 if (system_syntaxfile.empty()) {
393 cerr << "Error: Could not find syntax file \"syntax.default\"." << endl;
396 read_syntaxfile(system_syntaxfile);
397 if (!syntaxfile.empty())
398 read_syntaxfile(syntaxfile);
400 if (!IsFileReadable(argv[1])) {
401 cerr << "Could not open input file \"" << argv[1]
402 << "\" for reading." << endl;
406 if (lyx::support::AbsolutePath(argv[1]))
407 masterFilePath = lyx::support::OnlyPath(argv[1]);
409 masterFilePath = lyx::support::getcwd();
411 ifstream is(argv[1]);