Angus' second patch

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6105 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2003-02-11 15:57:08 +00:00
parent f3600e9f67
commit d33935be24
2 changed files with 74 additions and 27 deletions

View File

@ -1,3 +1,14 @@
2003-02-11 Angus Leeming <leeming@lyx.org>
* tex2lyx.C (handle_opt): prevent infinite loop if opts is empty.
(handle_package): new function, factoring code out of Parser::parse.
(trim): copied across from support/lstrings.C
(Parser::parse): handle '\usepackage{foo, bar}'.
2003-02-11 André Pönitz <poenitz@gmx.net>
* tex2lyx.C: first shot at real code
2003-02-11 Angus Leeming <leeming@lyx.org>
* .cvsignore: new file.

View File

@ -1,4 +1,3 @@
/** The .tex to .lyx converter
\author André Pönitz (2003)
*/
@ -88,6 +87,24 @@ stack<string> active_environments;
string const trim(string const & a, char const * p = " ")
{
// lyx::Assert(p);
if (a.empty() || !*p)
return a;
string::size_type r = a.find_last_not_of(p);
string::size_type l = a.find_first_not_of(p);
// Is this the minimal test? (lgb)
if (r == string::npos && l == string::npos)
return string();
return a.substr(l, r - l + 1);
}
void split(string const & s, vector<string> & result, char delim)
{
istringstream is(s);
@ -111,6 +128,9 @@ string join(vector<string> const & input, char delim)
void handle_opt(vector<string> & opts, char const ** what, string & target)
{
if (opts.empty())
return;
for ( ; what; ++what) {
vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
if (it != opts.end()) {
@ -131,6 +151,38 @@ void handle_ert(ostream & os, string const & s)
}
void handle_package(string const & name, string const & options)
{
if (name == "a4wide") {
h_papersize = "a4";
h_paperpackage = "widemarginsa4";
} else if (name == "ae")
h_fontscheme = "ae";
else if (name == "aecompl")
h_fontscheme = "ae";
else if (name == "amsmath")
h_use_amsmath = "1";
else if (name == "amssymb")
h_use_amsmath = "1";
else if (name == "babel")
; // ignore this
else if (name == "fontenc")
; // ignore this
else if (name == "inputenc")
h_inputencoding = options;
else if (name == "makeidx")
; // ignore this
else if (name == "verbatim")
; // ignore this
else {
if (options.size())
h_preamble += "\\usepackage[" + options + "]{" + name + "}\n";
else
h_preamble += "\\usepackage{" + name + "}\n";
}
}
string wrap(string const & cmd, string const & str)
{
return OPEN + cmd + ' ' + str + CLOSE;
@ -811,32 +863,16 @@ string Parser::parse(unsigned flags, mode_type mode)
else if (t.cs() == "usepackage") {
string const options = getArg('[', ']');
string const name = getArg('{', '}');
if (name == "a4wide") {
h_papersize = "a4";
h_paperpackage = "widemarginsa4";
} else if (name == "ae")
h_fontscheme = "ae";
else if (name == "aecompl")
h_fontscheme = "ae";
else if (name == "amsmath")
h_use_amsmath = "1";
else if (name == "amssymb")
h_use_amsmath = "1";
else if (name == "babel")
; // ignore this
else if (name == "fontenc")
; // ignore this
else if (name == "inputenc")
h_inputencoding = options;
else if (name == "makeidx")
; // ignore this
else if (name == "verbatim")
; // ignore this
else {
if (options.size())
h_preamble += "\\usepackage[" + options + "]{" + name + "}\n";
else
h_preamble += "\\usepackage{" + name + "}\n";
if (options.empty() && name.find(',')) {
vector<string> vecnames;
split(name, vecnames, ',');
vector<string>::const_iterator it = vecnames.begin();
vector<string>::const_iterator end = vecnames.end();
for (; it != end; ++it) {
handle_package(trim(*it), string());
}
} else {
handle_package(name, options);
}
}