]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
* funcrequest.C: initialize button_ member
[lyx.git] / src / tex2lyx / tex2lyx.C
1 /** The .tex to .lyx converter
2     \author André Pönitz (2003)
3  */
4
5 // {[(
6
7 #include "tex2lyx.h"
8 #include "context.h"
9
10 #include "debug.h"
11 #include "lyx_main.h"
12 #include "lyxtextclass.h"
13 #include "support/path_defines.h"
14 #include "support/os.h"
15
16 #include <cctype>
17 #include <fstream>
18 #include <iostream>
19 #include <string>
20 #include <sstream>
21 #include <vector>
22
23 using std::cout;
24 using std::cerr;
25 using std::endl;
26 using std::getline;
27 using std::istream;
28 using std::ifstream;
29 using std::istringstream;
30 using std::ostream;
31 using std::ostringstream;
32 using std::stringstream;
33 using std::string;
34 using std::vector;
35
36 // Hacks to allow the thing to link in the lyxlayout stuff
37 Debug::type const Debug::ANY = Debug::type(0);
38 DebugStream lyxerr;
39
40 void LyX::emergencyCleanup() {}
41
42 void handle_comment(Parser & p)
43 {
44         string s;
45         while (p.good()) {
46                 Token const & t = p.get_token();
47                 if (t.cat() == catNewline)
48                         break;
49                 s += t.asString();
50         }
51         //cerr << "comment: " << s << "\n";
52         p.skip_spaces();
53 }
54
55
56 string const trim(string const & a, char const * p)
57 {
58         // lyx::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 int main(int argc, char * argv[])
118 {
119         if (argc <= 1) {
120                 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
121                 return 2;
122         }
123
124         lyx::support::os::init(&argc, &argv);
125         lyx::support::setLyxPaths();
126
127         ifstream is(argv[1]);
128         Parser p(is);
129         //p.dump();
130
131         stringstream ss;
132         LyXTextClass textclass = parse_preamble(p, ss);
133         active_environments.push_back("document");
134         Context context(true, textclass);
135         parse_text(p, ss, FLAG_END, true, context);
136         context.check_end_layout(ss);
137         ss << "\n\\end_document\n";
138
139         ss.seekg(0);
140         cout << ss.str();
141         return 0;
142 }
143
144 // }])