]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/text.cpp
* Doxy: polish html output.
[lyx.git] / src / tex2lyx / text.cpp
index f49012a55941e2760996d1f3ee760a593ce798e6..fbc89d3f0bb731781048afce155141611c8a65fa 100644 (file)
@@ -3,8 +3,9 @@
  * 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 Jean-Marc Lasgouttes
+ * \author Uwe Stöhr
  *
  * Full author contact details are available in file CREDITS.
  */
 #include "Layout.h"
 #include "Length.h"
 
-#include "support/lstrings.h"
 #include "support/convert.h"
+#include "support/FileName.h"
 #include "support/filetools.h"
-
-#include <boost/tuple/tuple.hpp>
+#include "support/lstrings.h"
 
 #include <iostream>
 #include <map>
 #include <sstream>
 #include <vector>
 
-using std::cerr;
-using std::endl;
-
-using std::map;
-using std::ostream;
-using std::ostringstream;
-using std::istringstream;
-using std::string;
-using std::vector;
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
-using support::addExtension;
-using support::changeExtension;
-using support::FileName;
-using support::makeAbsPath;
-using support::makeRelPath;
-using support::rtrim;
-using support::suffixIs;
-using support::contains;
-using support::subst;
-
 
 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
                Context const & context)
@@ -104,7 +87,7 @@ string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
 }
 
 
-char const * const known_latex_commands[] = { "ref", "cite", "label", "href",
+char const * const known_latex_commands[] = { "ref", "cite", "nocite", "label",
  "index", "printindex", "pageref", "url", "vref", "vpageref", "prettyref",
  "eqref", 0 };
 
@@ -133,19 +116,23 @@ char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
 "citefield", "citetitle", "cite*", 0 };
 
 /// LaTeX names for quotes
-char const * const known_quotes[] = { "glqq", "grqq", "quotedblbase",
-"textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
+char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
+"guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
+"quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
+"guilsinglleft", "frq", "guilsinglright", 0};
 
 /// the same as known_quotes with .lyx names
-char const * const known_coded_quotes[] = { "gld", "grd", "gld",
-"grd", "gls", "fls", "frs", 0};
+char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
+"ald", "ald", "ald", "gls", "gld", "els", "els", "grd",
+"gld", "grd", "gls", "ers", "fls",
+"fls", "frs", "frs", 0};
 
 /// LaTeX names for font sizes
 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
 
 /// the same as known_sizes with .lyx names
-char const * const known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
+char const * const known_coded_sizes[] = { "default", "tiny", "scriptsize", "footnotesize",
 "small", "normal", "large", "larger", "largest",  "huge", "giant", 0};
 
 /// LaTeX 2.09 names for font families
@@ -262,7 +249,7 @@ bool splitLatexLength(string const & len, string & value, string & unit)
        if (contains(len, '\\'))
                unit = trim(string(len, i));
        else
-               unit = support::ascii_lowercase(trim(string(len, i)));
+               unit = ascii_lowercase(trim(string(len, i)));
        return true;
 }
 
@@ -434,7 +421,7 @@ void handle_comment(ostream & os, string const & s, Context & context)
 }
 
 
-class isLayout : public std::unary_function<LayoutPtr, bool> {
+class isLayout : public unary_function<LayoutPtr, bool> {
 public:
        isLayout(string const name) : name_(name) {}
        bool operator()(LayoutPtr const & ptr) const {
@@ -452,7 +439,7 @@ LayoutPtr findLayout(TextClass const & textclass,
        TextClass::const_iterator end = textclass.end();
 
        TextClass::const_iterator
-               it = std::find_if(beg, end, isLayout(name));
+               it = find_if(beg, end, isLayout(name));
 
        return (it == end) ? LayoutPtr() : *it;
 }
@@ -787,6 +774,26 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                p.skip_spaces();
        }
 
+       else if (name == "framed") {
+               eat_whitespace(p, os, parent_context, false);
+               parent_context.check_layout(os);
+               begin_inset(os, "Note Framed\n");
+               os << "status open\n";
+               parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
+               end_inset(os);
+               p.skip_spaces();
+       }
+
+       else if (name == "shaded") {
+               eat_whitespace(p, os, parent_context, false);
+               parent_context.check_layout(os);
+               begin_inset(os, "Note Shaded\n");
+               os << "status open\n";
+               parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
+               end_inset(os);
+               p.skip_spaces();
+       }
+
        else if (!parent_context.new_layout_allowed)
                parse_unknown_environment(p, name, os, FLAG_END, outer,
                                          parent_context);
@@ -976,25 +983,26 @@ void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
 
 
 /// get the arguments of a natbib or jurabib citation command
-std::pair<string, string> getCiteArguments(Parser & p, bool natbibOrder)
+void get_cite_arguments(Parser & p, bool natbibOrder,
+       string & before, string & after)
 {
        // We need to distinguish "" and "[]", so we can't use p.getOpt().
 
        // text before the citation
-       string before;
+       before.clear();
        // text after the citation
-       string after = p.getFullOpt();
+       after = p.getFullOpt();
 
        if (!after.empty()) {
                before = p.getFullOpt();
                if (natbibOrder && !before.empty())
-                       std::swap(before, after);
+                       swap(before, after);
        }
-       return std::make_pair(before, after);
 }
 
 
-/// Convert filenames with TeX macros and/or quotes to something LyX can understand
+/// Convert filenames with TeX macros and/or quotes to something LyX
+/// can understand
 string const normalize_filename(string const & name)
 {
        Parser p(trim(name, "\""));
@@ -1022,8 +1030,10 @@ string const normalize_filename(string const & name)
 /// convention (relative to .lyx file) if it is relative
 void fix_relative_filename(string & name)
 {
-       if (lyx::support::absolutePath(name))
+       FileName fname(name);
+       if (fname.isAbsolute())
                return;
+
        // FIXME UNICODE encoding of name may be wrong (makeAbsPath expects
        // utf8)
        name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFilename()),
@@ -1106,6 +1116,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();
@@ -1167,8 +1179,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                // extended to other quotes, but is not so easy (a
                // left english quote is the same as a right german
                // quote...)
-               else if (t.asInput() == "`"
-                        && p.next_token().asInput() == "`") {
+               else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
                        context.check_layout(os);
                        begin_inset(os, "Quotes ");
                        os << "eld";
@@ -1176,8 +1187,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        p.get_token();
                        skip_braces(p);
                }
-               else if (t.asInput() == "'"
-                        && p.next_token().asInput() == "'") {
+               else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
                        context.check_layout(os);
                        begin_inset(os, "Quotes ");
                        os << "erd";
@@ -1186,6 +1196,24 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        skip_braces(p);
                }
 
+               else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
+                       context.check_layout(os);
+                       begin_inset(os, "Quotes ");
+                       os << "ald";
+                       end_inset(os);
+                       p.get_token();
+                       skip_braces(p);
+               }
+
+               else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
+                       context.check_layout(os);
+                       begin_inset(os, "Quotes ");
+                       os << "ard";
+                       end_inset(os);
+                       p.get_token();
+                       skip_braces(p);
+               }
+
                else if (t.asInput() == "<"
                         && p.next_token().asInput() == "<" && noweb_mode) {
                        p.get_token();
@@ -1629,8 +1657,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                numberOfbbOptions++;
                        if (numberOfbbOptions == 4)
                                os << "\tBoundingBox "
-                                  << opts["bbllx"] << opts["bblly"]
-                                  << opts["bburx"] << opts["bbury"] << '\n';
+                                  << opts["bbllx"] << " " << opts["bblly"] << " "
+                                  << opts["bburx"] << " " << opts["bbury"] << '\n';
                        else if (numberOfbbOptions > 0)
                                cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
                        numberOfbbOptions = 0;
@@ -1640,7 +1668,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                numberOfbbOptions++;
                        if (numberOfbbOptions == 2)
                                os << "\tBoundingBox 0bp 0bp "
-                                  << opts["natwidth"] << opts["natheight"] << '\n';
+                                  << opts["natwidth"] << " " << opts["natheight"] << '\n';
                        else if (numberOfbbOptions > 0)
                                cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
                        ostringstream special;
@@ -1857,8 +1885,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        string before;
                        // text after the citation
                        string after;
+                       get_cite_arguments(p, true, before, after);
 
-                       boost::tie(before, after) = getCiteArguments(p, true);
                        if (command == "\\cite") {
                                // \cite without optional argument means
                                // \citet, \cite with at least one optional
@@ -1888,10 +1916,10 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        string const command = '\\' + t.cs();
                        char argumentOrder = '\0';
                        vector<string> const & options = used_packages["jurabib"];
-                       if (std::find(options.begin(), options.end(),
+                       if (find(options.begin(), options.end(),
                                      "natbiborder") != options.end())
                                argumentOrder = 'n';
-                       else if (std::find(options.begin(), options.end(),
+                       else if (find(options.begin(), options.end(),
                                           "jurabiborder") != options.end())
                                argumentOrder = 'j';
 
@@ -1899,9 +1927,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        string before;
                        // text after the citation
                        string after;
+                       get_cite_arguments(p, argumentOrder != 'j', before, after);
 
-                       boost::tie(before, after) =
-                               getCiteArguments(p, argumentOrder != 'j');
                        string const citation = p.verbatim_item();
                        if (!before.empty() && argumentOrder == '\0') {
                                cerr << "Warning: Assuming argument order "
@@ -2033,6 +2060,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);
@@ -2187,6 +2235,39 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        }
                }
 
+               else if (t.cs() == "newline" ||
+                       t.cs() == "linebreak") {
+                       context.check_layout(os);
+                       os << "\n\\" << t.cs() << "\n";
+                       skip_braces(p); // eat {}
+               }
+
+               else if (t.cs() == "href") {
+                       context.check_layout(os);
+                       begin_inset(os, "CommandInset ");
+                       os << t.cs() << "\n";
+                       os << "LatexCommand " << t.cs() << "\n";
+                       bool erase = false;
+                       size_t pos;
+                       // the first argument is "type:target", "type:" is optional
+                       // the second argument the name
+                       string href_target = subst(p.verbatim_item(), "\n", " ");
+                       string href_name = subst(p.verbatim_item(), "\n", " ");
+                       string href_type;
+                       // serach for the ":" to divide type from target
+                       if ((pos = href_target.find(":", 0)) != string::npos){
+                               href_type = href_target;
+                               href_type.erase(pos + 1, href_type.length());
+                               href_target.erase(0, pos + 1);
+                           erase = true;                                                                                       
+                       }
+                       os << "name " << '"' << href_name << '"' << "\n";
+                       os << "target " << '"' << href_target << '"' << "\n";
+                       if(erase)
+                               os << "type " << '"' << href_type << '"' << "\n";
+                       end_inset(os);
+               }
+
                else if (t.cs() == "input" || t.cs() == "include"
                         || t.cs() == "verbatiminput") {
                        string name = '\\' + t.cs();
@@ -2290,17 +2371,18 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                }
 
                else if (t.cs() == "newpage" ||
-                        t.cs() == "clearpage" ||
-                        t.cs() == "cleardoublepage") {
+                       t.cs() == "pagebreak" ||
+                       t.cs() == "clearpage" ||
+                       t.cs() == "cleardoublepage") {
                        context.check_layout(os);
-                       // FIXME: what about \\pagebreak?
                        os << "\n\\" << t.cs() << "\n";
                        skip_braces(p); // eat {}
                }
 
                else if (t.cs() == "newcommand" ||
                         t.cs() == "providecommand" ||
-                        t.cs() == "renewcommand") {
+                        t.cs() == "renewcommand" ||
+                        t.cs() == "newlyxcommand") {
                        // these could be handled by parse_command(), but
                        // we need to call add_known_command() here.
                        string name = t.asInput();
@@ -2311,11 +2393,18 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        }
                        string const command = p.verbatim_item();
                        string const opt1 = p.getOpt();
-                       string const opt2 = p.getFullOpt();
-                       add_known_command(command, opt1, !opt2.empty());
-                       string const ert = name + '{' + command + '}' +
-                                          opt1 + opt2 +
-                                          '{' + p.verbatim_item() + '}';
+                       string optionals;
+                       unsigned optionalsNum = 0;
+                       while (true) {
+                               string const opt = p.getFullOpt();
+                               if (opt.empty())
+                                       break;
+                               optionalsNum++;
+                               optionals += opt;
+                       }
+                       add_known_command(command, opt1, optionalsNum);
+                       string const ert = name + '{' + command + '}' + opt1
+                               + optionals + '{' + p.verbatim_item() + '}';
 
                        context.check_layout(os);
                        begin_inset(os, "FormulaMacro");