1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
20 using std::istringstream;
22 using std::ostringstream;
27 #include "mathed/math_gridinfo.h"
29 // filled in preamble.C
30 std::map<char, int> special_columns;
35 int string2int(string const & s, int deflt = 0)
44 string read_hlines(Parser & p)
49 if (p.next_token().cs() == "hline") {
52 } else if (p.next_token().cs() == "cline") {
54 os << "\\cline{" << p.verbatim_item() << "}";
59 //cerr << "read_hlines(), read: '" << os.str() << "'\n";
60 //cerr << "read_hlines(), next token: " << p.next_token() << "\n";
65 /* rather brutish way to code table structure in a string:
69 \multicolumn{2}{c}{4} & 5 //
76 \hline HLINE TAB 5 LINE
80 char const TAB = '\001';
81 char const LINE = '\002';
82 char const HLINE = '\004';
85 void handle_colalign(Parser & p, vector<ColInfo> & colinfo)
87 if (p.get_token().cat() != catBegin)
88 cerr << "wrong syntax for table column alignment. '{' expected\n";
91 bool leftline = false;
92 for (Token t=p.get_token(); p.good() && t.cat() != catEnd; t = p.get_token()){
94 cerr << "t: " << t << " c: '" << t.character() << "'\n";
97 switch (t.character()) {
102 ci.align = t.character();
103 if (colinfo.size() && colinfo.back().rightline > 1) {
105 --colinfo.back().rightline;
107 colinfo.push_back(ci);
111 colinfo.push_back(ColInfo());
112 colinfo.back().align = nextalign;
113 colinfo.back().width = p.verbatim_item();
120 ++colinfo.back().rightline;
123 string s = p.verbatim_item();
124 if (s == "\\raggedleft ")
126 else if (s == "\\raggedright ")
129 cerr << "unknown '>' column '" << s << "'\n";
133 if (special_columns.find(t.character()) != special_columns.end()) {
136 ci.special += t.character();
137 int const nargs = special_columns[t.character()];
138 for (int i = 0; i < nargs; ++i)
139 ci.special += "{" + p.verbatim_item() + "}";
140 //cerr << "handling special column '" << t << "' " << nargs
141 // << " '" << ci.special << "'\n";
142 colinfo.push_back(ci);
144 cerr << "ignoring special separator '" << t << "'\n";
149 if (colinfo.size() && leftline)
150 colinfo[0].leftline = true;
154 } // anonymous namespace
157 void parse_table(Parser & p, ostream & os, unsigned flags)
162 Token const & t = p.get_token();
165 cerr << "t: " << t << " flags: " << flags << "\n";
171 if (t.cat() == catMath) {
172 // we are inside some text mode thingy, so opening new math is allowed
173 Token const & n = p.get_token();
174 if (n.cat() == catMath) {
175 // TeX's $$...$$ syntax for displayed math
177 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
179 p.get_token(); // skip the second '$' token
181 // simple $...$ stuff
184 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
189 else if (t.cat() == catLetter ||
190 t.cat() == catSpace ||
191 t.cat() == catSuper ||
193 t.cat() == catOther ||
194 t.cat() == catActive ||
195 t.cat() == catNewline ||
196 t.cat() == catParameter)
199 else if (t.cat() == catBegin) {
201 parse_table(p, os, FLAG_BRACE_LAST);
205 else if (t.cat() == catEnd) {
206 if (flags & FLAG_BRACE_LAST)
208 cerr << "unexpected '}'\n";
211 else if (t.cat() == catAlign) {
215 else if (t.cs() == "tabularnewline" || t.cs() == "\\") {
216 // stuff before the line break
217 // and look ahead for stuff after the line break
218 os << HLINE << hlines << HLINE << LINE << read_hlines(p) << HLINE;
222 else if (t.cs() == "hline")
225 else if (t.cs() == "cline")
226 hlines += "\\cline{" + p.verbatim_item() + '}';
228 else if (t.cat() == catComment)
231 else if (t.cs() == "(") {
233 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
237 else if (t.cs() == "[") {
239 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
243 else if (t.cs() == "begin") {
244 string const name = p.getArg('{', '}');
245 active_environments.push_back(name);
246 parse_table(p, os, FLAG_END);
249 else if (t.cs() == "end") {
250 if (flags & FLAG_END) {
251 // eat environment name
252 string const name = p.getArg('{', '}');
253 if (name != active_environment())
254 p.error("\\end{" + name + "} does not match \\begin{"
255 + active_environment() + "}");
256 active_environments.pop_back();
259 p.error("found 'end' unexpectedly");
268 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
271 for (size_t col = 0; col < ci.size(); ++col)
272 ci[col].topline = true;
276 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
278 ri.bottomline = true;
279 for (size_t col = 0; col < ci.size(); ++col)
280 ci[col].bottomline = true;
284 void handle_tabular(Parser & p, ostream & os,
287 string posopts = p.getOpt();
289 cerr << "vertical tabular positioning '" << posopts << "' ignored\n";
291 vector<ColInfo> colinfo;
293 // handle column formatting
294 handle_colalign(p, colinfo);
296 // handle initial hlines
298 // first scan of cells
299 // use table mode to keep it minimal-invasive
300 // not exactly what's TeX doing...
301 vector<string> lines;
303 ss << read_hlines(p) << HLINE; // handle initial hlines
304 parse_table(p, ss, FLAG_END);
305 split(ss.str(), lines, LINE);
307 vector< vector<CellInfo> > cellinfo(lines.size());
308 vector<RowInfo> rowinfo(lines.size());
311 //cerr << "// split into rows\n";
312 for (size_t row = 0; row < rowinfo.size(); ++row) {
315 cellinfo[row].resize(colinfo.size());
318 vector<string> dummy;
319 //cerr << "\n########### LINE: " << lines[row] << "########\n";
320 split(lines[row], dummy, HLINE);
322 // handle horizontal line fragments
323 if (dummy.size() != 3) {
324 if (dummy.size() != 1)
325 cerr << "unexpected dummy size: " << dummy.size()
326 << " content: " << lines[row] << "\n";
329 lines[row] = dummy[1];
331 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
332 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
333 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
335 for (int i = 0; i <= 2; i += 2) {
336 //cerr << " reading from line string '" << dummy[i] << "'\n";
339 Token t = p1.get_token();
340 //cerr << "read token: " << t << "\n";
341 if (t.cs() == "hline") {
343 if (rowinfo[row].topline) {
344 if (row > 0) // extra bottomline above
345 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
347 cerr << "dropping extra hline\n";
348 //cerr << "below row: " << row-1 << endl;
350 handle_hline_above(rowinfo[row], cellinfo[row]);
351 //cerr << "above row: " << row << endl;
354 //cerr << "below row: " << row << endl;
355 handle_hline_below(rowinfo[row], cellinfo[row]);
357 } else if (t.cs() == "cline") {
358 string arg = p1.verbatim_item();
359 //cerr << "read cline arg: '" << arg << "'\n";
363 size_t from = string2int(t[0]) - 1;
364 size_t to = string2int(t[1]);
365 for (size_t col = from; col < to; ++col) {
366 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
368 rowinfo[row].topline = true;
369 cellinfo[row][col].topline = true;
371 rowinfo[row].bottomline = true;
372 cellinfo[row][col].bottomline = true;
376 cerr << "unexpected line token: " << t << endl;
382 vector<string> cells;
383 split(lines[row], cells, TAB);
384 for (size_t col = 0, cell = 0;
385 cell < cells.size() && col < colinfo.size(); ++col, ++cell) {
386 //cerr << "cell content: '" << cells[cell] << "'\n";
387 Parser p(cells[cell]);
389 //cells[cell] << "'\n";
390 if (p.next_token().cs() == "multicolumn") {
393 size_t const ncells = string2int(p.verbatim_item());
395 // special cell properties alignment
397 handle_colalign(p, t);
398 cellinfo[row][col].multi = 1;
399 cellinfo[row][col].align = t.front().align;
401 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
402 cellinfo[row][col].content = os.str();
403 cellinfo[row][col].leftline |= t.front().leftline;
404 cellinfo[row][col].rightline |= t.front().rightline;
406 // add dummy cells for multicol
407 for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
409 cellinfo[row][col].multi = 2;
410 cellinfo[row][col].align = 'c';
413 // more than one line on the right?
414 if (t.front().rightline > 1)
415 cellinfo[row][col + 1].leftline = true;
418 // FLAG_END is a hack, we need to read all of it
419 cellinfo[row][col].leftline = colinfo[col].leftline;
420 cellinfo[row][col].rightline = colinfo[col].rightline;
421 cellinfo[row][col].align = colinfo[col].align;
423 parse_text_in_inset(p, os, FLAG_CELL, false, context);
424 cellinfo[row][col].content = os.str();
428 //cerr << "// handle almost empty last row what we have\n";
429 // handle almost empty last row
430 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
431 //cerr << "remove empty last line\n";
432 if (rowinfo[row].topline)
433 rowinfo[row - 1].bottomline = true;
434 for (size_t col = 0; col < colinfo.size(); ++col)
435 if (cellinfo[row][col].topline)
436 cellinfo[row - 1][col].bottomline = true;
442 //cerr << "// output what we have\n";
443 // output what we have
444 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
445 << "\" columns=\"" << colinfo.size() << "\">\n"
448 //cerr << "// after header\n";
449 for (size_t col = 0; col < colinfo.size(); ++col) {
450 os << "<column alignment=\""
451 << verbose_align(colinfo[col].align) << "\"";
452 os << " valignment=\"top\"";
453 if (colinfo[col].leftline)
454 os << " leftline=\"true\"";
455 if (colinfo[col].rightline)
456 os << " rightline=\"true\"";
457 if (colinfo[col].width.size())
458 os << " width=\"" << colinfo[col].width << "\"";
459 if (colinfo[col].special.size())
460 os << " special=\"" << colinfo[col].special << "\"";
463 //cerr << "// after cols\n";
465 for (size_t row = 0; row < rowinfo.size(); ++row) {
467 if (rowinfo[row].topline)
468 os << " topline=\"true\"";
469 if (rowinfo[row].bottomline)
470 os << " bottomline=\"true\"";
472 for (size_t col = 0; col < colinfo.size(); ++col) {
473 CellInfo const & cell = cellinfo[row][col];
476 os << " multicolumn=\"" << cell.multi << "\"";
477 os << " alignment=\"" << verbose_align(cell.align)
479 << " valignment=\"top\"";
481 os << " topline=\"true\"";
483 os << " bottomline=\"true\"";
485 os << " leftline=\"true\"";
487 os << " rightline=\"true\"";
488 //cerr << "\nrow: " << row << " col: " << col;
490 // cerr << " topline=\"true\"";
491 //if (cell.bottomline)
492 // cerr << " bottomline=\"true\"";
493 os << " usebox=\"none\""
495 << "\n\\begin_inset Text\n"
497 << "\n\\end_inset \n"
503 os << "</lyxtabular>\n";