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;
47 using lyx::support::system_lyxdir;
48 using lyx::support::user_lyxdir;
49 using lyx::support::IsFileReadable;
50 using lyx::support::IsFileWriteable;
52 // Hacks to allow the thing to link in the lyxlayout stuff
53 LyXErr lyxerr(std::cerr.rdbuf());
55 void handle_comment(Parser & p)
59 Token const & t = p.get_token();
60 if (t.cat() == catNewline)
64 //cerr << "comment: " << s << "\n";
69 string const trim(string const & a, char const * p)
76 string::size_type r = a.find_last_not_of(p);
77 string::size_type l = a.find_first_not_of(p);
79 // Is this the minimal test? (lgb)
80 if (r == string::npos && l == string::npos)
83 return a.substr(l, r - l + 1);
87 void split(string const & s, vector<string> & result, char delim)
89 //cerr << "split 1: '" << s << "'\n";
92 while (getline(is, t, delim))
94 //cerr << "split 2\n";
98 string join(vector<string> const & input, char const * delim)
101 for (size_t i = 0; i < input.size(); ++i) {
110 char const ** is_known(string const & str, char const ** what)
112 for ( ; *what; ++what)
120 // current stack of nested environments
121 vector<string> active_environments;
124 string active_environment()
126 return active_environments.empty() ? string() : active_environments.back();
130 string documentclass;
131 bool overwrite_files = false;
134 /// return the number of arguments consumed
135 typedef boost::function<int(string const &, string const &)> cmd_helper;
138 int parse_help(string const &, string const &)
140 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
141 "Command line switches (case sensitive):\n"
142 "\t-help summarize tex2lyx usage\n"
143 "\t-f Force creation of .lyx files even if they exist already\n"
144 "\t-userdir dir try to set user directory to dir\n"
145 "\t-sysdir dir try to set system directory to dir\n"
146 "\t-c textclass declare the textclass" << endl;
151 int parse_class(string const & arg, string const &)
154 cerr << "Missing textclass string after -c switch" << endl;
162 int parse_sysdir(string const & arg, string const &)
165 cerr << "Missing directory for -sysdir switch" << endl;
173 int parse_userdir(string const & arg, string const &)
176 cerr << "Missing directory for -userdir switch" << endl;
184 int parse_force(string const &, string const &)
186 overwrite_files = true;
191 void easyParse(int & argc, char * argv[])
193 std::map<string, cmd_helper> cmdmap;
195 cmdmap["-c"] = parse_class;
196 cmdmap["-f"] = parse_force;
197 cmdmap["-help"] = parse_help;
198 cmdmap["--help"] = parse_help;
199 cmdmap["-sysdir"] = parse_sysdir;
200 cmdmap["-userdir"] = parse_userdir;
202 for (int i = 1; i < argc; ++i) {
203 std::map<string, cmd_helper>::const_iterator it
204 = cmdmap.find(argv[i]);
206 // don't complain if not found - may be parsed later
207 if (it == cmdmap.end())
210 string arg((i + 1 < argc) ? argv[i + 1] : "");
211 string arg2((i + 2 < argc) ? argv[i + 2] : "");
213 int const remove = 1 + it->second(arg, arg2);
215 // Now, remove used arguments by shifting
216 // the following ones remove places down.
218 for (int j = i; j < argc; ++j)
219 argv[j] = argv[j + remove];
225 void tex2lyx(std::istream &is, std::ostream &os)
231 LyXTextClass textclass = parse_preamble(p, ss, documentclass);
233 active_environments.push_back("document");
234 Context context(true, textclass);
235 parse_text(p, ss, FLAG_END, true, context);
236 context.check_end_layout(ss);
237 ss << "\n\\end_document\n";
238 active_environments.pop_back();
244 bool tex2lyx(string const &infilename, string const &outfilename)
246 if (!(IsFileReadable(infilename) && IsFileWriteable(outfilename))) {
249 if (!overwrite_files && IsFileReadable(outfilename)) {
250 cerr << "Not overwriting existing file " << outfilename << "\n";
253 ifstream is(infilename.c_str());
254 ofstream os(outfilename.c_str());
256 cerr << "File: " << infilename << "\n";
263 int main(int argc, char * argv[])
265 easyParse(argc, argv);
268 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
269 "See tex2lyx -help." << endl;
273 lyx::support::os::init(&argc, &argv);
274 lyx::support::setLyxPaths();
276 if (!IsFileReadable(argv[1])) {
277 cerr << "Could not open input file \"" << argv[1]
278 << "\" for reading." << endl;
281 ifstream is(argv[1]);