fix relative file names in tex2lyx

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9975 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Georg Baum 2005-05-25 16:01:19 +00:00
parent 3dd40899d8
commit 38044d2cf7
4 changed files with 93 additions and 26 deletions

View File

@ -1,3 +1,13 @@
2005-05-25 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* text.C (fix_relative_filename): new
* text.C (parse_text): use fix_relative_filename for included
graphics and .tex files
* tex2lyx.[Ch] (getParentFilePath): new
* tex2lyx.[Ch] (tex2lyx): new overload taking the input filename and
output stream
* tex2lyx.[Ch] (main): simplify, use the above
2005-05-20 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* tex2lyx.C (main): set correct masterFilePath for files like "subdir/file"

View File

@ -325,8 +325,10 @@ void easyParse(int & argc, char * argv[])
}
// path of the parsed file
// path of the first parsed file
string masterFilePath;
// path of the currently parsed file
string parentFilePath;
} // anonymous namespace
@ -336,7 +338,23 @@ string getMasterFilePath()
return masterFilePath;
}
string getParentFilePath()
{
return parentFilePath;
}
namespace {
/*!
* 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.
* Caution: Overwrites the existing preamble settings if the new document
* contains a preamble.
* You must ensure that \p parentFilePath is properly set before calling
* this function!
*/
void tex2lyx(std::istream &is, std::ostream &os)
{
Parser p(is);
@ -363,24 +381,43 @@ void tex2lyx(std::istream &is, std::ostream &os)
}
bool tex2lyx(string const &infilename, string const &outfilename)
/// convert TeX from \p infilename to LyX and write it to \p os
bool tex2lyx(string const &infilename, std::ostream &os)
{
BOOST_ASSERT(lyx::support::AbsolutePath(infilename));
ifstream is(infilename.c_str());
if (!is.good()) {
cerr << "Could not open file \"" << infilename
cerr << "Could not open input file \"" << infilename
<< "\" for reading." << endl;
return false;
}
string const oldParentFilePath = parentFilePath;
parentFilePath = OnlyPath(infilename);
tex2lyx(is, os);
parentFilePath = oldParentFilePath;
return true;
}
} // anonymous namespace
bool tex2lyx(string const &infilename, string const &outfilename)
{
if (!overwrite_files && IsFileReadable(outfilename)) {
cerr << "Not overwriting existing file " << outfilename << "\n";
return false;
}
ofstream os(outfilename.c_str());
if (!os.good()) {
cerr << "Could not open output file \"" << outfilename
<< "\" for writing." << endl;
return false;
}
#ifdef FILEDEBUG
cerr << "File: " << infilename << "\n";
cerr << "Input file: " << infilename << "\n";
cerr << "Output file: " << outfilename << "\n";
#endif
tex2lyx(is, os);
return true;
return tex2lyx(infilename, os);
}
@ -409,18 +446,14 @@ int main(int argc, char * argv[])
if (!syntaxfile.empty())
read_syntaxfile(syntaxfile);
ifstream is(argv[1]);
if (!is.good()) {
cerr << "Could not open input file \"" << argv[1]
<< "\" for reading." << endl;
return 2;
}
string const infilename = MakeAbsPath(argv[1]);
masterFilePath = OnlyPath(infilename);
parentFilePath = masterFilePath;
masterFilePath = OnlyPath(MakeAbsPath(argv[1]));
tex2lyx(is, cout);
return 0;
if (tex2lyx(infilename, cout))
return EXIT_SUCCESS;
else
return EXIT_FAILURE;
}
// }])

View File

@ -87,15 +87,18 @@ extern std::map<std::string, std::vector<ArgumentType> > known_commands;
/// path of the master .tex file
extern std::string getMasterFilePath();
/// path of the currently processed .tex file
extern std::string getParentFilePath();
/*! Reads tex input from \a is and writes lyx output to \a os.
/*!
* Reads tex input from \a infilename and writes lyx output to \a outfilename.
* Uses some common settings for the preamble, so this should only
* be used more than once for included documents.
* Caution: Overwrites the existing preamble settings if the new document
* contains a preamble. */
void tex2lyx(std::istream &, std::ostream &);
/// \return true if the conversion was successful, else false.
bool tex2lyx(std::string const &, std::string const &);
* contains a preamble.
* \return true if the conversion was successful, else false.
*/
bool tex2lyx(std::string const & infilename, std::string const & outfilename);
#endif

View File

@ -31,6 +31,7 @@
using lyx::support::ChangeExtension;
using lyx::support::MakeAbsPath;
using lyx::support::MakeRelPath;
using lyx::support::rtrim;
using lyx::support::suffixIs;
using lyx::support::contains;
@ -899,6 +900,17 @@ string const normalize_filename(string const & name)
return os.str();
}
/// Convert \p name from TeX convention (relative to master file) to LyX
/// convention (relative to .lyx file) if it is relative
void fix_relative_filename(string & name)
{
if (lyx::support::AbsolutePath(name))
return;
name = MakeRelPath(MakeAbsPath(name, getMasterFilePath()),
getParentFilePath());
}
} // anonymous namespace
@ -1259,11 +1271,13 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
name = dvips_name;
} else if (!pdftex_name.empty())
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 '"
<< name << "'." << endl;
}
context.check_layout(os);
begin_inset(os, "Graphics ");
@ -1651,6 +1665,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
context.check_layout(os);
context.font.family =
known_coded_font_families[where - known_font_families];
// FIXME: Only do this if it is necessary
os << "\n\\family " << context.font.family << '\n';
eat_whitespace(p, os, context, false);
}
@ -1661,6 +1676,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
context.check_layout(os);
context.font.series =
known_coded_font_series[where - known_font_series];
// FIXME: Only do this if it is necessary
os << "\n\\series " << context.font.series << '\n';
eat_whitespace(p, os, context, false);
}
@ -1671,6 +1687,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
context.check_layout(os);
context.font.shape =
known_coded_font_shapes[where - known_font_shapes];
// FIXME: Only do this if it is necessary
os << "\n\\shape " << context.font.shape << '\n';
eat_whitespace(p, os, context, false);
}
@ -1683,6 +1700,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
context.font.size = oldsize;
context.font.family =
known_coded_font_families[where - known_old_font_families];
// FIXME: Only do this if it is necessary
os << "\n\\family " << context.font.family << "\n"
<< "\\series " << context.font.series << "\n"
<< "\\shape " << context.font.shape << "\n";
@ -1698,6 +1716,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
context.font.size = oldsize;
context.font.series =
known_coded_font_series[where - known_old_font_series];
// FIXME: Only do this if it is necessary
os << "\n\\family " << context.font.family << "\n"
<< "\\series " << context.font.series << "\n"
<< "\\shape " << context.font.shape << "\n";
@ -1713,6 +1732,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
context.font.size = oldsize;
context.font.shape =
known_coded_font_shapes[where - known_old_font_shapes];
// FIXME: Only do this if it is necessary
os << "\n\\family " << context.font.family << "\n"
<< "\\series " << context.font.series << "\n"
<< "\\shape " << context.font.shape << "\n";
@ -1879,6 +1899,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
MakeAbsPath(filename, path);
string const abslyxname =
ChangeExtension(abstexname, ".lyx");
fix_relative_filename(filename);
string const lyxname =
ChangeExtension(filename, ".lyx");
if (t.cs() != "verbatiminput" &&