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

View File

@ -22,6 +22,7 @@
#include "LyXRC.h" #include "LyXRC.h"
#include "support/debug.h" #include "support/debug.h"
#include "support/gettext.h"
#include "support/FileName.h" #include "support/FileName.h"
#include "support/lstrings.h" #include "support/lstrings.h"
#include "support/textutils.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 vector<char_type> Encoding::symbolsList() const
{ {
// assure the used encoding is properly initialized // assure the used encoding is properly initialized

View File

@ -80,6 +80,19 @@ public:
* the command needs to be terminated by {} or a space. * the command needs to be terminated by {} or a space.
*/ */
std::pair<docstring, bool> latexChar(char_type c) const; 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? /// Which LaTeX package handles this encoding?
Package package() const { return package_; } Package package() const { return package_; }
/// A list of all characters usable in this encoding /// 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()) { if (!lbl.empty()) {
docstring latex_lbl; pair<docstring, docstring> latex_lbl =
for (size_t n = 0; n < lbl.size(); ++n) { runparams.encoding->latexString(lbl, runparams.dryrun);
try { return latex_lbl.first;
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;
} }
return from_ascii("99"); return from_ascii("99");

View File

@ -372,36 +372,16 @@ docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
docstring result; docstring result;
switch (handling) { switch (handling) {
case ParamInfo::HANDLING_LATEXIFY: { case ParamInfo::HANDLING_LATEXIFY: {
docstring uncodable; pair<docstring, docstring> command_latexed =
for (size_t n = 0; n < command.size(); ++n) { runparams.encoding->latexString(command, runparams.dryrun);
try { result = command_latexed.first;
char_type const c = command[n]; if (!command_latexed.second.empty()) {
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()) {
// issue a warning about omitted characters // issue a warning about omitted characters
// FIXME: should be passed to the error dialog // FIXME: should be passed to the error dialog
frontend::Alert::warning(_("Uncodable characters"), frontend::Alert::warning(_("Uncodable characters"),
bformat(_("The following characters that are used in the inset %1$s are not\n" 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."), "representable in the current encoding and therefore have been omitted:\n%2$s."),
from_utf8(insetType()), uncodable)); from_utf8(insetType()), command_latexed.second));
} }
break; break;
} }

View File

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