1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
20 using std::istringstream;
22 using std::ostringstream;
31 ColInfo() : rightline(0), leftline(false) {}
32 string align; // column alignment
33 string width; // column width
34 int rightline; // a line on the right?
41 RowInfo() : topline(false), bottomline(false) {}
42 bool topline; // horizontal line above
43 int bottomline; // horizontal line below
50 : multi(0), leftline(false), rightline(false),
51 topline(false), bottomline(false)
54 string content; // cell content
55 int multi; // multicolumn flag
56 string align; // cell alignment
57 bool leftline; // do we have a line on the left?
58 bool rightline; // do we have a line on the right?
59 bool topline; // do we have a line above?
60 bool bottomline; // do we have a line below?
64 int string2int(string const & s, int deflt = 0)
73 string read_hlines(Parser & p)
78 if (p.next_token().cs() == "hline") {
81 } else if (p.next_token().cs() == "cline") {
83 os << "\\cline{" << p.verbatim_item() << "}";
88 //cerr << "read_hlines(), read: '" << os.str() << "'\n";
89 //cerr << "read_hlines(), next token: " << p.next_token() << "\n";
95 /* rather brutish way to code table structure in a string:
99 \multicolumn{2}{c}{4} & 5 //
103 gets "translated" to:
106 \hline HLINE TAB 5 LINE
110 char const TAB = '\001';
111 char const LINE = '\002';
112 char const HLINE = '\004';
114 string get_align(char c)
117 case 'c': return "center";
118 case 'l': return "left";
119 case 'r': return "right";
120 case 'b': return "block";
126 void handle_colalign(Parser & p, vector<ColInfo> & colinfo)
128 if (p.get_token().cat() != catBegin)
129 cerr << "wrong syntax for table column alignment. '{' expected\n";
131 string nextalign = "block";
132 bool leftline = false;
133 for (Token t=p.get_token(); p.good() && t.cat() != catEnd; t = p.get_token()){
135 cerr << "t: " << t << " c: '" << t.character() << "'\n";
138 switch (t.character()) {
143 ci.align = get_align(t.character());
144 if (colinfo.size() && colinfo.back().rightline > 1) {
146 --colinfo.back().rightline;
148 colinfo.push_back(ci);
152 colinfo.push_back(ColInfo());
153 colinfo.back().align = nextalign;
154 colinfo.back().width = p.verbatim_item();
161 ++colinfo.back().rightline;
164 string s = p.verbatim_item();
165 if (s == "\\raggedleft ")
167 else if (s == "\\raggedright ")
170 cerr << "unknown '>' column '" << s << "'\n";
174 cerr << "ignoring special separator '" << t << "'\n";
178 if (colinfo.size() && leftline)
179 colinfo[0].leftline = true;
183 } // anonymous namespace
186 void parse_table(Parser & p, ostream & os, unsigned flags)
191 Token const & t = p.get_token();
194 cerr << "t: " << t << " flags: " << flags << "\n";
200 if (t.cat() == catMath) {
201 // we are inside some text mode thingy, so opening new math is allowed
202 Token const & n = p.get_token();
203 if (n.cat() == catMath) {
204 // TeX's $$...$$ syntax for displayed math
206 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
208 p.get_token(); // skip the second '$' token
210 // simple $...$ stuff
213 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
218 else if (t.cat() == catLetter ||
219 t.cat() == catSpace ||
220 t.cat() == catSuper ||
222 t.cat() == catOther ||
223 t.cat() == catActive ||
224 t.cat() == catNewline ||
225 t.cat() == catParameter)
228 else if (t.cat() == catBegin) {
230 parse_table(p, os, FLAG_BRACE_LAST);
234 else if (t.cat() == catEnd) {
235 if (flags & FLAG_BRACE_LAST)
237 cerr << "unexpected '}'\n";
240 else if (t.cat() == catAlign) {
244 else if (t.cs() == "tabularnewline" || t.cs() == "\\") {
245 //else if (t.cs() == "tabularnewline") {
246 // stuff before the line break
247 // and look ahead for stuff after the line break
248 os << HLINE << hlines << HLINE << LINE << read_hlines(p) << HLINE;
252 else if (t.cs() == "hline")
255 else if (t.cs() == "cline")
256 hlines += "\\cline{" + p.verbatim_item() + '}';
258 else if (t.cat() == catComment)
261 else if (t.cs() == "(") {
263 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
267 else if (t.cs() == "[") {
269 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
273 else if (t.cs() == "begin") {
274 string const name = p.getArg('{', '}');
275 active_environments.push_back(name);
276 parse_table(p, os, FLAG_END);
279 else if (t.cs() == "end") {
280 if (flags & FLAG_END) {
281 // eat environment name
282 string const name = p.getArg('{', '}');
283 if (name != active_environment())
284 p.error("\\end{" + name + "} does not match \\begin{"
285 + active_environment() + "}");
286 active_environments.pop_back();
289 p.error("found 'end' unexpectedly");
298 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
301 for (size_t col = 0; col < ci.size(); ++col)
302 ci[col].topline = true;
306 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
308 ri.bottomline = true;
309 for (size_t col = 0; col < ci.size(); ++col)
310 ci[col].bottomline = true;
314 void handle_tabular(Parser & p, ostream & os)
316 string posopts = p.getOpt();
318 cerr << "vertical tabular positioning '" << posopts << "' ignored\n";
320 vector<ColInfo> colinfo;
322 // handle column formatting
323 handle_colalign(p, colinfo);
325 // handle initial hlines
327 // first scan of cells
328 // use table mode to keep it minimal-invasive
329 // not exactly what's TeX doing...
330 vector<string> lines;
332 ss << read_hlines(p) << HLINE; // handle initial hlines
333 parse_table(p, ss, FLAG_END);
334 split(ss.str(), lines, LINE);
336 vector< vector<CellInfo> > cellinfo(lines.size());
337 vector<RowInfo> rowinfo(lines.size());
340 //cerr << "// split into rows\n";
341 for (size_t row = 0; row < rowinfo.size(); ++row) {
344 cellinfo[row].resize(colinfo.size());
347 vector<string> dummy;
348 //cerr << "\n########### LINE: " << lines[row] << "########\n";
349 split(lines[row], dummy, HLINE);
351 // handle horizontal line fragments
352 if (dummy.size() != 3) {
353 if (dummy.size() != 1)
354 cerr << "unexpected dummy size: " << dummy.size()
355 << " content: " << lines[row] << "\n";
358 lines[row] = dummy[1];
360 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
361 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
362 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
364 for (int i = 0; i <= 2; i += 2) {
365 //cerr << " reading from line string '" << dummy[i] << "'\n";
368 Token t = p1.get_token();
369 //cerr << "read token: " << t << "\n";
370 if (t.cs() == "hline") {
372 if (rowinfo[row].topline) {
373 if (row > 0) // extra bottomline above
374 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
376 cerr << "dropping extra hline\n";
377 //cerr << "below row: " << row-1 << endl;
379 handle_hline_above(rowinfo[row], cellinfo[row]);
380 //cerr << "above row: " << row << endl;
383 //cerr << "below row: " << row << endl;
384 handle_hline_below(rowinfo[row], cellinfo[row]);
386 } else if (t.cs() == "cline") {
387 string arg = p1.verbatim_item();
388 //cerr << "read cline arg: '" << arg << "'\n";
392 size_t from = string2int(t[0]) - 1;
393 size_t to = string2int(t[1]);
394 for (size_t col = from; col < to; ++col) {
395 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
397 rowinfo[row].topline = true;
398 cellinfo[row][col].topline = true;
400 rowinfo[row].bottomline = true;
401 cellinfo[row][col].bottomline = true;
405 cerr << "unexpected line token: " << t << endl;
411 vector<string> cells;
412 split(lines[row], cells, TAB);
413 for (size_t col = 0, cell = 0;
414 cell < cells.size() && col < colinfo.size(); ++col, ++cell) {
415 //cerr << "cell content: '" << cells[cell] << "'\n";
416 Parser p(cells[cell]);
418 //cells[cell] << "'\n";
419 if (p.next_token().cs() == "multicolumn") {
422 size_t const ncells = string2int(p.verbatim_item());
424 // special cell properties alignment
426 handle_colalign(p, t);
427 cellinfo[row][col].multi = 1;
428 cellinfo[row][col].align = t.front().align;
429 cellinfo[row][col].content = parse_text(p, FLAG_ITEM, false);
430 cellinfo[row][col].leftline |= t.front().leftline;
431 cellinfo[row][col].rightline |= t.front().rightline;
433 // add dummy cells for multicol
434 for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
436 cellinfo[row][col].multi = 2;
437 cellinfo[row][col].align = "center";
440 // more than one line on the right?
441 if (t.front().rightline > 1)
442 cellinfo[row][col + 1].leftline = true;
445 // FLAG_END is a hack, we need to read all of it
446 cellinfo[row][col].content = parse_text(p, FLAG_END, false);
450 //cerr << "// handle almost empty last row what we have\n";
451 // handle almost empty last row
452 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
453 //cerr << "remove empty last line\n";
454 if (rowinfo[row].topline)
455 rowinfo[row - 1].bottomline = true;
456 for (size_t col = 0; col < colinfo.size(); ++col)
457 if (cellinfo[row][col].topline)
458 cellinfo[row - 1][col].bottomline = true;
464 //cerr << "// output what we have\n";
465 // output what we have
466 os << "<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
467 << "\" columns=\"" << colinfo.size() << "\">\n"
470 //cerr << "// after header\n";
471 for (size_t col = 0; col < colinfo.size(); ++col) {
472 os << "<column alignment=\"" << colinfo[col].align << "\"";
473 if (colinfo[col].rightline)
474 os << " rightline=\"true\"";
475 if (colinfo[col].leftline)
476 os << " leftline=\"true\"";
477 os << " valignment=\"top\"";
478 os << " width=\"" << colinfo[col].width << "\"";
481 //cerr << "// after cols\n";
483 for (size_t row = 0; row < rowinfo.size(); ++row) {
485 if (rowinfo[row].topline)
486 os << " topline=\"true\"";
487 if (rowinfo[row].bottomline)
488 os << " bottomline=\"true\"";
490 for (size_t col = 0; col < colinfo.size(); ++col) {
491 CellInfo const & cell = cellinfo[row][col];
494 os << " multicolumn=\"" << cell.multi << "\"";
496 os << " leftline=\"true\"";
498 os << " rightline=\"true\"";
500 os << " topline=\"true\"";
502 os << " bottomline=\"true\"";
503 //cerr << "\nrow: " << row << " col: " << col;
505 // cerr << " topline=\"true\"";
506 //if (cell.bottomline)
507 // cerr << " bottomline=\"true\"";
508 os << " alignment=\"" << cell.align << "\""
509 << " valignment=\"top\""
510 << " usebox=\"none\""
512 << "\n\\begin_inset Text"
513 << "\n\n\\layout Standard\n\n"
515 << "\n\\end_inset\n\n"
521 os << "</lyxtabular>\n";