]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/text.cpp
tex2lyx/text.cpp: only whitespace
[lyx.git] / src / tex2lyx / text.cpp
index e1559f031df5c4552d09dac2a1f7b3a0f3d7f7cb..3e1594595deff7d4bf58e042528c8cb3745bc301 100644 (file)
 #include "Layout.h"
 #include "Length.h"
 
-#include "support/lstrings.h"
+#include "support/lassert.h"
 #include "support/convert.h"
+#include "support/FileName.h"
 #include "support/filetools.h"
+#include "support/lstrings.h"
 
+#include <algorithm>
 #include <iostream>
 #include <map>
 #include <sstream>
@@ -53,8 +56,8 @@ void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
                Context & context)
 {
        Context newcontext(context);
-       // Don't inherit the extra stuff
-       newcontext.extra_stuff.clear();
+       // Don't inherit the paragraph-level extra stuff
+       newcontext.par_extra_stuff.clear();
        parse_text(p, os, flags, outer, newcontext);
        // Make sure that we don't create invalid .lyx files
        context.need_layout = newcontext.need_layout;
@@ -79,7 +82,7 @@ string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
        newcontext.need_end_layout = false;
        newcontext.new_layout_allowed = false;
        // Avoid warning by Context::~Context()
-       newcontext.extra_stuff.clear();
+       newcontext.par_extra_stuff.clear();
        ostringstream os;
        parse_text_snippet(p, os, flags, outer, newcontext);
        return os.str();
@@ -130,9 +133,9 @@ char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
 
-/// the same as known_sizes with .lyx names
+/// the same as known_sizes with .lyx names plus a default entry
 char const * const known_coded_sizes[] = { "default", "tiny", "scriptsize", "footnotesize",
-"small", "normal", "large", "larger", "largest",  "huge", "giant", 0};
+"small", "normal", "large", "larger", "largest", "huge", "giant", 0};
 
 /// LaTeX 2.09 names for font families
 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
@@ -248,7 +251,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;
 }
 
@@ -420,36 +423,37 @@ void handle_comment(ostream & os, string const & s, Context & context)
 }
 
 
-class isLayout : public unary_function<LayoutPtr, bool> {
-public:
-       isLayout(string const name) : name_(name) {}
-       bool operator()(LayoutPtr const & ptr) const {
-               return ptr->latexname() == name_;
-       }
-private:
-       string const name_;
-};
-
-
-LayoutPtr findLayout(TextClass const & textclass,
-                        string const & name)
+Layout const * findLayout(TextClass const & textclass, string const & name)
 {
-       TextClass::const_iterator beg = textclass.begin();
-       TextClass::const_iterator end = textclass.end();
-
-       TextClass::const_iterator
-               it = find_if(beg, end, isLayout(name));
-
-       return (it == end) ? LayoutPtr() : *it;
+       DocumentClass::const_iterator lit = textclass.begin();
+       DocumentClass::const_iterator len = textclass.end();
+       for (; lit != len; ++lit)
+               if (lit->latexname() == name)
+                       return &*lit;
+       return 0;
 }
 
 
 void eat_whitespace(Parser &, ostream &, Context &, bool);
 
 
+Layout * captionlayout()
+{
+       static Layout * lay = 0;
+       if (!lay) {
+               lay = new Layout;
+               lay->name_ = from_ascii("Caption");
+               lay->latexname_ = "caption";
+               lay->latextype = LATEX_COMMAND;
+               lay->optionalargs = 1;
+       }
+       return lay;
+}
+
+
 void output_command_layout(ostream & os, Parser & p, bool outer,
                           Context & parent_context,
-                          LayoutPtr newlayout)
+                          Layout const * newlayout)
 {
        parent_context.check_end_layout(os);
        Context context(true, parent_context.textclass, newlayout,
@@ -705,7 +709,7 @@ void parse_unknown_environment(Parser & p, string const & name, ostream & os,
 void parse_environment(Parser & p, ostream & os, bool outer,
                       Context & parent_context)
 {
-       LayoutPtr newlayout;
+       Layout const * newlayout;
        string const name = p.getArg('{', '}');
        const bool is_starred = suffixIs(name, '*');
        string const unstarred_name = rtrim(name, "*");
@@ -761,6 +765,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
                end_inset(os);
                p.skip_spaces();
+               skip_braces(p); // eat {} that might by set by LyX behind comments
        }
 
        else if (name == "lyxgreyedout") {
@@ -797,23 +802,39 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                parse_unknown_environment(p, name, os, FLAG_END, outer,
                                          parent_context);
 
-       // Alignment settings
-       else if (name == "center" || name == "flushleft" || name == "flushright" ||
-                name == "centering" || name == "raggedright" || name == "raggedleft") {
+       // Alignment and spacing settings
+       // FIXME (bug xxxx): These settings can span multiple paragraphs and
+       //                                       therefore are totally broken!
+       // Note that \centering, raggedright, and raggedleft cannot be handled, as
+       // they are commands not environments. They are furthermore switches that
+       // can be ended by another switches, but also by commands like \footnote or
+       // \parbox. So the only safe way is to leave them untouched.
+       else if (name == "center" || name == "centering" ||
+                name == "flushleft" || name == "flushright" ||
+                name == "singlespace" || name == "onehalfspace" ||
+                name == "doublespace" || name == "spacing") {
                eat_whitespace(p, os, parent_context, false);
                // We must begin a new paragraph if not already done
                if (! parent_context.atParagraphStart()) {
                        parent_context.check_end_layout(os);
                        parent_context.new_paragraph(os);
                }
-               if (name == "flushleft" || name == "raggedright")
+               if (name == "flushleft")
                        parent_context.add_extra_stuff("\\align left\n");
-               else if (name == "flushright" || name == "raggedleft")
+               else if (name == "flushright")
                        parent_context.add_extra_stuff("\\align right\n");
-               else
+               else if (name == "center" || name == "centering")
                        parent_context.add_extra_stuff("\\align center\n");
+               else if (name == "singlespace")
+                       parent_context.add_extra_stuff("\\paragraph_spacing single\n");
+               else if (name == "onehalfspace")
+                       parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
+               else if (name == "doublespace")
+                       parent_context.add_extra_stuff("\\paragraph_spacing double\n");
+               else if (name == "spacing")
+                       parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
                parse_text(p, os, FLAG_END, outer, parent_context);
-               // Just in case the environment is empty ..
+               // Just in case the environment is empty
                parent_context.extra_stuff.erase();
                // We must begin a new paragraph to reset the alignment
                parent_context.new_paragraph(os);
@@ -821,7 +842,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
        }
 
        // The single '=' is meant here.
-       else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
+       else if ((newlayout = findLayout(parent_context.textclass, name)) &&
                  newlayout->isEnvironment()) {
                eat_whitespace(p, os, parent_context, false);
                Context context(true, parent_context.textclass, newlayout,
@@ -835,8 +856,8 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                parent_context.check_end_layout(os);
                switch (context.layout->latextype) {
                case  LATEX_LIST_ENVIRONMENT:
-                       context.extra_stuff = "\\labelwidthstring "
-                               + p.verbatim_item() + '\n';
+                       context.add_par_extra_stuff("\\labelwidthstring "
+                                                   + p.verbatim_item() + '\n');
                        p.skip_spaces();
                        break;
                case  LATEX_BIB_ENVIRONMENT:
@@ -914,7 +935,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
 /// parses a comment and outputs it to \p os.
 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
 {
-       BOOST_ASSERT(t.cat() == catComment);
+       LASSERT(t.cat() == catComment, return);
        if (!t.cs().empty()) {
                context.check_layout(os);
                handle_comment(os, '%' + t.cs(), context);
@@ -1029,8 +1050,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()),
@@ -1072,7 +1095,7 @@ void parse_noweb(Parser & p, ostream & os, Context & context)
        // always must be in an own paragraph.
        context.new_paragraph(os);
        Context newcontext(true, context.textclass,
-               context.textclass[from_ascii("Scrap")]);
+               &context.textclass[from_ascii("Scrap")]);
        newcontext.check_layout(os);
        os << name;
        while (p.good()) {
@@ -1112,7 +1135,7 @@ void parse_noweb(Parser & p, ostream & os, Context & context)
 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                Context & context)
 {
-       LayoutPtr newlayout;
+       Layout const * newlayout = 0;
        // store the current selectlanguage to be used after \foreignlanguage
        string selectlang;
        // Store the latest bibliographystyle (needed for bibtex inset)
@@ -1494,11 +1517,11 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
 
                else if (t.cs() == "noindent") {
                        p.skip_spaces();
-                       context.add_extra_stuff("\\noindent\n");
+                       context.add_par_extra_stuff("\\noindent\n");
                }
 
                else if (t.cs() == "appendix") {
-                       context.add_extra_stuff("\\start_of_appendix\n");
+                       context.add_par_extra_stuff("\\start_of_appendix\n");
                        // We need to start a new paragraph. Otherwise the
                        // appendix in 'bla\appendix\chapter{' would start
                        // too late.
@@ -1520,32 +1543,53 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        eat_whitespace(p, os, context, true);
                }
 
+               // Starred section headings
                // Must attempt to parse "Section*" before "Section".
                else if ((p.next_token().asInput() == "*") &&
                         context.new_layout_allowed &&
-                        // The single '=' is meant here.
-                        (newlayout = findLayout(context.textclass,
-                                                t.cs() + '*')).get() &&
+                        (newlayout = findLayout(context.textclass, t.cs() + '*')) &&
                         newlayout->isCommand()) {
+                       TeXFont const oldFont = context.font;
+                       // save the current font size
+                       string const size = oldFont.size;
+                       // reset the font size to default, because the font size switches don't
+                       // affect section headings and the like
+                       context.font.size = known_coded_sizes[0];
+                       output_font_change(os, oldFont, context.font);
+                       // write the layout
                        p.get_token();
                        output_command_layout(os, p, outer, context, newlayout);
+                       // set the font size to the original value
+                       context.font.size = size;
+                       output_font_change(os, oldFont, context.font);
                        p.skip_spaces();
                }
 
-               // The single '=' is meant here.
+               // Section headings and the like
                else if (context.new_layout_allowed &&
-                        (newlayout = findLayout(context.textclass, t.cs())).get() &&
+                        (newlayout = findLayout(context.textclass, t.cs())) &&
                         newlayout->isCommand()) {
+                       TeXFont const oldFont = context.font;
+                       // save the current font size
+                       string const size = oldFont.size;
+                       // reset the font size to default, because the font size switches don't
+                       // affect section headings and the like
+                       context.font.size = known_coded_sizes[0];
+                       output_font_change(os, oldFont, context.font);
+                       // write the layout
                        output_command_layout(os, p, outer, context, newlayout);
+                       // set the font size to the original value
+                       context.font.size = size;
+                       output_font_change(os, oldFont, context.font);
                        p.skip_spaces();
                }
 
                // Special handling for \caption
                // FIXME: remove this when InsetCaption is supported.
                else if (context.new_layout_allowed &&
-                        t.cs() == captionlayout->latexname()) {
+                        t.cs() == captionlayout()->latexname()) {
                        output_command_layout(os, p, outer, context, 
-                                             captionlayout);
+                                             captionlayout());
                        p.skip_spaces();
                }
 
@@ -1838,6 +1882,23 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                eat_whitespace(p, os, context, false);
                }
 
+               else if (t.cs() == "textcolor") {
+                       // scheme is \textcolor{color name}{text}
+                       string const color = p.verbatim_item();
+                       // we only support the predefined colors of the color package
+                       if (color == "black" || color == "blue" || color == "cyan"
+                               || color == "green" || color == "magenta" || color == "red"
+                               || color == "white" || color == "yellow") {
+                                       context.check_layout(os);
+                                       os << "\n\\color " << color << "\n";
+                                       parse_text_snippet(p, os, FLAG_ITEM, outer, context);
+                                       context.check_layout(os);
+                                       os << "\n\\color inherit\n";
+                       } else
+                               // for custom defined colors
+                               handle_ert(os, t.asInput() + "{" + color + "}", context);
+               }
+
                else if (t.cs() == "underbar") {
                        // Do NOT handle \underline.
                        // \underbar cuts through y, g, q, p etc.,
@@ -1857,6 +1918,11 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        os << "\n\\" << t.cs() << " default\n";
                }
 
+               else if (t.cs() == "lyxline") {
+                       context.check_layout(os);
+                       os << "\\lyxline";
+               }
+
                else if (use_natbib &&
                         is_known(t.cs(), known_natbib_commands) &&
                         ((t.cs() != "citefullauthor" &&
@@ -1975,7 +2041,9 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        char const * const * where = is_known(t.cs(), known_sizes);
                        context.check_layout(os);
                        TeXFont const oldFont = context.font;
-                       context.font.size = known_coded_sizes[where - known_sizes];
+                       // the font size index differs by 1, because the known_coded_sizes
+                       // has additionally a "default" entry
+                       context.font.size = known_coded_sizes[where - known_sizes + 1];
                        output_font_change(os, oldFont, context.font);
                        eat_whitespace(p, os, context, false);
                }
@@ -2239,32 +2307,6 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        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();
@@ -2336,6 +2378,19 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
 
                else if (t.cs() == "parbox")
                        parse_box(p, os, FLAG_ITEM, outer, context, true);
+               
+               //\makebox() is part of the picture environment and different from \makebox{}
+               //\makebox{} will be parsed by parse_box when bug 2956 is fixed
+               else if (t.cs() == "makebox") {
+                       string arg = t.asInput();
+                       if (p.next_token().character() == '(')
+                               //the syntax is: \makebox(x,y)[position]{content}
+                               arg += p.getFullParentheseArg();
+                       else
+                               //the syntax is: \makebox[width][position]{content}
+                               arg += p.getFullOpt();
+                       handle_ert(os, arg + p.getFullOpt(), context);
+               }
 
                else if (t.cs() == "smallskip" ||
                         t.cs() == "medskip" ||
@@ -2351,7 +2406,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                else if (is_known(t.cs(), known_spaces)) {
                        char const * const * where = is_known(t.cs(), known_spaces);
                        context.check_layout(os);
-                       begin_inset(os, "InsetSpace ");
+                       os << "\\InsetSpace ";
                        os << '\\' << known_coded_spaces[where - known_spaces]
                           << '\n';
                        // LaTeX swallows whitespace after all spaces except
@@ -2400,7 +2455,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        os << "\n" << ert;
                        end_inset(os);
                }
-
+               
                else if (t.cs() == "vspace") {
                        bool starred = false;
                        if (p.next_token().asInput() == "*") {