3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
15 #include "BiblioInfo.h"
17 #include "BufferParams.h"
18 #include "buffer_funcs.h"
19 #include "InsetIterator.h"
20 #include "Paragraph.h"
22 #include "insets/Inset.h"
23 #include "insets/InsetBibitem.h"
24 #include "insets/InsetBibtex.h"
25 #include "insets/InsetInclude.h"
27 #include "support/docstream.h"
28 #include "support/gettext.h"
29 #include "support/lassert.h"
30 #include "support/lstrings.h"
31 #include "support/textutils.h"
33 #include "boost/regex.hpp"
36 using namespace lyx::support;
41 //////////////////////////////////////////////////////////////////////
45 //////////////////////////////////////////////////////////////////////
47 BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
48 : is_bibtex_(true), bib_key_(key), entry_type_(type)
52 bool BibTeXInfo::hasField(docstring const & field) const
54 return count(field) == 1;
58 docstring const & BibTeXInfo::operator[](docstring const & field) const
60 BibTeXInfo::const_iterator it = find(field);
63 static docstring const empty_value = docstring();
68 docstring const & BibTeXInfo::operator[](string const & field) const
70 return operator[](from_ascii(field));
74 docstring familyName(docstring const & name)
79 // first we look for a comma, and take the last name to be everything
80 // preceding the right-most one, so that we also get the "jr" part.
81 docstring::size_type idx = name.rfind(',');
82 if (idx != docstring::npos)
83 return ltrim(name.substr(0, idx));
85 // OK, so now we want to look for the last name. We're going to
86 // include the "von" part. This isn't perfect.
87 // Split on spaces, to get various tokens.
88 vector<docstring> pieces = getVectorFromString(name, from_ascii(" "));
89 // If we only get two, assume the last one is the last name
90 if (pieces.size() <= 2)
93 // Now we look for the first token that begins with a lower case letter.
94 vector<docstring>::const_iterator it = pieces.begin();
95 vector<docstring>::const_iterator en = pieces.end();
96 for (; it != en; ++it) {
97 if ((*it).size() == 0)
99 char_type const c = (*it)[0];
104 if (it == en) // we never found a "von"
105 return pieces.back();
107 // reconstruct what we need to return
110 for (; it != en; ++it) {
120 docstring const BibTeXInfo::getAbbreviatedAuthor() const
123 docstring const opt = trim(operator[]("label"));
128 split(opt, authors, '(');
132 docstring author = operator[]("author");
133 if (author.empty()) {
134 author = operator[]("editor");
139 // OK, we've got some names. Let's format them.
140 // Try to split the author list on " and "
141 vector<docstring> const authors =
142 getVectorFromString(author, from_ascii(" and "));
144 if (authors.size() == 2)
145 return bformat(_("%1$s and %2$s"),
146 familyName(authors[0]), familyName(authors[1]));
148 if (authors.size() > 2)
149 return bformat(_("%1$s et al."), familyName(authors[0]));
151 return familyName(authors[0]);
155 docstring const BibTeXInfo::getYear() const
158 docstring const opt = trim(operator[]("label"));
163 docstring const tmp = split(opt, authors, '(');
165 split(tmp, year, ')');
169 docstring year = operator[]("year");
176 docstring const BibTeXInfo::getInfo() const
179 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
184 // This could be made a lot better using the entryType
185 // field to customize the output based upon entry type.
187 // Search for all possible "required" fields
188 docstring author = operator[]("author");
190 author = operator[]("editor");
192 docstring year = operator[]("year");
193 docstring title = operator[]("title");
194 docstring docLoc = operator[]("pages");
195 if (docLoc.empty()) {
196 docLoc = operator[]("chapter");
198 docLoc = from_ascii("Ch. ") + docLoc;
200 docLoc = from_ascii("pp. ") + docLoc;
203 docstring media = operator[]("journal");
205 media = operator[]("publisher");
207 media = operator[]("school");
209 media = operator[]("institution");
212 docstring volume = operator[]("volume");
214 odocstringstream result;
216 result << author << ", ";
220 result << ", " << media;
222 result << ", " << year;
224 result << ", " << docLoc;
226 docstring const result_str = rtrim(result.str());
227 if (!result_str.empty())
230 // This should never happen (or at least be very unusual!)
235 //////////////////////////////////////////////////////////////////////
239 //////////////////////////////////////////////////////////////////////
242 // A functor for use with sort, leading to case insensitive sorting
243 class compareNoCase: public binary_function<docstring, docstring, bool>
246 bool operator()(docstring const & s1, docstring const & s2) const {
247 return compare_no_case(s1, s2) < 0;
253 vector<docstring> const BiblioInfo::getKeys() const
255 vector<docstring> bibkeys;
256 BiblioInfo::const_iterator it = begin();
257 for (; it != end(); ++it)
258 bibkeys.push_back(it->first);
259 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
264 vector<docstring> const BiblioInfo::getFields() const
266 vector<docstring> bibfields;
267 set<docstring>::const_iterator it = field_names_.begin();
268 set<docstring>::const_iterator end = field_names_.end();
269 for (; it != end; ++it)
270 bibfields.push_back(*it);
271 sort(bibfields.begin(), bibfields.end());
276 vector<docstring> const BiblioInfo::getEntries() const
278 vector<docstring> bibentries;
279 set<docstring>::const_iterator it = entry_types_.begin();
280 set<docstring>::const_iterator end = entry_types_.end();
281 for (; it != end; ++it)
282 bibentries.push_back(*it);
283 sort(bibentries.begin(), bibentries.end());
288 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
290 BiblioInfo::const_iterator it = find(key);
293 BibTeXInfo const & data = it->second;
294 return data.getAbbreviatedAuthor();
298 docstring const BiblioInfo::getYear(docstring const & key) const
300 BiblioInfo::const_iterator it = find(key);
303 BibTeXInfo const & data = it->second;
304 return data.getYear();
308 docstring const BiblioInfo::getInfo(docstring const & key) const
310 BiblioInfo::const_iterator it = find(key);
313 BibTeXInfo const & data = it->second;
314 return data.getInfo();
318 vector<docstring> const BiblioInfo::getCiteStrings(
319 docstring const & key, Buffer const & buf) const
321 CiteEngine const engine = buf.params().citeEngine();
322 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
323 return getNumericalStrings(key, buf);
325 return getAuthorYearStrings(key, buf);
329 vector<docstring> const BiblioInfo::getNumericalStrings(
330 docstring const & key, Buffer const & buf) const
333 return vector<docstring>();
335 docstring const author = getAbbreviatedAuthor(key);
336 docstring const year = getYear(key);
337 if (author.empty() || year.empty())
338 return vector<docstring>();
340 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
342 vector<docstring> vec(styles.size());
343 for (size_t i = 0; i != vec.size(); ++i) {
349 str = from_ascii("[#ID]");
353 str = _("Add to bibliography only.");
357 str = author + " [#ID]";
361 str = author + " #ID";
365 str = from_ascii("#ID");
377 str = '(' + year + ')';
388 vector<docstring> const BiblioInfo::getAuthorYearStrings(
389 docstring const & key, Buffer const & buf) const
392 return vector<docstring>();
394 docstring const author = getAbbreviatedAuthor(key);
395 docstring const year = getYear(key);
396 if (author.empty() || year.empty())
397 return vector<docstring>();
399 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
401 vector<docstring> vec(styles.size());
402 for (size_t i = 0; i != vec.size(); ++i) {
407 // jurabib only: Author/Annotator
408 // (i.e. the "before" field, 2nd opt arg)
409 str = author + "/<" + _("before") + '>';
413 str = _("Add to bibliography only.");
417 str = author + " (" + year + ')';
421 str = '(' + author + ", " + year + ')';
425 str = author + ' ' + year ;
429 str = author + ", " + year ;
441 str = '(' + year + ')';
450 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
452 bimap_.insert(info.begin(), info.end());
456 //////////////////////////////////////////////////////////////////////
460 //////////////////////////////////////////////////////////////////////
465 char const * const citeCommands[] = {
466 "cite", "citet", "citep", "citealt", "citealp",
467 "citeauthor", "citeyear", "citeyearpar", "nocite" };
469 unsigned int const nCiteCommands =
470 sizeof(citeCommands) / sizeof(char *);
472 CiteStyle const citeStylesArray[] = {
473 CITE, CITET, CITEP, CITEALT, CITEALP,
474 CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
476 unsigned int const nCiteStyles =
477 sizeof(citeStylesArray) / sizeof(CiteStyle);
479 CiteStyle const citeStylesFull[] = {
480 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
482 unsigned int const nCiteStylesFull =
483 sizeof(citeStylesFull) / sizeof(CiteStyle);
485 CiteStyle const citeStylesUCase[] = {
486 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
488 unsigned int const nCiteStylesUCase =
489 sizeof(citeStylesUCase) / sizeof(CiteStyle);
494 CitationStyle citationStyleFromString(string const & command)
500 string cmd = command;
502 s.forceUpperCase = true;
506 size_t const n = cmd.size() - 1;
507 if (cmd != "cite" && cmd[n] == '*') {
509 cmd = cmd.substr(0, n);
512 char const * const * const last = citeCommands + nCiteCommands;
513 char const * const * const ptr = find(citeCommands, last, cmd);
516 size_t idx = ptr - citeCommands;
517 s.style = citeStylesArray[idx];
523 string citationStyleToString(const CitationStyle & s)
525 string cite = citeCommands[s.style];
527 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
528 if (find(citeStylesFull, last, s.style) != last)
532 if (s.forceUpperCase) {
533 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
534 if (find(citeStylesUCase, last, s.style) != last)
541 vector<CiteStyle> citeStyles(CiteEngine engine)
543 unsigned int nStyles = 0;
544 unsigned int start = 0;
551 case ENGINE_NATBIB_AUTHORYEAR:
552 case ENGINE_NATBIB_NUMERICAL:
553 nStyles = nCiteStyles - 1;
557 nStyles = nCiteStyles;
562 vector<CiteStyle> styles(nStyles);
565 for (; i != styles.size(); ++i, ++j)
566 styles[i] = citeStylesArray[j];