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
This commit is contained in:
Jürgen Spitzmüller 2010-02-13 13:08:32 +00:00
parent 23a58c0214
commit 1178b30c54
12 changed files with 99 additions and 76 deletions

View 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)
{

View 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);

View 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;
}

View 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(); }
///

View File

@ -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;

View 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_;

View 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;
}

View 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");

View 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;

View 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;
}

View 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;
}

View 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);