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
182 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
188 // This could be made a lot better using the entryType
189 // field to customize the output based upon entry type.
191 // Search for all possible "required" fields
192 docstring author = operator[]("author");
194 author = operator[]("editor");
196 docstring year = operator[]("year");
197 docstring title = operator[]("title");
198 docstring docLoc = operator[]("pages");
199 if (docLoc.empty()) {
200 docLoc = operator[]("chapter");
202 docLoc = from_ascii("Ch. ") + docLoc;
204 docLoc = from_ascii("pp. ") + docLoc;
207 docstring media = operator[]("journal");
209 media = operator[]("publisher");
211 media = operator[]("school");
213 media = operator[]("institution");
216 docstring volume = operator[]("volume");
218 odocstringstream result;
220 result << author << ", ";
224 result << ", " << media;
226 result << ", " << year;
228 result << ", " << docLoc;
230 docstring const result_str = rtrim(result.str());
231 if (!result_str.empty()) {
236 // This should never happen (or at least be very unusual!)
241 //////////////////////////////////////////////////////////////////////
245 //////////////////////////////////////////////////////////////////////
248 // A functor for use with sort, leading to case insensitive sorting
249 class compareNoCase: public binary_function<docstring, docstring, bool>
252 bool operator()(docstring const & s1, docstring const & s2) const {
253 return compare_no_case(s1, s2) < 0;
259 vector<docstring> const BiblioInfo::getKeys() const
261 vector<docstring> bibkeys;
262 BiblioInfo::const_iterator it = begin();
263 for (; it != end(); ++it)
264 bibkeys.push_back(it->first);
265 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
270 vector<docstring> const BiblioInfo::getFields() const
272 vector<docstring> bibfields;
273 set<docstring>::const_iterator it = field_names_.begin();
274 set<docstring>::const_iterator end = field_names_.end();
275 for (; it != end; ++it)
276 bibfields.push_back(*it);
277 sort(bibfields.begin(), bibfields.end());
282 vector<docstring> const BiblioInfo::getEntries() const
284 vector<docstring> bibentries;
285 set<docstring>::const_iterator it = entry_types_.begin();
286 set<docstring>::const_iterator end = entry_types_.end();
287 for (; it != end; ++it)
288 bibentries.push_back(*it);
289 sort(bibentries.begin(), bibentries.end());
294 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
296 BiblioInfo::const_iterator it = find(key);
299 BibTeXInfo const & data = it->second;
300 return data.getAbbreviatedAuthor();
304 docstring const BiblioInfo::getYear(docstring const & key) const
306 BiblioInfo::const_iterator it = find(key);
309 BibTeXInfo const & data = it->second;
310 return data.getYear();
314 docstring const BiblioInfo::getInfo(docstring const & key) const
316 BiblioInfo::const_iterator it = find(key);
319 BibTeXInfo const & data = it->second;
320 return data.getInfo();
324 vector<docstring> const BiblioInfo::getCiteStrings(
325 docstring const & key, Buffer const & buf) const
327 CiteEngine const engine = buf.params().citeEngine();
328 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
329 return getNumericalStrings(key, buf);
331 return getAuthorYearStrings(key, buf);
335 vector<docstring> const BiblioInfo::getNumericalStrings(
336 docstring const & key, Buffer const & buf) const
339 return vector<docstring>();
341 docstring const author = getAbbreviatedAuthor(key);
342 docstring const year = getYear(key);
343 if (author.empty() || year.empty())
344 return vector<docstring>();
346 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
348 vector<docstring> vec(styles.size());
349 for (size_t i = 0; i != vec.size(); ++i) {
355 str = from_ascii("[#ID]");
359 str = _("Add to bibliography only.");
363 str = author + " [#ID]";
367 str = author + " #ID";
371 str = from_ascii("#ID");
383 str = '(' + year + ')';
394 vector<docstring> const BiblioInfo::getAuthorYearStrings(
395 docstring const & key, Buffer const & buf) const
398 return vector<docstring>();
400 docstring const author = getAbbreviatedAuthor(key);
401 docstring const year = getYear(key);
402 if (author.empty() || year.empty())
403 return vector<docstring>();
405 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
407 vector<docstring> vec(styles.size());
408 for (size_t i = 0; i != vec.size(); ++i) {
413 // jurabib only: Author/Annotator
414 // (i.e. the "before" field, 2nd opt arg)
415 str = author + "/<" + _("before") + '>';
419 str = _("Add to bibliography only.");
423 str = author + " (" + year + ')';
427 str = '(' + author + ", " + year + ')';
431 str = author + ' ' + year ;
435 str = author + ", " + year ;
447 str = '(' + year + ')';
456 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
458 bimap_.insert(info.begin(), info.end());
462 //////////////////////////////////////////////////////////////////////
466 //////////////////////////////////////////////////////////////////////
471 char const * const citeCommands[] = {
472 "cite", "citet", "citep", "citealt", "citealp",
473 "citeauthor", "citeyear", "citeyearpar", "nocite" };
475 unsigned int const nCiteCommands =
476 sizeof(citeCommands) / sizeof(char *);
478 CiteStyle const citeStylesArray[] = {
479 CITE, CITET, CITEP, CITEALT, CITEALP,
480 CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
482 unsigned int const nCiteStyles =
483 sizeof(citeStylesArray) / sizeof(CiteStyle);
485 CiteStyle const citeStylesFull[] = {
486 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
488 unsigned int const nCiteStylesFull =
489 sizeof(citeStylesFull) / sizeof(CiteStyle);
491 CiteStyle const citeStylesUCase[] = {
492 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
494 unsigned int const nCiteStylesUCase =
495 sizeof(citeStylesUCase) / sizeof(CiteStyle);
500 CitationStyle citationStyleFromString(string const & command)
506 string cmd = command;
508 s.forceUpperCase = true;
512 size_t const n = cmd.size() - 1;
513 if (cmd != "cite" && cmd[n] == '*') {
515 cmd = cmd.substr(0, n);
518 char const * const * const last = citeCommands + nCiteCommands;
519 char const * const * const ptr = find(citeCommands, last, cmd);
522 size_t idx = ptr - citeCommands;
523 s.style = citeStylesArray[idx];
529 string citationStyleToString(const CitationStyle & s)
531 string cite = citeCommands[s.style];
533 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
534 if (find(citeStylesFull, last, s.style) != last)
538 if (s.forceUpperCase) {
539 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
540 if (find(citeStylesUCase, last, s.style) != last)
547 vector<CiteStyle> citeStyles(CiteEngine engine)
549 unsigned int nStyles = 0;
550 unsigned int start = 0;
557 case ENGINE_NATBIB_AUTHORYEAR:
558 case ENGINE_NATBIB_NUMERICAL:
559 nStyles = nCiteStyles - 1;
563 nStyles = nCiteStyles;
568 vector<CiteStyle> styles(nStyles);
571 for (; i != styles.size(); ++i, ++j)
572 styles[i] = citeStylesArray[j];