preamble.cpp, text.cpp:

- fix bug 27
 - support for multi-language files
 - code cleanups

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/branches/BRANCH_1_5_X@21951 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Uwe Stöhr 2007-12-04 20:48:23 +00:00
parent 64b7a7ef77
commit 8b30acfcbe
3 changed files with 61 additions and 21 deletions

View File

@ -3,7 +3,8 @@
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author André Pönitz
* \author André Pönitz
* \author Uwe Stöhr
*
* Full author contact details are available in file CREDITS.
*/
@ -37,6 +38,7 @@ using std::string;
using std::vector;
using std::cerr;
using std::endl;
using std::find;
using support::FileName;
using support::libFileSearch;
@ -71,7 +73,7 @@ ostringstream h_preamble;
string h_textclass = "article";
string h_options = string();
string h_language = "english";
string h_inputencoding = "latin1";
string h_inputencoding = "auto";
string h_fontscheme = "default";
string h_graphics = "default";
string h_paperfontsize = "default";
@ -99,13 +101,19 @@ void handle_opt(vector<string> & opts, char const * const * what, string & targe
if (opts.empty())
return;
for ( ; *what; ++what) {
vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
// the last language option is the document language (for babel and LyX)
// the last size option is the document font size
vector<string>::iterator it;
vector<string>::iterator position = opts.begin();
for (; *what; ++what) {
it = find(opts.begin(), opts.end(), *what);
if (it != opts.end()) {
//cerr << "### found option '" << *what << "'\n";
target = *what;
if (it >= position) {
target = *what;
position = it;
}
// remove found options from the list
opts.erase(it);
return;
}
}
}
@ -127,7 +135,7 @@ vector<string> split_options(string const & input)
while (p.good()) {
Token const & t = p.get_token();
if (t.asInput() == ",") {
options.push_back(option);
options.push_back(trim(option));
option.erase();
} else if (t.asInput() == "=") {
option += '=';
@ -139,7 +147,7 @@ vector<string> split_options(string const & input)
}
if (!option.empty())
options.push_back(option);
options.push_back(trim(option));
return options;
}
@ -187,7 +195,10 @@ void handle_package(string const & name, string const & opts)
else if (name == "fontenc")
; // ignore this
else if (name == "inputenc") {
h_inputencoding = opts;
// only set when there is not more than one inputenc option
// therefore check for the "," character
if (opts.find(",", 0) == string::npos)
h_inputencoding = opts;
options.clear();
} else if (name == "makeidx")
; // ignore this
@ -236,14 +247,15 @@ void handle_package(string const & name, string const & opts)
void end_preamble(ostream & os, TextClass const & /*textclass*/)
{
os << "#LyX file created by tex2lyx 0.1.2\n"
os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
<< "\\lyxformat 246\n"
<< "\\begin_document\n"
<< "\\begin_header\n"
<< "\\textclass " << h_textclass << "\n"
<< "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
<< "\\textclass " << h_textclass << "\n";
if (!h_preamble.str().empty())
os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
if (!h_options.empty())
os << "\\options " << h_options << "\n";
os << "\\options " << h_options << "\n";
os << "\\language " << h_language << "\n"
<< "\\inputencoding " << h_inputencoding << "\n"
<< "\\fontscheme " << h_fontscheme << "\n"
@ -372,8 +384,7 @@ TextClass const parse_preamble(Parser & p, ostream & os, string const & forcecla
}
else if (t.cs() == "documentclass") {
vector<string> opts;
split(p.getArg('[', ']'), opts, ',');
vector<string> opts = split_options(p.getArg('[', ']'));
handle_opt(opts, known_languages, h_language);
if (is_known(h_language, known_french_languages))
h_language = "french";

View File

@ -5,6 +5,7 @@
*
* \author André Pönitz
* \author Jean-Marc Lasgouttes
* \author Uwe Stöhr
*
* Full author contact details are available in file CREDITS.
*/
@ -104,8 +105,9 @@ string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
}
char const * const known_latex_commands[] = { "ref", "cite", "label", "index",
"printindex", "pageref", "url", "vref", "vpageref", "prettyref", "eqref", 0 };
char const * const known_latex_commands[] = { "ref", "cite", "label",
"index", "printindex", "pageref", "url", "vref", "vpageref", "prettyref",
"eqref", 0 };
/*!
* natbib commands.
@ -1125,6 +1127,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
Context & context)
{
Layout_ptr newlayout;
// store the current selectlanguage to be used after \foreignlanguage
string selectlang;
// Store the latest bibliographystyle (needed for bibtex inset)
string bibliographystyle;
bool const use_natbib = used_packages.find("natbib") != used_packages.end();
@ -2052,6 +2056,27 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
eat_whitespace(p, os, context, false);
}
else if (t.cs() == "selectlanguage") {
context.check_layout(os);
// save the language for the case that a \foreignlanguage is used
selectlang = subst(p.verbatim_item(), "\n", " ");
os << "\\lang " << selectlang << "\n";
}
else if (t.cs() == "foreignlanguage") {
context.check_layout(os);
os << "\n\\lang " << subst(p.verbatim_item(), "\n", " ") << "\n";
os << subst(p.verbatim_item(), "\n", " ");
// set back to last selectlanguage
os << "\n\\lang " << selectlang << "\n";
}
else if (t.cs() == "inputencoding")
// write nothing because this is done by LyX using the "\lang"
// information given by selectlanguage and foreignlanguage
subst(p.verbatim_item(), "\n", " ");
else if (t.cs() == "LyX" || t.cs() == "TeX"
|| t.cs() == "LaTeX") {
context.check_layout(os);

View File

@ -81,9 +81,10 @@ What's new
* DOCUMENT INPUT/OUTPUT
- tex2lyx imports the LaTeX-command \newline
- tex2lyx imports framed and shaded notes
- tex2lyx:
- import the LaTeX-command \newline
- import framed and shaded notes
- support for multi-language LaTeX-files
** Bug fixes:
@ -106,6 +107,9 @@ What's new
- If there exists a local layout file, use the local copy instead of the
system one.
- tex2lyx support for the LaTeX-commands \selectlanguage, \foreignlanguage,
and \inputencoding (bug 27).
* USER INTERFACE: