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.
20 #include "lyxtextclass.h"
21 #include "support/path_defines.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "support/os.h"
26 #include <boost/function.hpp>
43 using std::istringstream;
44 using std::ostringstream;
45 using std::stringstream;
50 using lyx::support::system_lyxdir;
51 using lyx::support::user_lyxdir;
52 using lyx::support::IsFileReadable;
53 using lyx::support::IsFileWriteable;
55 // Hacks to allow the thing to link in the lyxlayout stuff
56 LyXErr lyxerr(std::cerr.rdbuf());
59 // hack to link in libsupport
63 string const trim(string const & a, char const * p)
70 string::size_type r = a.find_last_not_of(p);
71 string::size_type l = a.find_first_not_of(p);
73 // Is this the minimal test? (lgb)
74 if (r == string::npos && l == string::npos)
77 return a.substr(l, r - l + 1);
81 void split(string const & s, vector<string> & result, char delim)
83 //cerr << "split 1: '" << s << "'\n";
86 while (getline(is, t, delim))
88 //cerr << "split 2\n";
92 string join(vector<string> const & input, char const * delim)
95 for (size_t i = 0; i < input.size(); ++i) {
104 char const * const * is_known(string const & str, char const * const * what)
106 for ( ; *what; ++what)
114 // current stack of nested environments
115 vector<string> active_environments;
118 string active_environment()
120 return active_environments.empty() ? string() : active_environments.back();
124 map<string, vector<ArgumentType> > known_commands;
131 * Read a list of TeX commands from a reLyX compatible syntax file.
132 * Since this list is used after all commands that have a LyX counterpart
133 * are handled, it does not matter that the "syntax.default" file from reLyX
134 * has almost all of them listed. For the same reason the reLyX-specific
135 * reLyXre environment is ignored.
137 void read_syntaxfile(string const & file_name)
139 if (!IsFileReadable(file_name)) {
140 cerr << "Could not open syntax file \"" << file_name
141 << "\" for reading." << endl;
144 ifstream is(file_name.c_str());
145 // We can use our TeX parser, since the syntax of the layout file is
146 // modeled after TeX.
147 // Unknown tokens are just silently ignored, this helps us to skip some
148 // reLyX specific things.
151 Token const & t = p.get_token();
152 if (t.cat() == catEscape) {
153 string command = t.asInput();
154 if (p.next_token().asInput() == "*") {
159 vector<ArgumentType> arguments;
160 while (p.next_token().cat() == catBegin ||
161 p.next_token().asInput() == "[") {
162 if (p.next_token().cat() == catBegin) {
163 string const arg = p.getArg('{', '}');
164 if (arg == "translate")
165 arguments.push_back(required);
167 arguments.push_back(verbatim);
170 arguments.push_back(optional);
173 known_commands[command] = arguments;
179 string documentclass;
181 bool overwrite_files = false;
184 /// return the number of arguments consumed
185 typedef boost::function<int(string const &, string const &)> cmd_helper;
188 int parse_help(string const &, string const &)
190 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
191 "Command line switches (case sensitive):\n"
192 "\t-help summarize tex2lyx usage\n"
193 "\t-f Force creation of .lyx files even if they exist already\n"
194 "\t-userdir dir try to set user directory to dir\n"
195 "\t-sysdir dir try to set system directory to dir\n"
196 "\t-c textclass declare the textclass\n"
197 "\t-s syntaxfile read additional syntax file" << endl;
202 int parse_class(string const & arg, string const &)
205 cerr << "Missing textclass string after -c switch" << endl;
213 int parse_syntaxfile(string const & arg, string const &)
216 cerr << "Missing syntaxfile string after -s switch" << endl;
224 int parse_sysdir(string const & arg, string const &)
227 cerr << "Missing directory for -sysdir switch" << endl;
235 int parse_userdir(string const & arg, string const &)
238 cerr << "Missing directory for -userdir switch" << endl;
246 int parse_force(string const &, string const &)
248 overwrite_files = true;
253 void easyParse(int & argc, char * argv[])
255 map<string, cmd_helper> cmdmap;
257 cmdmap["-c"] = parse_class;
258 cmdmap["-f"] = parse_force;
259 cmdmap["-s"] = parse_syntaxfile;
260 cmdmap["-help"] = parse_help;
261 cmdmap["--help"] = parse_help;
262 cmdmap["-sysdir"] = parse_sysdir;
263 cmdmap["-userdir"] = parse_userdir;
265 for (int i = 1; i < argc; ++i) {
266 std::map<string, cmd_helper>::const_iterator it
267 = cmdmap.find(argv[i]);
269 // don't complain if not found - may be parsed later
270 if (it == cmdmap.end())
273 string arg((i + 1 < argc) ? argv[i + 1] : "");
274 string arg2((i + 2 < argc) ? argv[i + 2] : "");
276 int const remove = 1 + it->second(arg, arg2);
278 // Now, remove used arguments by shifting
279 // the following ones remove places down.
281 for (int j = i; j < argc; ++j)
282 argv[j] = argv[j + remove];
288 // path of the parsed file
289 string masterFilePath;
291 } // anonymous namespace
294 string getMasterFilePath()
296 return masterFilePath;
300 void tex2lyx(std::istream &is, std::ostream &os)
306 LyXTextClass textclass = parse_preamble(p, ss, documentclass);
308 active_environments.push_back("document");
309 Context context(true, textclass);
310 parse_text(p, ss, FLAG_END, true, context);
311 context.check_end_layout(ss);
312 ss << "\n\\end_body\n\\end_document\n";
313 active_environments.pop_back();
318 ofstream parsertest("parsertest.tex");
320 parsertest << p.get_token().asInput();
321 // <origfile> and parsertest.tex should now have identical content
326 bool tex2lyx(string const &infilename, string const &outfilename)
328 if (!(IsFileReadable(infilename) && IsFileWriteable(outfilename))) {
331 if (!overwrite_files && IsFileReadable(outfilename)) {
332 cerr << "Not overwriting existing file " << outfilename << "\n";
335 ifstream is(infilename.c_str());
336 ofstream os(outfilename.c_str());
338 cerr << "File: " << infilename << "\n";
345 int main(int argc, char * argv[])
347 easyParse(argc, argv);
350 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
351 "See tex2lyx -help." << endl;
355 lyx::support::os::init(&argc, &argv);
356 lyx::support::setLyxPaths();
358 string const system_syntaxfile = lyx::support::LibFileSearch("reLyX", "syntax.default");
359 if (system_syntaxfile.empty()) {
360 cerr << "Error: Could not find syntax file \"syntax.default\"." << endl;
363 read_syntaxfile(system_syntaxfile);
364 if (!syntaxfile.empty())
365 read_syntaxfile(syntaxfile);
367 if (!IsFileReadable(argv[1])) {
368 cerr << "Could not open input file \"" << argv[1]
369 << "\" for reading." << endl;
373 if (lyx::support::AbsolutePath(argv[1]))
374 masterFilePath = lyx::support::OnlyPath(argv[1]);
376 masterFilePath = lyx::support::getcwd();
378 ifstream is(argv[1]);