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 "InsetCaption.h"
#include "Buffer.h"
#include "BufferParams.h"
#include "BufferView.h"
#include "FloatList.h"
#include "InsetList.h"
#include "output_xhtml.h"
#include "TextClass.h"
#include "TocBackend.h"
#include "support/docstream.h"
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,
UpdateType utype) const
{

View File

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

View File

@ -330,7 +330,7 @@ void InsetFloat::latex(otexstream & os, OutputParams const & runparams_in) const
OutputParams rp = runparams_in;
rp.moving_arg = true;
getCaption(os, rp);
os << getCaption(rp);
os << '{';
// The main argument is the contents of the float. This is not a moving argument.
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();
if (ins == 0)
return;
return TexString();
otexstringstream os;
ins->getArgs(os, runparams);
os << '[';
@ -525,6 +513,7 @@ void InsetFloat::getCaption(otexstream & os,
arg = '{' + arg + '}';
os << move(ts);
os << ']';
return os.release();
}

View File

@ -18,6 +18,9 @@
namespace lyx {
class TexString;
class InsetFloatParams
{
public:
@ -107,9 +110,7 @@ private:
///
Inset * clone() const { return new InsetFloat(*this); }
///
docstring getCaption(OutputParams const &) const;
///
void getCaption(otexstream & os, OutputParams const & runparams) const;
TexString getCaption(OutputParams const &) const;
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
odocstringstream ourlatex;
// FIXME: do Tex/Row correspondence (I don't currently understand what is
// being generated from latexstr below)
otexstream ots(ourlatex);
InsetText::latex(ots, runparams);
odocstringstream ourplain;

View File

@ -30,6 +30,7 @@
#include "output_xhtml.h"
#include "OutputParams.h"
#include "TextClass.h"
#include "TexRow.h"
#include "texstream.h"
#include "support/debug.h"
@ -222,13 +223,13 @@ void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
} else {
OutputParams rp = runparams;
rp.moving_arg = true;
docstring const caption = getCaption(rp);
if (param_string.empty() && caption.empty())
TexString caption = getCaption(rp);
if (param_string.empty() && caption.str.empty())
os << breakln << "\\begin{lstlisting}\n";
else {
os << breakln << "\\begin{lstlisting}[";
if (!caption.empty()) {
os << "caption={" << caption << '}';
if (!caption.str.empty()) {
os << "caption={" << move(caption) << '}';
if (!param_string.empty())
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();
if (ins == 0)
return docstring();
return TexString();
odocstringstream ods;
otexstream os(ods);
otexstringstream os;
ins->getArgs(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
// package prefer caption={}, label={}
docstring cap = ods.str();
if (!contains(to_utf8(cap), "\\label{"))
TexString cap = os.release();
if (!contains(cap.str, from_ascii("\\label{")))
return cap;
// convert from
// blah1\label{blah2} blah3
@ -420,7 +417,11 @@ docstring InsetListings::getCaption(OutputParams const & runparams) const
// NOTE that } is not allowed in blah2.
regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
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 {
class LaTeXFeatures;
class TexString;
/////////////////////////////////////////////////////////////////////////
//
@ -75,9 +76,9 @@ private:
///
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_;

View File

@ -27,7 +27,6 @@
#include "ErrorList.h"
#include "FuncRequest.h"
#include "FuncStatus.h"
#include "InsetCaption.h"
#include "InsetList.h"
#include "Intl.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)
{
return static_cast<InsetText::XHTMLOptions>((int)a1 | (int)a2);

View File

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