]> git.lyx.org Git - features.git/commitdiff
preamble.cpp, text.cpp:
authorUwe Stöhr <uwestoehr@web.de>
Tue, 4 Dec 2007 20:09:40 +0000 (20:09 +0000)
committerUwe Stöhr <uwestoehr@web.de>
Tue, 4 Dec 2007 20:09:40 +0000 (20:09 +0000)
 - fix bug 27
 - support for multi-language files
 - code cleanups

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21950 a592a061-630c-0410-9148-cb99ea01b6c8

src/tex2lyx/preamble.cpp
src/tex2lyx/text.cpp

index bf0af9d8a73a30e6ca3f450d2a1c68c14676e94e..af09708bee35622b65a641d468f6f6dc8d31cfe8 100644 (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.
  */
@@ -72,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";
@@ -100,13 +101,19 @@ void handle_opt(vector<string> & opts, char const * const * what, string & targe
        if (opts.empty())
                return;
 
+       // 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) {
-               vector<string>::iterator it = find(opts.begin(), opts.end(), *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;
                }
        }
 }
@@ -128,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 += '=';
@@ -140,7 +147,7 @@ vector<string> split_options(string const & input)
        }
 
        if (!option.empty())
-               options.push_back(option);
+               options.push_back(trim(option));
 
        return options;
 }
@@ -188,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
@@ -237,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"
@@ -373,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";
index 66a17ca31152f03c9f43b72236b908690060ae46..c1ee6f764cda9e734d6e7d5fc7f2438f6d6dbb81 100644 (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.
  */
@@ -1125,6 +1126,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                Context & context)
 {
        LayoutPtr 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();
@@ -2051,6 +2054,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);