Use otexstringstream for the captions of InsetCaptionables

* Enable TexRow for InsetListings caption.

* Move getCaption* from InsetText to InsetCaptionable.

* Clean-up caption generation for InsetFloat.
This commit is contained in:
Guillaume Munch 2016-09-24 00:49:00 +02:00
parent f3e099960a
commit 676a0639c5
9 changed files with 88 additions and 91 deletions

View File

@ -15,13 +15,19 @@
#include "InsetCaptionable.h" #include "InsetCaptionable.h"
#include "InsetCaption.h"
#include "Buffer.h" #include "Buffer.h"
#include "BufferParams.h" #include "BufferParams.h"
#include "BufferView.h" #include "BufferView.h"
#include "FloatList.h" #include "FloatList.h"
#include "InsetList.h"
#include "output_xhtml.h"
#include "TextClass.h" #include "TextClass.h"
#include "TocBackend.h" #include "TocBackend.h"
#include "support/docstream.h"
using namespace std; using namespace std;
@ -44,6 +50,52 @@ docstring InsetCaptionable::floatName(string const & type) const
} }
InsetCaption const * InsetCaptionable::getCaptionInset() const
{
ParagraphList::const_iterator pit = paragraphs().begin();
for (; pit != paragraphs().end(); ++pit) {
InsetList::const_iterator it = pit->insetList().begin();
for (; it != pit->insetList().end(); ++it) {
Inset & inset = *it->inset;
if (inset.lyxCode() == CAPTION_CODE) {
InsetCaption const * ins =
static_cast<InsetCaption const *>(it->inset);
return ins;
}
}
}
return 0;
}
docstring InsetCaptionable::getCaptionText(OutputParams const & runparams) const
{
InsetCaption const * ins = getCaptionInset();
if (ins == 0)
return docstring();
odocstringstream ods;
ins->getCaptionAsPlaintext(ods, runparams);
return ods.str();
}
docstring InsetCaptionable::getCaptionHTML(OutputParams const & runparams) const
{
InsetCaption const * ins = getCaptionInset();
if (ins == 0)
return docstring();
odocstringstream ods;
XHTMLStream xs(ods);
docstring def = ins->getCaptionAsHTML(xs, runparams);
if (!def.empty())
// should already have been escaped
xs << XHTMLStream::ESCAPE_NONE << def << '\n';
return ods.str();
}
void InsetCaptionable::addToToc(DocIterator const & cpit, bool output_active, void InsetCaptionable::addToToc(DocIterator const & cpit, bool output_active,
UpdateType utype) const UpdateType utype) const
{ {

View File

@ -23,9 +23,7 @@ namespace lyx {
class InsetCaptionable : public InsetCollapsable class InsetCaptionable : public InsetCollapsable
{ {
public: public:
InsetCaptionable(Buffer * buffer) InsetCaptionable(Buffer * buffer, std::string const & type = "senseless")
: InsetCollapsable(buffer), caption_type_("senseless") {}
InsetCaptionable(Buffer * buffer, std::string const & type)
: InsetCollapsable(buffer), caption_type_(type) {} : InsetCollapsable(buffer), caption_type_(type) {}
/// ///
std::string const & captionType() const { return caption_type_; } std::string const & captionType() const { return caption_type_; }
@ -33,6 +31,12 @@ public:
docstring floatName(std::string const & type) const; docstring floatName(std::string const & type) const;
/// ///
protected: protected:
///
InsetCaption const * getCaptionInset() const;
///
docstring getCaptionText(OutputParams const &) const;
///
docstring getCaptionHTML(OutputParams const &) const;
/// ///
virtual void setCaptionType(std::string const & type); virtual void setCaptionType(std::string const & type);
/// are our captions subcaptions? /// are our captions subcaptions?

View File

@ -330,7 +330,7 @@ void InsetFloat::latex(otexstream & os, OutputParams const & runparams_in) const
OutputParams rp = runparams_in; OutputParams rp = runparams_in;
rp.moving_arg = true; rp.moving_arg = true;
getCaption(os, rp); os << getCaption(rp);
os << '{'; os << '{';
// The main argument is the contents of the float. This is not a moving argument. // The main argument is the contents of the float. This is not a moving argument.
if (!paragraphs().empty()) if (!paragraphs().empty())
@ -494,25 +494,13 @@ bool InsetFloat::allowsCaptionVariation(std::string const & newtype) const
} }
docstring InsetFloat::getCaption(OutputParams const & runparams) const TexString InsetFloat::getCaption(OutputParams const & runparams) const
{ {
odocstringstream ods;
otexstream os(ods);
getCaption(os, runparams);
return ods.str();
}
void InsetFloat::getCaption(otexstream & os,
OutputParams const & runparams) const
{
if (paragraphs().empty())
return;
InsetCaption const * ins = getCaptionInset(); InsetCaption const * ins = getCaptionInset();
if (ins == 0) if (ins == 0)
return; return TexString();
otexstringstream os;
ins->getArgs(os, runparams); ins->getArgs(os, runparams);
os << '['; os << '[';
@ -525,6 +513,7 @@ void InsetFloat::getCaption(otexstream & os,
arg = '{' + arg + '}'; arg = '{' + arg + '}';
os << move(ts); os << move(ts);
os << ']'; os << ']';
return os.release();
} }

View File

@ -18,6 +18,9 @@
namespace lyx { namespace lyx {
class TexString;
class InsetFloatParams class InsetFloatParams
{ {
public: public:
@ -107,9 +110,7 @@ private:
/// ///
Inset * clone() const { return new InsetFloat(*this); } Inset * clone() const { return new InsetFloat(*this); }
/// ///
docstring getCaption(OutputParams const &) const; TexString getCaption(OutputParams const &) const;
///
void getCaption(otexstream & os, OutputParams const & runparams) const;
InsetFloatParams params_; InsetFloatParams params_;
}; };

View File

@ -76,6 +76,8 @@ void InsetIndex::latex(otexstream & os, OutputParams const & runparams_in) const
// get contents of InsetText as LaTeX and plaintext // get contents of InsetText as LaTeX and plaintext
odocstringstream ourlatex; odocstringstream ourlatex;
// FIXME: do Tex/Row correspondence (I don't currently understand what is
// being generated from latexstr below)
otexstream ots(ourlatex); otexstream ots(ourlatex);
InsetText::latex(ots, runparams); InsetText::latex(ots, runparams);
odocstringstream ourplain; odocstringstream ourplain;

View File

@ -30,6 +30,7 @@
#include "output_xhtml.h" #include "output_xhtml.h"
#include "OutputParams.h" #include "OutputParams.h"
#include "TextClass.h" #include "TextClass.h"
#include "TexRow.h"
#include "texstream.h" #include "texstream.h"
#include "support/debug.h" #include "support/debug.h"
@ -222,13 +223,13 @@ void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
} else { } else {
OutputParams rp = runparams; OutputParams rp = runparams;
rp.moving_arg = true; rp.moving_arg = true;
docstring const caption = getCaption(rp); TexString caption = getCaption(rp);
if (param_string.empty() && caption.empty()) if (param_string.empty() && caption.str.empty())
os << breakln << "\\begin{lstlisting}\n"; os << breakln << "\\begin{lstlisting}\n";
else { else {
os << breakln << "\\begin{lstlisting}["; os << breakln << "\\begin{lstlisting}[";
if (!caption.empty()) { if (!caption.str.empty()) {
os << "caption={" << caption << '}'; os << "caption={" << move(caption) << '}';
if (!param_string.empty()) if (!param_string.empty())
os << ','; os << ',';
} }
@ -388,17 +389,13 @@ bool InsetListings::showInsetDialog(BufferView * bv) const
} }
docstring InsetListings::getCaption(OutputParams const & runparams) const TexString InsetListings::getCaption(OutputParams const & runparams) const
{ {
if (paragraphs().empty())
return docstring();
InsetCaption const * ins = getCaptionInset(); InsetCaption const * ins = getCaptionInset();
if (ins == 0) if (ins == 0)
return docstring(); return TexString();
odocstringstream ods; otexstringstream os;
otexstream os(ods);
ins->getArgs(os, runparams); ins->getArgs(os, runparams);
ins->getArgument(os, runparams); ins->getArgument(os, runparams);
@ -407,8 +404,8 @@ docstring InsetListings::getCaption(OutputParams const & runparams) const
// the caption may contain \label{} but the listings // the caption may contain \label{} but the listings
// package prefer caption={}, label={} // package prefer caption={}, label={}
docstring cap = ods.str(); TexString cap = os.release();
if (!contains(to_utf8(cap), "\\label{")) if (!contains(cap.str, from_ascii("\\label{")))
return cap; return cap;
// convert from // convert from
// blah1\label{blah2} blah3 // blah1\label{blah2} blah3
@ -420,7 +417,11 @@ docstring InsetListings::getCaption(OutputParams const & runparams) const
// NOTE that } is not allowed in blah2. // NOTE that } is not allowed in blah2.
regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)"); regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
string const new_cap("$1$3},label={$2"); string const new_cap("$1$3},label={$2");
return from_utf8(regex_replace(to_utf8(cap), reg, new_cap)); // TexString validity: the substitution preserves the number of newlines.
// Moreover we assume that $2 does not contain newlines, so that the texrow
// information remains accurate.
cap.str = from_utf8(regex_replace(to_utf8(cap.str), reg, new_cap));
return cap;
} }

View File

@ -18,6 +18,7 @@
namespace lyx { namespace lyx {
class LaTeXFeatures; class LaTeXFeatures;
class TexString;
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// //
@ -75,9 +76,9 @@ private:
/// ///
docstring const buttonLabel(BufferView const & bv) const; docstring const buttonLabel(BufferView const & bv) const;
/// ///
docstring getCaption(OutputParams const &) const; TexString getCaption(OutputParams const &) const;
/// ///
bool insetAllowed(InsetCode c) const { return c == CAPTION_CODE; } bool insetAllowed(InsetCode c) const { return c == CAPTION_CODE; }
/// ///
InsetListingsParams params_; InsetListingsParams params_;

View File

@ -27,7 +27,6 @@
#include "ErrorList.h" #include "ErrorList.h"
#include "FuncRequest.h" #include "FuncRequest.h"
#include "FuncStatus.h" #include "FuncStatus.h"
#include "InsetCaption.h"
#include "InsetList.h" #include "InsetList.h"
#include "Intl.h" #include "Intl.h"
#include "Language.h" #include "Language.h"
@ -1019,52 +1018,6 @@ docstring InsetText::toolTipText(docstring prefix, size_t const len) const
} }
InsetCaption const * InsetText::getCaptionInset() const
{
ParagraphList::const_iterator pit = paragraphs().begin();
for (; pit != paragraphs().end(); ++pit) {
InsetList::const_iterator it = pit->insetList().begin();
for (; it != pit->insetList().end(); ++it) {
Inset & inset = *it->inset;
if (inset.lyxCode() == CAPTION_CODE) {
InsetCaption const * ins =
static_cast<InsetCaption const *>(it->inset);
return ins;
}
}
}
return 0;
}
docstring InsetText::getCaptionText(OutputParams const & runparams) const
{
InsetCaption const * ins = getCaptionInset();
if (ins == 0)
return docstring();
odocstringstream ods;
ins->getCaptionAsPlaintext(ods, runparams);
return ods.str();
}
docstring InsetText::getCaptionHTML(OutputParams const & runparams) const
{
InsetCaption const * ins = getCaptionInset();
if (ins == 0)
return docstring();
odocstringstream ods;
XHTMLStream xs(ods);
docstring def = ins->getCaptionAsHTML(xs, runparams);
if (!def.empty())
// should already have been escaped
xs << XHTMLStream::ESCAPE_NONE << def << '\n';
return ods.str();
}
InsetText::XHTMLOptions operator|(InsetText::XHTMLOptions a1, InsetText::XHTMLOptions a2) InsetText::XHTMLOptions operator|(InsetText::XHTMLOptions a1, InsetText::XHTMLOptions a2)
{ {
return static_cast<InsetText::XHTMLOptions>((int)a1 | (int)a2); return static_cast<InsetText::XHTMLOptions>((int)a1 | (int)a2);

View File

@ -219,12 +219,6 @@ public:
/// ///
void doDispatch(Cursor & cur, FuncRequest & cmd); void doDispatch(Cursor & cur, FuncRequest & cmd);
protected: protected:
///
InsetCaption const * getCaptionInset() const;
///
docstring getCaptionText(OutputParams const &) const;
///
docstring getCaptionHTML(OutputParams const &) const;
/// ///
void iterateForToc(DocIterator const & cdit, bool output_active, void iterateForToc(DocIterator const & cdit, bool output_active,
UpdateType utype) const; UpdateType utype) const;