From 51d591d16893e595ac1163c963e5c0928fdaff68 Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Sat, 30 Jun 2012 13:30:48 +0200 Subject: [PATCH] Factor out method to get a properly encoded latex string --- src/Changes.cpp | 35 +++++++++++---------------- src/Encoding.cpp | 39 +++++++++++++++++++++++++++++++ src/Encoding.h | 13 +++++++++++ src/insets/InsetBibitem.cpp | 17 +++----------- src/insets/InsetCommandParams.cpp | 30 ++++-------------------- src/insets/InsetNomencl.cpp | 22 +++++++---------- 6 files changed, 81 insertions(+), 75 deletions(-) diff --git a/src/Changes.cpp b/src/Changes.cpp index c14bf9b41e..ea7c5c998e 100644 --- a/src/Changes.cpp +++ b/src/Changes.cpp @@ -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 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; } diff --git a/src/Encoding.cpp b/src/Encoding.cpp index d9fe69110d..b758787e28 100644 --- a/src/Encoding.cpp +++ b/src/Encoding.cpp @@ -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 Encoding::latexChar(char_type c) const } +pair 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 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 Encoding::symbolsList() const { // assure the used encoding is properly initialized diff --git a/src/Encoding.h b/src/Encoding.h index 7b7e483b83..b57ad7aa08 100644 --- a/src/Encoding.h +++ b/src/Encoding.h @@ -80,6 +80,19 @@ public: * the command needs to be terminated by {} or a space. */ std::pair 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 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 diff --git a/src/insets/InsetBibitem.cpp b/src/insets/InsetBibitem.cpp index 6decdce7f4..f150d0d52a 100644 --- a/src/insets/InsetBibitem.cpp +++ b/src/insets/InsetBibitem.cpp @@ -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 latex_lbl = + runparams.encoding->latexString(lbl, runparams.dryrun); + return latex_lbl.first; } return from_ascii("99"); diff --git a/src/insets/InsetCommandParams.cpp b/src/insets/InsetCommandParams.cpp index cc75a0eb59..dfa0705dec 100644 --- a/src/insets/InsetCommandParams.cpp +++ b/src/insets/InsetCommandParams.cpp @@ -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 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; } diff --git a/src/insets/InsetNomencl.cpp b/src/insets/InsetNomencl.cpp index a32191b78a..a3fe1bc4dc 100644 --- a/src/insets/InsetNomencl.cpp +++ b/src/insets/InsetNomencl.cpp @@ -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 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 -- 2.39.5