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