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());
56 string const trim(string const & a, char const * p)
63 string::size_type r = a.find_last_not_of(p);
64 string::size_type l = a.find_first_not_of(p);
66 // Is this the minimal test? (lgb)
67 if (r == string::npos && l == string::npos)
70 return a.substr(l, r - l + 1);
74 void split(string const & s, vector<string> & result, char delim)
76 //cerr << "split 1: '" << s << "'\n";
79 while (getline(is, t, delim))
81 //cerr << "split 2\n";
85 string join(vector<string> const & input, char const * delim)
88 for (size_t i = 0; i < input.size(); ++i) {
97 char const ** is_known(string const & str, char const ** what)
99 for ( ; *what; ++what)
107 // current stack of nested environments
108 vector<string> active_environments;
111 string active_environment()
113 return active_environments.empty() ? string() : active_environments.back();
117 string documentclass;
118 bool overwrite_files = false;
121 /// return the number of arguments consumed
122 typedef boost::function<int(string const &, string const &)> cmd_helper;
125 int parse_help(string const &, string const &)
127 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
128 "Command line switches (case sensitive):\n"
129 "\t-help summarize tex2lyx usage\n"
130 "\t-f Force creation of .lyx files even if they exist already\n"
131 "\t-userdir dir try to set user directory to dir\n"
132 "\t-sysdir dir try to set system directory to dir\n"
133 "\t-c textclass declare the textclass" << endl;
138 int parse_class(string const & arg, string const &)
141 cerr << "Missing textclass string after -c switch" << endl;
149 int parse_sysdir(string const & arg, string const &)
152 cerr << "Missing directory for -sysdir switch" << endl;
160 int parse_userdir(string const & arg, string const &)
163 cerr << "Missing directory for -userdir switch" << endl;
171 int parse_force(string const &, string const &)
173 overwrite_files = true;
178 void easyParse(int & argc, char * argv[])
180 std::map<string, cmd_helper> cmdmap;
182 cmdmap["-c"] = parse_class;
183 cmdmap["-f"] = parse_force;
184 cmdmap["-help"] = parse_help;
185 cmdmap["--help"] = parse_help;
186 cmdmap["-sysdir"] = parse_sysdir;
187 cmdmap["-userdir"] = parse_userdir;
189 for (int i = 1; i < argc; ++i) {
190 std::map<string, cmd_helper>::const_iterator it
191 = cmdmap.find(argv[i]);
193 // don't complain if not found - may be parsed later
194 if (it == cmdmap.end())
197 string arg((i + 1 < argc) ? argv[i + 1] : "");
198 string arg2((i + 2 < argc) ? argv[i + 2] : "");
200 int const remove = 1 + it->second(arg, arg2);
202 // Now, remove used arguments by shifting
203 // the following ones remove places down.
205 for (int j = i; j < argc; ++j)
206 argv[j] = argv[j + remove];
212 void tex2lyx(std::istream &is, std::ostream &os)
218 LyXTextClass textclass = parse_preamble(p, ss, documentclass);
220 active_environments.push_back("document");
221 Context context(true, textclass);
222 parse_text(p, ss, FLAG_END, true, context);
223 context.check_end_layout(ss);
224 ss << "\n\\end_document\n";
225 active_environments.pop_back();
230 ofstream parsertest("parsertest.tex");
232 parsertest << p.get_token().asInput();
233 // <origfile> and parsertest.tex should now have identical content
238 bool tex2lyx(string const &infilename, string const &outfilename)
240 if (!(IsFileReadable(infilename) && IsFileWriteable(outfilename))) {
243 if (!overwrite_files && IsFileReadable(outfilename)) {
244 cerr << "Not overwriting existing file " << outfilename << "\n";
247 ifstream is(infilename.c_str());
248 ofstream os(outfilename.c_str());
250 cerr << "File: " << infilename << "\n";
257 int main(int argc, char * argv[])
259 easyParse(argc, argv);
262 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
263 "See tex2lyx -help." << endl;
267 lyx::support::os::init(&argc, &argv);
268 lyx::support::setLyxPaths();
270 if (!IsFileReadable(argv[1])) {
271 cerr << "Could not open input file \"" << argv[1]
272 << "\" for reading." << endl;
275 ifstream is(argv[1]);