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 extern bool need_layout;
31 // filled in preamble.C
32 std::map<char, int> special_columns;
37 int string2int(string const & s, int deflt = 0)
46 string read_hlines(Parser & p)
51 if (p.next_token().cs() == "hline") {
54 } else if (p.next_token().cs() == "cline") {
56 os << "\\cline{" << p.verbatim_item() << "}";
61 //cerr << "read_hlines(), read: '" << os.str() << "'\n";
62 //cerr << "read_hlines(), next token: " << p.next_token() << "\n";
67 /* rather brutish way to code table structure in a string:
71 \multicolumn{2}{c}{4} & 5 //
78 \hline HLINE TAB 5 LINE
82 char const TAB = '\001';
83 char const LINE = '\002';
84 char const HLINE = '\004';
87 void handle_colalign(Parser & p, vector<ColInfo> & colinfo)
89 if (p.get_token().cat() != catBegin)
90 cerr << "wrong syntax for table column alignment. '{' expected\n";
93 bool leftline = false;
94 for (Token t=p.get_token(); p.good() && t.cat() != catEnd; t = p.get_token()){
96 cerr << "t: " << t << " c: '" << t.character() << "'\n";
99 switch (t.character()) {
104 ci.align = t.character();
105 if (colinfo.size() && colinfo.back().rightline > 1) {
107 --colinfo.back().rightline;
109 colinfo.push_back(ci);
113 colinfo.push_back(ColInfo());
114 colinfo.back().align = nextalign;
115 colinfo.back().width = p.verbatim_item();
122 ++colinfo.back().rightline;
125 string s = p.verbatim_item();
126 if (s == "\\raggedleft ")
128 else if (s == "\\raggedright ")
131 cerr << "unknown '>' column '" << s << "'\n";
135 if (special_columns.find(t.character()) != special_columns.end()) {
138 ci.special += t.character();
139 int const nargs = special_columns[t.character()];
140 for (int i = 0; i < nargs; ++i)
141 ci.special += "{" + p.verbatim_item() + "}";
142 //cerr << "handling special column '" << t << "' " << nargs
143 // << " '" << ci.special << "'\n";
144 colinfo.push_back(ci);
146 cerr << "ignoring special separator '" << t << "'\n";
151 if (colinfo.size() && leftline)
152 colinfo[0].leftline = true;
156 } // anonymous namespace
159 void parse_table(Parser & p, ostream & os, unsigned flags)
164 Token const & t = p.get_token();
167 cerr << "t: " << t << " flags: " << flags << "\n";
173 if (t.cat() == catMath) {
174 // we are inside some text mode thingy, so opening new math is allowed
175 Token const & n = p.get_token();
176 if (n.cat() == catMath) {
177 // TeX's $$...$$ syntax for displayed math
179 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
181 p.get_token(); // skip the second '$' token
183 // simple $...$ stuff
186 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
191 else if (t.cat() == catLetter ||
192 t.cat() == catSpace ||
193 t.cat() == catSuper ||
195 t.cat() == catOther ||
196 t.cat() == catActive ||
197 t.cat() == catNewline ||
198 t.cat() == catParameter)
201 else if (t.cat() == catBegin) {
203 parse_table(p, os, FLAG_BRACE_LAST);
207 else if (t.cat() == catEnd) {
208 if (flags & FLAG_BRACE_LAST)
210 cerr << "unexpected '}'\n";
213 else if (t.cat() == catAlign) {
217 else if (t.cs() == "tabularnewline" || t.cs() == "\\") {
218 // stuff before the line break
219 // and look ahead for stuff after the line break
220 os << HLINE << hlines << HLINE << LINE << read_hlines(p) << HLINE;
224 else if (t.cs() == "hline")
227 else if (t.cs() == "cline")
228 hlines += "\\cline{" + p.verbatim_item() + '}';
230 else if (t.cat() == catComment)
233 else if (t.cs() == "(") {
235 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
239 else if (t.cs() == "[") {
241 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
245 else if (t.cs() == "begin") {
246 string const name = p.getArg('{', '}');
247 active_environments.push_back(name);
248 parse_table(p, os, FLAG_END);
251 else if (t.cs() == "end") {
252 if (flags & FLAG_END) {
253 // eat environment name
254 string const name = p.getArg('{', '}');
255 if (name != active_environment())
256 p.error("\\end{" + name + "} does not match \\begin{"
257 + active_environment() + "}");
258 active_environments.pop_back();
261 p.error("found 'end' unexpectedly");
270 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
273 for (size_t col = 0; col < ci.size(); ++col)
274 ci[col].topline = true;
278 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
280 ri.bottomline = true;
281 for (size_t col = 0; col < ci.size(); ++col)
282 ci[col].bottomline = true;
286 void handle_tabular(Parser & p, ostream & os,
287 LyXTextClass const & textclass)
289 string posopts = p.getOpt();
291 cerr << "vertical tabular positioning '" << posopts << "' ignored\n";
293 vector<ColInfo> colinfo;
295 // handle column formatting
296 handle_colalign(p, colinfo);
298 // handle initial hlines
300 // first scan of cells
301 // use table mode to keep it minimal-invasive
302 // not exactly what's TeX doing...
303 vector<string> lines;
305 ss << read_hlines(p) << HLINE; // handle initial hlines
306 parse_table(p, ss, FLAG_END);
307 split(ss.str(), lines, LINE);
309 vector< vector<CellInfo> > cellinfo(lines.size());
310 vector<RowInfo> rowinfo(lines.size());
313 //cerr << "// split into rows\n";
314 for (size_t row = 0; row < rowinfo.size(); ++row) {
317 cellinfo[row].resize(colinfo.size());
320 vector<string> dummy;
321 //cerr << "\n########### LINE: " << lines[row] << "########\n";
322 split(lines[row], dummy, HLINE);
324 // handle horizontal line fragments
325 if (dummy.size() != 3) {
326 if (dummy.size() != 1)
327 cerr << "unexpected dummy size: " << dummy.size()
328 << " content: " << lines[row] << "\n";
331 lines[row] = dummy[1];
333 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
334 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
335 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
337 for (int i = 0; i <= 2; i += 2) {
338 //cerr << " reading from line string '" << dummy[i] << "'\n";
341 Token t = p1.get_token();
342 //cerr << "read token: " << t << "\n";
343 if (t.cs() == "hline") {
345 if (rowinfo[row].topline) {
346 if (row > 0) // extra bottomline above
347 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
349 cerr << "dropping extra hline\n";
350 //cerr << "below row: " << row-1 << endl;
352 handle_hline_above(rowinfo[row], cellinfo[row]);
353 //cerr << "above row: " << row << endl;
356 //cerr << "below row: " << row << endl;
357 handle_hline_below(rowinfo[row], cellinfo[row]);
359 } else if (t.cs() == "cline") {
360 string arg = p1.verbatim_item();
361 //cerr << "read cline arg: '" << arg << "'\n";
365 size_t from = string2int(t[0]) - 1;
366 size_t to = string2int(t[1]);
367 for (size_t col = from; col < to; ++col) {
368 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
370 rowinfo[row].topline = true;
371 cellinfo[row][col].topline = true;
373 rowinfo[row].bottomline = true;
374 cellinfo[row][col].bottomline = true;
378 cerr << "unexpected line token: " << t << endl;
384 vector<string> cells;
385 split(lines[row], cells, TAB);
386 for (size_t col = 0, cell = 0;
387 cell < cells.size() && col < colinfo.size(); ++col, ++cell) {
388 //cerr << "cell content: '" << cells[cell] << "'\n";
389 Parser p(cells[cell]);
391 //cells[cell] << "'\n";
392 if (p.next_token().cs() == "multicolumn") {
395 size_t const ncells = string2int(p.verbatim_item());
397 // special cell properties alignment
399 handle_colalign(p, t);
400 cellinfo[row][col].multi = 1;
401 cellinfo[row][col].align = t.front().align;
402 cellinfo[row][col].content = parse_text(p, FLAG_ITEM, false, textclass);
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 cellinfo[row][col].content = parse_text(p, FLAG_END, false, textclass);
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";