]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/table.C
Removed all redundant using directives from the source.
[lyx.git] / src / tex2lyx / table.C
index 1159e5083e8c347f3f722a3a5bce3a787e3d30f6..7b02cf3d79a2d9a98b61ada778d7f5bacca2d95e 100644 (file)
@@ -1,19 +1,26 @@
-/** The .tex to .lyx converter
-    \author André Pönitz (2003)
+/**
+ * \file table.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author André Pönitz
+ * \author Jean-Marc Lasgouttes
+ *
+ * Full author contact details are available in file CREDITS.
  */
 
 // {[(
 
 #include <config.h>
 
-#include "Lsstream.h"
 #include "tex2lyx.h"
 
 #include <cctype>
 #include <fstream>
 #include <iostream>
-#include <string>
+#include <sstream>
 #include <vector>
+#include <map>
 
 using std::cerr;
 using std::endl;
@@ -23,43 +30,13 @@ using std::ostringstream;
 using std::string;
 using std::vector;
 
+#include "mathed/math_gridinfo.h"
 
-namespace {
-
-struct ColInfo
-{
-       ColInfo() : rightline(0), leftline(false) {}
-       string align;      // column alignment
-       string width;      // column width
-       int    rightline;  // a line on the right?
-       bool   leftline;
-};
-
-
-struct RowInfo
-{
-       RowInfo() : topline(false), bottomline(false) {} 
-       bool topline;     // horizontal line above
-       int  bottomline;  // horizontal line below
-};
-
-
-struct CellInfo
-{
-       CellInfo()
-               : multi(0), leftline(false), rightline(false),
-          topline(false), bottomline(false)
-       {}
+// filled in preamble.C
+std::map<char, int> special_columns;
 
-       string content;    // cell content
-       int multi;         // multicolumn flag
-       string align;      // cell alignment
-       bool leftline;     // do we have a line on the left?
-       bool rightline;    // do we have a line on the right?
-       bool topline;        // do we have a line above?
-       bool bottomline;   // do we have a line below?
-};
 
+namespace {
 
 int string2int(string const & s, int deflt = 0)
 {
@@ -73,7 +50,7 @@ int string2int(string const & s, int deflt = 0)
 string read_hlines(Parser & p)
 {
        ostringstream os;
-       p.skipSpaces();
+       p.skip_spaces();
        while (p.good()) {
                if (p.next_token().cs() == "hline") {
                        p.get_token();
@@ -83,7 +60,7 @@ string read_hlines(Parser & p)
                        os << "\\cline{" << p.verbatim_item() << "}";
                } else
                        break;
-               p.skipSpaces();
+               p.skip_spaces();
        };
        //cerr << "read_hlines(), read: '" << os.str() << "'\n";
        //cerr << "read_hlines(), next token: " << p.next_token() << "\n";
@@ -91,7 +68,6 @@ string read_hlines(Parser & p)
 }
 
 
-
 /* rather brutish way to code table structure in a string:
 
   \begin{tabular}{ccc}
@@ -111,24 +87,13 @@ char const TAB   = '\001';
 char const LINE  = '\002';
 char const HLINE = '\004';
 
-string get_align(char c)
-{
-       switch (c) {
-               case 'c': return "center";
-               case 'l': return "left";
-               case 'r': return "right";
-               case 'b': return "block";
-       }
-       return "center";
-}
-
 
 void handle_colalign(Parser & p, vector<ColInfo> & colinfo)
 {
        if (p.get_token().cat() != catBegin)
                cerr << "wrong syntax for table column alignment. '{' expected\n";
 
-       string nextalign = "block";
+       char nextalign = 'b';
        bool leftline = false;
        for (Token t=p.get_token(); p.good() && t.cat() != catEnd; t = p.get_token()){
 #ifdef FILEDEBUG
@@ -140,7 +105,7 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo)
                        case 'l':
                        case 'r': {
                                ColInfo ci;
-                               ci.align = get_align(t.character());
+                               ci.align = t.character();
                                if (colinfo.size() && colinfo.back().rightline > 1) {
                                        ci.leftline = true;
                                        --colinfo.back().rightline;
@@ -152,7 +117,7 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo)
                                colinfo.push_back(ColInfo());
                                colinfo.back().align = nextalign;
                                colinfo.back().width = p.verbatim_item();
-                               nextalign = "block";
+                               nextalign = 'b';
                                break;
                        case '|':
                                if (colinfo.empty())
@@ -163,15 +128,27 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo)
                        case '>': {
                                string s = p.verbatim_item();
                                if (s == "\\raggedleft ")
-                                       nextalign = "left";
+                                       nextalign = 'l';
                                else if (s == "\\raggedright ")
-                                       nextalign = "right";
+                                       nextalign = 'r';
                                else
                                        cerr << "unknown '>' column '" << s << "'\n";
                                break;
                        }
                        default:
-                               cerr << "ignoring special separator '" << t << "'\n";
+                               if (special_columns.find(t.character()) != special_columns.end()) {
+                                       ColInfo ci;
+                                       ci.align = 'c';
+                                       ci.special += t.character();
+                                       int const nargs = special_columns[t.character()];
+                                       for (int i = 0; i < nargs; ++i)
+                                               ci.special += "{" + p.verbatim_item() + "}"; 
+                                       //cerr << "handling special column '" << t << "' " << nargs
+                                       //      << "  '" << ci.special << "'\n";
+                                       colinfo.push_back(ci);
+                               } else {
+                                       cerr << "ignoring special separator '" << t << "'\n";
+                               }
                                break;
                        }
        }
@@ -242,7 +219,6 @@ void parse_table(Parser & p, ostream & os, unsigned flags)
                }
 
                else if (t.cs() == "tabularnewline" || t.cs() == "\\") {
-               //else if (t.cs() == "tabularnewline") {
                        // stuff before the line break
                        // and look ahead for stuff after the line break
                        os << HLINE << hlines << HLINE << LINE << read_hlines(p) << HLINE;
@@ -311,7 +287,8 @@ void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
 }
 
 
-void handle_tabular(Parser & p, ostream & os)
+void handle_tabular(Parser & p, ostream & os,
+                   Context & context)
 {
        string posopts = p.getOpt();
        if (posopts.size())
@@ -414,7 +391,7 @@ void handle_tabular(Parser & p, ostream & os)
                                cell < cells.size() && col < colinfo.size(); ++col, ++cell) {
                        //cerr << "cell content: '" << cells[cell] << "'\n";
                        Parser p(cells[cell]);
-                       p.skipSpaces(); 
+                       p.skip_spaces();        
                        //cells[cell] << "'\n";
                        if (p.next_token().cs() == "multicolumn") {
                                // how many cells?
@@ -426,7 +403,9 @@ void handle_tabular(Parser & p, ostream & os)
                                handle_colalign(p, t);
                                cellinfo[row][col].multi     = 1;
                                cellinfo[row][col].align     = t.front().align;
-                               cellinfo[row][col].content   = parse_text(p, FLAG_ITEM, false);
+                               ostringstream os;
+                               parse_text_in_inset(p, os, FLAG_ITEM, false, context);
+                               cellinfo[row][col].content   = os.str();
                                cellinfo[row][col].leftline  |= t.front().leftline;
                                cellinfo[row][col].rightline |= t.front().rightline;
 
@@ -434,7 +413,7 @@ void handle_tabular(Parser & p, ostream & os)
                                for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
                                        ++col;
                                        cellinfo[row][col].multi = 2;
-                                       cellinfo[row][col].align = "center";
+                                       cellinfo[row][col].align = 'c';
                                }
 
                                // more than one line on the right?
@@ -443,7 +422,12 @@ void handle_tabular(Parser & p, ostream & os)
 
                        } else {        
                                // FLAG_END is a hack, we need to read all of it
-                               cellinfo[row][col].content = parse_text(p, FLAG_END, false);
+                               cellinfo[row][col].leftline = colinfo[col].leftline;
+                               cellinfo[row][col].rightline = colinfo[col].rightline;
+                               cellinfo[row][col].align = colinfo[col].align;
+                               ostringstream os;
+                               parse_text_in_inset(p, os, FLAG_CELL, false, context);
+                               cellinfo[row][col].content   = os.str();
                        }
                }
 
@@ -463,19 +447,23 @@ void handle_tabular(Parser & p, ostream & os)
 
        //cerr << "// output what we have\n";
        // output what we have
-       os << "<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
+       os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
                 << "\" columns=\"" << colinfo.size() << "\">\n"
                 << "<features>\n";
 
        //cerr << "// after header\n";
        for (size_t col = 0; col < colinfo.size(); ++col) {
-               os << "<column alignment=\"" << colinfo[col].align << "\"";
-               if (colinfo[col].rightline)
-                       os << " rightline=\"true\"";
+               os << "<column alignment=\""
+                  << verbose_align(colinfo[col].align) << "\"";
+               os << " valignment=\"top\"";
                if (colinfo[col].leftline)
                        os << " leftline=\"true\"";
-               os << " valignment=\"top\"";
-               os << " width=\"" << colinfo[col].width << "\"";
+               if (colinfo[col].rightline)
+                       os << " rightline=\"true\"";
+               if (colinfo[col].width.size())
+                       os << " width=\"" << colinfo[col].width << "\"";
+               if (colinfo[col].special.size())
+                       os << " special=\"" << colinfo[col].special << "\"";
                os << ">\n";
        }
        //cerr << "// after cols\n";
@@ -492,27 +480,27 @@ void handle_tabular(Parser & p, ostream & os)
                        os << "<cell";
                        if (cell.multi)
                                os << " multicolumn=\"" << cell.multi << "\"";
-                       if (cell.leftline)
-                               os << " leftline=\"true\"";
-                       if (cell.rightline)
-                               os << " rightline=\"true\"";
+                       os << " alignment=\"" << verbose_align(cell.align) 
+                          << "\""
+                          << " valignment=\"top\"";
                        if (cell.topline)
                                os << " topline=\"true\"";
                        if (cell.bottomline)
                                os << " bottomline=\"true\"";
+                       if (cell.leftline)
+                               os << " leftline=\"true\"";
+                       if (cell.rightline)
+                               os << " rightline=\"true\"";
                        //cerr << "\nrow: " << row << " col: " << col;
                        //if (cell.topline)
                        //      cerr << " topline=\"true\"";
                        //if (cell.bottomline)
                        //      cerr << " bottomline=\"true\"";
-                       os << " alignment=\"" << cell.align << "\""
-                                << " valignment=\"top\""
-                                << " usebox=\"none\""
-                                << ">"
-                          << "\n\\begin_inset Text"
-                          << "\n\n\\layout Standard\n\n"
+                       os << " usebox=\"none\""
+                          << ">"
+                          << "\n\\begin_inset Text\n"
                           << cell.content
-                          << "\n\\end_inset\n\n"
+                          << "\n\\end_inset \n"
                           << "</cell>\n";
                }
                os << "</row>\n";