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/lyxlib.h"
23 #include "support/os.h"
25 #include <boost/function.hpp>
42 using std::istringstream;
43 using std::ostringstream;
44 using std::stringstream;
49 using lyx::support::system_lyxdir;
50 using lyx::support::user_lyxdir;
51 using lyx::support::IsFileReadable;
52 using lyx::support::IsFileWriteable;
54 // Hacks to allow the thing to link in the lyxlayout stuff
55 LyXErr lyxerr(std::cerr.rdbuf());
58 string const trim(string const & a, char const * p)
65 string::size_type r = a.find_last_not_of(p);
66 string::size_type l = a.find_first_not_of(p);
68 // Is this the minimal test? (lgb)
69 if (r == string::npos && l == string::npos)
72 return a.substr(l, r - l + 1);
76 void split(string const & s, vector<string> & result, char delim)
78 //cerr << "split 1: '" << s << "'\n";
81 while (getline(is, t, delim))
83 //cerr << "split 2\n";
87 string join(vector<string> const & input, char const * delim)
90 for (size_t i = 0; i < input.size(); ++i) {
99 char const * const * is_known(string const & str, char const * const * what)
101 for ( ; *what; ++what)
109 // current stack of nested environments
110 vector<string> active_environments;
113 string active_environment()
115 return active_environments.empty() ? string() : active_environments.back();
119 map<string, vector<ArgumentType> > known_commands;
126 * Read a list of TeX commands from a reLyX compatible syntax file.
127 * Since this list is used after all commands that have a LyX counterpart
128 * are handled, it does not matter that the "syntax.default" file from reLyX
129 * has almost all of them listed. For the same reason the reLyX-specific
130 * reLyXre environment is ignored.
132 void read_syntaxfile(string const & file_name)
134 if (!IsFileReadable(file_name)) {
135 cerr << "Could not open syntax file \"" << file_name
136 << "\" for reading." << endl;
139 ifstream is(file_name.c_str());
140 // We can use our TeX parser, since the syntax of the layout file is
141 // modeled after TeX.
142 // Unknown tokens are just silently ignored, this helps us to skip some
143 // reLyX specific things.
146 Token const & t = p.get_token();
147 if (t.cat() == catEscape) {
148 string command = t.asInput();
149 if (p.next_token().asInput() == "*") {
154 vector<ArgumentType> arguments;
155 while (p.next_token().cat() == catBegin ||
156 p.next_token().asInput() == "[") {
157 if (p.next_token().cat() == catBegin) {
158 string const arg = p.getArg('{', '}');
159 if (arg == "translate")
160 arguments.push_back(required);
162 arguments.push_back(verbatim);
165 arguments.push_back(optional);
168 known_commands[command] = arguments;
174 string documentclass;
176 bool overwrite_files = false;
179 /// return the number of arguments consumed
180 typedef boost::function<int(string const &, string const &)> cmd_helper;
183 int parse_help(string const &, string const &)
185 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
186 "Command line switches (case sensitive):\n"
187 "\t-help summarize tex2lyx usage\n"
188 "\t-f Force creation of .lyx files even if they exist already\n"
189 "\t-userdir dir try to set user directory to dir\n"
190 "\t-sysdir dir try to set system directory to dir\n"
191 "\t-c textclass declare the textclass\n"
192 "\t-s syntaxfile read additional syntax file" << endl;
197 int parse_class(string const & arg, string const &)
200 cerr << "Missing textclass string after -c switch" << endl;
208 int parse_syntaxfile(string const & arg, string const &)
211 cerr << "Missing syntaxfile string after -s switch" << endl;
219 int parse_sysdir(string const & arg, string const &)
222 cerr << "Missing directory for -sysdir switch" << endl;
230 int parse_userdir(string const & arg, string const &)
233 cerr << "Missing directory for -userdir switch" << endl;
241 int parse_force(string const &, string const &)
243 overwrite_files = true;
248 void easyParse(int & argc, char * argv[])
250 map<string, cmd_helper> cmdmap;
252 cmdmap["-c"] = parse_class;
253 cmdmap["-f"] = parse_force;
254 cmdmap["-s"] = parse_syntaxfile;
255 cmdmap["-help"] = parse_help;
256 cmdmap["--help"] = parse_help;
257 cmdmap["-sysdir"] = parse_sysdir;
258 cmdmap["-userdir"] = parse_userdir;
260 for (int i = 1; i < argc; ++i) {
261 std::map<string, cmd_helper>::const_iterator it
262 = cmdmap.find(argv[i]);
264 // don't complain if not found - may be parsed later
265 if (it == cmdmap.end())
268 string arg((i + 1 < argc) ? argv[i + 1] : "");
269 string arg2((i + 2 < argc) ? argv[i + 2] : "");
271 int const remove = 1 + it->second(arg, arg2);
273 // Now, remove used arguments by shifting
274 // the following ones remove places down.
276 for (int j = i; j < argc; ++j)
277 argv[j] = argv[j + remove];
283 // path of the parsed file
284 string masterFilePath;
286 } // anonymous namespace
289 string getMasterFilePath()
291 return masterFilePath;
295 void tex2lyx(std::istream &is, std::ostream &os)
301 LyXTextClass textclass = parse_preamble(p, ss, documentclass);
303 active_environments.push_back("document");
304 Context context(true, textclass);
305 parse_text(p, ss, FLAG_END, true, context);
306 context.check_end_layout(ss);
307 ss << "\n\\end_body\n\\end_document\n";
308 active_environments.pop_back();
313 ofstream parsertest("parsertest.tex");
315 parsertest << p.get_token().asInput();
316 // <origfile> and parsertest.tex should now have identical content
321 bool tex2lyx(string const &infilename, string const &outfilename)
323 if (!(IsFileReadable(infilename) && IsFileWriteable(outfilename))) {
326 if (!overwrite_files && IsFileReadable(outfilename)) {
327 cerr << "Not overwriting existing file " << outfilename << "\n";
330 ifstream is(infilename.c_str());
331 ofstream os(outfilename.c_str());
333 cerr << "File: " << infilename << "\n";
340 int main(int argc, char * argv[])
342 easyParse(argc, argv);
345 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
346 "See tex2lyx -help." << endl;
350 lyx::support::os::init(&argc, &argv);
351 lyx::support::setLyxPaths();
353 string const system_syntaxfile = lyx::support::LibFileSearch("reLyX", "syntax.default");
354 if (system_syntaxfile.empty()) {
355 cerr << "Error: Could not find syntax file \"syntax.default\"." << endl;
358 read_syntaxfile(system_syntaxfile);
359 if (!syntaxfile.empty())
360 read_syntaxfile(syntaxfile);
362 if (!IsFileReadable(argv[1])) {
363 cerr << "Could not open input file \"" << argv[1]
364 << "\" for reading." << endl;
368 if (lyx::support::AbsolutePath(argv[1]))
369 masterFilePath = lyx::support::OnlyPath(argv[1]);
371 masterFilePath = lyx::support::getcwd();
373 ifstream is(argv[1]);