]> git.lyx.org Git - features.git/commitdiff
generalize special handling of command inset parameters on LaTeX output (escaping...
authorJürgen Spitzmüller <spitz@lyx.org>
Sat, 13 Feb 2010 13:08:32 +0000 (13:08 +0000)
committerJürgen Spitzmüller <spitz@lyx.org>
Sat, 13 Feb 2010 13:08:32 +0000 (13:08 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@33461 a592a061-630c-0410-9148-cb99ea01b6c8

12 files changed:
src/insets/InsetBibitem.cpp
src/insets/InsetBibitem.h
src/insets/InsetCommand.cpp
src/insets/InsetCommand.h
src/insets/InsetCommandParams.cpp
src/insets/InsetCommandParams.h
src/insets/InsetIndex.cpp
src/insets/InsetLabel.cpp
src/insets/InsetLabel.h
src/insets/InsetNomencl.cpp
src/insets/InsetRef.cpp
src/mathed/MathFactory.cpp

index fc158a02334c3ef7c14263e24cc1aa1fa380becf..67df65175a9ca616b290976c147d779481e16f1a 100644 (file)
@@ -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)
 {
index 74ae2f824925c448cc056d3972ea6c8e97f47096..8891f8b795a4740e9b2fc9d836941d1816779f8f 100644 (file)
@@ -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);
index e75c7aeff032c2e73764f2ac97fc8c3e8da13d20..e68df838a52de70bdcc4321d1340f7b1569b6b06 100644 (file)
@@ -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;
 }
 
index fee67bf7be7c8312835f206f1f683509ea7df978..d1f4af0d0f3344bec806ecb64ae907e6f48adbff 100644 (file)
@@ -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(); }
        ///
index 13e31a7b0097ff0ac6e75fb0b8ab0f0da615bd80..608bc883c58100da7b1678030e8e1db3e8716c20 100644 (file)
 #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;
index 2ff6d5c27f9b0f5ae12f4f45cbe5407650432d40..8d2aaa8fa246cb5268a68296ff8fc1c9f871f9b3 100644 (file)
@@ -16,6 +16,8 @@
 
 #include "InsetCode.h"
 
+#include "OutputParams.h"
+
 #include "support/docstring.h"
 
 #include <string>
@@ -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_;
index 3fe758265f2db8f0eb9ce9bd513be014440ed084..bb86b4695700a9233bdc6b2d27d4332e524d3adf 100644 (file)
@@ -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;
 }
 
index 2393bbc8cffdeee37ee971394414b49ab452d48e..e35c3a76b0a2a0baaba39f7926f7381bed4f138f 100644 (file)
@@ -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");
index ab78761b96fb6231c5df138268dd6bbd7b49bfa7..3f083af55d8052af438a07bb9c4da6f974a6a3c3 100644 (file)
@@ -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;
index 306498feb6338ba4fb97c0d69d2f7fcdacaedbb1..c5ebfa2c271429027dd46eb9caedec8c12ecf5de 100644 (file)
@@ -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;
 }
 
index d983b50e5f02edeabb74bdbb95bd1fed416048b1..0b9227eff90a8695dbe144513cbe22fd52f35de2 100644 (file)
@@ -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;
 }
 
index 2a32e9397f861419fd303fc9d0351a6928241baa..4d91243fe6f91628dfe17e7c21bc8017e3d9c7a3 100644 (file)
@@ -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);