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