tex2lyx: support for inline listings (fixes last part of #8066)

OK also for branch?

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@40860 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Uwe Stöhr 2012-03-05 22:04:22 +00:00
parent 1a5891e1fd
commit 408728aed9
3 changed files with 48 additions and 5 deletions

View File

@ -502,6 +502,28 @@ string const Parser::plainEnvironment(string const & name)
}
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,13 @@ public:
* 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

@ -1118,7 +1118,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");
@ -1133,11 +1133,20 @@ void parse_listings(Parser & p, ostream & os, Context & parent_context)
}
os << "lstparams " << '"' << arg << '"' << '\n';
}
os << "inline false\n"
<< "status collapsed\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();
string const s = p.plainEnvironment("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";
@ -1399,7 +1408,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
eat_whitespace(p, os, parent_context, false);
// FIXME handle the automatic color package loading
// uwestoehr asks: In what case color is loaded?
parse_listings(p, os, parent_context);
parse_listings(p, os, parent_context, false);
p.skip_spaces();
}
@ -2787,6 +2796,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);