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

View File

@ -65,12 +65,11 @@ public:
/// \return formatted BibTeX data suitable for framing. /// \return formatted BibTeX data suitable for framing.
/// \param vector of pointers to crossref/xdata information /// \param vector of pointers to crossref/xdata information
docstring const & getInfo(BibTeXInfoList const xrefs, 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 /// \return formatted BibTeX data for a citation label
docstring const getLabel(BibTeXInfoList const xrefs, docstring const getLabel(BibTeXInfoList const xrefs,
Buffer const & buf, docstring const & format, bool richtext, Buffer const & buf, docstring const & format,
const docstring & before, const docstring & after, CiteItem const & ci, bool next = false, bool second = false) const;
const docstring & dialog, bool next = false) const;
/// ///
const_iterator find(docstring const & f) const { return bimap_.find(f); } 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 /// to get the data from xref BibTeXInfo objects, which would normally
/// be the one referenced in the crossref or xdata field. /// be the one referenced in the crossref or xdata field.
docstring getValueForKey(std::string const & key, Buffer const & buf, docstring getValueForKey(std::string const & key, Buffer const & buf,
docstring const & before, docstring const & after, docstring const & dialog, CiteItem const & ci, BibTeXInfoList const xrefs, size_t maxsize = 4096) const;
BibTeXInfoList const xrefs, size_t maxsize = 4096) const;
/// replace %keys% in a format string with their values /// replace %keys% in a format string with their values
/// called from getInfo() /// called from getInfo()
/// format strings may contain: /// format strings may contain:
@ -136,9 +134,8 @@ private:
/// so that things like "pp." and "vol." can be translated. /// so that things like "pp." and "vol." can be translated.
docstring expandFormat(docstring const & fmt, docstring expandFormat(docstring const & fmt,
BibTeXInfoList const xrefs, int & counter, BibTeXInfoList const xrefs, int & counter,
Buffer const & buf, docstring before = docstring(), Buffer const & buf, CiteItem const & ci,
docstring after = docstring(), docstring dialog = docstring(), bool next = false, bool second = false) const;
bool next = false) const;
/// true if from BibTeX; false if from bibliography environment /// true if from BibTeX; false if from bibliography environment
bool is_bibtex_; bool is_bibtex_;
/// the BibTeX key for this entry /// the BibTeX key for this entry
@ -208,16 +205,15 @@ public:
/// \return formatted BibTeX data associated with a given key. /// \return formatted BibTeX data associated with a given key.
/// Empty if no info exists. /// Empty if no info exists.
/// Note that this will retrieve data from the crossref or xdata as needed. /// 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 /// \param ci contains further context information, such as if it should
/// marked in the citation format and escape < and > elsewhere. /// 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, docstring const getInfo(docstring const & key, Buffer const & buf,
bool richtext = false) const; CiteItem const & ci) const;
/// \return formatted BibTeX data for citation labels. /// \return formatted BibTeX data for citation labels.
/// Citation labels can have more than one key. /// Citation labels can have more than one key.
docstring const getLabel(std::vector<docstring> keys, docstring const getLabel(std::vector<docstring> keys, Buffer const & buf,
Buffer const & buf, std::string const & style, bool for_xhtml, std::string const & style, CiteItem const & ci) const;
size_t max_size, docstring const & before, docstring const & after,
docstring const & dialog = docstring()) const;
/// Is this a reference from a bibtex database /// Is this a reference from a bibtex database
/// or from a bibliography environment? /// or from a bibliography environment?
bool isBibtex(docstring const & key) const; bool isBibtex(docstring const & key) const;
@ -226,8 +222,7 @@ public:
/// upon the active engine. /// upon the active engine.
std::vector<docstring> const getCiteStrings(std::vector<docstring> const & keys, std::vector<docstring> const getCiteStrings(std::vector<docstring> const & keys,
std::vector<CitationStyle> const & styles, Buffer const & buf, std::vector<CitationStyle> const & styles, Buffer const & buf,
docstring const & before, docstring const & after, docstring const & dialog, CiteItem const & ci) const;
size_t max_size) const;
/// A list of BibTeX keys cited in the current document, sorted by /// A list of BibTeX keys cited in the current document, sorted by
/// the last name of the author. /// the last name of the author.
/// Make sure you have called collectCitedEntries() before you try to /// 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")); infoML->setToolTip(qt_("Sketchy preview of the selected citation"));
CiteItem ci;
ci.richtext = true;
QString const keytxt = toqstr( 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); infoML->document()->setHtml(keytxt);
} }
@ -678,13 +680,14 @@ void GuiCitation::findKey(BiblioInfo const & bi,
QStringList GuiCitation::citationStyles(BiblioInfo const & bi, size_t max_size) 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<docstring> const keys = to_docstring_vector(cited_keys_);
vector<CitationStyle> styles = citeStyles_; vector<CitationStyle> styles = citeStyles_;
// FIXME: pass a dictionary instead of individual before, after, dialog, etc. CiteItem ci;
vector<docstring> ret = bi.getCiteStrings(keys, styles, documentBuffer(), ci.textBefore = qstring_to_ucs4(textBeforeED->text());
before, after, from_utf8("dialog"), max_size); 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); return to_qstring_list(ret);
} }

View File

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

View File

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

View File

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