]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
Add a Buffer::fully_loaded member function, returning true only when
[lyx.git] / src / tex2lyx / tex2lyx.C
1 /**
2  * \file tex2lyx.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 // {[(
12
13 #include <config.h>
14
15 #include "tex2lyx.h"
16 #include "context.h"
17
18 #include "debug.h"
19 #include "lyxtextclass.h"
20 #include "support/path_defines.h"
21 #include "support/os.h"
22
23 #include <cctype>
24 #include <fstream>
25 #include <iostream>
26 #include <string>
27 #include <sstream>
28 #include <vector>
29
30 using std::endl;
31 using std::cout;
32 using std::cerr;
33 using std::getline;
34
35 using std::ifstream;
36 using std::istringstream;
37 using std::ostringstream;
38 using std::stringstream;
39 using std::string;
40 using std::vector;
41
42 // Hacks to allow the thing to link in the lyxlayout stuff
43 LyXErr lyxerr(std::cerr.rdbuf());
44
45 void handle_comment(Parser & p)
46 {
47         string s;
48         while (p.good()) {
49                 Token const & t = p.get_token();
50                 if (t.cat() == catNewline)
51                         break;
52                 s += t.asString();
53         }
54         //cerr << "comment: " << s << "\n";
55         p.skip_spaces();
56 }
57
58
59 string const trim(string const & a, char const * p)
60 {
61         // BOOST_ASSERT(p);
62
63         if (a.empty() || !*p)
64                 return a;
65
66         string::size_type r = a.find_last_not_of(p);
67         string::size_type l = a.find_first_not_of(p);
68
69         // Is this the minimal test? (lgb)
70         if (r == string::npos && l == string::npos)
71                 return string();
72
73         return a.substr(l, r - l + 1);
74 }
75
76
77 void split(string const & s, vector<string> & result, char delim)
78 {
79         //cerr << "split 1: '" << s << "'\n";
80         istringstream is(s);
81         string t;
82         while (getline(is, t, delim))
83                 result.push_back(t);
84         //cerr << "split 2\n";
85 }
86
87
88 string join(vector<string> const & input, char const * delim)
89 {
90         ostringstream os;
91         for (size_t i = 0; i < input.size(); ++i) {
92                 if (i)
93                         os << delim;
94                 os << input[i];
95         }
96         return os.str();
97 }
98
99
100 char const ** is_known(string const & str, char const ** what)
101 {
102         for ( ; *what; ++what)
103                 if (str == *what)
104                         return what;
105         return 0;
106 }
107
108
109
110 // current stack of nested environments
111 vector<string> active_environments;
112
113
114 string active_environment()
115 {
116         return active_environments.empty() ? string() : active_environments.back();
117 }
118
119
120 int main(int argc, char * argv[])
121 {
122         if (argc <= 1) {
123                 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
124                 return 2;
125         }
126
127         lyx::support::os::init(&argc, &argv);
128         lyx::support::setLyxPaths();
129
130         ifstream is(argv[1]);
131         Parser p(is);
132         //p.dump();
133
134         stringstream ss;
135         LyXTextClass textclass = parse_preamble(p, ss);
136         active_environments.push_back("document");
137         Context context(true, textclass);
138         parse_text(p, ss, FLAG_END, true, context);
139         context.check_end_layout(ss);
140         ss << "\n\\end_document\n";
141
142         ss.seekg(0);
143         cout << ss.str();
144         return 0;
145 }
146
147 // }])