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