]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
Georg Baum's monster patch
[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 void handle_comment(Parser & p)
56 {
57         string s;
58         while (p.good()) {
59                 Token const & t = p.get_token();
60                 if (t.cat() == catNewline)
61                         break;
62                 s += t.asString();
63         }
64         //cerr << "comment: " << s << "\n";
65         p.skip_spaces();
66 }
67
68
69 string const trim(string const & a, char const * p)
70 {
71         // BOOST_ASSERT(p);
72
73         if (a.empty() || !*p)
74                 return a;
75
76         string::size_type r = a.find_last_not_of(p);
77         string::size_type l = a.find_first_not_of(p);
78
79         // Is this the minimal test? (lgb)
80         if (r == string::npos && l == string::npos)
81                 return string();
82
83         return a.substr(l, r - l + 1);
84 }
85
86
87 void split(string const & s, vector<string> & result, char delim)
88 {
89         //cerr << "split 1: '" << s << "'\n";
90         istringstream is(s);
91         string t;
92         while (getline(is, t, delim))
93                 result.push_back(t);
94         //cerr << "split 2\n";
95 }
96
97
98 string join(vector<string> const & input, char const * delim)
99 {
100         ostringstream os;
101         for (size_t i = 0; i < input.size(); ++i) {
102                 if (i)
103                         os << delim;
104                 os << input[i];
105         }
106         return os.str();
107 }
108
109
110 char const ** is_known(string const & str, char const ** what)
111 {
112         for ( ; *what; ++what)
113                 if (str == *what)
114                         return what;
115         return 0;
116 }
117
118
119
120 // current stack of nested environments
121 vector<string> active_environments;
122
123
124 string active_environment()
125 {
126         return active_environments.empty() ? string() : active_environments.back();
127 }
128
129
130 string documentclass;
131 bool overwrite_files = false;
132
133
134 /// return the number of arguments consumed
135 typedef boost::function<int(string const &, string const &)> cmd_helper;
136
137
138 int parse_help(string const &, string const &)
139 {
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;
147         exit(0);
148 }
149
150
151 int parse_class(string const & arg, string const &)
152 {
153         if (arg.empty()) {
154                 cerr << "Missing textclass string after -c switch" << endl;
155                 exit(1);
156         }
157         documentclass = arg;
158         return 1;
159 }
160
161
162 int parse_sysdir(string const & arg, string const &)
163 {
164         if (arg.empty()) {
165                 cerr << "Missing directory for -sysdir switch" << endl;
166                 exit(1);
167         }
168         system_lyxdir(arg);
169         return 1;
170 }
171
172
173 int parse_userdir(string const & arg, string const &)
174 {
175         if (arg.empty()) {
176                 cerr << "Missing directory for -userdir switch" << endl;
177                 exit(1);
178         }
179         user_lyxdir(arg);
180         return 1;
181 }
182
183
184 int parse_force(string const &, string const &)
185 {
186         overwrite_files = true;
187         return 0;
188 }
189
190
191 void easyParse(int & argc, char * argv[])
192 {
193         std::map<string, cmd_helper> cmdmap;
194
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;
201
202         for (int i = 1; i < argc; ++i) {
203                 std::map<string, cmd_helper>::const_iterator it
204                         = cmdmap.find(argv[i]);
205
206                 // don't complain if not found - may be parsed later
207                 if (it == cmdmap.end())
208                         continue;
209
210                 string arg((i + 1 < argc) ? argv[i + 1] : "");
211                 string arg2((i + 2 < argc) ? argv[i + 2] : "");
212
213                 int const remove = 1 + it->second(arg, arg2);
214
215                 // Now, remove used arguments by shifting
216                 // the following ones remove places down.
217                 argc -= remove;
218                 for (int j = i; j < argc; ++j)
219                         argv[j] = argv[j + remove];
220                 --i;
221         }
222 }
223
224
225 void tex2lyx(std::istream &is, std::ostream &os)
226 {
227         Parser p(is);
228         //p.dump();
229
230         stringstream ss;
231         LyXTextClass textclass = parse_preamble(p, ss, documentclass);
232
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();
239         ss.seekg(0);
240         os << ss.str();
241 }
242
243
244 bool tex2lyx(string const &infilename, string const &outfilename)
245 {
246         if (!(IsFileReadable(infilename) && IsFileWriteable(outfilename))) {
247                 return false;
248         }
249         if (!overwrite_files && IsFileReadable(outfilename)) {
250                 cerr << "Not overwriting existing file " << outfilename << "\n";
251                 return false;
252         }
253         ifstream is(infilename.c_str());
254         ofstream os(outfilename.c_str());
255 #ifdef FILEDEBUG
256         cerr << "File: " << infilename << "\n";
257 #endif
258         tex2lyx(is, os);
259         return true;
260 }
261
262
263 int main(int argc, char * argv[])
264 {
265         easyParse(argc, argv);
266
267         if (argc <= 1) {
268                 cerr << "Usage: tex2lyx [ command line switches ] <infile.tex>\n"
269                           "See tex2lyx -help." << endl;
270                 return 2;
271         }
272
273         lyx::support::os::init(&argc, &argv);
274         lyx::support::setLyxPaths();
275
276         if (!IsFileReadable(argv[1])) {
277                 cerr << "Could not open input file \"" << argv[1]
278                      << "\" for reading." << endl;
279                 return 2;
280         }
281         ifstream is(argv[1]);
282         tex2lyx(is, cout);
283
284         return 0;
285 }
286
287 // }])