]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
All day for not very much...
[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
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 void clean_layouts(istream & is, ostream & os)
118 {
119         string last;
120         string line;
121         bool eating = false;
122         while (getline(is, line)) {
123                 string tline = trim(line, " ");
124                 if (line.substr(0, 8) == "\\layout ") {
125                         //cerr << "layout: " << line << "\n";
126                         last = line;
127                         eating = true;
128                 } else if (eating && tline.empty()) {
129                         //cerr << "eat empty line\n"; 
130                 } else if (line.substr(0, 13) == "\\begin_deeper") {
131                         os << line << "\n";
132                 } else {
133                         // ordinary line  
134                         //cerr << "ordinary line\n"; 
135                         if (eating) {
136                                 eating = false;
137                                 os << last << "\n\n";
138                         }
139                         os << line << "\n";
140                 }
141         }
142 }
143
144
145 int main(int argc, char * argv[])
146 {
147         if (argc <= 1) {
148                 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
149                 return 2;
150         }
151
152         lyx::support::os::init(&argc, &argv);
153         lyx::support::setLyxPaths();
154
155         ifstream is(argv[1]);
156         Parser p(is);
157         //p.dump();
158
159         stringstream ss;
160         LyXTextClass textclass = parse_preamble(p, ss);
161         active_environments.push_back("document");
162         parse_text(p, ss, FLAG_END, true, textclass);
163         ss << "\n\\the_end\n";
164
165         ss.seekg(0);
166         clean_layouts(ss, cout);
167
168         return 0;
169 }
170
171 // }])