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