multicol; small stuff

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6211 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2003-02-20 17:05:54 +00:00
parent d02cff2442
commit b99a5acc7f

View File

@ -37,15 +37,33 @@ using std::vector;
namespace { namespace {
void parse(Parser & p, ostream & os, unsigned flags, mode_type mode);
char const OPEN = '<'; char const OPEN = '<';
char const CLOSE = '>'; char const CLOSE = '>';
char const TAB = '\001';
// rather brutish way to code table structure in a string:
//
// \begin{tabular}{ccc}
// 1 & 2 & 3\\
// \multicolumn{2}{c}{4} & 5\\
// 6 & 7 \\
// \end{tabular}
//
// gets "translated" to:
//
// 1 TAB 2 TAB 3 LINE
// 2 MULT c MULT 4 TAB 5 LINE
// 5 TAB 7 LINE
char const TAB = '\001';
char const LINE = '\002'; char const LINE = '\002';
char const MULT = '\003';
const char * known_languages[] = { "austrian", "babel", "bahasa", "basque", const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
"breton", "british", "bulgarian", "catalan", "croatian", "czech", "breton", "british", "bulgarian", "catalan", "croatian", "czech", "danish",
"danish", "dutch", "english", "esperanto", "estonian", "finnish", "dutch", "english", "esperanto", "estonian", "finnish", "francais",
"francais", "frenchb", "galician", "germanb", "greek", "hebcal", "hebfont", "frenchb", "galician", "german", "germanb", "greek", "hebcal", "hebfont",
"hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen", "hebrew", "hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen",
"icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss", "icelandic", "irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss",
"lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr", "lgrcmtt", "lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr",
@ -55,12 +73,24 @@ const char * known_languages[] = { "austrian", "babel", "bahasa", "basque",
"russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish", "russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
"swedish", "turkish", "ukraineb", "usorbian", "welsh", 0}; "swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
const char * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 }; char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
char const * known_headings[] = { "caption", "title", "author",
"paragraph", "chapter", "section", "subsection", "subsubsection", 0 };
const char * known_math_envs[] = {"equation", "eqnarray", "eqnarray*", char const * known_math_envs[] = { "equation", "eqnarray", "eqnarray*",
"align", "align*", 0}; "align", "align*", 0};
char const * known_latex_commands[] = { "ref", "cite", "label", "index",
"printindex", 0 };
// LaTeX names for quotes
char const * known_quotes[] = { "glqq", "grqq", 0};
// the same as known_quotes with .lyx names
char const * known_coded_quotes[] = { "gld", "grd", 0};
// some ugly stuff // some ugly stuff
ostringstream h_preamble; ostringstream h_preamble;
@ -144,6 +174,15 @@ string join(vector<string> const & input, char delim)
} }
char const ** is_known(string const & str, char const ** what)
{
for ( ; *what; ++what)
if (str == *what)
return what;
return 0;
}
void handle_opt(vector<string> & opts, char const ** what, string & target) void handle_opt(vector<string> & opts, char const ** what, string & target)
{ {
if (opts.empty()) if (opts.empty())
@ -170,31 +209,6 @@ bool is_math_env(string const & name)
} }
bool is_heading(string const & name)
{
return
name == "caption" ||
name == "title" ||
name == "author" ||
name == "paragraph" ||
name == "chapter" ||
name == "section" ||
name == "subsection" ||
name == "subsubsection";
}
bool is_latex_command(string const & name)
{
return
name == "ref" ||
name == "cite" ||
name == "label" ||
name == "index" ||
name == "printindex";
}
void begin_inset(ostream & os, string const & name) void begin_inset(ostream & os, string const & name)
{ {
os << "\n\\begin_inset " << name; os << "\n\\begin_inset " << name;
@ -247,6 +261,7 @@ void handle_par(ostream & os)
void handle_package(string const & name, string const & options) void handle_package(string const & name, string const & options)
{ {
//cerr << "handle_package: '" << name << "'\n";
if (name == "a4wide") { if (name == "a4wide") {
h_papersize = "a4paper"; h_papersize = "a4paper";
h_paperpackage = "widemarginsa4"; h_paperpackage = "widemarginsa4";
@ -268,7 +283,10 @@ void handle_package(string const & name, string const & options)
; // ignore this ; // ignore this
else if (name == "verbatim") else if (name == "verbatim")
; // ignore this ; // ignore this
else { else if (is_known(name, known_languages)) {
h_language = name;
h_quotes_language = name;
} else {
if (options.size()) if (options.size())
h_preamble << "\\usepackage[" << options << "]{" << name << "}\n"; h_preamble << "\\usepackage[" << options << "]{" << name << "}\n";
else else
@ -277,10 +295,72 @@ void handle_package(string const & name, string const & options)
} }
void handle_table(Parser &, ostream &) vector<string> extract_col_align(string const & s)
{ {
// \begin{table} has been read vector<string> res;
//parse(end for (size_t i = 0; i < s.size(); ++i) {
switch (s[i]) {
case 'c': res.push_back("center"); break;
case 'l': res.push_back("left"); break;
case 'r': res.push_back("right"); break;
default : res.push_back("right"); break;
}
}
return res;
}
void handle_tabular(Parser & p, ostream & os, mode_type mode)
{
begin_inset(os, "Tabular \n");
string colopts = p.verbatimItem();
vector<string> colalign = extract_col_align(colopts);
ostringstream ss;
parse(p, ss, FLAG_END, mode);
vector<string> lines;
split(ss.str(), lines, LINE);
const size_t cols = colalign.size();
const size_t rows = lines.size();
os << "<lyxtabular version=\"3\" rows=\"" << rows
<< "\" columns=\"" << cols << "\">\n"
<< "<features>\n";
for (size_t c = 0; c < cols; ++c)
os << "<column alignment=\"" << colalign[c] << "\""
<< " valignment=\"top\""
<< " width=\"0pt\""
<< ">\n";
for (size_t r = 0; r < rows; ++r) {
vector<string> cells;
split(lines[r], cells, TAB);
while (cells.size() < cols)
cells.push_back(string());
//os << "<row bottomline=\"true\">\n";
os << "<row>\n";
for (size_t c = 0; c < cols; ++c) {
os << "<cell";
string alignment = "center";
vector<string> parts;
split(cells[c], parts, MULT);
if (parts.size() > 2) {
os << " multicolumn=\"" << parts[0] << "\"";
alignment = parts[1];
}
os << " alignment=\"" << alignment << "\""
<< " valignment=\"top\""
<< " topline=\"true\""
<< " leftline=\"true\""
<< " usebox=\"none\""
<< ">";
begin_inset(os, "Text");
os << "\n\n\\layout Standard\n\n";
os << parts.back();
end_inset(os);
os << "</cell>\n";
}
os << "</row>\n";
}
os << "</lyxtabular>\n";
end_inset(os);
} }
@ -290,29 +370,6 @@ string wrap(string const & cmd, string const & str)
} }
vector<string> extract_col_align(string const & s)
{
vector<string> res;
for (size_t i = 0; i < s.size(); ++i) {
switch (s[i]) {
case 'c':
res.push_back("center");
break;
case 'l':
res.push_back("left");
break;
case 'r':
res.push_back("right");
break;
default:
res.push_back("right");
break;
}
}
return res;
}
void end_preamble(ostream & os) void end_preamble(ostream & os)
{ {
in_preamble = false; in_preamble = false;
@ -494,6 +551,12 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
os << LINE; os << LINE;
} }
else if (t.cs() == "\\" && mode == MATH_MODE)
os << t.asInput();
else if (t.cs() == "\\" && curr_env() == "tabular")
os << LINE;
else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) { else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
//cerr << "finished reading option\n"; //cerr << "finished reading option\n";
return; return;
@ -575,47 +638,7 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
os << "\\end{" << name << "}"; os << "\\end{" << name << "}";
end_inset(os); end_inset(os);
} else if (name == "tabular") { } else if (name == "tabular") {
begin_inset(os, "Tabular \n"); handle_tabular(p, os, mode);
string colopts = p.verbatimItem();
vector<string> colalign = extract_col_align(colopts);
ostringstream ss;
parse(p, ss, FLAG_END, mode);
vector<string> lines;
split(ss.str(), lines, LINE);
const size_t cols = colalign.size();
const size_t rows = lines.size();
os << "<lyxtabular version=\"3\" rows=\"" << rows
<< "\" columns=\"" << cols << "\">\n"
<< "<features>\n";
for (size_t c = 0; c < cols; ++c)
os << "<column alignment=\"" << colalign[c] << "\""
<< " valignment=\"top\""
<< " width=\"0pt\""
<< ">\n";
for (size_t r = 0; r < rows; ++r) {
vector<string> cells;
split(lines[r], cells, TAB);
while (cells.size() < cols)
cells.push_back(string());
//os << "<row bottomline=\"true\">\n";
os << "<row>\n";
for (size_t c = 0; c < cols; ++c) {
os << "<cell alignment=\"center\""
<< " valignment=\"top\""
<< " topline=\"true\""
<< " leftline=\"true\""
<< " usebox=\"none\""
<< ">";
begin_inset(os, "Text");
os << "\n\n\\layout Standard\n\n";
os << cells[c];
end_inset(os);
os << "</cell>\n";
}
os << "</row>\n";
}
os << "</lyxtabular>\n";
end_inset(os);
} else if (name == "table") { } else if (name == "table") {
begin_inset(os, "Float table\n"); begin_inset(os, "Float table\n");
os << "wide false\n" os << "wide false\n"
@ -650,6 +673,7 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
p.error("found 'end' unexpectedly"); p.error("found 'end' unexpectedly");
} }
else if (t.cs() == "item") else if (t.cs() == "item")
handle_par(os); handle_par(os);
@ -733,7 +757,7 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
else if (t.cs() == "par") else if (t.cs() == "par")
handle_par(os); handle_par(os);
else if (is_heading(t.cs())) { else if (is_known(t.cs(), known_headings)) {
string name = t.cs(); string name = t.cs();
if (p.nextToken().asInput() == "*") { if (p.nextToken().asInput() == "*") {
p.getToken(); p.getToken();
@ -749,6 +773,15 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
else if (t.cs() == "tableofcontents") else if (t.cs() == "tableofcontents")
p.verbatimItem(); // swallow this p.verbatimItem(); // swallow this
else if (t.cs() == "multicolumn" && mode == TEXT_MODE) {
// brutish...
parse(p, os, FLAG_ITEM, mode);
os << MULT;
parse(p, os, FLAG_ITEM, mode);
os << MULT;
parse(p, os, FLAG_ITEM, mode);
}
else if (t.cs() == "textrm") { else if (t.cs() == "textrm") {
os << '\\' << t.cs() << '{'; os << '\\' << t.cs() << '{';
parse(p, os, FLAG_ITEM, MATHTEXT_MODE); parse(p, os, FLAG_ITEM, MATHTEXT_MODE);
@ -761,7 +794,7 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
os << "\n\\" << t.cs() << " default\n"; os << "\n\\" << t.cs() << " default\n";
} }
else if (is_latex_command(t.cs()) && mode == TEXT_MODE) { else if (is_known(t.cs(), known_latex_commands) && mode == TEXT_MODE) {
begin_inset(os, "LatexCommand "); begin_inset(os, "LatexCommand ");
os << '\\' << t.cs() << '{'; os << '\\' << t.cs() << '{';
parse(p, os, FLAG_ITEM, TEXT_MODE); parse(p, os, FLAG_ITEM, TEXT_MODE);
@ -777,6 +810,12 @@ void parse(Parser & p, ostream & os, unsigned flags, mode_type mode)
os << '{' << p.getArg('{','}') << '}' << "\n\n"; os << '{' << p.getArg('{','}') << '}' << "\n\n";
} }
else if (char const ** where = is_known(t.cs(), known_quotes)) {
begin_inset(os, "Quotes ");
os << known_coded_quotes[where - known_quotes];
end_inset(os);
}
else if (t.cs() == "textasciitilde") else if (t.cs() == "textasciitilde")
os << '~'; os << '~';