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