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"
20 #include "support/filetools.h"
21 #include "support/lstrings.h"
22 #include "support/lyxlib.h"
23 #include "support/os.h"
24 #include "support/package.h"
26 #include <boost/function.hpp>
43 using std::istringstream;
44 using std::ostringstream;
45 using std::stringstream;
50 using lyx::support::isStrUnsignedInt;
51 using lyx::support::ltrim;
52 using lyx::support::rtrim;
53 using lyx::support::strToUnsignedInt;
54 using lyx::support::IsFileReadable;
55 using lyx::support::IsFileWriteable;
57 // Hacks to allow the thing to link in the lyxlayout stuff
58 LyXErr lyxerr(std::cerr.rdbuf());
61 string const trim(string const & a, char const * p)
68 string::size_type r = a.find_last_not_of(p);
69 string::size_type l = a.find_first_not_of(p);
71 // Is this the minimal test? (lgb)
72 if (r == string::npos && l == string::npos)
75 return a.substr(l, r - l + 1);
79 void split(string const & s, vector<string> & result, char delim)
81 //cerr << "split 1: '" << s << "'\n";
84 while (getline(is, t, delim))
86 //cerr << "split 2\n";
90 string join(vector<string> const & input, char const * delim)
93 for (size_t i = 0; i < input.size(); ++i) {
102 char const * const * is_known(string const & str, char const * const * what)
104 for ( ; *what; ++what)
112 // current stack of nested environments
113 vector<string> active_environments;
116 string active_environment()
118 return active_environments.empty() ? string() : active_environments.back();
122 map<string, vector<ArgumentType> > known_commands;
125 void add_known_command(string const & command, string const & o1,
128 // We have to handle the following cases:
129 // definition o1 o2 invocation result
130 // \newcommand{\foo}{bar} "" false \foo bar
131 // \newcommand{\foo}[1]{bar #1} "[1]" false \foo{x} bar x
132 // \newcommand{\foo}[1][]{bar #1} "[1]" true \foo bar
133 // \newcommand{\foo}[1][]{bar #1} "[1]" true \foo[x] bar x
134 // \newcommand{\foo}[1][x]{bar #1} "[1]" true \foo[x] bar x
135 unsigned int nargs = 0;
136 vector<ArgumentType> arguments;
137 string const opt1 = rtrim(ltrim(o1, "["), "]");
138 if (isStrUnsignedInt(opt1)) {
139 // The command has arguments
140 nargs = strToUnsignedInt(opt1);
141 if (nargs > 0 && o2) {
142 // The first argument is optional
143 arguments.push_back(optional);
147 for (unsigned int i = 0; i < nargs; ++i)
148 arguments.push_back(required);
149 known_commands[command] = arguments;
157 * Read a list of TeX commands from a reLyX compatible syntax file.
158 * Since this list is used after all commands that have a LyX counterpart
159 * are handled, it does not matter that the "syntax.default" file from reLyX
160 * has almost all of them listed. For the same reason the reLyX-specific
161 * reLyXre environment is ignored.
163 void read_syntaxfile(string const & file_name)
165 if (!IsFileReadable(file_name)) {
166 cerr << "Could not open syntax file \"" << file_name
167 << "\" for reading." << endl;
170 ifstream is(file_name.c_str());
171 // We can use our TeX parser, since the syntax of the layout file is
172 // modeled after TeX.
173 // Unknown tokens are just silently ignored, this helps us to skip some
174 // reLyX specific things.
177 Token const & t = p.get_token();
178 if (t.cat() == catEscape) {
179 string command = t.asInput();
180 if (p.next_token().asInput() == "*") {
185 vector<ArgumentType> arguments;
186 while (p.next_token().cat() == catBegin ||
187 p.next_token().asInput() == "[") {
188 if (p.next_token().cat() == catBegin) {
189 string const arg = p.getArg('{', '}');
190 if (arg == "translate")
191 arguments.push_back(required);
193 arguments.push_back(verbatim);
196 arguments.push_back(optional);
199 known_commands[command] = arguments;
205 string documentclass;
207 bool overwrite_files = false;
210 /// return the number of arguments consumed
211 typedef boost::function<int(string const &, string const &)> cmd_helper;
214 int parse_help(string const &, string const &)
216 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
217 "Command line switches (case sensitive):\n"
218 "\t-help summarize tex2lyx usage\n"
219 "\t-f Force creation of .lyx files even if they exist already\n"
220 "\t-userdir dir try to set user directory to dir\n"
221 "\t-sysdir dir try to set system directory to dir\n"
222 "\t-c textclass declare the textclass\n"
223 "\t-s syntaxfile read additional syntax file" << endl;
228 int parse_class(string const & arg, string const &)
231 cerr << "Missing textclass string after -c switch" << endl;
239 int parse_syntaxfile(string const & arg, string const &)
242 cerr << "Missing syntaxfile string after -s switch" << endl;
250 // Filled with the command line arguments "foo" of "-sysdir foo" or
252 string cl_system_support;
253 string cl_user_support;
256 int parse_sysdir(string const & arg, string const &)
259 cerr << "Missing directory for -sysdir switch" << endl;
262 cl_system_support = arg;
267 int parse_userdir(string const & arg, string const &)
270 cerr << "Missing directory for -userdir switch" << endl;
273 cl_user_support = arg;
278 int parse_force(string const &, string const &)
280 overwrite_files = true;
285 void easyParse(int & argc, char * argv[])
287 map<string, cmd_helper> cmdmap;
289 cmdmap["-c"] = parse_class;
290 cmdmap["-f"] = parse_force;
291 cmdmap["-s"] = parse_syntaxfile;
292 cmdmap["-help"] = parse_help;
293 cmdmap["--help"] = parse_help;
294 cmdmap["-sysdir"] = parse_sysdir;
295 cmdmap["-userdir"] = parse_userdir;
297 for (int i = 1; i < argc; ++i) {
298 std::map<string, cmd_helper>::const_iterator it
299 = cmdmap.find(argv[i]);
301 // don't complain if not found - may be parsed later
302 if (it == cmdmap.end())
305 string arg((i + 1 < argc) ? argv[i + 1] : "");
306 string arg2((i + 2 < argc) ? argv[i + 2] : "");
308 int const remove = 1 + it->second(arg, arg2);
310 // Now, remove used arguments by shifting
311 // the following ones remove places down.
313 for (int j = i; j < argc; ++j)
314 argv[j] = argv[j + remove];
320 // path of the parsed file
321 string masterFilePath;
323 } // anonymous namespace
326 string getMasterFilePath()
328 return masterFilePath;
332 void tex2lyx(std::istream &is, std::ostream &os)
338 LyXTextClass textclass = parse_preamble(p, ss, documentclass);
340 active_environments.push_back("document");
341 Context context(true, textclass);
342 parse_text(p, ss, FLAG_END, true, context);
343 context.check_end_layout(ss);
344 ss << "\n\\end_body\n\\end_document\n";
345 active_environments.pop_back();
350 ofstream parsertest("parsertest.tex");
352 parsertest << p.get_token().asInput();
353 // <origfile> and parsertest.tex should now have identical content
358 bool tex2lyx(string const &infilename, string const &outfilename)
360 if (!(IsFileReadable(infilename) && IsFileWriteable(outfilename))) {
363 if (!overwrite_files && IsFileReadable(outfilename)) {
364 cerr << "Not overwriting existing file " << outfilename << "\n";
367 ifstream is(infilename.c_str());
368 ofstream os(outfilename.c_str());
370 cerr << "File: " << infilename << "\n";
377 int main(int argc, char * argv[])
379 easyParse(argc, argv);
382 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
383 "See tex2lyx -help." << endl;
387 lyx::support::os::init(argc, argv);
388 lyx::support::init_package(argv[0], cl_system_support, cl_user_support);
390 string const system_syntaxfile = lyx::support::LibFileSearch("reLyX", "syntax.default");
391 if (system_syntaxfile.empty()) {
392 cerr << "Error: Could not find syntax file \"syntax.default\"." << endl;
395 read_syntaxfile(system_syntaxfile);
396 if (!syntaxfile.empty())
397 read_syntaxfile(syntaxfile);
399 if (!IsFileReadable(argv[1])) {
400 cerr << "Could not open input file \"" << argv[1]
401 << "\" for reading." << endl;
405 if (lyx::support::AbsolutePath(argv[1]))
406 masterFilePath = lyx::support::OnlyPath(argv[1]);
408 masterFilePath = lyx::support::getcwd();
410 ifstream is(argv[1]);