mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-12-27 06:19:36 +00:00
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:
parent
3dd40899d8
commit
38044d2cf7
@ -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>
|
2005-05-20 Georg Baum <Georg.Baum@post.rwth-aachen.de>
|
||||||
|
|
||||||
* tex2lyx.C (main): set correct masterFilePath for files like "subdir/file"
|
* tex2lyx.C (main): set correct masterFilePath for files like "subdir/file"
|
||||||
|
@ -325,8 +325,10 @@ void easyParse(int & argc, char * argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// path of the parsed file
|
// path of the first parsed file
|
||||||
string masterFilePath;
|
string masterFilePath;
|
||||||
|
// path of the currently parsed file
|
||||||
|
string parentFilePath;
|
||||||
|
|
||||||
} // anonymous namespace
|
} // anonymous namespace
|
||||||
|
|
||||||
@ -336,7 +338,23 @@ string getMasterFilePath()
|
|||||||
return masterFilePath;
|
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)
|
void tex2lyx(std::istream &is, std::ostream &os)
|
||||||
{
|
{
|
||||||
Parser p(is);
|
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());
|
ifstream is(infilename.c_str());
|
||||||
if (!is.good()) {
|
if (!is.good()) {
|
||||||
cerr << "Could not open file \"" << infilename
|
cerr << "Could not open input file \"" << infilename
|
||||||
<< "\" for reading." << endl;
|
<< "\" for reading." << endl;
|
||||||
return false;
|
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)) {
|
if (!overwrite_files && IsFileReadable(outfilename)) {
|
||||||
cerr << "Not overwriting existing file " << outfilename << "\n";
|
cerr << "Not overwriting existing file " << outfilename << "\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
ofstream os(outfilename.c_str());
|
ofstream os(outfilename.c_str());
|
||||||
|
if (!os.good()) {
|
||||||
|
cerr << "Could not open output file \"" << outfilename
|
||||||
|
<< "\" for writing." << endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
#ifdef FILEDEBUG
|
#ifdef FILEDEBUG
|
||||||
cerr << "File: " << infilename << "\n";
|
cerr << "Input file: " << infilename << "\n";
|
||||||
|
cerr << "Output file: " << outfilename << "\n";
|
||||||
#endif
|
#endif
|
||||||
tex2lyx(is, os);
|
return tex2lyx(infilename, os);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -409,18 +446,14 @@ int main(int argc, char * argv[])
|
|||||||
if (!syntaxfile.empty())
|
if (!syntaxfile.empty())
|
||||||
read_syntaxfile(syntaxfile);
|
read_syntaxfile(syntaxfile);
|
||||||
|
|
||||||
ifstream is(argv[1]);
|
string const infilename = MakeAbsPath(argv[1]);
|
||||||
if (!is.good()) {
|
masterFilePath = OnlyPath(infilename);
|
||||||
cerr << "Could not open input file \"" << argv[1]
|
parentFilePath = masterFilePath;
|
||||||
<< "\" for reading." << endl;
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
masterFilePath = OnlyPath(MakeAbsPath(argv[1]));
|
if (tex2lyx(infilename, cout))
|
||||||
|
return EXIT_SUCCESS;
|
||||||
tex2lyx(is, cout);
|
else
|
||||||
|
return EXIT_FAILURE;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// }])
|
// }])
|
||||||
|
@ -87,15 +87,18 @@ extern std::map<std::string, std::vector<ArgumentType> > known_commands;
|
|||||||
|
|
||||||
/// path of the master .tex file
|
/// path of the master .tex file
|
||||||
extern std::string getMasterFilePath();
|
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
|
* Uses some common settings for the preamble, so this should only
|
||||||
* be used more than once for included documents.
|
* be used more than once for included documents.
|
||||||
* Caution: Overwrites the existing preamble settings if the new document
|
* Caution: Overwrites the existing preamble settings if the new document
|
||||||
* contains a preamble. */
|
* contains a preamble.
|
||||||
void tex2lyx(std::istream &, std::ostream &);
|
* \return true if the conversion was successful, else false.
|
||||||
/// \return true if the conversion was successful, else false.
|
*/
|
||||||
bool tex2lyx(std::string const &, std::string const &);
|
bool tex2lyx(std::string const & infilename, std::string const & outfilename);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
|
|
||||||
using lyx::support::ChangeExtension;
|
using lyx::support::ChangeExtension;
|
||||||
using lyx::support::MakeAbsPath;
|
using lyx::support::MakeAbsPath;
|
||||||
|
using lyx::support::MakeRelPath;
|
||||||
using lyx::support::rtrim;
|
using lyx::support::rtrim;
|
||||||
using lyx::support::suffixIs;
|
using lyx::support::suffixIs;
|
||||||
using lyx::support::contains;
|
using lyx::support::contains;
|
||||||
@ -899,6 +900,17 @@ string const normalize_filename(string const & name)
|
|||||||
return os.str();
|
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
|
} // anonymous namespace
|
||||||
|
|
||||||
|
|
||||||
@ -1259,12 +1271,14 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
|
|||||||
name = dvips_name;
|
name = dvips_name;
|
||||||
} else if (!pdftex_name.empty())
|
} else if (!pdftex_name.empty())
|
||||||
name = pdftex_name;
|
name = pdftex_name;
|
||||||
|
|
||||||
if (!fs::exists(MakeAbsPath(name, path)))
|
|
||||||
cerr << "Warning: Could not find graphics file '"
|
|
||||||
<< name << "'." << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (fs::exists(MakeAbsPath(name, path)))
|
||||||
|
fix_relative_filename(name);
|
||||||
|
else
|
||||||
|
cerr << "Warning: Could not find graphics file '"
|
||||||
|
<< name << "'." << endl;
|
||||||
|
|
||||||
context.check_layout(os);
|
context.check_layout(os);
|
||||||
begin_inset(os, "Graphics ");
|
begin_inset(os, "Graphics ");
|
||||||
os << "\n\tfilename " << name << '\n';
|
os << "\n\tfilename " << name << '\n';
|
||||||
@ -1651,6 +1665,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
|
|||||||
context.check_layout(os);
|
context.check_layout(os);
|
||||||
context.font.family =
|
context.font.family =
|
||||||
known_coded_font_families[where - known_font_families];
|
known_coded_font_families[where - known_font_families];
|
||||||
|
// FIXME: Only do this if it is necessary
|
||||||
os << "\n\\family " << context.font.family << '\n';
|
os << "\n\\family " << context.font.family << '\n';
|
||||||
eat_whitespace(p, os, context, false);
|
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.check_layout(os);
|
||||||
context.font.series =
|
context.font.series =
|
||||||
known_coded_font_series[where - known_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';
|
os << "\n\\series " << context.font.series << '\n';
|
||||||
eat_whitespace(p, os, context, false);
|
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.check_layout(os);
|
||||||
context.font.shape =
|
context.font.shape =
|
||||||
known_coded_font_shapes[where - known_font_shapes];
|
known_coded_font_shapes[where - known_font_shapes];
|
||||||
|
// FIXME: Only do this if it is necessary
|
||||||
os << "\n\\shape " << context.font.shape << '\n';
|
os << "\n\\shape " << context.font.shape << '\n';
|
||||||
eat_whitespace(p, os, context, false);
|
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.size = oldsize;
|
||||||
context.font.family =
|
context.font.family =
|
||||||
known_coded_font_families[where - known_old_font_families];
|
known_coded_font_families[where - known_old_font_families];
|
||||||
|
// FIXME: Only do this if it is necessary
|
||||||
os << "\n\\family " << context.font.family << "\n"
|
os << "\n\\family " << context.font.family << "\n"
|
||||||
<< "\\series " << context.font.series << "\n"
|
<< "\\series " << context.font.series << "\n"
|
||||||
<< "\\shape " << context.font.shape << "\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.size = oldsize;
|
||||||
context.font.series =
|
context.font.series =
|
||||||
known_coded_font_series[where - known_old_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"
|
os << "\n\\family " << context.font.family << "\n"
|
||||||
<< "\\series " << context.font.series << "\n"
|
<< "\\series " << context.font.series << "\n"
|
||||||
<< "\\shape " << context.font.shape << "\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.size = oldsize;
|
||||||
context.font.shape =
|
context.font.shape =
|
||||||
known_coded_font_shapes[where - known_old_font_shapes];
|
known_coded_font_shapes[where - known_old_font_shapes];
|
||||||
|
// FIXME: Only do this if it is necessary
|
||||||
os << "\n\\family " << context.font.family << "\n"
|
os << "\n\\family " << context.font.family << "\n"
|
||||||
<< "\\series " << context.font.series << "\n"
|
<< "\\series " << context.font.series << "\n"
|
||||||
<< "\\shape " << context.font.shape << "\n";
|
<< "\\shape " << context.font.shape << "\n";
|
||||||
@ -1879,6 +1899,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
|
|||||||
MakeAbsPath(filename, path);
|
MakeAbsPath(filename, path);
|
||||||
string const abslyxname =
|
string const abslyxname =
|
||||||
ChangeExtension(abstexname, ".lyx");
|
ChangeExtension(abstexname, ".lyx");
|
||||||
|
fix_relative_filename(filename);
|
||||||
string const lyxname =
|
string const lyxname =
|
||||||
ChangeExtension(filename, ".lyx");
|
ChangeExtension(filename, ".lyx");
|
||||||
if (t.cs() != "verbatiminput" &&
|
if (t.cs() != "verbatiminput" &&
|
||||||
|
Loading…
Reference in New Issue
Block a user