From: Uwe Stöhr Date: Sun, 4 Mar 2012 13:27:53 +0000 (+0000) Subject: - text.cpp: - fix tex2lyx parsing of verbatim environments X-Git-Tag: 2.1.0beta1~1998 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=df95041141c01c04fe8d4af43acc1cc1bc1b4795;p=lyx.git - text.cpp: - fix tex2lyx parsing of verbatim environments - Parser.cpp: - new function to parse verbatim environments - test/test-structure.tex: updated example git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@40850 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/tex2lyx/Parser.cpp b/src/tex2lyx/Parser.cpp index c48301207a..75eb518203 100644 --- a/src/tex2lyx/Parser.cpp +++ b/src/tex2lyx/Parser.cpp @@ -481,6 +481,30 @@ 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.cat() == catBegin) { + putback(); + os << '{' << verbatim_item() << '}'; + } else 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(); +} + + void Parser::tokenize_one() { catInit(); diff --git a/src/tex2lyx/Parser.h b/src/tex2lyx/Parser.h index 5e749e3019..2f4e26a587 100644 --- a/src/tex2lyx/Parser.h +++ b/src/tex2lyx/Parser.h @@ -196,6 +196,12 @@ 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); /*! * Returns the character of the current token and increments * the token position. diff --git a/src/tex2lyx/test/test-structure.tex b/src/tex2lyx/test/test-structure.tex index ec61f6fc43..b487bf17b4 100644 --- a/src/tex2lyx/test/test-structure.tex +++ b/src/tex2lyx/test/test-structure.tex @@ -293,10 +293,11 @@ verbatim: verbat im % $ 02/19/12 hjkh jkh \ blah -atesta +\begin{centering} zzz +\end{raggedleft} \end{verbatim} and bibliography: diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index 99db3eab11..e4c16027dc 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -1344,8 +1344,8 @@ void parse_environment(Parser & p, ostream & os, bool outer, } else if (name == "verbatim") { - os << "\n\\begin_layout Verbatim\n"; - string const s = p.verbatimEnvironment("verbatim"); + os << "\n\\end_layout\n\n\\begin_layout Verbatim\n"; + string const s = p.plainEnvironment("verbatim"); string::const_iterator it2 = s.begin(); for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) { if (*it == '\\')