]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
* text.C (check_layout): \layout -> \begin_layout
[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
9 #include "debug.h"
10 #include "lyx_main.h"
11 #include "lyxtextclass.h"
12 #include "support/path_defines.h"
13 #include "support/os.h"
14
15 #include <cctype>
16 #include <fstream>
17 #include <iostream>
18 #include <string>
19 #include <sstream>
20 #include <vector>
21
22 using std::cout;
23 using std::cerr;
24 using std::endl;
25 using std::getline;
26 using std::istream;
27 using std::ifstream;
28 using std::istringstream;
29 using std::ostream;
30 using std::ostringstream;
31 using std::stringstream;
32 using std::string;
33 using std::vector;
34
35 // Hacks to allow the thing to link in the lyxlayout stuff
36 Debug::type const Debug::ANY = Debug::type(0);
37 DebugStream lyxerr;
38
39 void LyX::emergencyCleanup() {}
40
41 void handle_comment(Parser & p)
42 {
43         string s;
44         while (p.good()) {
45                 Token const & t = p.get_token();
46                 if (t.cat() == catNewline)
47                         break;
48                 s += t.asString();
49         }
50         //cerr << "comment: " << s << "\n";
51         p.skip_spaces();
52 }
53
54
55 string const trim(string const & a, char const * p)
56 {
57         // lyx::Assert(p);
58
59         if (a.empty() || !*p)
60                 return a;
61
62         string::size_type r = a.find_last_not_of(p);
63         string::size_type l = a.find_first_not_of(p);
64
65         // Is this the minimal test? (lgb)
66         if (r == string::npos && l == string::npos)
67                 return string();
68
69         return a.substr(l, r - l + 1);
70 }
71
72
73 void split(string const & s, vector<string> & result, char delim)
74 {
75         //cerr << "split 1: '" << s << "'\n";
76         istringstream is(s);
77         string t;
78         while (getline(is, t, delim))
79                 result.push_back(t);
80         //cerr << "split 2\n";
81 }
82
83
84 string join(vector<string> const & input, char const * delim)
85 {
86         ostringstream os;
87         for (size_t i = 0; i < input.size(); ++i) {
88                 if (i)
89                         os << delim;
90                 os << input[i];
91         }
92         return os.str();
93 }
94
95
96 char const ** is_known(string const & str, char const ** what)
97 {
98         for ( ; *what; ++what)
99                 if (str == *what)
100                         return what;
101         return 0;
102 }
103
104
105
106 // current stack of nested environments
107 vector<string> active_environments;
108
109
110 string active_environment()
111 {
112         return active_environments.empty() ? string() : active_environments.back();
113 }
114
115
116 int main(int argc, char * argv[])
117 {
118         if (argc <= 1) {
119                 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
120                 return 2;
121         }
122
123         lyx::support::os::init(&argc, &argv);
124         lyx::support::setLyxPaths();
125
126         ifstream is(argv[1]);
127         Parser p(is);
128         //p.dump();
129
130         stringstream ss;
131         LyXTextClass textclass = parse_preamble(p, ss);
132         active_environments.push_back("document");
133         parse_text(p, ss, FLAG_END, true, textclass);
134         check_end_layout(ss);
135         ss << "\n\\end_document\n";
136
137         ss.seekg(0);
138         cout << ss.str();
139         return 0;
140 }
141
142 // }])