Unify Paragraph::asString() methods.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@24882 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2008-05-22 14:01:33 +00:00
parent 0dea68828b
commit eb28cb8755
10 changed files with 40 additions and 42 deletions

View File

@ -1850,11 +1850,14 @@ void Cursor::errorMessage(docstring const & msg) const
}
docstring Cursor::selectionAsString(bool label) const
docstring Cursor::selectionAsString(bool with_label) const
{
if (!selection())
return docstring();
int const label = with_label
? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
if (inTexted()) {
ParagraphList const & pars = text()->paragraphs();

View File

@ -552,7 +552,7 @@ vector<docstring> availableSelections()
ParagraphList::const_iterator pit = pars.begin();
ParagraphList::const_iterator pend = pars.end();
for (; pit != pend; ++pit) {
asciiSel += pit->asString(false);
asciiSel += pit->asString(AS_STR_INSETS);
if (asciiSel.size() > 25) {
asciiSel.replace(22, docstring::npos,
from_ascii("..."));
@ -770,7 +770,7 @@ void clearCutStack()
docstring selection(size_t sel_index)
{
return sel_index < theCuts.size()
? theCuts[sel_index].first.back().asString(false)
? theCuts[sel_index].first.back().asString(AS_STR_INSETS)
: docstring();
}

View File

@ -2302,40 +2302,26 @@ bool Paragraph::isMultiLingual(BufferParams const & bparams) const
}
docstring const Paragraph::printableString(bool label) const
docstring Paragraph::asString(int options) const
{
odocstringstream os;
if (label && !params().labelString().empty())
os << params().labelString() << ' ';
pos_type end = size();
for (pos_type i = 0; i < end; ++i) {
char_type const c = d->text_[i];
if (isPrintable(c))
os.put(c);
}
return os.str();
return asString(0, size(), options);
}
docstring Paragraph::asString(bool label) const
docstring Paragraph::asString(pos_type beg, pos_type end, int options) const
{
return asString(0, size(), label);
}
docstring Paragraph::asString(pos_type beg, pos_type end, bool label) const
{
odocstringstream os;
if (beg == 0 && label && !d->params_.labelString().empty())
if (beg == 0
&& options & AS_STR_LABEL
&& !d->params_.labelString().empty())
os << d->params_.labelString() << ' ';
for (pos_type i = beg; i < end; ++i) {
char_type const c = d->text_[i];
if (isPrintable(c))
os.put(c);
else if (c == META_INSET)
else if (c == META_INSET && options & AS_STR_INSETS)
getInset(i)->textString(os);
}

View File

@ -72,6 +72,15 @@ enum TextCase {
};
///
enum AsStringParameter
{
AS_STR_NONE = 0, ///< No option, only printable characters.
AS_STR_LABEL = 1, ///< Prefix with paragraph label.
AS_STR_INSETS = 2 ///< Go into insets.
};
/// A Paragraph holds all text, attributes and insets in a text paragraph
class Paragraph
{
@ -98,15 +107,15 @@ public:
bool isMultiLingual(BufferParams const &) const;
/// Convert the paragraph to a string.
/// This method doesn't go inside insets, only printable characters in this
/// paragraph are used.
/// Used for building the table of contents
docstring const printableString(bool label) const;
/// Convert the paragraph to a string.
docstring asString(bool label) const;
/// \param AsStringParameter options. This can contain any combination of
/// asStringParameter values. Valid examples:
/// asString(AS_STR_LABEL)
/// asString(AS_STR_LABEL | AS_STR_INSETS)
/// asString(AS_STR_INSETS)
docstring asString(int options = AS_STR_NONE) const;
///
docstring asString(pos_type beg, pos_type end, bool label) const;
docstring asString(pos_type beg, pos_type end,
int options = AS_STR_NONE) const;
///
void write(std::ostream &, BufferParams const &,

View File

@ -1377,7 +1377,7 @@ docstring Text::getPossibleLabel(Cursor & cur) const
Layout const * layout = &(pars_[pit].layout());
docstring text;
docstring par_text = pars_[pit].asString(false);
docstring par_text = pars_[pit].asString();
string piece;
// the return string of math matrices might contain linebreaks
par_text = subst(par_text, '\n', '-');
@ -1527,7 +1527,7 @@ docstring Text::previousWord(CursorSlice const & sl) const
return docstring();
Paragraph const & par = sl.paragraph();
return par.asString(from.pos(), to.pos(), false);
return par.asString(from.pos(), to.pos());
}

View File

@ -134,7 +134,7 @@ void TocBackend::updateItem(DocIterator const & dit)
*static_cast<InsetOptArg&>(inset).paragraphs().begin();
if (!par.labelString().empty())
tocstring = par.labelString() + ' ';
tocstring += inset_par.printableString(false);
tocstring += inset_par.asString();
break;
}
}
@ -142,7 +142,7 @@ void TocBackend::updateItem(DocIterator const & dit)
int const toclevel = par.layout().toclevel;
if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
&& tocstring.empty())
tocstring = par.printableString(true);
tocstring = par.asString(AS_STR_LABEL);
const_cast<TocItem &>(*toc_item).str_ = tocstring;
}
@ -181,7 +181,7 @@ void TocBackend::update()
*static_cast<InsetOptArg&>(inset).paragraphs().begin();
if (!pit->labelString().empty())
tocstring = pit->labelString() + ' ';
tocstring += par.printableString(false);
tocstring += par.asString();
break;
}
default:
@ -196,7 +196,7 @@ void TocBackend::update()
pit.pos() = 0;
// insert this into the table of contents
if (tocstring.empty())
tocstring = pit->printableString(true);
tocstring = pit->asString(AS_STR_LABEL);
toc.push_back(TocItem(pit, toclevel - min_toclevel,
tocstring));
}

View File

@ -231,7 +231,7 @@ void InsetBibitem::fillWithBibKeys(BiblioInfo & keys, InsetIterator const & it)
keyvalmap[from_ascii("label")] = getParam("label");
DocIterator doc_it(it);
doc_it.forwardPos();
keyvalmap[from_ascii("ref")] = doc_it.paragraph().asString(false);
keyvalmap[from_ascii("ref")] = doc_it.paragraph().asString();
keys[key] = keyvalmap;
}

View File

@ -235,7 +235,7 @@ int InsetBranch::docbook(odocstream & os,
void InsetBranch::textString(odocstream & os) const
{
if (isBranchSelected())
os << paragraphs().begin()->asString(true);
os << paragraphs().begin()->asString(AS_STR_LABEL | AS_STR_INSETS);
}

View File

@ -111,7 +111,7 @@ void InsetCaption::addToToc(DocIterator const & cpit)
pit.push_back(CursorSlice(*this));
Toc & toc = buffer().tocBackend().toc(type_);
docstring const str = full_label_ + ". " + text_.getPar(0).printableString(false);
docstring const str = full_label_ + ". " + text_.getPar(0).asString();
toc.push_back(TocItem(pit, 0, str));
}

View File

@ -110,7 +110,7 @@ int InsetFlex::docbook(odocstream & os, OutputParams const & runparams) const
void InsetFlex::textString(odocstream & os) const
{
os << paragraphs().begin()->asString(true);
os << paragraphs().begin()->asString(AS_STR_LABEL | AS_STR_INSETS);
}