tex2lyx: support for listings with options (bug 8066)

This commit is contained in:
Uwe Stöhr 2013-01-22 01:07:52 +01:00
parent 313c2b6416
commit 09e2b217db
4 changed files with 81 additions and 13 deletions

View File

@ -492,6 +492,49 @@ string const Parser::verbatimEnvironment(string const & name)
}
string const Parser::plainEnvironment(string const & name)
{
if (!good())
return string();
ostringstream os;
for (Token t = get_token(); good(); t = get_token()) {
if (t.asInput() == "\\end") {
string const end = getArg('{', '}');
if (end == name)
return os.str();
else
os << "\\end{" << end << '}';
} else
os << t.asInput();
}
cerr << "unexpected end of input" << endl;
return os.str();
}
string const Parser::plainCommand(char left, char right, string const & name)
{
if (!good())
return string();
// ceck if first token is really the start character
Token tok = get_token();
if (tok.character() != left) {
cerr << "first character does not match start character of command \\" << name << endl;
return string();
}
ostringstream os;
for (Token t = get_token(); good(); t = get_token()) {
if (t.character() == right) {
return os.str();
} else
os << t.asInput();
}
cerr << "unexpected end of input" << endl;
return os.str();
}
void Parser::tokenize_one()
{
catInit();

View File

@ -202,6 +202,19 @@ public:
* is parsed but not returned.
*/
std::string const verbatimEnvironment(std::string const & name);
/*
* The same as verbatimEnvironment(std::string const & name) but
* \begin and \end commands inside the name environment are not parsed.
* This function is designed to parse verbatim environments.
*/
std::string const plainEnvironment(std::string const & name);
/*
* Basically the same as plainEnvironment(std::string const & name) but
* instead of \begin and \end commands the parsing is started/stopped
* at given characters.
* This function is designed to parse verbatim commands.
*/
std::string const plainCommand(char left, char right, std::string const & name);
/*!
* Returns the character of the current token and increments
* the token position.

View File

@ -1110,7 +1110,7 @@ void parse_outer_box(Parser & p, ostream & os, unsigned flags, bool outer,
}
void parse_listings(Parser & p, ostream & os, Context & parent_context)
void parse_listings(Parser & p, ostream & os, Context & parent_context, bool in_line)
{
parent_context.check_layout(os);
begin_inset(os, "listings\n");
@ -1118,14 +1118,26 @@ void parse_listings(Parser & p, ostream & os, Context & parent_context)
string arg = p.verbatimOption();
os << "lstparams " << '"' << arg << '"' << '\n';
if (arg.find("\\color") != string::npos)
preamble.registerAutomaticallyLoadedPackage("color");
preamble.registerAutomaticallyLoadedPackage("color");
}
os << "inline false\n"
<< "status collapsed\n";
if (p.hasOpt()) {
string arg = p.verbatimOption();
os << "lstparams " << '"' << arg << '"' << '\n';
}
if (in_line)
os << "inline true\n";
else
os << "inline false\n";
os << "status collapsed\n";
Context context(true, parent_context.textclass);
context.layout = &parent_context.textclass.plainLayout();
context.check_layout(os);
string const s = p.verbatimEnvironment("lstlisting");
string s;
if (in_line) {
s = p.plainCommand('!', '!', "lstinline");
context.new_paragraph(os);
context.check_layout(os);
} else
s = p.plainEnvironment("lstlisting");
for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
if (*it == '\\')
os << "\n\\backslash\n";
@ -1374,13 +1386,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
else if (name == "lstlisting") {
eat_whitespace(p, os, parent_context, false);
// FIXME handle listings with parameters
if (p.hasOpt())
parse_unknown_environment(p, name, os, FLAG_END,
outer, parent_context);
else
parse_listings(p, os, parent_context);
p.skip_spaces();
parse_listings(p, os, parent_context, false);
}
else if (!parent_context.new_layout_allowed)
@ -2758,6 +2764,11 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
end_inset(os);
}
else if (t.cs() == "lstinline") {
p.skip_spaces();
parse_listings(p, os, context, true);
}
else if (t.cs() == "ensuremath") {
p.skip_spaces();
context.check_layout(os);

View File

@ -34,6 +34,7 @@ What's new
* TEX2LYX IMPROVEMENTS
- support for listings with options (bug #8066).
- add new option -m to select needed modules (bug #8393).