Add a new command line parameter -e that sets the default (latex) encoding.

This is overridden by any subsequent inputenc command.

The current encoding is also passed down to \input or \included files.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@28295 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Jean-Marc Lasgouttes 2009-01-30 14:47:06 +00:00
parent a4391b65ca
commit f83ae91f05
6 changed files with 42 additions and 14 deletions

View File

@ -132,14 +132,15 @@ string Token::asInput() const
Parser::Parser(idocstream & is)
: lineno_(0), pos_(0), iss_(0), is_(is)
: lineno_(0), pos_(0), iss_(0), is_(is), encoding_latex_("utf8")
{
}
Parser::Parser(string const & s)
: lineno_(0), pos_(0),
iss_(new idocstringstream(from_utf8(s))), is_(*iss_)
iss_(new idocstringstream(from_utf8(s))), is_(*iss_),
encoding_latex_("utf8")
{
}
@ -153,8 +154,9 @@ Parser::~Parser()
void Parser::setEncoding(std::string const & e)
{
Encoding const * enc = encodings.fromLaTeXName(e);
cerr << "setting encoding to " << enc->iconvName();
//cerr << "setting encoding to " << enc->iconvName()<<std::endl;
is_ << lyx::setEncoding(enc->iconvName());
encoding_latex_ = e;
}

View File

@ -119,8 +119,10 @@ public:
///
~Parser();
/// change the encoding of the input stream
/// change the latex encoding of the input stream
void setEncoding(std::string const & encoding);
/// get the current latex encoding of the input stream
std::string getEncoding() const { return encoding_latex_; }
///
int lineno() const { return lineno_; }
@ -217,6 +219,8 @@ private:
idocstringstream * iss_;
///
idocstream & is_;
/// latex name of the current encoding
std::string encoding_latex_;
};

View File

@ -2,4 +2,5 @@
\subsection{\label{sub:External-Subsection}External Subsection}
This is a small dummy child document to show how files can be inserted
to another document.
to another document. Here are some accented characters to make sure
the encoding is passed to included files: éè

View File

@ -236,6 +236,7 @@ void read_syntaxfile(FileName const & file_name)
string documentclass;
string default_encoding;
string syntaxfile;
bool overwrite_files = false;
@ -253,6 +254,7 @@ int parse_help(string const &, string const &)
"\t-userdir dir try to set user directory to dir\n"
"\t-sysdir dir try to set system directory to dir\n"
"\t-c textclass declare the textclass\n"
"\t-e encoding set the default encoding (latex name)\n"
"\t-n translate a noweb (aka literate programming) file.\n"
"\t-s syntaxfile read additional syntax file" << endl;
exit(0);
@ -270,6 +272,17 @@ int parse_class(string const & arg, string const &)
}
int parse_encoding(string const & arg, string const &)
{
if (arg.empty()) {
cerr << "Missing encoding string after -e switch" << endl;
exit(1);
}
default_encoding = arg;
return 1;
}
int parse_syntaxfile(string const & arg, string const &)
{
if (arg.empty()) {
@ -328,6 +341,7 @@ void easyParse(int & argc, char * argv[])
map<string, cmd_helper> cmdmap;
cmdmap["-c"] = parse_class;
cmdmap["-e"] = parse_encoding;
cmdmap["-f"] = parse_force;
cmdmap["-s"] = parse_syntaxfile;
cmdmap["-help"] = parse_help;
@ -389,9 +403,11 @@ namespace {
* You must ensure that \p parentFilePath is properly set before calling
* this function!
*/
void tex2lyx(idocstream & is, ostream & os)
void tex2lyx(idocstream & is, ostream & os, string const & encoding)
{
Parser p(is);
if (!encoding.empty())
p.setEncoding(encoding);
//p.dump();
stringstream ss;
@ -420,7 +436,7 @@ void tex2lyx(idocstream & is, ostream & os)
/// convert TeX from \p infilename to LyX and write it to \p os
bool tex2lyx(FileName const & infilename, ostream & os)
bool tex2lyx(FileName const & infilename, ostream & os, string const & encoding)
{
ifdocstream is;
// forbid buffering on this stream
@ -433,7 +449,7 @@ bool tex2lyx(FileName const & infilename, ostream & os)
}
string const oldParentFilePath = parentFilePath;
parentFilePath = onlyPath(infilename.absFilename());
tex2lyx(is, os);
tex2lyx(is, os, encoding);
parentFilePath = oldParentFilePath;
return true;
}
@ -441,7 +457,8 @@ bool tex2lyx(FileName const & infilename, ostream & os)
} // anonymous namespace
bool tex2lyx(string const & infilename, FileName const & outfilename)
bool tex2lyx(string const & infilename, FileName const & outfilename,
string const & encoding)
{
if (outfilename.isReadableFile()) {
if (overwrite_files) {
@ -465,7 +482,7 @@ bool tex2lyx(string const & infilename, FileName const & outfilename)
cerr << "Input file: " << infilename << "\n";
cerr << "Output file: " << outfilename << "\n";
#endif
return tex2lyx(FileName(infilename), os);
return tex2lyx(FileName(infilename), os, encoding);
}
} // namespace lyx
@ -542,12 +559,12 @@ int main(int argc, char * argv[])
masterFilePath = onlyPath(infilename);
parentFilePath = masterFilePath;
if (outfilename == "-") {
if (tex2lyx(FileName(infilename), cout))
if (tex2lyx(FileName(infilename), cout, default_encoding))
return EXIT_SUCCESS;
else
return EXIT_FAILURE;
} else {
if (tex2lyx(infilename, FileName(outfilename)))
if (tex2lyx(infilename, FileName(outfilename), default_encoding))
return EXIT_SUCCESS;
else
return EXIT_FAILURE;

View File

@ -114,13 +114,16 @@ extern std::string getParentFilePath();
/*!
* Reads tex input from \a infilename and writes lyx output to \a outfilename.
* The (latex) encoding can be provided as \a encoding.
* 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.
* \return true if the conversion was successful, else false.
*/
bool tex2lyx(std::string const & infilename, support::FileName const & outfilename);
bool tex2lyx(std::string const & infilename,
support::FileName const & outfilename,
std::string const & encoding);
} // namespace lyx

View File

@ -2319,7 +2319,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
string const lyxname =
changeExtension(filename, ".lyx");
if (t.cs() != "verbatiminput" &&
tex2lyx(abstexname, FileName(abslyxname))) {
tex2lyx(abstexname, FileName(abslyxname),
p.getEncoding())) {
os << name << '{' << lyxname << "}\n";
} else {
os << name << '{' << filename << "}\n";