Implement CiteItem in the chain

This allows us to get rid of many idiosyncratic arguments and gives us
a cleaner chain (plus easier extensibility).
This commit is contained in:
Juergen Spitzmueller 2017-01-07 17:12:08 +01:00
parent 2f1d1a60ac
commit 3a0d1d1049
6 changed files with 81 additions and 69 deletions

View File

@ -17,6 +17,7 @@
#include "Buffer.h"
#include "BufferParams.h"
#include "buffer_funcs.h"
#include "Citation.h"
#include "Encoding.h"
#include "InsetIterator.h"
#include "Language.h"
@ -478,7 +479,7 @@ bit of work, however.
*/
docstring BibTeXInfo::expandFormat(docstring const & format,
BibTeXInfoList const xrefs, int & counter, Buffer const & buf,
docstring before, docstring after, docstring dialog, bool next) const
CiteItem const & ci, bool next, bool second) const
{
// incorrect use of macros could put us in an infinite loop
static int const max_passes = 5000;
@ -526,7 +527,7 @@ docstring BibTeXInfo::expandFormat(docstring const & format,
ret << trans;
} else {
docstring const val =
getValueForKey(key, buf, before, after, dialog, xrefs, max_keysize);
getValueForKey(key, buf, ci, xrefs, max_keysize);
if (!scanning_rich)
ret << from_ascii("{!<span class=\"bib-" + key + "\">!}");
ret << val;
@ -557,17 +558,17 @@ docstring BibTeXInfo::expandFormat(docstring const & format,
return _("ERROR!");
fmt = newfmt;
docstring const val =
getValueForKey(optkey, buf, before, after, dialog, xrefs);
getValueForKey(optkey, buf, ci, xrefs);
if (optkey == "next" && next)
ret << ifpart; // without expansion
else if (!val.empty()) {
int newcounter = 0;
ret << expandFormat(ifpart, xrefs, newcounter, buf,
before, after, dialog, next);
ci, next);
} else if (!elsepart.empty()) {
int newcounter = 0;
ret << expandFormat(elsepart, xrefs, newcounter, buf,
before, after, dialog, next);
ci, next);
}
// fmt will have been shortened for us already
continue;
@ -616,8 +617,10 @@ docstring BibTeXInfo::expandFormat(docstring const & format,
docstring const & BibTeXInfo::getInfo(BibTeXInfoList const xrefs,
Buffer const & buf, bool richtext) const
Buffer const & buf, CiteItem const & ci) const
{
bool const richtext = ci.richtext;
if (!richtext && !info_.empty())
return info_;
if (richtext && !info_richtext_.empty())
@ -635,7 +638,7 @@ docstring const & BibTeXInfo::getInfo(BibTeXInfoList const xrefs,
from_utf8(dc.getCiteFormat(engine_type, to_utf8(entry_type_)));
int counter = 0;
info_ = expandFormat(format, xrefs, counter, buf,
docstring(), docstring(), docstring(), false);
ci, false, false);
if (info_.empty()) {
// this probably shouldn't happen
@ -653,18 +656,16 @@ docstring const & BibTeXInfo::getInfo(BibTeXInfoList const xrefs,
docstring const BibTeXInfo::getLabel(BibTeXInfoList const xrefs,
Buffer const & buf, docstring const & format, bool richtext,
docstring const & before, docstring const & after,
docstring const & dialog, bool next) const
Buffer const & buf, docstring const & format,
CiteItem const & ci, bool next, bool second) const
{
docstring loclabel;
int counter = 0;
loclabel = expandFormat(format, xrefs, counter, buf,
before, after, dialog, next);
loclabel = expandFormat(format, xrefs, counter, buf, ci, next, second);
if (!loclabel.empty() && !next) {
loclabel = processRichtext(loclabel, richtext);
loclabel = processRichtext(loclabel, ci.richtext);
loclabel = convertLaTeXCommands(loclabel);
}
@ -689,8 +690,7 @@ docstring const & BibTeXInfo::operator[](string const & field) const
docstring BibTeXInfo::getValueForKey(string const & oldkey, Buffer const & buf,
docstring const & before, docstring const & after, docstring const & dialog,
BibTeXInfoList const xrefs, size_t maxsize) const
CiteItem const & ci, BibTeXInfoList const xrefs, size_t maxsize) const
{
// anything less is pointless
LASSERT(maxsize >= 16, maxsize = 16);
@ -715,8 +715,8 @@ docstring BibTeXInfo::getValueForKey(string const & oldkey, Buffer const & buf,
if (ret.empty()) {
// some special keys
// FIXME: dialog, textbefore and textafter have nothing to do with this
if (key == "dialog")
ret = dialog;
if (key == "dialog" && ci.context == CiteItem::Dialog)
ret = from_ascii("x"); // any non-empty string will do
else if (key == "entrytype")
ret = entry_type_;
else if (key == "key")
@ -751,12 +751,11 @@ docstring BibTeXInfo::getValueForKey(string const & oldkey, Buffer const & buf,
docstring const & format =
from_utf8(dc.getCiteFormat(engine_type, to_utf8(entry_type_)));
int counter = 0;
ret = expandFormat(format, xrefs, counter, buf,
docstring(), docstring(), docstring(), false);
ret = expandFormat(format, xrefs, counter, buf, ci, false, false);
} else if (key == "textbefore")
ret = before;
ret = ci.textBefore;
else if (key == "textafter")
ret = after;
ret = ci.textAfter;
else if (key == "year")
ret = getYear();
}
@ -927,7 +926,7 @@ docstring const BiblioInfo::getYear(docstring const & key, Buffer const & buf, b
docstring const BiblioInfo::getInfo(docstring const & key,
Buffer const & buf, bool richtext) const
Buffer const & buf, CiteItem const & ci) const
{
BiblioInfo::const_iterator it = find(key);
if (it == end())
@ -944,15 +943,14 @@ docstring const BiblioInfo::getInfo(docstring const & key,
xrefptrs.push_back(&(xrefit->second));
}
}
return data.getInfo(xrefptrs, buf, richtext);
return data.getInfo(xrefptrs, buf, ci);
}
docstring const BiblioInfo::getLabel(vector<docstring> keys,
Buffer const & buf, string const & style, bool for_xhtml,
size_t max_size, docstring const & before, docstring const & after,
docstring const & dialog) const
Buffer const & buf, string const & style, CiteItem const & ci) const
{
size_t max_size = ci.max_size;
// shorter makes no sense
LASSERT(max_size >= 16, max_size = 16);
@ -986,8 +984,7 @@ docstring const BiblioInfo::getLabel(vector<docstring> keys,
}
}
}
ret = data.getLabel(xrefptrs, buf, ret, for_xhtml,
before, after, dialog, key + 1 != ken);
ret = data.getLabel(xrefptrs, buf, ret, ci, key + 1 != ken, i == 1);
}
if (too_many_keys)
@ -1010,8 +1007,7 @@ bool BiblioInfo::isBibtex(docstring const & key) const
vector<docstring> const BiblioInfo::getCiteStrings(
vector<docstring> const & keys, vector<CitationStyle> const & styles,
Buffer const & buf, docstring const & before,
docstring const & after, docstring const & dialog, size_t max_size) const
Buffer const & buf, CiteItem const & ci) const
{
if (empty())
return vector<docstring>();
@ -1020,7 +1016,7 @@ vector<docstring> const BiblioInfo::getCiteStrings(
vector<docstring> vec(styles.size());
for (size_t i = 0; i != vec.size(); ++i) {
style = styles[i].name;
vec[i] = getLabel(keys, buf, style, false, max_size, before, after, dialog);
vec[i] = getLabel(keys, buf, style, ci);
}
return vec;

View File

@ -65,12 +65,11 @@ public:
/// \return formatted BibTeX data suitable for framing.
/// \param vector of pointers to crossref/xdata information
docstring const & getInfo(BibTeXInfoList const xrefs,
Buffer const & buf, bool richtext) const;
Buffer const & buf, CiteItem const & ci) const;
/// \return formatted BibTeX data for a citation label
docstring const getLabel(BibTeXInfoList const xrefs,
Buffer const & buf, docstring const & format, bool richtext,
const docstring & before, const docstring & after,
const docstring & dialog, bool next = false) const;
Buffer const & buf, docstring const & format,
CiteItem const & ci, bool next = false, bool second = false) const;
///
const_iterator find(docstring const & f) const { return bimap_.find(f); }
///
@ -116,8 +115,7 @@ private:
/// to get the data from xref BibTeXInfo objects, which would normally
/// be the one referenced in the crossref or xdata field.
docstring getValueForKey(std::string const & key, Buffer const & buf,
docstring const & before, docstring const & after, docstring const & dialog,
BibTeXInfoList const xrefs, size_t maxsize = 4096) const;
CiteItem const & ci, BibTeXInfoList const xrefs, size_t maxsize = 4096) const;
/// replace %keys% in a format string with their values
/// called from getInfo()
/// format strings may contain:
@ -136,9 +134,8 @@ private:
/// so that things like "pp." and "vol." can be translated.
docstring expandFormat(docstring const & fmt,
BibTeXInfoList const xrefs, int & counter,
Buffer const & buf, docstring before = docstring(),
docstring after = docstring(), docstring dialog = docstring(),
bool next = false) const;
Buffer const & buf, CiteItem const & ci,
bool next = false, bool second = false) const;
/// true if from BibTeX; false if from bibliography environment
bool is_bibtex_;
/// the BibTeX key for this entry
@ -208,16 +205,15 @@ public:
/// \return formatted BibTeX data associated with a given key.
/// Empty if no info exists.
/// Note that this will retrieve data from the crossref or xdata as needed.
/// If \param richtext is true, then it will output any richtext tags
/// marked in the citation format and escape < and > elsewhere.
/// \param ci contains further context information, such as if it should
/// output any richtext tags marked in the citation format and escape < and >
/// elsewhere, and the general output context.
docstring const getInfo(docstring const & key, Buffer const & buf,
bool richtext = false) const;
CiteItem const & ci) const;
/// \return formatted BibTeX data for citation labels.
/// Citation labels can have more than one key.
docstring const getLabel(std::vector<docstring> keys,
Buffer const & buf, std::string const & style, bool for_xhtml,
size_t max_size, docstring const & before, docstring const & after,
docstring const & dialog = docstring()) const;
docstring const getLabel(std::vector<docstring> keys, Buffer const & buf,
std::string const & style, CiteItem const & ci) const;
/// Is this a reference from a bibtex database
/// or from a bibliography environment?
bool isBibtex(docstring const & key) const;
@ -226,8 +222,7 @@ public:
/// upon the active engine.
std::vector<docstring> const getCiteStrings(std::vector<docstring> const & keys,
std::vector<CitationStyle> const & styles, Buffer const & buf,
docstring const & before, docstring const & after, docstring const & dialog,
size_t max_size) const;
CiteItem const & ci) const;
/// A list of BibTeX keys cited in the current document, sorted by
/// the last name of the author.
/// Make sure you have called collectCitedEntries() before you try to

View File

@ -397,8 +397,10 @@ void GuiCitation::updateInfo(BiblioInfo const & bi, QModelIndex const & idx)
}
infoML->setToolTip(qt_("Sketchy preview of the selected citation"));
CiteItem ci;
ci.richtext = true;
QString const keytxt = toqstr(
bi.getInfo(qstring_to_ucs4(idx.data().toString()), documentBuffer(), true));
bi.getInfo(qstring_to_ucs4(idx.data().toString()), documentBuffer(), ci));
infoML->document()->setHtml(keytxt);
}
@ -678,13 +680,14 @@ void GuiCitation::findKey(BiblioInfo const & bi,
QStringList GuiCitation::citationStyles(BiblioInfo const & bi, size_t max_size)
{
docstring const before = qstring_to_ucs4(textBeforeED->text());
docstring const after = qstring_to_ucs4(textAfterED->text());
vector<docstring> const keys = to_docstring_vector(cited_keys_);
vector<CitationStyle> styles = citeStyles_;
// FIXME: pass a dictionary instead of individual before, after, dialog, etc.
vector<docstring> ret = bi.getCiteStrings(keys, styles, documentBuffer(),
before, after, from_utf8("dialog"), max_size);
CiteItem ci;
ci.textBefore = qstring_to_ucs4(textBeforeED->text());
ci.textAfter = qstring_to_ucs4(textAfterED->text());
ci.context = CiteItem::Dialog;
ci.max_size = max_size;
vector<docstring> ret = bi.getCiteStrings(keys, styles, documentBuffer(), ci);
return to_qstring_list(ret);
}

View File

@ -1543,9 +1543,6 @@ void MenuDefinition::expandCiteStyles(BufferView const * bv)
return;
}
docstring const & before = citinset->getParam("before");
docstring const & after = citinset->getParam("after");
size_t const n = cmd.size();
bool const force = isUpperCase(cmd[0]);
bool const star = cmd[n] == '*';
@ -1553,10 +1550,13 @@ void MenuDefinition::expandCiteStyles(BufferView const * bv)
vector<docstring> const keys = getVectorFromString(key);
vector<CitationStyle> const citeStyleList = buf->params().citeStyles();
static const size_t max_length = 40;
CiteItem ci;
ci.textBefore = citinset->getParam("before");
ci.textAfter = citinset->getParam("after");
ci.context = CiteItem::Dialog;
ci.max_size = 40;
vector<docstring> citeStrings =
buf->masterBibInfo().getCiteStrings(keys, citeStyleList, bv->buffer(),
before, after, from_utf8("dialog"), max_length);
buf->masterBibInfo().getCiteStrings(keys, citeStyleList, bv->buffer(), ci);
vector<docstring>::const_iterator cit = citeStrings.begin();
vector<docstring>::const_iterator end = citeStrings.end();

View File

@ -947,6 +947,10 @@ int InsetBibtex::plaintext(odocstringstream & os,
docstring refoutput;
refoutput += reflabel + "\n\n";
// Tell BiblioInfo our purpose
CiteItem ci;
ci.context = CiteItem::Export;
// Now we loop over the entries
vector<docstring>::const_iterator vit = cites.begin();
vector<docstring>::const_iterator const ven = cites.end();
@ -961,7 +965,7 @@ int InsetBibtex::plaintext(odocstringstream & os,
// FIXME Right now, we are calling BibInfo::getInfo on the key,
// which will give us all the cross-referenced info. But for every
// entry, so there's a lot of repitition. This should be fixed.
refoutput += bibinfo.getInfo(entry.key(), buffer(), false) + "\n\n";
refoutput += bibinfo.getInfo(entry.key(), buffer(), ci) + "\n\n";
}
os << refoutput;
return refoutput.size();
@ -980,6 +984,12 @@ docstring InsetBibtex::xhtml(XHTMLStream & xs, OutputParams const &) const
docstring const reflabel = buffer().B_("References");
// tell BiblioInfo our purpose
CiteItem ci;
ci.context = CiteItem::Export;
ci.richtext = true;
ci.max_key_size = UINT_MAX;
xs << html::StartTag("h2", "class='bibtex'")
<< reflabel
<< html::EndTag("h2")
@ -1010,7 +1020,7 @@ docstring InsetBibtex::xhtml(XHTMLStream & xs, OutputParams const &) const
// entry, so there's a lot of repitition. This should be fixed.
xs << html::StartTag("span", "class='bibtexinfo'")
<< XHTMLStream::ESCAPE_AND
<< bibinfo.getInfo(entry.key(), buffer(), true)
<< bibinfo.getInfo(entry.key(), buffer(), ci)
<< html::EndTag("span")
<< html::EndTag("div")
<< html::CR();

View File

@ -156,14 +156,16 @@ docstring InsetCitation::toolTip(BufferView const & bv, int, int) const
if (key.empty())
return _("No citations selected!");
CiteItem ci;
ci.richtext = true;
vector<docstring> keys = getVectorFromString(key);
if (keys.size() == 1)
return bi.getInfo(keys[0], buffer(), true);
return bi.getInfo(keys[0], buffer(), ci);
docstring tip;
tip += "<ol>";
for (docstring const & key : keys) {
docstring const key_info = bi.getInfo(key, buffer(), true);
docstring const key_info = bi.getInfo(key, buffer(), ci);
if (key_info.empty())
continue;
tip += "<li>" + key_info + "</li>";
@ -266,9 +268,6 @@ docstring InsetCitation::complexLabel(bool for_xhtml) const
if (!alias.empty())
cite_type = alias;
docstring const & before = getParam("before");
docstring const & after = getParam("after");
// FIXME: allow to add cite macros
/*
buffer().params().documentClass().addCiteMacro("!textbefore", to_utf8(before));
@ -276,7 +275,16 @@ docstring InsetCitation::complexLabel(bool for_xhtml) const
*/
docstring label;
vector<docstring> keys = getVectorFromString(key);
label = biblist.getLabel(keys, buffer(), cite_type, for_xhtml, UINT_MAX, before, after);
CiteItem ci;
ci.textBefore = getParam("before");
ci.textAfter = getParam("after");
ci.max_size = UINT_MAX;
if (for_xhtml) {
ci.max_key_size = UINT_MAX;
ci.context = CiteItem::Export;
}
ci.richtext = for_xhtml;
label = biblist.getLabel(keys, buffer(), cite_type, ci);
return label;
}