make tex2lyx produce version 235 lyx files

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8854 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Georg Baum 2004-07-29 17:03:37 +00:00
parent 0718aa87bf
commit 27b0ba7483
5 changed files with 119 additions and 6 deletions

View File

@ -1,3 +1,15 @@
2004-07-29 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* 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 <Georg.Baum@post.rwth-aachen.de>
* math.C (parse_math): change tex "\\cr" line ending to "\\\\"

View File

@ -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"

View File

@ -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 <boost/function.hpp>
@ -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);

View File

@ -71,6 +71,11 @@ enum ArgumentType {
/// Known TeX commands with arguments that get parsed into ERT.
extern std::map<std::string, std::vector<ArgumentType> > 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.

View File

@ -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 <sstream>
#include <vector>
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<string, string> 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<string, string> 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';