]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/preamble.C
Removed all redundant using directives from the source.
[lyx.git] / src / tex2lyx / preamble.C
index e94968c8d49929c3a98fd4beb4d9df052a070081..777aec4695853857ba32ef7ca280253c63b52537 100644 (file)
@@ -1,29 +1,41 @@
-/** The .tex to .lyx converter
-    \author André Pönitz (2003)
+/**
+ * \file preamble.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
  */
 
 // {[(
 
 #include <config.h>
 
-#include "Lsstream.h"
 #include "tex2lyx.h"
 
+#include "layout.h"
+#include "lyxtextclass.h"
+#include "lyxlex.h"
+#include "support/filetools.h"
+
 #include <algorithm>
 #include <iostream>
+#include <sstream>
 #include <string>
 #include <vector>
+#include <map>
 
-using std::cerr;
-using std::endl;
-using std::getline;
-using std::istream;
 using std::istringstream;
 using std::ostream;
 using std::ostringstream;
 using std::string;
 using std::vector;
 
+using lyx::support::LibFileSearch;
+
+// special columntypes
+extern std::map<char, int> special_columns;
 
 namespace {
 
@@ -45,7 +57,7 @@ char const * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
 // some ugly stuff
 ostringstream h_preamble;
 string h_textclass               = "article";
-string h_options                 = "";
+string h_options                 = string();
 string h_language                = "english";
 string h_inputencoding           = "latin1";
 string h_fontscheme              = "default";
@@ -125,14 +137,15 @@ void handle_package(string const & name, string const & options)
 
 
 
-void end_preamble(ostream & os)
+void end_preamble(ostream & os, LyXTextClass const & /*textclass*/)
 {
-       os << "# tex2lyx 0.0.2 created this file\n"
-          << "\\lyxformat 222\n"
+       os << "#LyX file created by  tex2lyx 0.1.2 \n"
+          << "\\lyxformat 225\n"
           << "\\textclass " << h_textclass << "\n"
-          << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n"
-          << "\\options " << h_options << "\n"
-          << "\\language " << h_language << "\n"
+          << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
+       if (h_options.size())
+          os << "\\options " << h_options << "\n";
+       os << "\\language " << h_language << "\n"
           << "\\inputencoding " << h_inputencoding << "\n"
           << "\\fontscheme " << h_fontscheme << "\n"
           << "\\graphics " << h_graphics << "\n"
@@ -154,16 +167,19 @@ void end_preamble(ostream & os)
           << "\\papercolumns " << h_papercolumns << "\n"
           << "\\papersides " << h_papersides << "\n"
           << "\\paperpagestyle " << h_paperpagestyle << "\n"
-          << "\\tracking_changes " << h_tracking_changes << "\n";
+          << "\\tracking_changes " << h_tracking_changes << "\n"
+          << "\\end_header\n";
 }
 
-
 } // anonymous namespace
 
-void parse_preamble(Parser & p, ostream & os)
+LyXTextClass const parse_preamble(Parser & p, ostream & os)
 {
+       // initialize fixed types
+       special_columns['D'] = 3;
+
        while (p.good()) {
-               Token const & t = p.getToken();
+               Token const & t = p.get_token();
 
 #ifdef FILEDEBUG
                cerr << "t: " << t << " flags: " << flags << "\n";
@@ -191,7 +207,7 @@ void parse_preamble(Parser & p, ostream & os)
                        handle_comment(p);
 
                else if (t.cs() == "pagestyle")
-                       h_paperpagestyle == p.verbatimItem();
+                       h_paperpagestyle == p.verbatim_item();
 
                else if (t.cs() == "makeatletter") {
                        p.setCatCode('@', catLetter);
@@ -206,13 +222,13 @@ void parse_preamble(Parser & p, ostream & os)
                else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
                            || t.cs() == "providecommand") {
                        bool star = false;
-                       if (p.nextToken().character() == '*') {
-                               p.getToken();
+                       if (p.next_token().character() == '*') {
+                               p.get_token();
                                star = true;
                        }
-                       string const name = p.verbatimItem();
+                       string const name = p.verbatim_item();
                        string const opts = p.getOpt();
-                       string const body = p.verbatimItem();
+                       string const body = p.verbatim_item();
                        // only non-lyxspecific stuff
                        if (name != "\\noun "
                                  && name != "\\tabularnewline "
@@ -267,8 +283,8 @@ void parse_preamble(Parser & p, ostream & os)
                        ss << "\\newenvironment{" << name << "}";
                        ss << p.getOpt();
                        ss << p.getOpt();
-                       ss << '{' << p.verbatimItem() << '}';
-                       ss << '{' << p.verbatimItem() << '}';
+                       ss << '{' << p.verbatim_item() << '}';
+                       ss << '{' << p.verbatim_item() << '}';
                        ss << '\n';
                        if (name != "lyxcode" && name != "lyxlist"
                                        && name != "lyxrightadress" && name != "lyxaddress")
@@ -276,10 +292,27 @@ void parse_preamble(Parser & p, ostream & os)
                }
 
                else if (t.cs() == "def") {
-                       string name = p.getToken().cs();
-                       while (p.nextToken().cat() != catBegin)
-                               name += p.getToken().asString();
-                       h_preamble << "\\def\\" << name << '{' << p.verbatimItem() << "}\n";
+                       string name = p.get_token().cs();
+                       while (p.next_token().cat() != catBegin)
+                               name += p.get_token().asString();
+                       h_preamble << "\\def\\" << name << '{' << p.verbatim_item() << "}\n";
+               }
+
+               else if (t.cs() == "newcolumntype") {
+                       string const name = p.getArg('{', '}');
+                       trim(name);
+                       int nargs = 0;
+                       string opts = p.getOpt();
+                       if (opts.size()) {
+                               istringstream is(string(opts, 1));
+                               //cerr << "opt: " << is.str() << "\n";
+                               is >> nargs;
+                       }
+                       special_columns[name[0]] = nargs;
+                       h_preamble << "\\newcolumntype{" << name << "}";
+                       if (nargs)
+                               h_preamble << "[" << nargs << "]";
+                       h_preamble << "{" << p.verbatim_item() << "}\n";
                }
 
                else if (t.cs() == "setcounter") {
@@ -294,14 +327,14 @@ void parse_preamble(Parser & p, ostream & os)
                }
 
                else if (t.cs() == "setlength") {
-                       string const name = p.verbatimItem();
-                       string const content = p.verbatimItem();
+                       string const name = p.verbatim_item();
+                       string const content = p.verbatim_item();
                        if (name == "parskip")
                                h_paragraph_separation = "skip";
                        else if (name == "parindent")
                                h_paragraph_separation = "skip";
                        else
-                               h_preamble << "\\setlength{" + name + "}{" + content + "}\n";
+                               h_preamble << "\\setlength{" << name << "}{" << content << "}\n";
                }
 
                else if (t.cs() == "par")
@@ -309,18 +342,19 @@ void parse_preamble(Parser & p, ostream & os)
 
                else if (t.cs() == "begin") {
                        string const name = p.getArg('{', '}');
-                       if (name == "document") {
-                               end_preamble(os);
-                               os << "\n\n\\layout Standard\n\n";
-                               return;
-                       }
+                       if (name == "document")
+                               break;
                        h_preamble << "\\begin{" << name << "}";
                }
 
                else if (t.cs().size())
                        h_preamble << '\\' << t.cs() << ' ';
        }
-}
 
+       LyXTextClass textclass;
+       textclass.Read(LibFileSearch("layouts", h_textclass, "layout"));
+       end_preamble(os, textclass);
+       return textclass;
+}
 
 // }])