]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/text.C
hopefully fix tex2lyx linking.
[lyx.git] / src / tex2lyx / text.C
index a34222917a0968c41247ca17eeaa9fc22f0e8528..f7f1a02d28743121817d1c46bf802fd56bdece48 100644 (file)
 #include <sstream>
 #include <vector>
 
-using lyx::support::ChangeExtension;
-using lyx::support::MakeAbsPath;
-using lyx::support::MakeRelPath;
+
+namespace lyx {
+
+using lyx::support::changeExtension;
+using lyx::support::makeAbsPath;
+using lyx::support::makeRelPath;
 using lyx::support::rtrim;
 using lyx::support::suffixIs;
 using lyx::support::contains;
@@ -120,8 +123,9 @@ char const * const known_natbib_commands[] = { "cite", "citet", "citep",
  * No starred form other than "cite*" known.
  */
 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
-"citealt", "citealp", "citeauthor", "citeyear", "citeyearpar", "fullcite",
+"citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
 // jurabib commands not (yet) supported by LyX:
+// "fullcite",
 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
 // "footciteauthor", "footciteyear", "footciteyearpar",
 "citefield", "citetitle", "cite*", 0 };
@@ -198,6 +202,15 @@ char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
  */
 char const * const known_tex_extensions[] = {"tex", 0};
 
+/// spaces known by InsetSpace
+char const * const known_spaces[] = { " ", "space", ",", "thinspace", "quad",
+"qquad", "enspace", "enskip", "negthinspace", 0};
+
+/// the same as known_spaces with .lyx names
+char const * const known_coded_spaces[] = { "space{}", "space{}",
+"thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
+"negthinspace{}", 0};
+
 
 /// splits "x=z, y=b" into a map
 map<string, string> split_map(string const & s)
@@ -344,7 +357,7 @@ string find_file(string const & name, string const & path,
                // We don't use ChangeExtension() because it does the wrong
                // thing if name contains a dot.
                string const trial = name + '.' + (*what);
-               if (fs::exists(MakeAbsPath(trial, path)))
+               if (fs::exists(makeAbsPath(trial, path)))
                        return trial;
        }
        return string();
@@ -433,7 +446,7 @@ private:
 LyXLayout_ptr findLayout(LyXTextClass const & textclass,
                         string const & name)
 {
-       LyXTextClass::const_iterator beg  = textclass.begin();
+       LyXTextClass::const_iterator beg = textclass.begin();
        LyXTextClass::const_iterator end = textclass.end();
 
        LyXTextClass::const_iterator
@@ -536,6 +549,7 @@ void parse_arguments(string const & command,
                case required:
                        // This argument contains regular LaTeX
                        handle_ert(os, ert + '{', context);
+                       eat_whitespace(p, os, context, false);
                        parse_text(p, os, FLAG_ITEM, outer, context);
                        ert = "}";
                        break;
@@ -1004,12 +1018,74 @@ 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))
+       if (lyx::support::absolutePath(name))
                return;
-       name = MakeRelPath(MakeAbsPath(name, getMasterFilePath()),
+       name = makeRelPath(makeAbsPath(name, getMasterFilePath()),
                           getParentFilePath());
 }
 
+
+/// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
+void parse_noweb(Parser & p, ostream & os, Context & context)
+{
+       // assemble the rest of the keyword
+       string name("<<");
+       bool scrap = false;
+       while (p.good()) {
+               Token const & t = p.get_token();
+               if (t.asInput() == ">" && p.next_token().asInput() == ">") {
+                       name += ">>";
+                       p.get_token();
+                       scrap = (p.good() && p.next_token().asInput() == "=");
+                       if (scrap)
+                               name += p.get_token().asInput();
+                       break;
+               }
+               name += t.asInput();
+       }
+
+       if (!scrap || !context.new_layout_allowed ||
+           !context.textclass.hasLayout("Scrap")) {
+               cerr << "Warning: Could not interpret '" << name
+                    << "'. Ignoring it." << endl;
+               return;
+       }
+
+       context.check_end_layout(os);
+       Context newcontext(true, context.textclass, context.textclass["Scrap"]);
+       newcontext.check_layout(os);
+       os << name;
+       while (p.good()) {
+               Token const & t = p.get_token();
+               // We abuse the parser a bit, because this is no TeX syntax
+               // at all.
+               if (t.cat() == catEscape)
+                       os << subst(t.asInput(), "\\", "\n\\backslash\n");
+               else
+                       os << subst(t.asInput(), "\n", "\n\\newline\n");
+               // The scrap chunk is ended by an @ at the beginning of a line.
+               // After the @ the line may contain a comment and/or
+               // whitespace, but nothing else.
+               if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
+                   (p.next_token().cat() == catSpace ||
+                    p.next_token().cat() == catNewline ||
+                    p.next_token().cat() == catComment)) {
+                       while (p.good() && p.next_token().cat() == catSpace)
+                               os << p.get_token().asInput();
+                       if (p.next_token().cat() == catComment)
+                               // The comment includes a final '\n'
+                               os << p.get_token().asInput();
+                       else {
+                               if (p.next_token().cat() == catNewline)
+                                       p.get_token();
+                               os << '\n';
+                       }
+                       break;
+               }
+       }
+       newcontext.check_end_layout(os);
+}
+
 } // anonymous namespace
 
 
@@ -1097,9 +1173,28 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        skip_braces(p);
                }
 
+               else if (t.asInput() == "<"
+                        && p.next_token().asInput() == "<" && noweb_mode) {
+                       p.get_token();
+                       parse_noweb(p, os, context);
+               }
+
                else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
                        check_space(p, os, context);
 
+               else if (t.character() == '[' && noweb_mode &&
+                        p.next_token().character() == '[') {
+                       // These can contain underscores
+                       p.putback();
+                       string const s = p.getFullOpt() + ']';
+                       if (p.next_token().character() == ']')
+                               p.get_token();
+                       else
+                               cerr << "Warning: Inserting missing ']' in '"
+                                    << s << "'." << endl;
+                       handle_ert(os, s, context);
+               }
+
                else if (t.cat() == catLetter ||
                               t.cat() == catOther ||
                               t.cat() == catAlign ||
@@ -1384,7 +1479,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        string const path = getMasterFilePath();
                        // We want to preserve relative / absolute filenames,
                        // therefore path is only used for testing
-                       if (!fs::exists(MakeAbsPath(name, path))) {
+                       if (!fs::exists(makeAbsPath(name, path))) {
                                // The file extension is probably missing.
                                // Now try to find it out.
                                string const dvips_name =
@@ -1414,7 +1509,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                        name = pdftex_name;
                        }
 
-                       if (fs::exists(MakeAbsPath(name, path)))
+                       if (fs::exists(makeAbsPath(name, path)))
                                fix_relative_filename(name);
                        else
                                cerr << "Warning: Could not find graphics file '"
@@ -2036,7 +2131,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        // We want to preserve relative / absolute filenames,
                        // therefore path is only used for testing
                        if (t.cs() == "include" &&
-                           !fs::exists(MakeAbsPath(filename, path))) {
+                           !fs::exists(makeAbsPath(filename, path))) {
                                // The file extension is probably missing.
                                // Now try to find it out.
                                string const tex_name =
@@ -2045,14 +2140,14 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                if (!tex_name.empty())
                                        filename = tex_name;
                        }
-                       if (fs::exists(MakeAbsPath(filename, path))) {
+                       if (fs::exists(makeAbsPath(filename, path))) {
                                string const abstexname =
-                                       MakeAbsPath(filename, path);
+                                       makeAbsPath(filename, path);
                                string const abslyxname =
-                                       ChangeExtension(abstexname, ".lyx");
+                                       changeExtension(abstexname, ".lyx");
                                fix_relative_filename(filename);
                                string const lyxname =
-                                       ChangeExtension(filename, ".lyx");
+                                       changeExtension(filename, ".lyx");
                                if (t.cs() != "verbatiminput" &&
                                    tex2lyx(abstexname, abslyxname)) {
                                        os << name << '{' << lyxname << "}\n";
@@ -2102,6 +2197,25 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        skip_braces(p);
                }
 
+               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 << '\\' << known_coded_spaces[where - known_spaces]
+                          << '\n';
+                       // LaTeX swallows whitespace after all spaces except
+                       // "\\,". We have to do that here, too, because LyX
+                       // adds "{}" which would make the spaces significant.
+                       if (t.cs() !=  ",")
+                               eat_whitespace(p, os, context, false);
+                       // LyX adds "{}" after all spaces except "\\ " and
+                       // "\\,", so we have to remove "{}".
+                       // "\\,{}" is equivalent to "\\," in LaTeX, so we
+                       // remove the braces after "\\,", too.
+                       if (t.cs() != " ")
+                               skip_braces(p);
+               }
+
                else if (t.cs() == "newpage") {
                        context.check_layout(os);
                        // FIXME: what about \\clearpage and \\pagebreak?
@@ -2242,3 +2356,6 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
 }
 
 // }])
+
+
+} // namespace lyx