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/path_defines.h"
21 #include "support/filetools.h"
22 #include "support/os.h"
24 #include <boost/function.hpp>
41 using std::istringstream;
42 using std::ostringstream;
43 using std::stringstream;
48 using lyx::support::system_lyxdir;
49 using lyx::support::user_lyxdir;
50 using lyx::support::IsFileReadable;
51 using lyx::support::IsFileWriteable;
53 // Hacks to allow the thing to link in the lyxlayout stuff
54 LyXErr lyxerr(std::cerr.rdbuf());
57 string const trim(string const & a, char const * p)
64 string::size_type r = a.find_last_not_of(p);
65 string::size_type l = a.find_first_not_of(p);
67 // Is this the minimal test? (lgb)
68 if (r == string::npos && l == string::npos)
71 return a.substr(l, r - l + 1);
75 void split(string const & s, vector<string> & result, char delim)
77 //cerr << "split 1: '" << s << "'\n";
80 while (getline(is, t, delim))
82 //cerr << "split 2\n";
86 string join(vector<string> const & input, char const * delim)
89 for (size_t i = 0; i < input.size(); ++i) {
98 char const ** is_known(string const & str, char const ** what)
100 for ( ; *what; ++what)
108 // current stack of nested environments
109 vector<string> active_environments;
112 string active_environment()
114 return active_environments.empty() ? string() : active_environments.back();
118 map<string, vector<ArgumentType> > known_commands;
125 * Read a list of TeX commands from a reLyX compatible syntax file.
126 * Since this list is used after all commands that have a LyX counterpart
127 * are handled, it does not matter that the "syntax.default" file from reLyX
128 * has almost all of them listed. For the same reason the reLyX-specific
129 * reLyXre environment is ignored.
131 void read_syntaxfile(string const & file_name)
133 if (!IsFileReadable(file_name)) {
134 cerr << "Could not open syntax file \"" << file_name
135 << "\" for reading." << endl;
138 ifstream is(file_name.c_str());
139 // We can use our TeX parser, since the syntax of the layout file is
140 // modeled after TeX.
141 // Unknown tokens are just silently ignored, this helps us to skip some
142 // reLyX specific things.
145 Token const & t = p.get_token();
146 if (t.cat() == catEscape) {
147 string command = t.asInput();
148 if (p.next_token().asInput() == "*") {
153 vector<ArgumentType> arguments;
154 while (p.next_token().cat() == catBegin ||
155 p.next_token().asInput() == "[") {
156 if (p.next_token().cat() == catBegin) {
157 string const arg = p.getArg('{', '}');
158 if (arg == "translate")
159 arguments.push_back(required);
161 arguments.push_back(verbatim);
164 arguments.push_back(optional);
167 known_commands[command] = arguments;
173 string documentclass;
175 bool overwrite_files = false;
178 /// return the number of arguments consumed
179 typedef boost::function<int(string const &, string const &)> cmd_helper;
182 int parse_help(string const &, string const &)
184 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
185 "Command line switches (case sensitive):\n"
186 "\t-help summarize tex2lyx usage\n"
187 "\t-f Force creation of .lyx files even if they exist already\n"
188 "\t-userdir dir try to set user directory to dir\n"
189 "\t-sysdir dir try to set system directory to dir\n"
190 "\t-c textclass declare the textclass\n"
191 "\t-s syntaxfile read additional syntax file" << endl;
196 int parse_class(string const & arg, string const &)
199 cerr << "Missing textclass string after -c switch" << endl;
207 int parse_syntaxfile(string const & arg, string const &)
210 cerr << "Missing syntaxfile string after -s switch" << endl;
218 int parse_sysdir(string const & arg, string const &)
221 cerr << "Missing directory for -sysdir switch" << endl;
229 int parse_userdir(string const & arg, string const &)
232 cerr << "Missing directory for -userdir switch" << endl;
240 int parse_force(string const &, string const &)
242 overwrite_files = true;
247 void easyParse(int & argc, char * argv[])
249 map<string, cmd_helper> cmdmap;
251 cmdmap["-c"] = parse_class;
252 cmdmap["-f"] = parse_force;
253 cmdmap["-s"] = parse_syntaxfile;
254 cmdmap["-help"] = parse_help;
255 cmdmap["--help"] = parse_help;
256 cmdmap["-sysdir"] = parse_sysdir;
257 cmdmap["-userdir"] = parse_userdir;
259 for (int i = 1; i < argc; ++i) {
260 std::map<string, cmd_helper>::const_iterator it
261 = cmdmap.find(argv[i]);
263 // don't complain if not found - may be parsed later
264 if (it == cmdmap.end())
267 string arg((i + 1 < argc) ? argv[i + 1] : "");
268 string arg2((i + 2 < argc) ? argv[i + 2] : "");
270 int const remove = 1 + it->second(arg, arg2);
272 // Now, remove used arguments by shifting
273 // the following ones remove places down.
275 for (int j = i; j < argc; ++j)
276 argv[j] = argv[j + remove];
281 } // anonymous namespace
284 void tex2lyx(std::istream &is, std::ostream &os)
290 LyXTextClass textclass = parse_preamble(p, ss, documentclass);
292 active_environments.push_back("document");
293 Context context(true, textclass);
294 parse_text(p, ss, FLAG_END, true, context);
295 context.check_end_layout(ss);
296 ss << "\n\\end_document\n";
297 active_environments.pop_back();
302 ofstream parsertest("parsertest.tex");
304 parsertest << p.get_token().asInput();
305 // <origfile> and parsertest.tex should now have identical content
310 bool tex2lyx(string const &infilename, string const &outfilename)
312 if (!(IsFileReadable(infilename) && IsFileWriteable(outfilename))) {
315 if (!overwrite_files && IsFileReadable(outfilename)) {
316 cerr << "Not overwriting existing file " << outfilename << "\n";
319 ifstream is(infilename.c_str());
320 ofstream os(outfilename.c_str());
322 cerr << "File: " << infilename << "\n";
329 int main(int argc, char * argv[])
331 easyParse(argc, argv);
334 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
335 "See tex2lyx -help." << endl;
339 lyx::support::os::init(&argc, &argv);
340 lyx::support::setLyxPaths();
342 string const system_syntaxfile = lyx::support::LibFileSearch("reLyX", "syntax.default");
343 if (system_syntaxfile.empty()) {
344 cerr << "Error: Could not find syntax file \"syntax.default\"." << endl;
347 read_syntaxfile(system_syntaxfile);
348 if (!syntaxfile.empty())
349 read_syntaxfile(syntaxfile);
351 if (!IsFileReadable(argv[1])) {
352 cerr << "Could not open input file \"" << argv[1]
353 << "\" for reading." << endl;
356 ifstream is(argv[1]);