]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
Georg's latest improvements
[lyx.git] / src / tex2lyx / tex2lyx.C
1 /**
2  * \file tex2lyx.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 // {[(
12
13 #include <config.h>
14
15 #include "tex2lyx.h"
16 #include "context.h"
17
18 #include "debug.h"
19 #include "lyxtextclass.h"
20 #include "support/path_defines.h"
21 #include "support/filetools.h"
22 #include "support/os.h"
23
24 #include <boost/function.hpp>
25
26 #include <cctype>
27 #include <fstream>
28 #include <iostream>
29 #include <string>
30 #include <sstream>
31 #include <vector>
32 #include <map>
33
34 using std::endl;
35 using std::cout;
36 using std::cerr;
37 using std::getline;
38
39 using std::ifstream;
40 using std::ofstream;
41 using std::istringstream;
42 using std::ostringstream;
43 using std::stringstream;
44 using std::string;
45 using std::vector;
46
47 using lyx::support::system_lyxdir;
48 using lyx::support::user_lyxdir;
49 using lyx::support::IsFileReadable;
50 using lyx::support::IsFileWriteable;
51
52 // Hacks to allow the thing to link in the lyxlayout stuff
53 LyXErr lyxerr(std::cerr.rdbuf());
54
55
56 string const trim(string const & a, char const * p)
57 {
58         // BOOST_ASSERT(p);
59
60         if (a.empty() || !*p)
61                 return a;
62
63         string::size_type r = a.find_last_not_of(p);
64         string::size_type l = a.find_first_not_of(p);
65
66         // Is this the minimal test? (lgb)
67         if (r == string::npos && l == string::npos)
68                 return string();
69
70         return a.substr(l, r - l + 1);
71 }
72
73
74 void split(string const & s, vector<string> & result, char delim)
75 {
76         //cerr << "split 1: '" << s << "'\n";
77         istringstream is(s);
78         string t;
79         while (getline(is, t, delim))
80                 result.push_back(t);
81         //cerr << "split 2\n";
82 }
83
84
85 string join(vector<string> const & input, char const * delim)
86 {
87         ostringstream os;
88         for (size_t i = 0; i < input.size(); ++i) {
89                 if (i)
90                         os << delim;
91                 os << input[i];
92         }
93         return os.str();
94 }
95
96
97 char const ** is_known(string const & str, char const ** what)
98 {
99         for ( ; *what; ++what)
100                 if (str == *what)
101                         return what;
102         return 0;
103 }
104
105
106
107 // current stack of nested environments
108 vector<string> active_environments;
109
110
111 string active_environment()
112 {
113         return active_environments.empty() ? string() : active_environments.back();
114 }
115
116
117 string documentclass;
118 bool overwrite_files = false;
119
120
121 /// return the number of arguments consumed
122 typedef boost::function<int(string const &, string const &)> cmd_helper;
123
124
125 int parse_help(string const &, string const &)
126 {
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;
134         exit(0);
135 }
136
137
138 int parse_class(string const & arg, string const &)
139 {
140         if (arg.empty()) {
141                 cerr << "Missing textclass string after -c switch" << endl;
142                 exit(1);
143         }
144         documentclass = arg;
145         return 1;
146 }
147
148
149 int parse_sysdir(string const & arg, string const &)
150 {
151         if (arg.empty()) {
152                 cerr << "Missing directory for -sysdir switch" << endl;
153                 exit(1);
154         }
155         system_lyxdir(arg);
156         return 1;
157 }
158
159
160 int parse_userdir(string const & arg, string const &)
161 {
162         if (arg.empty()) {
163                 cerr << "Missing directory for -userdir switch" << endl;
164                 exit(1);
165         }
166         user_lyxdir(arg);
167         return 1;
168 }
169
170
171 int parse_force(string const &, string const &)
172 {
173         overwrite_files = true;
174         return 0;
175 }
176
177
178 void easyParse(int & argc, char * argv[])
179 {
180         std::map<string, cmd_helper> cmdmap;
181
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;
188
189         for (int i = 1; i < argc; ++i) {
190                 std::map<string, cmd_helper>::const_iterator it
191                         = cmdmap.find(argv[i]);
192
193                 // don't complain if not found - may be parsed later
194                 if (it == cmdmap.end())
195                         continue;
196
197                 string arg((i + 1 < argc) ? argv[i + 1] : "");
198                 string arg2((i + 2 < argc) ? argv[i + 2] : "");
199
200                 int const remove = 1 + it->second(arg, arg2);
201
202                 // Now, remove used arguments by shifting
203                 // the following ones remove places down.
204                 argc -= remove;
205                 for (int j = i; j < argc; ++j)
206                         argv[j] = argv[j + remove];
207                 --i;
208         }
209 }
210
211
212 void tex2lyx(std::istream &is, std::ostream &os)
213 {
214         Parser p(is);
215         //p.dump();
216
217         stringstream ss;
218         LyXTextClass textclass = parse_preamble(p, ss, documentclass);
219
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();
226         ss.seekg(0);
227         os << ss.str();
228 #ifdef TEST_PARSER
229         p.reset();
230         ofstream parsertest("parsertest.tex");
231         while (p.good())
232                 parsertest << p.get_token().asInput();
233         // <origfile> and parsertest.tex should now have identical content
234 #endif
235 }
236
237
238 bool tex2lyx(string const &infilename, string const &outfilename)
239 {
240         if (!(IsFileReadable(infilename) && IsFileWriteable(outfilename))) {
241                 return false;
242         }
243         if (!overwrite_files && IsFileReadable(outfilename)) {
244                 cerr << "Not overwriting existing file " << outfilename << "\n";
245                 return false;
246         }
247         ifstream is(infilename.c_str());
248         ofstream os(outfilename.c_str());
249 #ifdef FILEDEBUG
250         cerr << "File: " << infilename << "\n";
251 #endif
252         tex2lyx(is, os);
253         return true;
254 }
255
256
257 int main(int argc, char * argv[])
258 {
259         easyParse(argc, argv);
260
261         if (argc <= 1) {
262                 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
263                           "See tex2lyx -help." << endl;
264                 return 2;
265         }
266
267         lyx::support::os::init(&argc, &argv);
268         lyx::support::setLyxPaths();
269
270         if (!IsFileReadable(argv[1])) {
271                 cerr << "Could not open input file \"" << argv[1]
272                      << "\" for reading." << endl;
273                 return 2;
274         }
275         ifstream is(argv[1]);
276         tex2lyx(is, cout);
277
278         return 0;
279 }
280
281 // }])