2007-08-14 16:50:51 +00:00
|
|
|
/**
|
2007-08-20 16:30:02 +00:00
|
|
|
* \file BiblioInfo.cpp
|
2007-08-14 16:50:51 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Angus Leeming
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Herbert Voß
|
2007-08-20 16:30:02 +00:00
|
|
|
* \author Richard Heck
|
2007-08-14 16:50:51 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
2007-08-14 22:18:27 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2007-08-20 17:04:36 +00:00
|
|
|
#include "BiblioInfo.h"
|
2007-08-20 16:30:02 +00:00
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferParams.h"
|
2007-08-15 08:57:58 +00:00
|
|
|
#include "buffer_funcs.h"
|
2007-08-14 16:50:51 +00:00
|
|
|
#include "InsetIterator.h"
|
|
|
|
#include "Paragraph.h"
|
|
|
|
|
|
|
|
#include "insets/Inset.h"
|
|
|
|
#include "insets/InsetBibitem.h"
|
|
|
|
#include "insets/InsetBibtex.h"
|
|
|
|
#include "insets/InsetInclude.h"
|
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2007-11-01 22:17:22 +00:00
|
|
|
#include "support/docstream.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/gettext.h"
|
|
|
|
#include "support/lstrings.h"
|
2007-08-14 16:50:51 +00:00
|
|
|
|
2007-08-14 22:18:27 +00:00
|
|
|
#include "boost/regex.hpp"
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2007-08-14 16:50:51 +00:00
|
|
|
|
2007-11-05 20:33:20 +00:00
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
namespace lyx {
|
2007-08-14 16:50:51 +00:00
|
|
|
|
2007-11-05 20:33:20 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
2007-08-20 16:30:02 +00:00
|
|
|
//
|
|
|
|
// BibTeXInfo
|
|
|
|
//
|
2007-11-05 20:33:20 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
2007-08-14 16:50:51 +00:00
|
|
|
|
2008-02-14 05:00:54 +00:00
|
|
|
BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
|
2008-02-14 17:00:40 +00:00
|
|
|
: is_bibtex_(true), bib_key_(key), entry_type_(type)
|
2007-08-16 01:59:20 +00:00
|
|
|
{}
|
|
|
|
|
2008-04-25 20:03:03 +00:00
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
bool BibTeXInfo::hasField(docstring const & field) const
|
|
|
|
{
|
|
|
|
return count(field) == 1;
|
|
|
|
}
|
2007-08-16 01:59:20 +00:00
|
|
|
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring const & BibTeXInfo::getValueForField(docstring const & field) const
|
2007-08-16 01:59:20 +00:00
|
|
|
{
|
2007-08-20 16:30:02 +00:00
|
|
|
BibTeXInfo::const_iterator it = find(field);
|
|
|
|
if (it != end())
|
|
|
|
return it->second;
|
|
|
|
static docstring const empty_value = docstring();
|
|
|
|
return empty_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring const & BibTeXInfo::getValueForField(string const & field) const
|
|
|
|
{
|
|
|
|
return getValueForField(from_ascii(field));
|
2007-08-16 01:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-25 20:03:03 +00:00
|
|
|
docstring familyName(docstring const & name)
|
2007-11-05 20:33:20 +00:00
|
|
|
{
|
|
|
|
if (name.empty())
|
|
|
|
return docstring();
|
2007-08-14 16:50:51 +00:00
|
|
|
|
2007-11-05 20:33:20 +00:00
|
|
|
// Very simple parser
|
|
|
|
docstring fname = name;
|
|
|
|
|
|
|
|
// possible authorname combinations are:
|
|
|
|
// "Surname, FirstName"
|
|
|
|
// "Surname, F."
|
|
|
|
// "FirstName Surname"
|
|
|
|
// "F. Surname"
|
|
|
|
docstring::size_type idx = fname.find(',');
|
|
|
|
if (idx != docstring::npos)
|
|
|
|
return ltrim(fname.substr(0, idx));
|
|
|
|
idx = fname.rfind('.');
|
|
|
|
if (idx != docstring::npos && idx + 1 < fname.size())
|
|
|
|
fname = ltrim(fname.substr(idx + 1));
|
|
|
|
// test if we have a LaTeX Space in front
|
|
|
|
if (fname[0] == '\\')
|
|
|
|
return fname.substr(2);
|
|
|
|
return rtrim(fname);
|
|
|
|
}
|
2007-08-14 16:50:51 +00:00
|
|
|
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring const BibTeXInfo::getAbbreviatedAuthor() const
|
2007-08-14 16:50:51 +00:00
|
|
|
{
|
2008-02-14 17:00:40 +00:00
|
|
|
if (!is_bibtex_)
|
2007-08-16 01:59:20 +00:00
|
|
|
return docstring();
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring author = getValueForField("author");
|
2007-08-16 01:59:20 +00:00
|
|
|
|
2007-08-14 16:50:51 +00:00
|
|
|
if (author.empty()) {
|
2007-08-20 16:30:02 +00:00
|
|
|
author = getValueForField("editor");
|
|
|
|
if (author.empty())
|
2008-02-14 17:00:40 +00:00
|
|
|
return bib_key_;
|
2007-08-14 16:50:51 +00:00
|
|
|
}
|
|
|
|
|
2007-11-05 20:33:20 +00:00
|
|
|
// OK, we've got some names. Let's format them.
|
|
|
|
// Try to split the author list on " and "
|
2007-08-20 16:30:02 +00:00
|
|
|
vector<docstring> const authors =
|
|
|
|
getVectorFromString(author, from_ascii(" and "));
|
2007-08-16 01:59:20 +00:00
|
|
|
|
2007-08-14 16:50:51 +00:00
|
|
|
if (authors.size() == 2)
|
|
|
|
return bformat(_("%1$s and %2$s"),
|
2007-08-20 16:30:02 +00:00
|
|
|
familyName(authors[0]), familyName(authors[1]));
|
2007-11-05 20:33:20 +00:00
|
|
|
|
|
|
|
if (authors.size() > 2)
|
2007-08-14 16:50:51 +00:00
|
|
|
return bformat(_("%1$s et al."), familyName(authors[0]));
|
2007-11-05 20:33:20 +00:00
|
|
|
|
|
|
|
return familyName(authors[0]);
|
2007-08-14 16:50:51 +00:00
|
|
|
}
|
|
|
|
|
2007-11-05 20:33:20 +00:00
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring const BibTeXInfo::getYear() const
|
2007-08-14 16:50:51 +00:00
|
|
|
{
|
2008-02-14 17:00:40 +00:00
|
|
|
if (!is_bibtex_)
|
2007-08-16 01:59:20 +00:00
|
|
|
return docstring();
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring year = getValueForField("year");
|
2007-08-14 16:50:51 +00:00
|
|
|
if (year.empty())
|
|
|
|
year = _("No year");
|
|
|
|
return year;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring const BibTeXInfo::getInfo() const
|
2007-08-14 16:50:51 +00:00
|
|
|
{
|
2008-02-14 17:00:40 +00:00
|
|
|
if (!is_bibtex_) {
|
2007-08-20 16:30:02 +00:00
|
|
|
BibTeXInfo::const_iterator it = find(from_ascii("ref"));
|
|
|
|
return it->second;
|
2007-08-14 16:50:51 +00:00
|
|
|
}
|
2007-08-16 01:59:20 +00:00
|
|
|
|
2007-11-05 20:33:20 +00:00
|
|
|
// FIXME
|
|
|
|
// This could be made a lot better using the entryType
|
|
|
|
// field to customize the output based upon entry type.
|
2007-08-16 01:59:20 +00:00
|
|
|
|
2007-11-05 20:33:20 +00:00
|
|
|
// Search for all possible "required" fields
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring author = getValueForField("author");
|
2007-08-14 16:50:51 +00:00
|
|
|
if (author.empty())
|
2007-08-20 16:30:02 +00:00
|
|
|
author = getValueForField("editor");
|
2007-08-16 01:59:20 +00:00
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring year = getValueForField("year");
|
|
|
|
docstring title = getValueForField("title");
|
|
|
|
docstring docLoc = getValueForField("pages");
|
2007-08-16 01:59:20 +00:00
|
|
|
if (docLoc.empty()) {
|
2007-08-20 16:30:02 +00:00
|
|
|
docLoc = getValueForField("chapter");
|
2007-08-16 01:59:20 +00:00
|
|
|
if (!docLoc.empty())
|
|
|
|
docLoc = from_ascii("Ch. ") + docLoc;
|
2007-11-05 20:33:20 +00:00
|
|
|
} else {
|
2007-08-16 01:59:20 +00:00
|
|
|
docLoc = from_ascii("pp. ") + docLoc;
|
2007-11-05 20:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
docstring media = getValueForField("journal");
|
|
|
|
if (media.empty()) {
|
|
|
|
media = getValueForField("publisher");
|
2007-08-16 01:59:20 +00:00
|
|
|
if (media.empty()) {
|
2007-11-05 20:33:20 +00:00
|
|
|
media = getValueForField("school");
|
|
|
|
if (media.empty())
|
|
|
|
media = getValueForField("institution");
|
2007-08-16 01:59:20 +00:00
|
|
|
}
|
2007-11-05 20:33:20 +00:00
|
|
|
}
|
|
|
|
docstring volume = getValueForField("volume");
|
|
|
|
|
|
|
|
odocstringstream result;
|
|
|
|
if (!author.empty())
|
|
|
|
result << author << ", ";
|
|
|
|
if (!title.empty())
|
|
|
|
result << title;
|
|
|
|
if (!media.empty())
|
|
|
|
result << ", " << media;
|
|
|
|
if (!year.empty())
|
|
|
|
result << ", " << year;
|
|
|
|
if (!docLoc.empty())
|
|
|
|
result << ", " << docLoc;
|
|
|
|
|
|
|
|
docstring const result_str = rtrim(result.str());
|
|
|
|
if (!result_str.empty())
|
|
|
|
return result_str;
|
|
|
|
|
|
|
|
// This should never happen (or at least be very unusual!)
|
|
|
|
return docstring();
|
2007-08-14 16:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-05 20:33:20 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
2007-08-20 16:30:02 +00:00
|
|
|
//
|
|
|
|
// BiblioInfo
|
|
|
|
//
|
2007-11-05 20:33:20 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
2007-08-20 16:30:02 +00:00
|
|
|
|
2007-08-14 16:50:51 +00:00
|
|
|
namespace {
|
2007-12-12 19:28:07 +00:00
|
|
|
// A functor for use with sort, leading to case insensitive sorting
|
|
|
|
class compareNoCase: public binary_function<docstring, docstring, bool>
|
2007-08-20 16:30:02 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool operator()(docstring const & s1, docstring const & s2) const {
|
|
|
|
return compare_no_case(s1, s2) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace anon
|
|
|
|
|
2007-08-14 16:50:51 +00:00
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
vector<docstring> const BiblioInfo::getKeys() const
|
2007-08-14 16:50:51 +00:00
|
|
|
{
|
2007-08-20 16:30:02 +00:00
|
|
|
vector<docstring> bibkeys;
|
|
|
|
BiblioInfo::const_iterator it = begin();
|
|
|
|
for (; it != end(); ++it)
|
|
|
|
bibkeys.push_back(it->first);
|
2007-12-12 19:28:07 +00:00
|
|
|
sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
|
2007-08-20 16:30:02 +00:00
|
|
|
return bibkeys;
|
2007-08-14 16:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
vector<docstring> const BiblioInfo::getFields() const
|
2007-08-14 16:50:51 +00:00
|
|
|
{
|
2007-08-20 16:30:02 +00:00
|
|
|
vector<docstring> bibfields;
|
2008-02-14 17:00:40 +00:00
|
|
|
set<docstring>::const_iterator it = field_names_.begin();
|
|
|
|
set<docstring>::const_iterator end = field_names_.end();
|
2007-08-20 16:30:02 +00:00
|
|
|
for (; it != end; ++it)
|
|
|
|
bibfields.push_back(*it);
|
2007-12-12 19:28:07 +00:00
|
|
|
sort(bibfields.begin(), bibfields.end());
|
2007-08-20 16:30:02 +00:00
|
|
|
return bibfields;
|
|
|
|
}
|
2007-08-14 16:50:51 +00:00
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
|
|
|
|
vector<docstring> const BiblioInfo::getEntries() const
|
|
|
|
{
|
|
|
|
vector<docstring> bibentries;
|
2008-02-14 17:00:40 +00:00
|
|
|
set<docstring>::const_iterator it = entry_types_.begin();
|
|
|
|
set<docstring>::const_iterator end = entry_types_.end();
|
2007-08-20 16:30:02 +00:00
|
|
|
for (; it != end; ++it)
|
|
|
|
bibentries.push_back(*it);
|
2007-12-12 19:28:07 +00:00
|
|
|
sort(bibentries.begin(), bibentries.end());
|
2007-08-20 16:30:02 +00:00
|
|
|
return bibentries;
|
|
|
|
}
|
2007-08-14 16:50:51 +00:00
|
|
|
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
|
2007-08-14 16:50:51 +00:00
|
|
|
{
|
2007-08-20 16:30:02 +00:00
|
|
|
BiblioInfo::const_iterator it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return docstring();
|
|
|
|
BibTeXInfo const & data = it->second;
|
|
|
|
return data.getAbbreviatedAuthor();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring const BiblioInfo::getYear(docstring const & key) const
|
|
|
|
{
|
|
|
|
BiblioInfo::const_iterator it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return docstring();
|
|
|
|
BibTeXInfo const & data = it->second;
|
|
|
|
return data.getYear();
|
2007-08-14 16:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring const BiblioInfo::getInfo(docstring const & key) const
|
2007-08-14 16:50:51 +00:00
|
|
|
{
|
2007-08-20 16:30:02 +00:00
|
|
|
BiblioInfo::const_iterator it = find(key);
|
|
|
|
if (it == end())
|
2007-08-14 16:50:51 +00:00
|
|
|
return docstring();
|
2007-08-20 16:30:02 +00:00
|
|
|
BibTeXInfo const & data = it->second;
|
|
|
|
return data.getInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<docstring> const BiblioInfo::getCiteStrings(
|
|
|
|
docstring const & key, Buffer const & buf) const
|
|
|
|
{
|
2008-04-20 15:00:11 +00:00
|
|
|
CiteEngine const engine = buf.params().citeEngine();
|
|
|
|
if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
|
2007-08-20 16:30:02 +00:00
|
|
|
return getNumericalStrings(key, buf);
|
|
|
|
else
|
|
|
|
return getAuthorYearStrings(key, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<docstring> const BiblioInfo::getNumericalStrings(
|
|
|
|
docstring const & key, Buffer const & buf) const
|
|
|
|
{
|
|
|
|
if (empty())
|
|
|
|
return vector<docstring>();
|
|
|
|
|
|
|
|
docstring const author = getAbbreviatedAuthor(key);
|
|
|
|
docstring const year = getYear(key);
|
|
|
|
if (author.empty() || year.empty())
|
|
|
|
return vector<docstring>();
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
|
2007-08-20 16:30:02 +00:00
|
|
|
|
|
|
|
vector<docstring> vec(styles.size());
|
2008-04-20 15:00:11 +00:00
|
|
|
for (size_t i = 0; i != vec.size(); ++i) {
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring str;
|
|
|
|
|
|
|
|
switch (styles[i]) {
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITE:
|
|
|
|
case CITEP:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = from_ascii("[#ID]");
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case NOCITE:
|
2007-12-20 15:46:14 +00:00
|
|
|
str = _("Add to bibliography only.");
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITET:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = author + " [#ID]";
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITEALT:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = author + " #ID";
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITEALP:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = from_ascii("#ID");
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITEAUTHOR:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = author;
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITEYEAR:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = year;
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITEYEARPAR:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = '(' + year + ')';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec[i] = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<docstring> const BiblioInfo::getAuthorYearStrings(
|
|
|
|
docstring const & key, Buffer const & buf) const
|
|
|
|
{
|
|
|
|
if (empty())
|
|
|
|
return vector<docstring>();
|
|
|
|
|
|
|
|
docstring const author = getAbbreviatedAuthor(key);
|
|
|
|
docstring const year = getYear(key);
|
|
|
|
if (author.empty() || year.empty())
|
|
|
|
return vector<docstring>();
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
|
2007-08-20 16:30:02 +00:00
|
|
|
|
|
|
|
vector<docstring> vec(styles.size());
|
2008-04-20 15:00:11 +00:00
|
|
|
for (size_t i = 0; i != vec.size(); ++i) {
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring str;
|
|
|
|
|
|
|
|
switch (styles[i]) {
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITE:
|
2007-08-20 16:30:02 +00:00
|
|
|
// jurabib only: Author/Annotator
|
|
|
|
// (i.e. the "before" field, 2nd opt arg)
|
|
|
|
str = author + "/<" + _("before") + '>';
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case NOCITE:
|
2007-12-20 15:46:14 +00:00
|
|
|
str = _("Add to bibliography only.");
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITET:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = author + " (" + year + ')';
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITEP:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = '(' + author + ", " + year + ')';
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITEALT:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = author + ' ' + year ;
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITEALP:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = author + ", " + year ;
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITEAUTHOR:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = author;
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITEYEAR:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = year;
|
|
|
|
break;
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
case CITEYEARPAR:
|
2007-08-20 16:30:02 +00:00
|
|
|
str = '(' + year + ')';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
vec[i] = str;
|
|
|
|
}
|
|
|
|
return vec;
|
2007-08-14 16:50:51 +00:00
|
|
|
}
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
|
2008-04-25 20:03:03 +00:00
|
|
|
void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
|
|
|
|
{
|
|
|
|
bimap_.insert(info.begin(), info.end());
|
2007-08-20 16:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-05 20:33:20 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
2007-08-20 16:30:02 +00:00
|
|
|
//
|
|
|
|
// CitationStyle
|
|
|
|
//
|
2007-11-05 20:33:20 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
2007-08-20 16:30:02 +00:00
|
|
|
|
2007-08-14 16:50:51 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
|
|
char const * const citeCommands[] = {
|
2008-10-25 13:26:30 +00:00
|
|
|
"cite", "citet", "citep", "citealt", "citealp",
|
|
|
|
"citeauthor", "citeyear", "citeyearpar", "nocite" };
|
2007-08-14 16:50:51 +00:00
|
|
|
|
|
|
|
unsigned int const nCiteCommands =
|
|
|
|
sizeof(citeCommands) / sizeof(char *);
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
CiteStyle const citeStylesArray[] = {
|
2008-09-25 18:13:25 +00:00
|
|
|
CITE, CITET, CITEP, CITEALT, CITEALP,
|
|
|
|
CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
|
2007-08-14 16:50:51 +00:00
|
|
|
|
|
|
|
unsigned int const nCiteStyles =
|
2008-04-20 15:00:11 +00:00
|
|
|
sizeof(citeStylesArray) / sizeof(CiteStyle);
|
2007-08-14 16:50:51 +00:00
|
|
|
|
|
|
|
CiteStyle const citeStylesFull[] = {
|
|
|
|
CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
|
|
|
|
|
|
|
|
unsigned int const nCiteStylesFull =
|
|
|
|
sizeof(citeStylesFull) / sizeof(CiteStyle);
|
|
|
|
|
|
|
|
CiteStyle const citeStylesUCase[] = {
|
|
|
|
CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
|
|
|
|
|
|
|
|
unsigned int const nCiteStylesUCase =
|
|
|
|
sizeof(citeStylesUCase) / sizeof(CiteStyle);
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
CitationStyle citationStyleFromString(string const & command)
|
2007-08-14 16:50:51 +00:00
|
|
|
{
|
2008-04-20 15:00:11 +00:00
|
|
|
CitationStyle s;
|
2007-08-14 16:50:51 +00:00
|
|
|
if (command.empty())
|
2008-04-20 15:00:11 +00:00
|
|
|
return s;
|
2007-08-14 16:50:51 +00:00
|
|
|
|
|
|
|
string cmd = command;
|
|
|
|
if (cmd[0] == 'C') {
|
2008-04-20 15:00:11 +00:00
|
|
|
s.forceUpperCase = true;
|
2007-08-14 16:50:51 +00:00
|
|
|
cmd[0] = 'c';
|
|
|
|
}
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
size_t const n = cmd.size() - 1;
|
2007-08-14 16:50:51 +00:00
|
|
|
if (cmd != "cite" && cmd[n] == '*') {
|
2008-04-20 15:00:11 +00:00
|
|
|
s.full = true;
|
2008-09-08 01:18:33 +00:00
|
|
|
cmd = cmd.substr(0, n);
|
2007-08-14 16:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char const * const * const last = citeCommands + nCiteCommands;
|
2007-12-12 19:28:07 +00:00
|
|
|
char const * const * const ptr = find(citeCommands, last, cmd);
|
2007-08-14 16:50:51 +00:00
|
|
|
|
|
|
|
if (ptr != last) {
|
|
|
|
size_t idx = ptr - citeCommands;
|
2008-04-20 15:00:11 +00:00
|
|
|
s.style = citeStylesArray[idx];
|
2007-08-14 16:50:51 +00:00
|
|
|
}
|
2008-04-20 15:00:11 +00:00
|
|
|
return s;
|
2007-08-14 16:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
string citationStyleToString(const CitationStyle & s)
|
2007-08-14 16:50:51 +00:00
|
|
|
{
|
2008-04-20 15:00:11 +00:00
|
|
|
string cite = citeCommands[s.style];
|
|
|
|
if (s.full) {
|
2007-08-14 16:50:51 +00:00
|
|
|
CiteStyle const * last = citeStylesFull + nCiteStylesFull;
|
2008-04-20 15:00:11 +00:00
|
|
|
if (find(citeStylesFull, last, s.style) != last)
|
2007-08-14 16:50:51 +00:00
|
|
|
cite += '*';
|
|
|
|
}
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
if (s.forceUpperCase) {
|
2007-08-14 16:50:51 +00:00
|
|
|
CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
|
2008-04-20 15:00:11 +00:00
|
|
|
if (find(citeStylesUCase, last, s.style) != last)
|
2007-08-14 16:50:51 +00:00
|
|
|
cite[0] = 'C';
|
|
|
|
}
|
|
|
|
|
|
|
|
return cite;
|
|
|
|
}
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
vector<CiteStyle> citeStyles(CiteEngine engine)
|
2007-08-14 16:50:51 +00:00
|
|
|
{
|
|
|
|
unsigned int nStyles = 0;
|
|
|
|
unsigned int start = 0;
|
|
|
|
|
|
|
|
switch (engine) {
|
|
|
|
case ENGINE_BASIC:
|
2007-12-20 15:46:14 +00:00
|
|
|
nStyles = 2;
|
2007-08-14 16:50:51 +00:00
|
|
|
start = 0;
|
|
|
|
break;
|
|
|
|
case ENGINE_NATBIB_AUTHORYEAR:
|
|
|
|
case ENGINE_NATBIB_NUMERICAL:
|
|
|
|
nStyles = nCiteStyles - 1;
|
|
|
|
start = 1;
|
|
|
|
break;
|
|
|
|
case ENGINE_JURABIB:
|
|
|
|
nStyles = nCiteStyles;
|
|
|
|
start = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-20 15:00:11 +00:00
|
|
|
vector<CiteStyle> styles(nStyles);
|
2007-11-05 20:33:20 +00:00
|
|
|
size_t i = 0;
|
2007-08-14 16:50:51 +00:00
|
|
|
int j = start;
|
|
|
|
for (; i != styles.size(); ++i, ++j)
|
2008-04-20 15:00:11 +00:00
|
|
|
styles[i] = citeStylesArray[j];
|
2007-08-14 16:50:51 +00:00
|
|
|
|
|
|
|
return styles;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace lyx
|
|
|
|
|