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