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"
30 // filled in preamble.C
31 std::map<char, int> special_columns;
36 int string2int(string const & s, int deflt = 0)
45 string read_hlines(Parser & p)
50 if (p.next_token().cs() == "hline") {
53 } else if (p.next_token().cs() == "cline") {
55 os << "\\cline{" << p.verbatim_item() << "}";
60 //cerr << "read_hlines(), read: '" << os.str() << "'\n";
61 //cerr << "read_hlines(), next token: " << p.next_token() << "\n";
66 /* rather brutish way to code table structure in a string:
70 \multicolumn{2}{c}{4} & 5 //
77 \hline HLINE TAB 5 LINE
81 char const TAB = '\001';
82 char const LINE = '\002';
83 char const HLINE = '\004';
86 void handle_colalign(Parser & p, vector<ColInfo> & colinfo)
88 if (p.get_token().cat() != catBegin)
89 cerr << "wrong syntax for table column alignment. '{' expected\n";
92 bool leftline = false;
93 for (Token t=p.get_token(); p.good() && t.cat() != catEnd; t = p.get_token()){
95 cerr << "t: " << t << " c: '" << t.character() << "'\n";
98 switch (t.character()) {
103 ci.align = t.character();
104 if (colinfo.size() && colinfo.back().rightline > 1) {
106 --colinfo.back().rightline;
108 colinfo.push_back(ci);
112 colinfo.push_back(ColInfo());
113 colinfo.back().align = nextalign;
114 colinfo.back().width = p.verbatim_item();
121 ++colinfo.back().rightline;
124 string s = p.verbatim_item();
125 if (s == "\\raggedleft ")
127 else if (s == "\\raggedright ")
130 cerr << "unknown '>' column '" << s << "'\n";
134 if (special_columns.find(t.character()) != special_columns.end()) {
137 ci.special += t.character();
138 int const nargs = special_columns[t.character()];
139 for (int i = 0; i < nargs; ++i)
140 ci.special += "{" + p.verbatim_item() + "}";
141 //cerr << "handling special column '" << t << "' " << nargs
142 // << " '" << ci.special << "'\n";
143 colinfo.push_back(ci);
145 cerr << "ignoring special separator '" << t << "'\n";
150 if (colinfo.size() && leftline)
151 colinfo[0].leftline = true;
155 } // anonymous namespace
158 void parse_table(Parser & p, ostream & os, unsigned flags)
163 Token const & t = p.get_token();
166 cerr << "t: " << t << " flags: " << flags << "\n";
172 if (t.cat() == catMath) {
173 // we are inside some text mode thingy, so opening new math is allowed
174 Token const & n = p.get_token();
175 if (n.cat() == catMath) {
176 // TeX's $$...$$ syntax for displayed math
178 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
180 p.get_token(); // skip the second '$' token
182 // simple $...$ stuff
185 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
190 else if (t.cat() == catLetter ||
191 t.cat() == catSpace ||
192 t.cat() == catSuper ||
194 t.cat() == catOther ||
195 t.cat() == catActive ||
196 t.cat() == catNewline ||
197 t.cat() == catParameter)
200 else if (t.cat() == catBegin) {
202 parse_table(p, os, FLAG_BRACE_LAST);
206 else if (t.cat() == catEnd) {
207 if (flags & FLAG_BRACE_LAST)
209 cerr << "unexpected '}'\n";
212 else if (t.cat() == catAlign) {
216 else if (t.cs() == "tabularnewline" || t.cs() == "\\") {
217 // stuff before the line break
218 // and look ahead for stuff after the line break
219 os << HLINE << hlines << HLINE << LINE << read_hlines(p) << HLINE;
223 else if (t.cs() == "hline")
226 else if (t.cs() == "cline")
227 hlines += "\\cline{" + p.verbatim_item() + '}';
229 else if (t.cat() == catComment)
232 else if (t.cs() == "(") {
234 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
238 else if (t.cs() == "[") {
240 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
244 else if (t.cs() == "begin") {
245 string const name = p.getArg('{', '}');
246 active_environments.push_back(name);
247 parse_table(p, os, FLAG_END);
250 else if (t.cs() == "end") {
251 if (flags & FLAG_END) {
252 // eat environment name
253 string const name = p.getArg('{', '}');
254 if (name != active_environment())
255 p.error("\\end{" + name + "} does not match \\begin{"
256 + active_environment() + "}");
257 active_environments.pop_back();
260 p.error("found 'end' unexpectedly");
269 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
272 for (size_t col = 0; col < ci.size(); ++col)
273 ci[col].topline = true;
277 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
279 ri.bottomline = true;
280 for (size_t col = 0; col < ci.size(); ++col)
281 ci[col].bottomline = true;
285 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;
400 cellinfo[row][col].content = parse_text(p, FLAG_ITEM, false);
401 cellinfo[row][col].leftline |= t.front().leftline;
402 cellinfo[row][col].rightline |= t.front().rightline;
404 // add dummy cells for multicol
405 for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
407 cellinfo[row][col].multi = 2;
408 cellinfo[row][col].align = 'c';
411 // more than one line on the right?
412 if (t.front().rightline > 1)
413 cellinfo[row][col + 1].leftline = true;
416 // FLAG_END is a hack, we need to read all of it
417 cellinfo[row][col].content = parse_text(p, FLAG_END, false);
421 //cerr << "// handle almost empty last row what we have\n";
422 // handle almost empty last row
423 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
424 //cerr << "remove empty last line\n";
425 if (rowinfo[row].topline)
426 rowinfo[row - 1].bottomline = true;
427 for (size_t col = 0; col < colinfo.size(); ++col)
428 if (cellinfo[row][col].topline)
429 cellinfo[row - 1][col].bottomline = true;
435 //cerr << "// output what we have\n";
436 // output what we have
437 os << "<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
438 << "\" columns=\"" << colinfo.size() << "\">\n"
441 //cerr << "// after header\n";
442 for (size_t col = 0; col < colinfo.size(); ++col) {
443 os << "<column alignment=\"" << colinfo[col].align << "\"";
444 if (colinfo[col].rightline)
445 os << " rightline=\"true\"";
446 if (colinfo[col].leftline)
447 os << " leftline=\"true\"";
448 if (colinfo[col].special.size())
449 os << " special=\"" << colinfo[col].special << "\"";
450 os << " valignment=\"top\"";
451 os << " width=\"" << colinfo[col].width << "\"";
454 //cerr << "// after cols\n";
456 for (size_t row = 0; row < rowinfo.size(); ++row) {
458 if (rowinfo[row].topline)
459 os << " topline=\"true\"";
460 if (rowinfo[row].bottomline)
461 os << " bottomline=\"true\"";
463 for (size_t col = 0; col < colinfo.size(); ++col) {
464 CellInfo const & cell = cellinfo[row][col];
467 os << " multicolumn=\"" << cell.multi << "\"";
469 os << " leftline=\"true\"";
471 os << " rightline=\"true\"";
473 os << " topline=\"true\"";
475 os << " bottomline=\"true\"";
476 //cerr << "\nrow: " << row << " col: " << col;
478 // cerr << " topline=\"true\"";
479 //if (cell.bottomline)
480 // cerr << " bottomline=\"true\"";
481 os << " alignment=\"" << verbose_align(cell.align) << "\""
482 << " valignment=\"top\""
483 << " usebox=\"none\""
485 << "\n\\begin_inset Text"
486 << "\n\n\\layout Standard\n\n"
488 << "\n\\end_inset\n\n"
494 os << "</lyxtabular>\n";