From 1178b30c542266a42e91e46900825a0722049623 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=BCrgen=20Spitzm=C3=BCller?= Date: Sat, 13 Feb 2010 13:08:32 +0000 Subject: [PATCH] generalize special handling of command inset parameters on LaTeX output (escaping, etc.). git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@33461 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/insets/InsetBibitem.cpp | 46 ++-------------------- src/insets/InsetBibitem.h | 2 - src/insets/InsetCommand.cpp | 5 ++- src/insets/InsetCommand.h | 2 +- src/insets/InsetCommandParams.cpp | 64 +++++++++++++++++++++++++++---- src/insets/InsetCommandParams.h | 23 +++++++++-- src/insets/InsetIndex.cpp | 5 ++- src/insets/InsetLabel.cpp | 10 +---- src/insets/InsetLabel.h | 2 - src/insets/InsetNomencl.cpp | 5 ++- src/insets/InsetRef.cpp | 7 ++-- src/mathed/MathFactory.cpp | 4 +- 12 files changed, 99 insertions(+), 76 deletions(-) diff --git a/src/insets/InsetBibitem.cpp b/src/insets/InsetBibitem.cpp index fc158a0233..67df65175a 100644 --- a/src/insets/InsetBibitem.cpp +++ b/src/insets/InsetBibitem.cpp @@ -103,8 +103,10 @@ ParamInfo const & InsetBibitem::findInfo(string const & /* cmdName */) { static ParamInfo param_info_; if (param_info_.empty()) { - param_info_.add("label", ParamInfo::LATEX_OPTIONAL); - param_info_.add("key", ParamInfo::LATEX_REQUIRED); + param_info_.add("label", ParamInfo::LATEX_OPTIONAL, + ParamInfo::HANDLING_LATEXIFY); + param_info_.add("key", ParamInfo::LATEX_REQUIRED, + ParamInfo::HANDLING_ESCAPE); } return param_info_; } @@ -175,46 +177,6 @@ int InsetBibitem::plaintext(odocstream & os, OutputParams const &) const } -int InsetBibitem::latex(odocstream & os, OutputParams const & runparams) const -{ - docstring cmd = '\\' + from_ascii(defaultCommand()); - docstring uncodable; - if (!getParam("label").empty()) { - cmd += '['; - docstring orig = getParam("label"); - for (size_t n = 0; n < orig.size(); ++n) { - try { - cmd += runparams.encoding->latexChar(orig[n]); - } catch (EncodingException & /* e */) { - LYXERR0("Uncodable character in bibitem!"); - if (runparams.dryrun) { - cmd += "<" + _("LyX Warning: ") - + _("uncodable character") + " '"; - cmd += docstring(1, orig[n]); - cmd += "'>"; - } else - uncodable += orig[n]; - } - } - cmd += ']'; - } - cmd += '{' + escape(getParam("key")) + '}'; - - os << cmd; - - if (!uncodable.empty()) { - // issue a warning about omitted characters - // FIXME: should be passed to the error dialog - frontend::Alert::warning(_("Uncodable characters in bibliography item"), - bformat(_("The following characters in one of the bibliography items are\n" - "not representable in the current encoding and have been omitted:\n%1$s."), - uncodable)); - } - - return 0; -} - - // ale070405 docstring bibitemWidest(Buffer const & buffer, OutputParams const & runparams) { diff --git a/src/insets/InsetBibitem.h b/src/insets/InsetBibitem.h index 74ae2f8249..8891f8b795 100644 --- a/src/insets/InsetBibitem.h +++ b/src/insets/InsetBibitem.h @@ -63,8 +63,6 @@ private: /// docstring xhtml(XHTMLStream &, OutputParams const &) const; /// - int latex(odocstream &, OutputParams const &) const; - /// virtual void fillWithBibKeys(BiblioInfo &, InsetIterator const &) const; /// Update the counter of this inset void updateLabels(ParIterator const &, UpdateType); diff --git a/src/insets/InsetCommand.cpp b/src/insets/InsetCommand.cpp index e75c7aeff0..e68df838a5 100644 --- a/src/insets/InsetCommand.cpp +++ b/src/insets/InsetCommand.cpp @@ -106,9 +106,10 @@ void InsetCommand::setParams(InsetCommandParams const & p) } -int InsetCommand::latex(odocstream & os, OutputParams const &) const +int InsetCommand::latex(odocstream & os, OutputParams const & runparams_in) const { - os << getCommand(); + OutputParams runparams = runparams_in; + os << getCommand(runparams); return 0; } diff --git a/src/insets/InsetCommand.h b/src/insets/InsetCommand.h index fee67bf7be..d1f4af0d0f 100644 --- a/src/insets/InsetCommand.h +++ b/src/insets/InsetCommand.h @@ -75,7 +75,7 @@ protected: /// bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const; /// - docstring const getCommand() const { return p_.getCommand(); } + docstring const getCommand(OutputParams & rp) const { return p_.getCommand(rp); } /// std::string const & getCmdName() const { return p_.getCmdName(); } /// diff --git a/src/insets/InsetCommandParams.cpp b/src/insets/InsetCommandParams.cpp index 13e31a7b00..608bc883c5 100644 --- a/src/insets/InsetCommandParams.cpp +++ b/src/insets/InsetCommandParams.cpp @@ -27,7 +27,11 @@ #include "InsetRef.h" #include "InsetTOC.h" +#include "Encoding.h" #include "Lexer.h" +#include "OutputParams.h" + +#include "frontends/alert.h" #include "support/debug.h" #include "support/docstream.h" @@ -88,8 +92,9 @@ static ParamInfo const & findInfo(InsetCode code, string const & cmdName) // ///////////////////////////////////////////////////////////////////// -ParamInfo::ParamData::ParamData(std::string const & s, ParamType t) - : name_(s), type_(t) +ParamInfo::ParamData::ParamData(std::string const & s, ParamType t, + ParamHandling h) + : name_(s), type_(t), handling_(h) {} @@ -101,7 +106,8 @@ bool ParamInfo::ParamData::isOptional() const bool ParamInfo::ParamData::operator==(ParamInfo::ParamData const & rhs) const { - return name() == rhs.name() && type() == rhs.type(); + return name() == rhs.name() && type() == rhs.type() + && handling() == rhs.handling(); } @@ -117,9 +123,10 @@ bool ParamInfo::hasParam(std::string const & name) const } -void ParamInfo::add(std::string const & name, ParamType type) +void ParamInfo::add(std::string const & name, ParamType type, + ParamHandling handling) { - info_.push_back(ParamData(name, type)); + info_.push_back(ParamData(name, type, handling)); } @@ -351,7 +358,46 @@ bool InsetCommandParams::writeEmptyOptional(ParamInfo::const_iterator ci) const } -docstring InsetCommandParams::getCommand() const + +docstring InsetCommandParams::prepareCommand(OutputParams const & runparams, + docstring const & command, + ParamInfo::ParamHandling handling) const +{ + docstring result; + if (handling == ParamInfo::HANDLING_LATEXIFY) { + docstring uncodable; + for (size_t n = 0; n < command.size(); ++n) { + try { + result += runparams.encoding->latexChar(command[n]); + } 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 + // FIXME: should be passed to the error dialog + frontend::Alert::warning(_("Uncodable characters"), + bformat(_("The following characters that are used in an inset (%1$s) are\n" + "not representable in the current encoding and have been omitted:\n%2$s."), + from_utf8(insetType()), uncodable)); + } + } else if (handling == ParamInfo::HANDLING_ESCAPE) + result = escape(command); + else + result = command; + + return result; +} + + +docstring InsetCommandParams::getCommand(OutputParams const & runparams) const { docstring s = '\\' + from_ascii(cmdName_); bool noparam = true; @@ -364,13 +410,15 @@ docstring InsetCommandParams::getCommand() const break; case ParamInfo::LATEX_REQUIRED: { - docstring const & data = (*this)[name]; + docstring const & data = + prepareCommand(runparams, (*this)[name], it->handling()); s += '{' + data + '}'; noparam = false; break; } case ParamInfo::LATEX_OPTIONAL: { - docstring const & data = (*this)[name]; + docstring const & data = + prepareCommand(runparams, (*this)[name], it->handling()); if (!data.empty()) { s += '[' + data + ']'; noparam = false; diff --git a/src/insets/InsetCommandParams.h b/src/insets/InsetCommandParams.h index 2ff6d5c27f..8d2aaa8fa2 100644 --- a/src/insets/InsetCommandParams.h +++ b/src/insets/InsetCommandParams.h @@ -16,6 +16,8 @@ #include "InsetCode.h" +#include "OutputParams.h" + #include "support/docstring.h" #include @@ -35,17 +37,25 @@ public: LATEX_REQUIRED, /// normal required argument LYX_INTERNAL /// a parameter used internally by LyX }; + /// Special handling on output + enum ParamHandling { + HANDLING_NONE, /// no special handling + HANDLING_ESCAPE, /// escape special characters + HANDLING_LATEXIFY /// transform special characters to LaTeX macros + }; /// class ParamData { // No parameter may be named "preview", because that is a required // flag for all commands. public: /// - ParamData(std::string const &, ParamType); + ParamData(std::string const &, ParamType, ParamHandling = HANDLING_NONE); /// std::string name() const { return name_; } /// ParamType type() const { return type_; } + /// + ParamHandling handling() const { return handling_; } /// whether this is an optional LaTeX argument bool isOptional() const; /// @@ -58,10 +68,13 @@ public: std::string name_; /// ParamType type_; + /// do we need special handling on latex output? + ParamHandling handling_; }; /// adds a new parameter - void add(std::string const & name, ParamType type); + void add(std::string const & name, ParamType type, + ParamHandling = HANDLING_NONE); /// bool empty() const { return info_.empty(); } /// @@ -103,7 +116,7 @@ public: /// void write(std::ostream &) const; /// Build the complete LaTeX command - docstring getCommand() const; + docstring getCommand(OutputParams const &) const; /// Return the command name std::string const & getCmdName() const { return cmdName_; } /// Set the name to \p n. This must be a known name. All parameters @@ -133,6 +146,10 @@ private: /// checks whether we need to write an empty optional parameter /// \return true if a non-empty optional parameter follows ci bool writeEmptyOptional(ParamInfo::const_iterator ci) const; + /// + docstring prepareCommand(OutputParams const & runparams, + docstring const & command, + ParamInfo::ParamHandling handling) const; /// Description of all command properties ParamInfo info_; diff --git a/src/insets/InsetIndex.cpp b/src/insets/InsetIndex.cpp index 3fe758265f..bb86b46957 100644 --- a/src/insets/InsetIndex.cpp +++ b/src/insets/InsetIndex.cpp @@ -548,14 +548,15 @@ bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd, } -int InsetPrintIndex::latex(odocstream & os, OutputParams const &) const +int InsetPrintIndex::latex(odocstream & os, OutputParams const & runparams_in) const { if (!buffer().masterBuffer()->params().use_indices) { if (getParam("type") == from_ascii("idx")) os << "\\printindex{}"; return 0; } - os << getCommand(); + OutputParams runparams = runparams_in; + os << getCommand(runparams); return 0; } diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp index 2393bbc8cf..e35c3a76b0 100644 --- a/src/insets/InsetLabel.cpp +++ b/src/insets/InsetLabel.cpp @@ -108,7 +108,8 @@ ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */) { static ParamInfo param_info_; if (param_info_.empty()) - param_info_.add("name", ParamInfo::LATEX_REQUIRED); + param_info_.add("name", ParamInfo::LATEX_REQUIRED, + ParamInfo::HANDLING_ESCAPE); return param_info_; } @@ -230,13 +231,6 @@ void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd) } -int InsetLabel::latex(odocstream & os, OutputParams const &) const -{ - os << escape(getCommand()); - return 0; -} - - int InsetLabel::plaintext(odocstream & os, OutputParams const &) const { docstring const str = getParam("name"); diff --git a/src/insets/InsetLabel.h b/src/insets/InsetLabel.h index ab78761b96..3f083af55d 100644 --- a/src/insets/InsetLabel.h +++ b/src/insets/InsetLabel.h @@ -40,8 +40,6 @@ public: /// InsetCode lyxCode() const { return LABEL_CODE; } /// - int latex(odocstream &, OutputParams const &) const; - /// int plaintext(odocstream &, OutputParams const &) const; /// int docbook(odocstream &, OutputParams const &) const; diff --git a/src/insets/InsetNomencl.cpp b/src/insets/InsetNomencl.cpp index 306498feb6..c5ebfa2c27 100644 --- a/src/insets/InsetNomencl.cpp +++ b/src/insets/InsetNomencl.cpp @@ -270,7 +270,7 @@ docstring nomenclWidest(Buffer const & buffer) } -int InsetPrintNomencl::latex(odocstream & os, OutputParams const &) const +int InsetPrintNomencl::latex(odocstream & os, OutputParams const & runparams_in) const { int lines = 0; if (getParam("set_width") == "auto") { @@ -295,7 +295,8 @@ int InsetPrintNomencl::latex(odocstream & os, OutputParams const &) const return lines; } // output the command \printnomenclature - os << getCommand(); + OutputParams runparams = runparams_in; + os << getCommand(runparams); return lines; } diff --git a/src/insets/InsetRef.cpp b/src/insets/InsetRef.cpp index d983b50e5f..0b9227eff9 100644 --- a/src/insets/InsetRef.cpp +++ b/src/insets/InsetRef.cpp @@ -61,7 +61,8 @@ ParamInfo const & InsetRef::findInfo(string const & /* cmdName */) static ParamInfo param_info_; if (param_info_.empty()) { param_info_.add("name", ParamInfo::LATEX_OPTIONAL); - param_info_.add("reference", ParamInfo::LATEX_REQUIRED); + param_info_.add("reference", ParamInfo::LATEX_REQUIRED, + ParamInfo::HANDLING_ESCAPE); } return param_info_; } @@ -73,13 +74,13 @@ docstring InsetRef::screenLabel() const } -int InsetRef::latex(odocstream & os, OutputParams const &) const +int InsetRef::latex(odocstream & os, OutputParams const & runparams) const { // We don't want to output p_["name"], since that is only used // in docbook. So we construct new params, without it, and use that. InsetCommandParams p(REF_CODE, getCmdName()); p["reference"] = getParam("reference"); - os << escape(p.getCommand()); + os << p.getCommand(runparams); return 0; } diff --git a/src/mathed/MathFactory.cpp b/src/mathed/MathFactory.cpp index 2a32e9397f..4d91243fe6 100644 --- a/src/mathed/MathFactory.cpp +++ b/src/mathed/MathFactory.cpp @@ -514,7 +514,9 @@ bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar) InsetCommandParams icp(REF_CODE); // FIXME UNICODE InsetCommand::string2params("ref", to_utf8(str), icp); - mathed_parse_cell(ar, icp.getCommand()); + Encoding const * const utf8 = encodings.fromLyXName("utf8"); + OutputParams op(utf8); + mathed_parse_cell(ar, icp.getCommand(op)); } else if (name == "mathspace") { InsetSpaceParams isp(true); InsetSpace::string2params(to_utf8(str), isp); -- 2.39.5