diff --git a/src/tex2lyx/ChangeLog b/src/tex2lyx/ChangeLog index abc8f1e42a..9520dad905 100644 --- a/src/tex2lyx/ChangeLog +++ b/src/tex2lyx/ChangeLog @@ -1,3 +1,15 @@ +2004-07-29 Georg Baum + + * preamble.C (end_preamble): change file format from 228 to 235 + * preamble.C (end_preamble): add use_bibtopic flag + * preamble.C (end_preamble): replace use_natbib with cite_engine + * preamble.C: change the paperpackage default from "default" to + "none". The old setting only worked by accident. + * tex2lyx.[Ch] (getMasterFilePath): new, needed for included graphics + * text.C (parse_environment): add sideways flag for float insets + * text.C (parse_text): add graphics file extension if necessary + * text.C (find_file): new helper function + 2004-07-23 Georg Baum * math.C (parse_math): change tex "\\cr" line ending to "\\\\" diff --git a/src/tex2lyx/preamble.C b/src/tex2lyx/preamble.C index 52531678b0..d4f80b9b1a 100644 --- a/src/tex2lyx/preamble.C +++ b/src/tex2lyx/preamble.C @@ -68,11 +68,11 @@ string h_paperfontsize = "default"; string h_spacing = "single"; // Match the entry in ../src/tex-strings.C. Why not "default"? string h_papersize = "Default"; -string h_paperpackage = "default"; +string h_paperpackage = "none"; string h_use_geometry = "0"; string h_use_amsmath = "0"; -string h_use_natbib = "0"; -string h_use_numerical_citations = "0"; +string h_cite_engine = "basic"; +string h_use_bibtopic = "0"; string h_paperorientation = "portrait"; string h_secnumdepth = "3"; string h_tocdepth = "3"; @@ -143,7 +143,7 @@ void handle_package(string const & name, string const & options) void end_preamble(ostream & os, LyXTextClass const & /*textclass*/) { os << "#LyX file created by tex2lyx 0.1.2 \n" - << "\\lyxformat 228\n" + << "\\lyxformat 235\n" << "\\textclass " << h_textclass << "\n" << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n"; if (!h_options.empty()) @@ -158,8 +158,8 @@ void end_preamble(ostream & os, LyXTextClass const & /*textclass*/) << "\\paperpackage " << h_paperpackage << "\n" << "\\use_geometry " << h_use_geometry << "\n" << "\\use_amsmath " << h_use_amsmath << "\n" - << "\\use_natbib " << h_use_natbib << "\n" - << "\\use_numerical_citations " << h_use_numerical_citations << "\n" + << "\\cite_engine " << h_cite_engine << "\n" + << "\\use_bibtopic " << h_use_bibtopic << "\n" << "\\paperorientation " << h_paperorientation << "\n" << "\\secnumdepth " << h_secnumdepth << "\n" << "\\tocdepth " << h_tocdepth << "\n" diff --git a/src/tex2lyx/tex2lyx.C b/src/tex2lyx/tex2lyx.C index c14d3c098d..8af7883760 100644 --- a/src/tex2lyx/tex2lyx.C +++ b/src/tex2lyx/tex2lyx.C @@ -19,6 +19,7 @@ #include "lyxtextclass.h" #include "support/path_defines.h" #include "support/filetools.h" +#include "support/lyxlib.h" #include "support/os.h" #include @@ -278,9 +279,19 @@ void easyParse(int & argc, char * argv[]) } } + +// path of the parsed file +string masterFilePath; + } // anonymous namespace +string getMasterFilePath() +{ + return masterFilePath; +} + + void tex2lyx(std::istream &is, std::ostream &os) { Parser p(is); @@ -353,6 +364,12 @@ int main(int argc, char * argv[]) << "\" for reading." << endl; return 2; } + + if (lyx::support::AbsolutePath(argv[1])) + masterFilePath = lyx::support::OnlyPath(argv[1]); + else + masterFilePath = lyx::support::getcwd(); + ifstream is(argv[1]); tex2lyx(is, cout); diff --git a/src/tex2lyx/tex2lyx.h b/src/tex2lyx/tex2lyx.h index ec94dd92c7..f93bc0398e 100644 --- a/src/tex2lyx/tex2lyx.h +++ b/src/tex2lyx/tex2lyx.h @@ -71,6 +71,11 @@ enum ArgumentType { /// Known TeX commands with arguments that get parsed into ERT. extern std::map > known_commands; + +/// path of the master .tex file +extern std::string getMasterFilePath(); + + /*! Reads tex input from \a is and writes lyx output to \a os. * Uses some common settings for the preamble, so this should only * be used more than once for included documents. diff --git a/src/tex2lyx/text.C b/src/tex2lyx/text.C index 3450acd8a4..37b553bdbc 100644 --- a/src/tex2lyx/text.C +++ b/src/tex2lyx/text.C @@ -17,6 +17,7 @@ #include "context.h" #include "FloatList.h" #include "lengthcommon.h" +#include "support/FileInfo.h" #include "support/lstrings.h" #include "support/tostr.h" #include "support/filetools.h" @@ -26,6 +27,8 @@ #include #include +using lyx::support::FileInfo; +using lyx::support::MakeAbsPath; using lyx::support::rtrim; using lyx::support::suffixIs; using lyx::support::contains; @@ -126,6 +129,26 @@ char const * const known_font_shapes[] = { "itshape", "slshape", "scshape", char const * const known_coded_font_shapes[] = { "italic", "slanted", "smallcaps", "up", 0}; +/*! + * Graphics file extensions known by the dvips driver of the graphics package. + * These extensions are used to complete the filename of an included + * graphics file if it does not contain an extension. + * The order must be the same that latex uses to find a file, because we + * will use the first extension that matches. + * This is only an approximation for the common cases. If we would want to + * do it right in all cases, we would need to know which graphics driver is + * used and know the extensions of every driver of the graphics package. + */ +char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz", +"ps.gz", "eps.Z", "ps.Z", 0}; + +/*! + * Graphics file extensions known by the pdftex driver of the graphics package. + * \see known_dvips_graphics_formats + */ +char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg", +"mps", "tif", 0}; + /// splits "x=z, y=b" into a map map split_map(string const & s) @@ -257,6 +280,24 @@ void translate_box_len(string const & length, string & value, string & unit, str } +/*! + * Find a file with basename \p name in path \p path and an extension + * in \p extensions. + */ +string find_file(string const & name, string const & path, + char const * const * extensions) +{ + for (char const * const * what = extensions; *what; ++what) { + // We don't use ChangeExtension() because it does the wrong + // thing if name contains a dot. + string const trial = name + '.' + (*what); + if (FileInfo(MakeAbsPath(trial, path)).exist()) + return trial; + } + return string(); +} + + void begin_inset(ostream & os, string const & name) { os << "\n\\begin_inset " << name; @@ -599,6 +640,7 @@ void parse_environment(Parser & p, ostream & os, bool outer, os << "placement " << p.getArg('[', ']') << '\n'; } os << "wide " << tostr(is_starred) + << "\nsideways false" << "\nstatus open\n\n"; parse_text_in_inset(p, os, FLAG_END, outer, parent_context); end_inset(os); @@ -1071,6 +1113,43 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, map opts = split_map(p.getArg('[', ']')); string name = subst(p.verbatim_item(), "\\lyxdot ", "."); + string const path = getMasterFilePath(); + // We want to preserve relative / absolute filenames, + // therefore path is only used for testing + if (!FileInfo(MakeAbsPath(name, path)).exist()) { + // The file extension is probably missing. + // Now try to find it out. + string const dvips_name = + find_file(name, path, + known_dvips_graphics_formats); + string const pdftex_name = + find_file(name, path, + known_pdftex_graphics_formats); + if (!dvips_name.empty()) { + if (!pdftex_name.empty()) { + cerr << "This file contains the " + "latex snippet\n" + "\"\\includegraphics{" + << name << "}\".\n" + "However, files\n\"" + << dvips_name << "\" and\n\"" + << pdftex_name << "\"\n" + "both exist, so I had to make a " + "choice and took the first one.\n" + "Please move the unwanted one " + "someplace else and try again\n" + "if my choice was wrong." + << endl; + } + name = dvips_name; + } else if (!pdftex_name.empty()) + name = pdftex_name; + + if (!FileInfo(MakeAbsPath(name, path)).exist()) + cerr << "Warning: Could not find graphics file '" + << name << "'." << endl; + } + context.check_layout(os); begin_inset(os, "Graphics "); os << "\n\tfilename " << name << '\n';