Factor out method to get a properly encoded latex string

This commit is contained in:
Juergen Spitzmueller 2012-06-30 13:30:48 +02:00
parent 708979fb0e
commit 51d591d168
6 changed files with 81 additions and 75 deletions

View File

@ -354,35 +354,26 @@ docstring getLaTeXMarkup(docstring const & macro, docstring const & author,
ods << macro;
// convert utf8 author name to something representable
// in the current encoding
docstring author_latexed;
for (size_t n = 0; n < author.size(); ++n) {
try {
author_latexed += runparams.encoding->latexChar(author[n]).first;
} catch (EncodingException & /* e */) {
if (runparams.dryrun) {
ods << "<" << _("LyX Warning: ")
<< _("uncodable character") << " '";
ods.put(author[n]);
ods << "'>";
} else {
LYXERR0("Omitting uncodable character '"
<< docstring(1, author[n])
<< "' in change author name!");
uncodable_author = author;
}
}
pair<docstring, docstring> author_latexed =
runparams.encoding->latexString(author, runparams.dryrun);
if (!author_latexed.second.empty()) {
LYXERR0("Omitting uncodable characters '"
<< author_latexed.second
<< "' in change author name!");
uncodable_author = author;
}
ods << author_latexed << "}{" << chgTime << "}{";
ods << author_latexed.first << "}{" << chgTime << "}{";
// warn user (once) if we found uncodable glyphs.
if (uncodable_author != warned_author) {
frontend::Alert::warning(_("Uncodable character in author name"),
support::bformat(_("The author name '%1$s',\n"
"used for change tracking, contains glyphs that cannot be\n"
"represented in the current encoding. The respective glyphs\n"
"will be omitted in the exported LaTeX file.\n\n"
"used for change tracking, contains the following glyphs that\n"
"cannot be represented in the current encoding: %2$s.\n"
"These glyphs will be omitted in the exported LaTeX file.\n\n"
"Choose an appropriate document encoding (such as utf8)\n"
"or change the spelling of the author name."), uncodable_author));
"or change the spelling of the author name."),
uncodable_author, author_latexed.second));
warned_author = uncodable_author;
}

View File

@ -22,6 +22,7 @@
#include "LyXRC.h"
#include "support/debug.h"
#include "support/gettext.h"
#include "support/FileName.h"
#include "support/lstrings.h"
#include "support/textutils.h"
@ -399,6 +400,44 @@ pair<docstring, bool> Encoding::latexChar(char_type c) const
}
pair<docstring, docstring> Encoding::latexString(docstring const input, bool dryrun) const
{
docstring result;
docstring uncodable;
bool terminate = false;
for (size_t n = 0; n < input.size(); ++n) {
try {
char_type const c = input[n];
pair<docstring, bool> latex_char = latexChar(c);
docstring const latex = latex_char.first;
if (terminate && !prefixIs(latex, '\\')
&& !prefixIs(latex, '{')
&& !prefixIs(latex, '}')) {
// Prevent eating of a following
// space or command corruption by
// following characters
if (latex == " ")
result += "{}";
else
result += " ";
}
result += latex;
terminate = latex_char.second;
} catch (EncodingException & /* e */) {
LYXERR0("Uncodable character in latexString!");
if (dryrun) {
result += "<" + _("LyX Warning: ")
+ _("uncodable character") + " '";
result += docstring(1, input[n]);
result += "'>";
} else
uncodable += input[n];
}
}
return make_pair(result, uncodable);
}
vector<char_type> Encoding::symbolsList() const
{
// assure the used encoding is properly initialized

View File

@ -80,6 +80,19 @@ public:
* the command needs to be terminated by {} or a space.
*/
std::pair<docstring, bool> latexChar(char_type c) const;
/**
* Convert \p input to something that LaTeX can understand.
* This is either the string itself (if it is representable
* in this encoding), or a LaTeX macro.
* If a character is not representable in this encoding, but no
* LaTeX macro is known, a warning is given of lyxerr, and the
* character is returned in the second string of the pair and
* omitted in the first.
* \p dryrun specifies whether the string is used within source
* preview (which yields a special warning).
*/
std::pair<docstring, docstring> latexString(docstring const input,
bool dryrun = false) const;
/// Which LaTeX package handles this encoding?
Package package() const { return package_; }
/// A list of all characters usable in this encoding

View File

@ -291,20 +291,9 @@ docstring bibitemWidest(Buffer const & buffer, OutputParams const & runparams)
}
if (!lbl.empty()) {
docstring latex_lbl;
for (size_t n = 0; n < lbl.size(); ++n) {
try {
latex_lbl += runparams.encoding->latexChar(lbl[n]).first;
} catch (EncodingException & /* e */) {
if (runparams.dryrun) {
latex_lbl += "<" + _("LyX Warning: ")
+ _("uncodable character") + " '";
latex_lbl += docstring(1, lbl[n]);
latex_lbl += "'>";
}
}
}
return latex_lbl;
pair<docstring, docstring> latex_lbl =
runparams.encoding->latexString(lbl, runparams.dryrun);
return latex_lbl.first;
}
return from_ascii("99");

View File

@ -372,36 +372,16 @@ docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
docstring result;
switch (handling) {
case ParamInfo::HANDLING_LATEXIFY: {
docstring uncodable;
for (size_t n = 0; n < command.size(); ++n) {
try {
char_type const c = command[n];
docstring const latex = runparams.encoding->latexChar(c).first;
result += latex;
if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
// Prevent eating of a following
// space or command corruption by
// following characters
result += "{}";
}
} catch (EncodingException & /* e */) {
LYXERR0("Uncodable character in command inset!");
if (runparams.dryrun) {
result += "<" + _("LyX Warning: ")
+ _("uncodable character") + " '";
result += docstring(1, command[n]);
result += "'>";
} else
uncodable += command[n];
}
}
if (!uncodable.empty()) {
pair<docstring, docstring> command_latexed =
runparams.encoding->latexString(command, runparams.dryrun);
result = command_latexed.first;
if (!command_latexed.second.empty()) {
// issue a warning about omitted characters
// FIXME: should be passed to the error dialog
frontend::Alert::warning(_("Uncodable characters"),
bformat(_("The following characters that are used in the inset %1$s are not\n"
"representable in the current encoding and therefore have been omitted:\n%2$s."),
from_utf8(insetType()), uncodable));
from_utf8(insetType()), command_latexed.second));
}
break;
}

View File

@ -33,6 +33,7 @@
#include "frontends/FontMetrics.h"
#include "support/debug.h"
#include "support/docstream.h"
#include "support/gettext.h"
#include "support/lstrings.h"
@ -276,20 +277,13 @@ docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
return symb;
// we have to encode the string properly
docstring latex_symb;
for (size_t n = 0; n < symb.size(); ++n) {
try {
latex_symb += runparams.encoding->latexChar(symb[n]).first;
} catch (EncodingException & /* e */) {
if (runparams.dryrun) {
latex_symb += "<" + _("LyX Warning: ")
+ _("uncodable character") + " '";
latex_symb += docstring(1, symb[n]);
latex_symb += "'>";
}
}
}
return latex_symb;
pair<docstring, docstring> latex_symb =
runparams.encoding->latexString(symb, runparams.dryrun);
if (!latex_symb.second.empty())
LYXERR0("Omitting uncodable characters '"
<< latex_symb.second
<< "' in nomencl widest string!");
return latex_symb.first;
}
} // namespace anon