1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
19 using std::istringstream;
21 using std::ostringstream;
25 #include "mathed/math_gridinfo.h"
29 int string2int(string const & s, int deflt = 0)
38 string read_hlines(Parser & p)
43 if (p.next_token().cs() == "hline") {
46 } else if (p.next_token().cs() == "cline") {
48 os << "\\cline{" << p.verbatim_item() << "}";
53 //cerr << "read_hlines(), read: '" << os.str() << "'\n";
54 //cerr << "read_hlines(), next token: " << p.next_token() << "\n";
60 /* rather brutish way to code table structure in a string:
64 \multicolumn{2}{c}{4} & 5 //
71 \hline HLINE TAB 5 LINE
75 char const TAB = '\001';
76 char const LINE = '\002';
77 char const HLINE = '\004';
80 void handle_colalign(Parser & p, vector<ColInfo> & colinfo)
82 if (p.get_token().cat() != catBegin)
83 cerr << "wrong syntax for table column alignment. '{' expected\n";
86 bool leftline = false;
87 for (Token t=p.get_token(); p.good() && t.cat() != catEnd; t = p.get_token()){
89 cerr << "t: " << t << " c: '" << t.character() << "'\n";
92 switch (t.character()) {
97 ci.align = t.character();
98 if (colinfo.size() && colinfo.back().rightline > 1) {
100 --colinfo.back().rightline;
102 colinfo.push_back(ci);
106 colinfo.push_back(ColInfo());
107 colinfo.back().align = nextalign;
108 colinfo.back().width = p.verbatim_item();
115 ++colinfo.back().rightline;
118 string s = p.verbatim_item();
119 if (s == "\\raggedleft ")
121 else if (s == "\\raggedright ")
124 cerr << "unknown '>' column '" << s << "'\n";
128 cerr << "ignoring special separator '" << t << "'\n";
132 if (colinfo.size() && leftline)
133 colinfo[0].leftline = true;
137 } // anonymous namespace
140 void parse_table(Parser & p, ostream & os, unsigned flags)
145 Token const & t = p.get_token();
148 cerr << "t: " << t << " flags: " << flags << "\n";
154 if (t.cat() == catMath) {
155 // we are inside some text mode thingy, so opening new math is allowed
156 Token const & n = p.get_token();
157 if (n.cat() == catMath) {
158 // TeX's $$...$$ syntax for displayed math
160 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
162 p.get_token(); // skip the second '$' token
164 // simple $...$ stuff
167 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
172 else if (t.cat() == catLetter ||
173 t.cat() == catSpace ||
174 t.cat() == catSuper ||
176 t.cat() == catOther ||
177 t.cat() == catActive ||
178 t.cat() == catNewline ||
179 t.cat() == catParameter)
182 else if (t.cat() == catBegin) {
184 parse_table(p, os, FLAG_BRACE_LAST);
188 else if (t.cat() == catEnd) {
189 if (flags & FLAG_BRACE_LAST)
191 cerr << "unexpected '}'\n";
194 else if (t.cat() == catAlign) {
198 else if (t.cs() == "tabularnewline" || t.cs() == "\\") {
199 // stuff before the line break
200 // and look ahead for stuff after the line break
201 os << HLINE << hlines << HLINE << LINE << read_hlines(p) << HLINE;
205 else if (t.cs() == "hline")
208 else if (t.cs() == "cline")
209 hlines += "\\cline{" + p.verbatim_item() + '}';
211 else if (t.cat() == catComment)
214 else if (t.cs() == "(") {
216 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
220 else if (t.cs() == "[") {
222 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
226 else if (t.cs() == "begin") {
227 string const name = p.getArg('{', '}');
228 active_environments.push_back(name);
229 parse_table(p, os, FLAG_END);
232 else if (t.cs() == "end") {
233 if (flags & FLAG_END) {
234 // eat environment name
235 string const name = p.getArg('{', '}');
236 if (name != active_environment())
237 p.error("\\end{" + name + "} does not match \\begin{"
238 + active_environment() + "}");
239 active_environments.pop_back();
242 p.error("found 'end' unexpectedly");
251 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
254 for (size_t col = 0; col < ci.size(); ++col)
255 ci[col].topline = true;
259 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
261 ri.bottomline = true;
262 for (size_t col = 0; col < ci.size(); ++col)
263 ci[col].bottomline = true;
267 void handle_tabular(Parser & p, ostream & os)
269 string posopts = p.getOpt();
271 cerr << "vertical tabular positioning '" << posopts << "' ignored\n";
273 vector<ColInfo> colinfo;
275 // handle column formatting
276 handle_colalign(p, colinfo);
278 // handle initial hlines
280 // first scan of cells
281 // use table mode to keep it minimal-invasive
282 // not exactly what's TeX doing...
283 vector<string> lines;
285 ss << read_hlines(p) << HLINE; // handle initial hlines
286 parse_table(p, ss, FLAG_END);
287 split(ss.str(), lines, LINE);
289 vector< vector<CellInfo> > cellinfo(lines.size());
290 vector<RowInfo> rowinfo(lines.size());
293 //cerr << "// split into rows\n";
294 for (size_t row = 0; row < rowinfo.size(); ++row) {
297 cellinfo[row].resize(colinfo.size());
300 vector<string> dummy;
301 //cerr << "\n########### LINE: " << lines[row] << "########\n";
302 split(lines[row], dummy, HLINE);
304 // handle horizontal line fragments
305 if (dummy.size() != 3) {
306 if (dummy.size() != 1)
307 cerr << "unexpected dummy size: " << dummy.size()
308 << " content: " << lines[row] << "\n";
311 lines[row] = dummy[1];
313 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
314 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
315 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
317 for (int i = 0; i <= 2; i += 2) {
318 //cerr << " reading from line string '" << dummy[i] << "'\n";
321 Token t = p1.get_token();
322 //cerr << "read token: " << t << "\n";
323 if (t.cs() == "hline") {
325 if (rowinfo[row].topline) {
326 if (row > 0) // extra bottomline above
327 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
329 cerr << "dropping extra hline\n";
330 //cerr << "below row: " << row-1 << endl;
332 handle_hline_above(rowinfo[row], cellinfo[row]);
333 //cerr << "above row: " << row << endl;
336 //cerr << "below row: " << row << endl;
337 handle_hline_below(rowinfo[row], cellinfo[row]);
339 } else if (t.cs() == "cline") {
340 string arg = p1.verbatim_item();
341 //cerr << "read cline arg: '" << arg << "'\n";
345 size_t from = string2int(t[0]) - 1;
346 size_t to = string2int(t[1]);
347 for (size_t col = from; col < to; ++col) {
348 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
350 rowinfo[row].topline = true;
351 cellinfo[row][col].topline = true;
353 rowinfo[row].bottomline = true;
354 cellinfo[row][col].bottomline = true;
358 cerr << "unexpected line token: " << t << endl;
364 vector<string> cells;
365 split(lines[row], cells, TAB);
366 for (size_t col = 0, cell = 0;
367 cell < cells.size() && col < colinfo.size(); ++col, ++cell) {
368 //cerr << "cell content: '" << cells[cell] << "'\n";
369 Parser p(cells[cell]);
371 //cells[cell] << "'\n";
372 if (p.next_token().cs() == "multicolumn") {
375 size_t const ncells = string2int(p.verbatim_item());
377 // special cell properties alignment
379 handle_colalign(p, t);
380 cellinfo[row][col].multi = 1;
381 cellinfo[row][col].align = t.front().align;
382 cellinfo[row][col].content = parse_text(p, FLAG_ITEM, false);
383 cellinfo[row][col].leftline |= t.front().leftline;
384 cellinfo[row][col].rightline |= t.front().rightline;
386 // add dummy cells for multicol
387 for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
389 cellinfo[row][col].multi = 2;
390 cellinfo[row][col].align = 'c';
393 // more than one line on the right?
394 if (t.front().rightline > 1)
395 cellinfo[row][col + 1].leftline = true;
398 // FLAG_END is a hack, we need to read all of it
399 cellinfo[row][col].content = parse_text(p, FLAG_END, false);
403 //cerr << "// handle almost empty last row what we have\n";
404 // handle almost empty last row
405 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
406 //cerr << "remove empty last line\n";
407 if (rowinfo[row].topline)
408 rowinfo[row - 1].bottomline = true;
409 for (size_t col = 0; col < colinfo.size(); ++col)
410 if (cellinfo[row][col].topline)
411 cellinfo[row - 1][col].bottomline = true;
417 //cerr << "// output what we have\n";
418 // output what we have
419 os << "<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
420 << "\" columns=\"" << colinfo.size() << "\">\n"
423 //cerr << "// after header\n";
424 for (size_t col = 0; col < colinfo.size(); ++col) {
425 os << "<column alignment=\"" << colinfo[col].align << "\"";
426 if (colinfo[col].rightline)
427 os << " rightline=\"true\"";
428 if (colinfo[col].leftline)
429 os << " leftline=\"true\"";
430 os << " valignment=\"top\"";
431 os << " width=\"" << colinfo[col].width << "\"";
434 //cerr << "// after cols\n";
436 for (size_t row = 0; row < rowinfo.size(); ++row) {
438 if (rowinfo[row].topline)
439 os << " topline=\"true\"";
440 if (rowinfo[row].bottomline)
441 os << " bottomline=\"true\"";
443 for (size_t col = 0; col < colinfo.size(); ++col) {
444 CellInfo const & cell = cellinfo[row][col];
447 os << " multicolumn=\"" << cell.multi << "\"";
449 os << " leftline=\"true\"";
451 os << " rightline=\"true\"";
453 os << " topline=\"true\"";
455 os << " bottomline=\"true\"";
456 //cerr << "\nrow: " << row << " col: " << col;
458 // cerr << " topline=\"true\"";
459 //if (cell.bottomline)
460 // cerr << " bottomline=\"true\"";
461 os << " alignment=\"" << verbose_align(cell.align) << "\""
462 << " valignment=\"top\""
463 << " usebox=\"none\""
465 << "\n\\begin_inset Text"
466 << "\n\n\\layout Standard\n\n"
468 << "\n\\end_inset\n\n"
474 os << "</lyxtabular>\n";