2002-04-24 10:00:39 +00:00
|
|
|
/**
|
2007-04-25 16:11:45 +00:00
|
|
|
* \file InsetCitation.cpp
|
2002-09-25 14:26:13 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2000-07-07 07:46:37 +00:00
|
|
|
*
|
2002-09-25 14:26:13 +00:00
|
|
|
* \author Angus Leeming
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Herbert Voß
|
2002-09-25 14:26:13 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-04-24 10:00:39 +00:00
|
|
|
*/
|
2000-06-07 08:53:40 +00:00
|
|
|
|
2000-07-07 07:46:37 +00:00
|
|
|
#include <config.h>
|
2000-06-07 08:53:40 +00:00
|
|
|
|
2007-04-25 16:11:45 +00:00
|
|
|
#include "InsetCitation.h"
|
2003-09-05 09:01:27 +00:00
|
|
|
|
2010-10-31 00:31:57 +00:00
|
|
|
#include "BiblioInfo.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Buffer.h"
|
2008-03-10 15:01:57 +00:00
|
|
|
#include "buffer_funcs.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "BufferParams.h"
|
2009-03-25 12:23:43 +00:00
|
|
|
#include "BufferView.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "DispatchResult.h"
|
2001-07-19 14:12:37 +00:00
|
|
|
#include "LaTeXFeatures.h"
|
2009-11-30 22:59:18 +00:00
|
|
|
#include "output_xhtml.h"
|
2008-03-04 17:47:47 +00:00
|
|
|
#include "ParIterator.h"
|
|
|
|
#include "TocBackend.h"
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
2007-11-28 22:12:03 +00:00
|
|
|
#include "support/docstream.h"
|
2008-04-19 15:52:35 +00:00
|
|
|
#include "support/FileNameList.h"
|
2008-03-04 17:47:47 +00:00
|
|
|
#include "support/gettext.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/lstrings.h"
|
2000-07-15 23:51:46 +00:00
|
|
|
|
2006-10-09 14:21:11 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2010-01-09 22:30:56 +00:00
|
|
|
ParamInfo InsetCitation::param_info_;
|
|
|
|
|
|
|
|
|
|
|
|
InsetCitation::InsetCitation(Buffer * buf, InsetCommandParams const & p)
|
2010-10-29 00:46:21 +00:00
|
|
|
: InsetCommand(buf, p)
|
2010-01-09 22:30:56 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
ParamInfo const & InsetCitation::findInfo(string const & /* cmdName */)
|
|
|
|
{
|
|
|
|
// standard cite does only take one argument if jurabib is
|
|
|
|
// not used, but jurabib extends this to two arguments, so
|
|
|
|
// we have to allow both here. InsetCitation takes care that
|
|
|
|
// LaTeX output is nevertheless correct.
|
|
|
|
if (param_info_.empty()) {
|
|
|
|
param_info_.add("after", ParamInfo::LATEX_OPTIONAL);
|
|
|
|
param_info_.add("before", ParamInfo::LATEX_OPTIONAL);
|
|
|
|
param_info_.add("key", ParamInfo::LATEX_REQUIRED);
|
|
|
|
}
|
|
|
|
return param_info_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
namespace {
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
vector<string> const init_possible_cite_commands()
|
|
|
|
{
|
2007-09-11 22:31:24 +00:00
|
|
|
char const * const possible[] = {
|
2007-12-20 15:46:14 +00:00
|
|
|
"cite", "nocite", "citet", "citep", "citealt", "citealp",
|
2007-08-20 16:30:02 +00:00
|
|
|
"citeauthor", "citeyear", "citeyearpar",
|
|
|
|
"citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
|
|
|
|
"Citet", "Citep", "Citealt", "Citealp", "Citeauthor",
|
|
|
|
"Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
|
|
|
|
"fullcite",
|
|
|
|
"footcite", "footcitet", "footcitep", "footcitealt",
|
|
|
|
"footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
|
|
|
|
"citefield", "citetitle", "cite*"
|
|
|
|
};
|
2007-09-11 22:31:24 +00:00
|
|
|
size_t const size_possible = sizeof(possible) / sizeof(possible[0]);
|
2007-08-20 16:30:02 +00:00
|
|
|
|
2007-09-11 22:31:24 +00:00
|
|
|
return vector<string>(possible, possible + size_possible);
|
2007-08-20 16:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-15 00:02:41 +00:00
|
|
|
vector<string> const & possibleCiteCommands()
|
2007-08-20 16:30:02 +00:00
|
|
|
{
|
2007-09-11 22:31:24 +00:00
|
|
|
static vector<string> const possible = init_possible_cite_commands();
|
|
|
|
return possible;
|
2007-08-20 16:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-09 22:30:56 +00:00
|
|
|
} // anon namespace
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetCitation::isCompatibleCommand(string const & cmd)
|
|
|
|
{
|
|
|
|
vector<string> const & possibles = possibleCiteCommands();
|
|
|
|
vector<string>::const_iterator const end = possibles.end();
|
|
|
|
return find(possibles.begin(), end, cmd) != end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring InsetCitation::toolTip(BufferView const & bv, int, int) const
|
|
|
|
{
|
|
|
|
Buffer const & buf = bv.buffer();
|
|
|
|
// Only after the buffer is loaded from file...
|
|
|
|
if (!buf.isFullyLoaded())
|
|
|
|
return docstring();
|
|
|
|
|
|
|
|
BiblioInfo const & bi = buf.masterBibInfo();
|
|
|
|
if (bi.empty())
|
|
|
|
return _("No bibliography defined!");
|
|
|
|
|
|
|
|
docstring const & key = getParam("key");
|
|
|
|
if (key.empty())
|
|
|
|
return _("No citations selected!");
|
|
|
|
|
|
|
|
vector<docstring> keys = getVectorFromString(key);
|
|
|
|
vector<docstring>::const_iterator it = keys.begin();
|
|
|
|
vector<docstring>::const_iterator en = keys.end();
|
|
|
|
docstring tip;
|
|
|
|
for (; it != en; ++it) {
|
2010-03-29 18:37:25 +00:00
|
|
|
docstring const key_info = bi.getInfo(*it, buffer());
|
2010-01-09 22:30:56 +00:00
|
|
|
if (key_info.empty())
|
|
|
|
continue;
|
|
|
|
if (!tip.empty())
|
|
|
|
tip += "\n";
|
|
|
|
tip += wrap(key_info, -4);
|
|
|
|
}
|
|
|
|
return tip;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2008-03-15 00:02:41 +00:00
|
|
|
// FIXME See the header for the issue.
|
2008-04-20 15:00:11 +00:00
|
|
|
string defaultCiteCommand(CiteEngine engine)
|
2007-08-20 16:30:02 +00:00
|
|
|
{
|
|
|
|
string str;
|
|
|
|
switch (engine) {
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_BASIC:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = "cite";
|
|
|
|
break;
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_NATBIB_AUTHORYEAR:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = "citet";
|
|
|
|
break;
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_NATBIB_NUMERICAL:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = "citep";
|
|
|
|
break;
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_JURABIB:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = "cite";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2008-04-28 15:21:55 +00:00
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
string asValidLatexCommand(string const & input, CiteEngine const engine)
|
2007-08-20 16:30:02 +00:00
|
|
|
{
|
2008-03-15 00:02:41 +00:00
|
|
|
string const default_str = defaultCiteCommand(engine);
|
2007-10-25 04:13:56 +00:00
|
|
|
if (!InsetCitation::isCompatibleCommand(input))
|
2007-08-20 16:30:02 +00:00
|
|
|
return default_str;
|
|
|
|
|
|
|
|
string output;
|
|
|
|
switch (engine) {
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_BASIC:
|
2008-05-20 07:22:55 +00:00
|
|
|
if (input == "nocite")
|
|
|
|
output = input;
|
|
|
|
else
|
|
|
|
output = default_str;
|
2007-08-20 16:30:02 +00:00
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_NATBIB_AUTHORYEAR:
|
|
|
|
case ENGINE_NATBIB_NUMERICAL:
|
2008-05-20 07:22:55 +00:00
|
|
|
if (input == "cite" || input == "citefield"
|
|
|
|
|| input == "citetitle" || input == "cite*")
|
2007-08-20 16:30:02 +00:00
|
|
|
output = default_str;
|
|
|
|
else if (prefixIs(input, "foot"))
|
|
|
|
output = input.substr(4);
|
|
|
|
else
|
|
|
|
output = input;
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_JURABIB: {
|
2007-09-20 06:22:07 +00:00
|
|
|
// Jurabib does not support the 'uppercase' natbib style.
|
|
|
|
if (input[0] == 'C')
|
|
|
|
output = string(1, 'c') + input.substr(1);
|
|
|
|
else
|
|
|
|
output = input;
|
|
|
|
|
|
|
|
// Jurabib does not support the 'full' natbib style.
|
|
|
|
string::size_type const n = output.size() - 1;
|
|
|
|
if (output != "cite*" && output[n] == '*')
|
|
|
|
output = output.substr(0, n);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2007-08-20 16:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2010-01-11 16:11:55 +00:00
|
|
|
|
|
|
|
inline docstring wrapCitation(docstring const & key,
|
|
|
|
docstring const & content, bool for_xhtml)
|
|
|
|
{
|
|
|
|
if (!for_xhtml)
|
|
|
|
return content;
|
|
|
|
// we have to do the escaping here, because we will ultimately
|
|
|
|
// write this as a raw string, so as not to escape the tags.
|
2010-11-24 15:27:36 +00:00
|
|
|
return "<a href='#" + key + "'>" +
|
|
|
|
html::htmlize(content, XHTMLStream::ESCAPE_ALL) + "</a>";
|
2010-01-11 16:11:55 +00:00
|
|
|
}
|
|
|
|
|
2010-01-09 22:30:56 +00:00
|
|
|
} // anonymous namespace
|
2010-01-09 22:30:12 +00:00
|
|
|
|
2010-01-11 16:11:55 +00:00
|
|
|
docstring InsetCitation::generateLabel(bool for_xhtml) const
|
2010-01-09 22:30:12 +00:00
|
|
|
{
|
|
|
|
docstring label;
|
2010-01-11 16:11:55 +00:00
|
|
|
label = complexLabel(for_xhtml);
|
2010-01-09 22:30:12 +00:00
|
|
|
|
|
|
|
// Fallback to fail-safe
|
|
|
|
if (label.empty())
|
2010-01-11 16:11:55 +00:00
|
|
|
label = basicLabel(for_xhtml);
|
2010-01-09 22:30:12 +00:00
|
|
|
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-11 16:11:55 +00:00
|
|
|
docstring InsetCitation::complexLabel(bool for_xhtml) const
|
2000-06-07 08:53:40 +00:00
|
|
|
{
|
2010-01-09 22:30:12 +00:00
|
|
|
Buffer const & buf = buffer();
|
2003-10-22 13:15:18 +00:00
|
|
|
// Only start the process off after the buffer is loaded from file.
|
2010-01-09 22:30:12 +00:00
|
|
|
if (!buf.isFullyLoaded())
|
2006-12-17 10:52:04 +00:00
|
|
|
return docstring();
|
2003-10-22 13:15:18 +00:00
|
|
|
|
2010-01-09 22:30:12 +00:00
|
|
|
BiblioInfo const & biblist = buf.masterBibInfo();
|
2007-08-20 16:30:02 +00:00
|
|
|
if (biblist.empty())
|
2006-12-17 10:52:04 +00:00
|
|
|
return docstring();
|
2002-07-28 18:13:51 +00:00
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
// the natbib citation-styles
|
|
|
|
// CITET: author (year)
|
|
|
|
// CITEP: (author,year)
|
|
|
|
// CITEALT: author year
|
|
|
|
// CITEALP: author, year
|
|
|
|
// CITEAUTHOR: author
|
|
|
|
// CITEYEAR: year
|
|
|
|
// CITEYEARPAR: (year)
|
2004-03-07 14:33:17 +00:00
|
|
|
// jurabib supports these plus
|
|
|
|
// CITE: author/<before field>
|
2002-04-24 10:00:39 +00:00
|
|
|
|
2010-01-09 22:30:12 +00:00
|
|
|
CiteEngine const engine = buffer().params().citeEngine();
|
2002-05-14 09:47:12 +00:00
|
|
|
// We don't currently use the full or forceUCase fields.
|
2010-01-09 22:30:12 +00:00
|
|
|
string cite_type = asValidLatexCommand(getCmdName(), engine);
|
2004-05-14 15:27:13 +00:00
|
|
|
if (cite_type[0] == 'C')
|
2010-01-08 14:38:31 +00:00
|
|
|
// If we were going to use them, this would mean ForceUCase
|
2004-05-14 15:27:13 +00:00
|
|
|
cite_type = string(1, 'c') + cite_type.substr(1);
|
|
|
|
if (cite_type[cite_type.size() - 1] == '*')
|
2010-01-08 14:38:31 +00:00
|
|
|
// and this would mean FULL
|
2004-05-14 15:27:13 +00:00
|
|
|
cite_type = cite_type.substr(0, cite_type.size() - 1);
|
2002-07-28 18:13:51 +00:00
|
|
|
|
2010-01-09 22:30:12 +00:00
|
|
|
docstring const & before = getParam("before");
|
2006-12-17 10:52:04 +00:00
|
|
|
docstring before_str;
|
2002-04-24 10:00:39 +00:00
|
|
|
if (!before.empty()) {
|
|
|
|
// In CITET and CITEALT mode, the "before" string is
|
|
|
|
// attached to the label associated with each and every key.
|
|
|
|
// In CITEP, CITEALP and CITEYEARPAR mode, it is attached
|
|
|
|
// to the front of the whole only.
|
|
|
|
// In other modes, it is not used at all.
|
2002-05-14 09:47:12 +00:00
|
|
|
if (cite_type == "citet" ||
|
|
|
|
cite_type == "citealt" ||
|
|
|
|
cite_type == "citep" ||
|
2002-07-28 18:13:51 +00:00
|
|
|
cite_type == "citealp" ||
|
2002-05-14 09:47:12 +00:00
|
|
|
cite_type == "citeyearpar")
|
2002-04-24 10:00:39 +00:00
|
|
|
before_str = before + ' ';
|
2004-03-07 14:33:17 +00:00
|
|
|
// In CITE (jurabib), the "before" string is used to attach
|
2004-04-03 08:37:12 +00:00
|
|
|
// the annotator (of legal texts) to the author(s) of the
|
|
|
|
// first reference.
|
2004-03-07 14:33:17 +00:00
|
|
|
else if (cite_type == "cite")
|
|
|
|
before_str = '/' + before;
|
2002-04-24 10:00:39 +00:00
|
|
|
}
|
|
|
|
|
2010-01-09 22:30:12 +00:00
|
|
|
docstring const & after = getParam("after");
|
2006-12-17 10:52:04 +00:00
|
|
|
docstring after_str;
|
2007-12-20 15:46:14 +00:00
|
|
|
// The "after" key is appended only to the end of the whole.
|
|
|
|
if (cite_type == "nocite")
|
|
|
|
after_str = " (" + _("not cited") + ')';
|
|
|
|
else if (!after.empty()) {
|
2002-04-24 10:00:39 +00:00
|
|
|
after_str = ", " + after;
|
|
|
|
}
|
|
|
|
|
|
|
|
// One day, these might be tunable (as they are in BibTeX).
|
2007-12-20 15:46:14 +00:00
|
|
|
char op, cp; // opening and closing parenthesis.
|
2008-11-15 17:40:05 +00:00
|
|
|
const char * sep; // punctuation mark separating citation entries.
|
2008-04-20 15:00:11 +00:00
|
|
|
if (engine == ENGINE_BASIC) {
|
2007-12-20 15:46:14 +00:00
|
|
|
op = '[';
|
|
|
|
cp = ']';
|
|
|
|
sep = ",";
|
|
|
|
} else {
|
|
|
|
op = '(';
|
|
|
|
cp = ')';
|
|
|
|
sep = ";";
|
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
|
2007-10-20 10:03:45 +00:00
|
|
|
docstring const op_str = ' ' + docstring(1, op);
|
|
|
|
docstring const cp_str = docstring(1, cp) + ' ';
|
|
|
|
docstring const sep_str = from_ascii(sep) + ' ';
|
2000-07-19 08:37:26 +00:00
|
|
|
|
2006-12-17 10:52:04 +00:00
|
|
|
docstring label;
|
2010-01-09 22:30:12 +00:00
|
|
|
vector<docstring> keys = getVectorFromString(getParam("key"));
|
2007-08-20 16:30:02 +00:00
|
|
|
vector<docstring>::const_iterator it = keys.begin();
|
|
|
|
vector<docstring>::const_iterator end = keys.end();
|
2002-07-28 18:13:51 +00:00
|
|
|
for (; it != end; ++it) {
|
2002-04-24 10:00:39 +00:00
|
|
|
// get the bibdata corresponding to the key
|
2010-01-09 16:57:08 +00:00
|
|
|
docstring const author = biblist.getAbbreviatedAuthor(*it);
|
2010-01-11 16:11:55 +00:00
|
|
|
docstring const year = biblist.getYear(*it, for_xhtml);
|
|
|
|
docstring const citenum = for_xhtml ? biblist.getCiteNumber(*it) : *it;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
if (author.empty() || year.empty())
|
2010-01-11 16:11:55 +00:00
|
|
|
// We can't construct a "complex" label without that info.
|
|
|
|
// So fail safely.
|
2006-12-17 10:52:04 +00:00
|
|
|
return docstring();
|
2002-04-24 10:00:39 +00:00
|
|
|
|
2004-03-07 14:33:17 +00:00
|
|
|
// authors1/<before>; ... ;
|
|
|
|
// authors_last, <after>
|
2007-12-20 15:46:14 +00:00
|
|
|
if (cite_type == "cite") {
|
2008-04-20 15:00:11 +00:00
|
|
|
if (engine == ENGINE_BASIC) {
|
2010-01-11 16:11:55 +00:00
|
|
|
label += wrapCitation(*it, citenum, for_xhtml) + sep_str;
|
2008-04-20 15:00:11 +00:00
|
|
|
} else if (engine == ENGINE_JURABIB) {
|
2007-12-20 15:46:14 +00:00
|
|
|
if (it == keys.begin())
|
2010-01-11 16:11:55 +00:00
|
|
|
label += wrapCitation(*it, author, for_xhtml) + before_str + sep_str;
|
2007-12-20 15:46:14 +00:00
|
|
|
else
|
2010-01-11 16:11:55 +00:00
|
|
|
label += wrapCitation(*it, author, for_xhtml) + sep_str;
|
2007-12-20 15:46:14 +00:00
|
|
|
}
|
2010-01-11 16:11:55 +00:00
|
|
|
}
|
2007-12-20 15:46:14 +00:00
|
|
|
// nocite
|
2010-01-11 16:11:55 +00:00
|
|
|
else if (cite_type == "nocite") {
|
2007-12-20 15:46:14 +00:00
|
|
|
label += *it + sep_str;
|
2010-01-11 16:11:55 +00:00
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
// (authors1 (<before> year); ... ;
|
|
|
|
// authors_last (<before> year, <after>)
|
2010-01-11 16:11:55 +00:00
|
|
|
else if (cite_type == "citet") {
|
2004-05-13 20:44:35 +00:00
|
|
|
switch (engine) {
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_NATBIB_AUTHORYEAR:
|
2004-05-13 20:44:35 +00:00
|
|
|
label += author + op_str + before_str +
|
2010-01-11 16:11:55 +00:00
|
|
|
wrapCitation(*it, year, for_xhtml) + cp + sep_str;
|
2004-05-13 20:44:35 +00:00
|
|
|
break;
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_NATBIB_NUMERICAL:
|
2010-01-11 16:11:55 +00:00
|
|
|
label += author + op_str + before_str +
|
|
|
|
wrapCitation(*it, citenum, for_xhtml) + cp + sep_str;
|
2004-05-13 20:44:35 +00:00
|
|
|
break;
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_JURABIB:
|
2004-05-13 20:44:35 +00:00
|
|
|
label += before_str + author + op_str +
|
2010-01-11 16:11:55 +00:00
|
|
|
wrapCitation(*it, year, for_xhtml) + cp + sep_str;
|
2004-05-13 20:44:35 +00:00
|
|
|
break;
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_BASIC:
|
2004-05-13 20:44:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-01-11 16:11:55 +00:00
|
|
|
}
|
|
|
|
// author, year; author, year; ...
|
|
|
|
else if (cite_type == "citep" ||
|
2002-05-14 09:47:12 +00:00
|
|
|
cite_type == "citealp") {
|
2008-04-20 15:00:11 +00:00
|
|
|
if (engine == ENGINE_NATBIB_NUMERICAL) {
|
2010-01-11 16:11:55 +00:00
|
|
|
label += wrapCitation(*it, citenum, for_xhtml) + sep_str;
|
2002-04-24 10:00:39 +00:00
|
|
|
} else {
|
2010-01-11 16:11:55 +00:00
|
|
|
label += wrapCitation(*it, author + ", " + year, for_xhtml) + sep_str;
|
2002-04-24 10:00:39 +00:00
|
|
|
}
|
|
|
|
|
2010-01-11 16:11:55 +00:00
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
// (authors1 <before> year;
|
|
|
|
// authors_last <before> year, <after>)
|
2010-01-11 16:11:55 +00:00
|
|
|
else if (cite_type == "citealt") {
|
2004-05-13 20:44:35 +00:00
|
|
|
switch (engine) {
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_NATBIB_AUTHORYEAR:
|
2004-05-13 20:44:35 +00:00
|
|
|
label += author + ' ' + before_str +
|
2010-01-11 16:11:55 +00:00
|
|
|
wrapCitation(*it, year, for_xhtml) + sep_str;
|
2004-05-13 20:44:35 +00:00
|
|
|
break;
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_NATBIB_NUMERICAL:
|
2010-01-11 16:11:55 +00:00
|
|
|
label += author + ' ' + before_str + '#' +
|
|
|
|
wrapCitation(*it, citenum, for_xhtml) + sep_str;
|
2004-05-13 20:44:35 +00:00
|
|
|
break;
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_JURABIB:
|
2010-01-11 16:11:55 +00:00
|
|
|
label += before_str +
|
|
|
|
wrapCitation(*it, author + ' ' + year, for_xhtml) + sep_str;
|
2004-05-13 20:44:35 +00:00
|
|
|
break;
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_BASIC:
|
2004-05-13 20:44:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
|
2010-01-11 16:11:55 +00:00
|
|
|
|
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
// author; author; ...
|
2010-01-11 16:11:55 +00:00
|
|
|
else if (cite_type == "citeauthor") {
|
|
|
|
label += wrapCitation(*it, author, for_xhtml) + sep_str;
|
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
// year; year; ...
|
2010-01-11 16:11:55 +00:00
|
|
|
else if (cite_type == "citeyear" ||
|
2002-05-14 09:47:12 +00:00
|
|
|
cite_type == "citeyearpar") {
|
2010-01-11 16:11:55 +00:00
|
|
|
label += wrapCitation(*it, year, for_xhtml) + sep_str;
|
2002-04-24 10:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-28 22:50:13 +00:00
|
|
|
label = rtrim(rtrim(label), sep);
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
if (!after_str.empty()) {
|
2002-05-14 09:47:12 +00:00
|
|
|
if (cite_type == "citet") {
|
2002-04-24 10:00:39 +00:00
|
|
|
// insert "after" before last ')'
|
2002-07-28 18:13:51 +00:00
|
|
|
label.insert(label.size() - 1, after_str);
|
2002-04-24 10:00:39 +00:00
|
|
|
} else {
|
2004-05-13 20:44:35 +00:00
|
|
|
bool const add =
|
2008-04-20 15:00:11 +00:00
|
|
|
!(engine == ENGINE_NATBIB_NUMERICAL &&
|
2004-05-13 20:44:35 +00:00
|
|
|
(cite_type == "citeauthor" ||
|
|
|
|
cite_type == "citeyear"));
|
2002-04-24 10:00:39 +00:00
|
|
|
if (add)
|
|
|
|
label += after_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-14 09:47:12 +00:00
|
|
|
if (!before_str.empty() && (cite_type == "citep" ||
|
2002-07-28 18:13:51 +00:00
|
|
|
cite_type == "citealp" ||
|
2002-05-14 09:47:12 +00:00
|
|
|
cite_type == "citeyearpar")) {
|
2002-04-24 10:00:39 +00:00
|
|
|
label = before_str + label;
|
|
|
|
}
|
|
|
|
|
2007-12-20 15:46:14 +00:00
|
|
|
if (cite_type == "citep" || cite_type == "citeyearpar" ||
|
2008-04-20 15:00:11 +00:00
|
|
|
(cite_type == "cite" && engine == ENGINE_BASIC) )
|
2006-12-17 10:52:04 +00:00
|
|
|
label = op + label + cp;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-11 16:11:55 +00:00
|
|
|
docstring InsetCitation::basicLabel(bool for_xhtml) const
|
2002-04-24 10:00:39 +00:00
|
|
|
{
|
2010-01-09 22:30:12 +00:00
|
|
|
docstring keys = getParam("key");
|
2006-12-17 10:52:04 +00:00
|
|
|
docstring label;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
2010-01-21 19:58:21 +00:00
|
|
|
docstring key;
|
|
|
|
do {
|
|
|
|
// if there is no comma, then everything goes into key
|
|
|
|
// and keys will be empty.
|
|
|
|
keys = trim(split(keys, key, ','));
|
|
|
|
key = trim(key);
|
|
|
|
if (!label.empty())
|
|
|
|
label += ", ";
|
|
|
|
label += wrapCitation(key, key, for_xhtml);
|
|
|
|
} while (!keys.empty());
|
2002-04-24 10:00:39 +00:00
|
|
|
|
2010-01-09 22:30:12 +00:00
|
|
|
docstring const & after = getParam("after");
|
2002-04-24 10:00:39 +00:00
|
|
|
if (!after.empty())
|
|
|
|
label += ", " + after;
|
2002-07-28 18:13:51 +00:00
|
|
|
|
2002-11-27 10:30:28 +00:00
|
|
|
return '[' + label + ']';
|
2002-04-24 10:00:39 +00:00
|
|
|
}
|
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
docstring InsetCitation::screenLabel() const
|
2008-03-04 17:47:47 +00:00
|
|
|
{
|
|
|
|
return cache.screen_label;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-03 22:13:45 +00:00
|
|
|
void InsetCitation::updateBuffer(ParIterator const &, UpdateType)
|
2002-04-24 10:00:39 +00:00
|
|
|
{
|
2008-04-20 15:00:11 +00:00
|
|
|
CiteEngine const engine = buffer().params().citeEngine();
|
2004-05-10 20:55:00 +00:00
|
|
|
if (cache.params == params() && cache.engine == engine)
|
2008-03-04 17:47:47 +00:00
|
|
|
return;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
// The label has changed, so we have to re-create it.
|
2008-02-27 20:43:16 +00:00
|
|
|
docstring const glabel = generateLabel();
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
unsigned int const maxLabelChars = 45;
|
|
|
|
|
2006-10-11 19:40:50 +00:00
|
|
|
docstring label = glabel;
|
2002-04-24 10:00:39 +00:00
|
|
|
if (label.size() > maxLabelChars) {
|
2010-02-26 20:17:29 +00:00
|
|
|
label.erase(maxLabelChars - 3);
|
2002-04-24 10:00:39 +00:00
|
|
|
label += "...";
|
|
|
|
}
|
|
|
|
|
2004-05-10 20:55:00 +00:00
|
|
|
cache.engine = engine;
|
2002-04-24 10:00:39 +00:00
|
|
|
cache.params = params();
|
|
|
|
cache.generated_label = glabel;
|
|
|
|
cache.screen_label = label;
|
2008-03-04 17:47:47 +00:00
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
|
2008-03-04 17:47:47 +00:00
|
|
|
|
2008-05-13 08:23:44 +00:00
|
|
|
void InsetCitation::addToToc(DocIterator const & cpit)
|
2008-03-04 17:47:47 +00:00
|
|
|
{
|
2010-07-30 15:50:42 +00:00
|
|
|
// NOTE
|
|
|
|
// XHTML output uses the TOC to collect the citations
|
|
|
|
// from the document. So if this gets changed, then we
|
|
|
|
// will need to change how the citations are collected.
|
|
|
|
docstring const tocitem = getParam("key");
|
2008-03-04 17:47:47 +00:00
|
|
|
Toc & toc = buffer().tocBackend().toc("citation");
|
2010-07-30 15:50:42 +00:00
|
|
|
toc.push_back(TocItem(cpit, 0, tocitem));
|
2000-06-07 08:53:40 +00:00
|
|
|
}
|
2000-07-14 07:52:03 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
int InsetCitation::plaintext(odocstream & os, OutputParams const &) const
|
2001-05-27 17:51:16 +00:00
|
|
|
{
|
2008-03-10 15:01:57 +00:00
|
|
|
os << cache.generated_label;
|
|
|
|
return cache.generated_label.size();
|
2001-05-27 17:51:16 +00:00
|
|
|
}
|
2001-07-19 14:12:37 +00:00
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
|
2007-09-11 22:31:24 +00:00
|
|
|
static docstring const cleanupWhitespace(docstring const & citelist)
|
2004-10-21 22:39:20 +00:00
|
|
|
{
|
2006-12-17 10:52:04 +00:00
|
|
|
docstring::const_iterator it = citelist.begin();
|
|
|
|
docstring::const_iterator end = citelist.end();
|
2004-10-21 22:39:20 +00:00
|
|
|
// Paranoia check: make sure that there is no whitespace in here
|
|
|
|
// -- at least not behind commas or at the beginning
|
2006-12-17 10:52:04 +00:00
|
|
|
docstring result;
|
|
|
|
char_type last = ',';
|
2004-10-21 22:39:20 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
if (*it != ' ')
|
|
|
|
last = *it;
|
|
|
|
if (*it != ' ' || last != ',')
|
|
|
|
result += *it;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
int InsetCitation::docbook(odocstream & os, OutputParams const &) const
|
2004-10-21 22:39:20 +00:00
|
|
|
{
|
2007-11-28 22:12:03 +00:00
|
|
|
os << from_ascii("<citation>")
|
2007-05-28 22:27:45 +00:00
|
|
|
<< cleanupWhitespace(getParam("key"))
|
2007-11-28 22:12:03 +00:00
|
|
|
<< from_ascii("</citation>");
|
2004-10-21 22:39:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-30 22:59:18 +00:00
|
|
|
docstring InsetCitation::xhtml(XHTMLStream & xs, OutputParams const &) const
|
2009-06-11 21:04:15 +00:00
|
|
|
{
|
2010-01-08 18:49:32 +00:00
|
|
|
string const & cmd = getCmdName();
|
|
|
|
if (cmd == "nocite")
|
|
|
|
return docstring();
|
|
|
|
|
2010-01-11 16:11:55 +00:00
|
|
|
// have to output this raw, because generateLabel() will include tags
|
2010-11-24 15:27:36 +00:00
|
|
|
xs << XHTMLStream::ESCAPE_NONE << generateLabel(true);
|
2009-06-11 21:04:15 +00:00
|
|
|
|
2009-06-12 17:23:17 +00:00
|
|
|
return docstring();
|
2009-06-11 21:04:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 01:34:04 +00:00
|
|
|
void InsetCitation::toString(odocstream & os) const
|
2005-11-25 14:40:34 +00:00
|
|
|
{
|
2008-02-27 20:43:16 +00:00
|
|
|
plaintext(os, OutputParams(0));
|
2005-11-25 14:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-20 21:55:09 +00:00
|
|
|
void InsetCitation::forToc(docstring & os, size_t) const
|
|
|
|
{
|
|
|
|
os += screenLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-19 14:12:37 +00:00
|
|
|
// Have to overwrite the default InsetCommand method in order to check that
|
|
|
|
// the \cite command is valid. Eg, the user has natbib enabled, inputs some
|
|
|
|
// citations and then changes his mind, turning natbib support off. The output
|
|
|
|
// should revert to \cite[]{}
|
2009-05-07 10:57:52 +00:00
|
|
|
int InsetCitation::latex(odocstream & os, OutputParams const & runparams) const
|
2001-07-19 14:12:37 +00:00
|
|
|
{
|
2008-04-20 15:00:11 +00:00
|
|
|
CiteEngine cite_engine = buffer().params().citeEngine();
|
2010-02-12 16:08:30 +00:00
|
|
|
BiblioInfo const & bi = buffer().masterBibInfo();
|
2006-10-19 16:51:30 +00:00
|
|
|
// FIXME UNICODE
|
2006-10-21 00:16:43 +00:00
|
|
|
docstring const cite_str = from_utf8(
|
2007-08-20 16:30:02 +00:00
|
|
|
asValidLatexCommand(getCmdName(), cite_engine));
|
2004-10-05 10:11:42 +00:00
|
|
|
|
2009-05-07 10:57:52 +00:00
|
|
|
if (runparams.inulemcmd)
|
|
|
|
os << "\\mbox{";
|
|
|
|
|
2004-05-14 15:27:13 +00:00
|
|
|
os << "\\" << cite_str;
|
2004-10-05 10:11:42 +00:00
|
|
|
|
2006-10-19 16:51:30 +00:00
|
|
|
docstring const & before = getParam("before");
|
|
|
|
docstring const & after = getParam("after");
|
2008-04-20 15:00:11 +00:00
|
|
|
if (!before.empty() && cite_engine != ENGINE_BASIC)
|
2002-11-27 10:30:28 +00:00
|
|
|
os << '[' << before << "][" << after << ']';
|
2002-04-24 10:00:39 +00:00
|
|
|
else if (!after.empty())
|
2002-11-27 10:30:28 +00:00
|
|
|
os << '[' << after << ']';
|
2004-03-05 14:49:10 +00:00
|
|
|
|
2010-02-12 16:08:30 +00:00
|
|
|
if (!bi.isBibtex(getParam("key")))
|
|
|
|
// escape chars with bibitems
|
|
|
|
os << '{' << escape(cleanupWhitespace(getParam("key"))) << '}';
|
|
|
|
else
|
|
|
|
os << '{' << cleanupWhitespace(getParam("key")) << '}';
|
2001-07-19 14:12:37 +00:00
|
|
|
|
2009-05-07 10:57:52 +00:00
|
|
|
if (runparams.inulemcmd)
|
|
|
|
os << "}";
|
|
|
|
|
2001-07-19 14:12:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetCitation::validate(LaTeXFeatures & features) const
|
|
|
|
{
|
2008-03-15 00:02:41 +00:00
|
|
|
switch (features.bufferParams().citeEngine()) {
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_BASIC:
|
2004-05-13 20:44:35 +00:00
|
|
|
break;
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_NATBIB_AUTHORYEAR:
|
|
|
|
case ENGINE_NATBIB_NUMERICAL:
|
2001-11-19 15:34:11 +00:00
|
|
|
features.require("natbib");
|
2004-05-13 20:44:35 +00:00
|
|
|
break;
|
2008-04-20 15:00:11 +00:00
|
|
|
case ENGINE_JURABIB:
|
2004-03-07 14:33:17 +00:00
|
|
|
features.require("jurabib");
|
2004-05-13 20:44:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-07-19 14:12:37 +00:00
|
|
|
}
|
2006-10-09 14:21:11 +00:00
|
|
|
|
|
|
|
|
2010-11-29 21:27:25 +00:00
|
|
|
docstring InsetCitation::contextMenuName() const
|
2008-04-28 15:21:55 +00:00
|
|
|
{
|
|
|
|
return from_ascii("context-citation");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|