3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
28 using std::istringstream;
30 using std::ostringstream;
35 // filled in preamble.C
36 std::map<char, int> special_columns;
43 ColInfo() : align('c'), rightline(false), leftline(false) {}
48 /// special column alignment
50 /// how many lines on the right?
52 /// a line on the left?
57 /// row type for longtables
64 /// part of head on first page
68 /// part of foot on last page
75 RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
77 /// horizontal line above
79 /// horizontal line below
81 /// These are for longtabulars only
82 /// row type (head, foot, firsthead etc.)
84 /// row for a pagebreak
91 CellInfo() : multi(0), align('n'), leftline(false), rightline(false),
92 topline(false), bottomline(false) {}
99 /// do we have a line on the left?
101 /// do we have a line on the right?
103 /// do we have a line above?
105 /// do we have a line below?
110 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
111 inline char const * verbose_align(char c)
113 return c == 'c' ? "center" : c == 'r' ? "right" : c == 'l' ? "left" : "none";
117 // stripped down from tabluar.C. We use it currently only for bools and
119 string const write_attribute(string const & name, bool const & b)
121 // we write only true attribute values so we remove a bit of the
122 // file format bloat for tabulars.
123 return b ? ' ' + name + "=\"true\"" : string();
127 string const write_attribute(string const & name, string const & s)
129 return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
133 int string2int(string const & s, int deflt = 0)
142 /* rather brutish way to code table structure in a string:
146 \multicolumn{2}{c}{4} & 5 //
150 gets "translated" to:
152 HLINE 1 TAB 2 TAB 3 HLINE HLINE LINE
153 \hline HLINE \multicolumn{2}{c}{4} TAB 5 HLINE HLINE LINE
154 HLINE 6 TAB 7 HLINE HLINE LINE
157 char const TAB = '\001';
158 char const LINE = '\002';
159 char const HLINE = '\004';
162 /// handle column specifications for tabulars and multicolumns
163 void handle_colalign(Parser & p, vector<ColInfo> & colinfo)
165 if (p.get_token().cat() != catBegin)
166 cerr << "wrong syntax for table column alignment. '{' expected\n";
168 char nextalign = 'b';
169 bool leftline = false;
170 for (Token t=p.get_token(); p.good() && t.cat() != catEnd; t = p.get_token()){
172 cerr << "t: " << t << " c: '" << t.character() << "'\n";
175 // We cannot handle comments here
176 if (t.cat() == catComment) {
177 if (t.cs().empty()) {
181 cerr << "Ignoring comment: " << t.asInput();
185 switch (t.character()) {
190 ci.align = t.character();
191 if (colinfo.size() && colinfo.back().rightline > 1) {
193 --colinfo.back().rightline;
195 colinfo.push_back(ci);
199 colinfo.push_back(ColInfo());
200 colinfo.back().align = nextalign;
201 colinfo.back().width = p.verbatim_item();
208 ++colinfo.back().rightline;
211 string s = p.verbatim_item();
212 if (s == "\\raggedleft ")
214 else if (s == "\\raggedright ")
217 cerr << "unknown '>' column '" << s << "'\n";
221 if (special_columns.find(t.character()) != special_columns.end()) {
224 ci.special += t.character();
225 int const nargs = special_columns[t.character()];
226 for (int i = 0; i < nargs; ++i)
227 ci.special += "{" + p.verbatim_item() + "}";
228 //cerr << "handling special column '" << t << "' " << nargs
229 // << " '" << ci.special << "'\n";
230 colinfo.push_back(ci);
232 cerr << "ignoring special separator '" << t << "'\n";
237 if (colinfo.size() && leftline)
238 colinfo[0].leftline = true;
243 * Parse hlines and similar stuff.
244 * \returns wether the token \p t was parsed
246 bool parse_hlines(Parser & p, Token const & t, string & hlines,
247 bool is_long_tabular)
249 BOOST_ASSERT(t.cat() == catEscape);
251 if (t.cs() == "hline")
254 else if (t.cs() == "cline")
255 hlines += "\\cline{" + p.verbatim_item() + '}';
257 else if (is_long_tabular && t.cs() == "newpage")
258 hlines += "\\newpage";
267 /// Position in a row
269 /// At the very beginning, before the first token
271 /// After the first token and before any column token
273 /// After the first column token. Comments and whitespace are only
274 /// treated as tokens in this position
276 /// After the first non-column token at the end
282 * Parse table structure.
283 * We parse tables in a two-pass process: This function extracts the table
284 * structure (rows, columns, hlines etc.), but does not change the cell
285 * content. The cell content is parsed in a second step in handle_tabular().
287 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
288 RowPosition & pos, unsigned flags)
290 // table structure commands such as \hline
293 // comments that occur at places where we can't handle them
297 Token const & t = p.get_token();
300 cerr << "t: " << t << " flags: " << flags << "\n";
303 // comments and whitespace in hlines
306 case IN_HLINES_START:
308 if (t.cat() == catComment) {
313 // We can't handle comments here,
314 // store them for later use
315 comments += t.asInput();
317 } else if (t.cat() == catSpace ||
318 t.cat() == catNewline) {
319 // whitespace is irrelevant here, we
320 // need to recognize hline stuff
329 // We need to handle structure stuff first in order to
330 // determine wether we need to output a HLINE separator
331 // before the row or not.
332 if (t.cat() == catEscape) {
333 if (parse_hlines(p, t, hlines, is_long_tabular)) {
336 pos = IN_HLINES_START;
341 case IN_HLINES_START:
348 else if (t.cs() == "tabularnewline" ||
352 cerr << "Warning: Converting TeX "
353 "'\\cr' to LaTeX '\\\\'."
355 // stuff before the line break
356 os << comments << HLINE << hlines << HLINE
358 //cerr << "hlines: " << hlines << endl;
365 else if (is_long_tabular &&
366 (t.cs() == "endhead" ||
367 t.cs() == "endfirsthead" ||
368 t.cs() == "endfoot" ||
369 t.cs() == "endlastfoot")) {
370 hlines += t.asInput();
374 // these commands are implicit line
376 os << comments << HLINE << hlines
383 pos = IN_HLINES_START;
385 case IN_HLINES_START:
393 // We need a HLINE separator if we either have no hline
394 // stuff at all and are just starting a row or if we just
395 // got the first non-hline token.
398 // no hline tokens exist, first token at row start
399 case IN_HLINES_START:
400 // hline tokens exist, first non-hline token at row
402 os << hlines << HLINE << comments;
408 // Oops, there is still cell content after hline
409 // stuff. This does not work in LaTeX, so we ignore
411 cerr << "Ignoring '" << hlines << "' in a cell"
422 // If we come here we have normal cell content
426 if (t.cat() == catMath) {
427 // we are inside some text mode thingy, so opening new math is allowed
428 Token const & n = p.get_token();
429 if (n.cat() == catMath) {
430 // TeX's $$...$$ syntax for displayed math
432 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
434 p.get_token(); // skip the second '$' token
436 // simple $...$ stuff
439 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
444 else if (t.cat() == catSpace || t.cat() == catNewline)
447 else if (t.cat() == catLetter ||
448 t.cat() == catSuper ||
450 t.cat() == catOther ||
451 t.cat() == catActive ||
452 t.cat() == catParameter)
455 else if (t.cat() == catBegin) {
457 parse_table(p, os, is_long_tabular, pos,
462 else if (t.cat() == catEnd) {
463 if (flags & FLAG_BRACE_LAST)
465 cerr << "unexpected '}'\n";
468 else if (t.cat() == catAlign) {
473 else if (t.cat() == catComment)
476 else if (t.cs() == "(") {
478 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
482 else if (t.cs() == "[") {
484 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
488 else if (t.cs() == "begin") {
489 string const name = p.getArg('{', '}');
490 active_environments.push_back(name);
491 os << "\\begin{" << name << '}';
492 if (is_math_env(name)) {
493 parse_math(p, os, FLAG_END, MATH_MODE);
495 parse_table(p, os, is_long_tabular, pos,
498 os << "\\end{" << name << '}';
499 active_environments.pop_back();
502 else if (t.cs() == "end") {
503 if (flags & FLAG_END) {
504 // eat environment name
505 string const name = p.getArg('{', '}');
506 if (name != active_environment())
507 p.error("\\end{" + name + "} does not match \\begin{"
508 + active_environment() + "}");
511 p.error("found 'end' unexpectedly");
518 // We can have comments if the last line is incomplete
521 // We can have hline stuff if the last line is incomplete
522 if (!hlines.empty()) {
523 // this does not work in LaTeX, so we ignore it
524 cerr << "Ignoring '" << hlines << "' at end of tabular"
530 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
533 for (size_t col = 0; col < ci.size(); ++col)
534 ci[col].topline = true;
538 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
540 ri.bottomline = true;
541 for (size_t col = 0; col < ci.size(); ++col)
542 ci[col].bottomline = true;
546 } // anonymous namespace
549 void handle_tabular(Parser & p, ostream & os, bool is_long_tabular,
552 string posopts = p.getOpt();
553 if (!posopts.empty()) {
555 cerr << "horizontal longtable";
557 cerr << "vertical tabular";
558 cerr << " positioning '" << posopts << "' ignored\n";
561 vector<ColInfo> colinfo;
563 // handle column formatting
564 handle_colalign(p, colinfo);
566 // first scan of cells
567 // use table mode to keep it minimal-invasive
568 // not exactly what's TeX doing...
569 vector<string> lines;
571 RowPosition rowpos = ROW_START;
572 parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
573 split(ss.str(), lines, LINE);
575 vector< vector<CellInfo> > cellinfo(lines.size());
576 vector<RowInfo> rowinfo(lines.size());
579 //cerr << "// split into rows\n";
580 for (size_t row = 0; row < rowinfo.size(); ++row) {
583 cellinfo[row].resize(colinfo.size());
586 vector<string> dummy;
587 //cerr << "\n########### LINE: " << lines[row] << "########\n";
588 split(lines[row], dummy, HLINE);
590 // handle horizontal line fragments
591 // we do only expect this for a last line without '\\'
592 if (dummy.size() != 3) {
593 if ((dummy.size() != 1 && dummy.size() != 2) ||
594 row != rowinfo.size() - 1)
595 cerr << "unexpected dummy size: " << dummy.size()
596 << " content: " << lines[row] << "\n";
599 lines[row] = dummy[1];
601 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
602 //cerr << "line: " << row << " below 2: " << dummy[2] << "\n";
603 //cerr << "line: " << row << " cells 1: " << dummy[1] << "\n";
605 for (int i = 0; i <= 2; i += 2) {
606 //cerr << " reading from line string '" << dummy[i] << "'\n";
609 Token t = p1.get_token();
610 //cerr << "read token: " << t << "\n";
611 if (t.cs() == "hline") {
613 if (rowinfo[row].topline) {
614 if (row > 0) // extra bottomline above
615 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
617 cerr << "dropping extra hline\n";
618 //cerr << "below row: " << row-1 << endl;
620 handle_hline_above(rowinfo[row], cellinfo[row]);
621 //cerr << "above row: " << row << endl;
624 //cerr << "below row: " << row << endl;
625 handle_hline_below(rowinfo[row], cellinfo[row]);
627 } else if (t.cs() == "cline") {
628 string arg = p1.verbatim_item();
629 //cerr << "read cline arg: '" << arg << "'\n";
633 size_t from = string2int(t[0]) - 1;
634 if (from >= colinfo.size()) {
635 cerr << "cline starts at non "
637 << (from + 1) << endl;
638 from = colinfo.size() - 1;
640 size_t to = string2int(t[1]) - 1;
641 if (to >= colinfo.size()) {
642 cerr << "cline ends at non "
645 to = colinfo.size() - 1;
647 for (size_t col = from; col <= to; ++col) {
648 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
650 rowinfo[row].topline = true;
651 cellinfo[row][col].topline = true;
653 rowinfo[row].bottomline = true;
654 cellinfo[row][col].bottomline = true;
657 } else if (t.cs() == "endhead") {
659 rowinfo[row].type = LT_HEAD;
660 for (int r = row - 1; r >= 0; --r) {
661 if (rowinfo[r].type != LT_NORMAL)
663 rowinfo[r].type = LT_HEAD;
665 } else if (t.cs() == "endfirsthead") {
667 rowinfo[row].type = LT_FIRSTHEAD;
668 for (int r = row - 1; r >= 0; --r) {
669 if (rowinfo[r].type != LT_NORMAL)
671 rowinfo[r].type = LT_FIRSTHEAD;
673 } else if (t.cs() == "endfoot") {
675 rowinfo[row].type = LT_FOOT;
676 for (int r = row - 1; r >= 0; --r) {
677 if (rowinfo[r].type != LT_NORMAL)
679 rowinfo[r].type = LT_FOOT;
681 } else if (t.cs() == "endlastfoot") {
683 rowinfo[row].type = LT_LASTFOOT;
684 for (int r = row - 1; r >= 0; --r) {
685 if (rowinfo[r].type != LT_NORMAL)
687 rowinfo[r].type = LT_LASTFOOT;
689 } else if (t.cs() == "newpage") {
692 rowinfo[row - 1].newpage = true;
699 rowinfo[row].newpage = true;
701 cerr << "unexpected line token: " << t << endl;
707 vector<string> cells;
708 split(lines[row], cells, TAB);
709 // Has the last multicolumn cell a rightline?
710 bool last_rightline = false;
711 for (size_t col = 0, cell = 0;
712 cell < cells.size() && col < colinfo.size(); ++col, ++cell) {
713 //cerr << "cell content: '" << cells[cell] << "'\n";
714 Parser p(cells[cell]);
716 //cells[cell] << "'\n";
717 if (p.next_token().cs() == "multicolumn") {
720 size_t const ncells = string2int(p.verbatim_item());
722 // special cell properties alignment
724 handle_colalign(p, t);
725 cellinfo[row][col].multi = 1;
726 cellinfo[row][col].align = t.front().align;
728 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
729 cellinfo[row][col].content = os.str();
731 // multicolumn cells are tricky: This
732 // \multicolumn{2}{|c|}{col1-2}&
733 // \multicolumn{2}{|c|}{col3-4} "\\"
734 // gives | col1-2 | col3-4 | and not
735 // | col1-2 || col3-4 |
737 if (last_rightline && t.front().leftline) {
738 t.front().leftline = false;
740 last_rightline = t.front().rightline;
742 // multicolumn lines override normal cell lines
743 cellinfo[row][col].leftline = t.front().leftline;
744 cellinfo[row][col].rightline = t.front().rightline;
746 // add dummy cells for multicol
747 for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
749 cellinfo[row][col].multi = 2;
750 cellinfo[row][col].align = 'c';
753 // more than one line on the right?
754 if (t.front().rightline > 1)
755 cellinfo[row][col + 1].leftline = true;
758 // FLAG_END is a hack, we need to read all of it
759 cellinfo[row][col].leftline = colinfo[col].leftline;
760 cellinfo[row][col].rightline = colinfo[col].rightline;
761 cellinfo[row][col].align = colinfo[col].align;
763 parse_text_in_inset(p, os, FLAG_CELL, false, context);
764 cellinfo[row][col].content = os.str();
765 last_rightline = false;
769 //cerr << "// handle almost empty last row what we have\n";
770 // handle almost empty last row
771 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
772 //cerr << "remove empty last line\n";
773 if (rowinfo[row].topline)
774 rowinfo[row - 1].bottomline = true;
775 for (size_t col = 0; col < colinfo.size(); ++col)
776 if (cellinfo[row][col].topline)
777 cellinfo[row - 1][col].bottomline = true;
783 //cerr << "// output what we have\n";
784 // output what we have
785 os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
786 << "\" columns=\"" << colinfo.size() << "\">\n";
788 << write_attribute("islongtable", is_long_tabular)
791 //cerr << "// after header\n";
792 for (size_t col = 0; col < colinfo.size(); ++col) {
793 os << "<column alignment=\""
794 << verbose_align(colinfo[col].align) << "\""
795 << " valignment=\"top\""
796 << write_attribute("leftline", colinfo[col].leftline)
797 << write_attribute("rightline", colinfo[col].rightline)
798 << write_attribute("width", colinfo[col].width)
799 << write_attribute("special", colinfo[col].special)
802 //cerr << "// after cols\n";
804 for (size_t row = 0; row < rowinfo.size(); ++row) {
806 << write_attribute("topline", rowinfo[row].topline)
807 << write_attribute("bottomline", rowinfo[row].bottomline)
808 << write_attribute("endhead",
809 rowinfo[row].type == LT_HEAD)
810 << write_attribute("endfirsthead",
811 rowinfo[row].type == LT_FIRSTHEAD)
812 << write_attribute("endfoot",
813 rowinfo[row].type == LT_FOOT)
814 << write_attribute("endlastfoot",
815 rowinfo[row].type == LT_LASTFOOT)
816 << write_attribute("newpage", rowinfo[row].newpage)
818 for (size_t col = 0; col < colinfo.size(); ++col) {
819 CellInfo const & cell = cellinfo[row][col];
822 os << " multicolumn=\"" << cell.multi << "\"";
823 os << " alignment=\"" << verbose_align(cell.align)
825 << " valignment=\"top\""
826 << write_attribute("topline", cell.topline)
827 << write_attribute("bottomline", cell.bottomline)
828 << write_attribute("leftline", cell.leftline)
829 << write_attribute("rightline", cell.rightline);
830 //cerr << "\nrow: " << row << " col: " << col;
832 // cerr << " topline=\"true\"";
833 //if (cell.bottomline)
834 // cerr << " bottomline=\"true\"";
835 os << " usebox=\"none\""
837 << "\n\\begin_inset Text\n"
845 os << "</lyxtabular>\n";