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/assert.h"
28 #include "support/docstream.h"
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
32 #include "boost/regex.hpp"
35 using namespace lyx::support;
40 //////////////////////////////////////////////////////////////////////
44 //////////////////////////////////////////////////////////////////////
46 BibTeXInfo::BibTeXInfo(bool ib)
51 BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
52 : is_bibtex_(true), bib_key_(key), entry_type_(type)
56 bool BibTeXInfo::hasField(docstring const & field) const
58 return count(field) == 1;
62 docstring const & BibTeXInfo::getValueForField(docstring const & field) const
64 BibTeXInfo::const_iterator it = find(field);
67 static docstring const empty_value = docstring();
72 docstring const & BibTeXInfo::getValueForField(string const & field) const
74 return getValueForField(from_ascii(field));
78 static docstring familyName(docstring const & name)
84 docstring fname = name;
86 // possible authorname combinations are:
87 // "Surname, FirstName"
89 // "FirstName Surname"
91 docstring::size_type idx = fname.find(',');
92 if (idx != docstring::npos)
93 return ltrim(fname.substr(0, idx));
94 idx = fname.rfind('.');
95 if (idx != docstring::npos && idx + 1 < fname.size())
96 fname = ltrim(fname.substr(idx + 1));
97 // test if we have a LaTeX Space in front
99 return fname.substr(2);
104 docstring const BibTeXInfo::getAbbreviatedAuthor() const
109 docstring author = getValueForField("author");
111 if (author.empty()) {
112 author = getValueForField("editor");
117 // OK, we've got some names. Let's format them.
118 // Try to split the author list on " and "
119 vector<docstring> const authors =
120 getVectorFromString(author, from_ascii(" and "));
122 if (authors.size() == 2)
123 return bformat(_("%1$s and %2$s"),
124 familyName(authors[0]), familyName(authors[1]));
126 if (authors.size() > 2)
127 return bformat(_("%1$s et al."), familyName(authors[0]));
129 return familyName(authors[0]);
133 docstring const BibTeXInfo::getYear() const
138 docstring year = getValueForField("year");
145 docstring const BibTeXInfo::getInfo() const
148 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
153 // This could be made a lot better using the entryType
154 // field to customize the output based upon entry type.
156 // Search for all possible "required" fields
157 docstring author = getValueForField("author");
159 author = getValueForField("editor");
161 docstring year = getValueForField("year");
162 docstring title = getValueForField("title");
163 docstring docLoc = getValueForField("pages");
164 if (docLoc.empty()) {
165 docLoc = getValueForField("chapter");
167 docLoc = from_ascii("Ch. ") + docLoc;
169 docLoc = from_ascii("pp. ") + docLoc;
172 docstring media = getValueForField("journal");
174 media = getValueForField("publisher");
176 media = getValueForField("school");
178 media = getValueForField("institution");
181 docstring volume = getValueForField("volume");
183 odocstringstream result;
185 result << author << ", ";
189 result << ", " << media;
191 result << ", " << year;
193 result << ", " << docLoc;
195 docstring const result_str = rtrim(result.str());
196 if (!result_str.empty())
199 // This should never happen (or at least be very unusual!)
204 //////////////////////////////////////////////////////////////////////
208 //////////////////////////////////////////////////////////////////////
211 // A functor for use with sort, leading to case insensitive sorting
212 class compareNoCase: public binary_function<docstring, docstring, bool>
215 bool operator()(docstring const & s1, docstring const & s2) const {
216 return compare_no_case(s1, s2) < 0;
222 vector<docstring> const BiblioInfo::getKeys() const
224 vector<docstring> bibkeys;
225 BiblioInfo::const_iterator it = begin();
226 for (; it != end(); ++it)
227 bibkeys.push_back(it->first);
228 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
233 vector<docstring> const BiblioInfo::getFields() const
235 vector<docstring> bibfields;
236 set<docstring>::const_iterator it = field_names_.begin();
237 set<docstring>::const_iterator end = field_names_.end();
238 for (; it != end; ++it)
239 bibfields.push_back(*it);
240 sort(bibfields.begin(), bibfields.end());
245 vector<docstring> const BiblioInfo::getEntries() const
247 vector<docstring> bibentries;
248 set<docstring>::const_iterator it = entry_types_.begin();
249 set<docstring>::const_iterator end = entry_types_.end();
250 for (; it != end; ++it)
251 bibentries.push_back(*it);
252 sort(bibentries.begin(), bibentries.end());
257 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
259 BiblioInfo::const_iterator it = find(key);
262 BibTeXInfo const & data = it->second;
263 return data.getAbbreviatedAuthor();
267 docstring const BiblioInfo::getYear(docstring const & key) const
269 BiblioInfo::const_iterator it = find(key);
272 BibTeXInfo const & data = it->second;
273 return data.getYear();
277 docstring const BiblioInfo::getInfo(docstring const & key) const
279 BiblioInfo::const_iterator it = find(key);
282 BibTeXInfo const & data = it->second;
283 return data.getInfo();
287 vector<docstring> const BiblioInfo::getCiteStrings(
288 docstring const & key, Buffer const & buf) const
290 biblio::CiteEngine const engine = buf.params().citeEngine();
291 if (engine == biblio::ENGINE_BASIC ||
292 engine == biblio::ENGINE_NATBIB_NUMERICAL)
293 return getNumericalStrings(key, buf);
295 return getAuthorYearStrings(key, buf);
299 vector<docstring> const BiblioInfo::getNumericalStrings(
300 docstring const & key, Buffer const & buf) const
303 return vector<docstring>();
305 docstring const author = getAbbreviatedAuthor(key);
306 docstring const year = getYear(key);
307 if (author.empty() || year.empty())
308 return vector<docstring>();
310 vector<biblio::CiteStyle> const & styles =
311 biblio::getCiteStyles(buf.params().citeEngine());
313 vector<docstring> vec(styles.size());
314 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
320 str = from_ascii("[#ID]");
324 str = _("Add to bibliography only.");
328 str = author + " [#ID]";
331 case biblio::CITEALT:
332 str = author + " #ID";
335 case biblio::CITEALP:
336 str = from_ascii("#ID");
339 case biblio::CITEAUTHOR:
343 case biblio::CITEYEAR:
347 case biblio::CITEYEARPAR:
348 str = '(' + year + ')';
359 vector<docstring> const BiblioInfo::getAuthorYearStrings(
360 docstring const & key, Buffer const & buf) const
363 return vector<docstring>();
365 docstring const author = getAbbreviatedAuthor(key);
366 docstring const year = getYear(key);
367 if (author.empty() || year.empty())
368 return vector<docstring>();
370 vector<biblio::CiteStyle> const & styles =
371 getCiteStyles(buf.params().citeEngine());
373 vector<docstring> vec(styles.size());
374 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
379 // jurabib only: Author/Annotator
380 // (i.e. the "before" field, 2nd opt arg)
381 str = author + "/<" + _("before") + '>';
385 str = _("Add to bibliography only.");
389 str = author + " (" + year + ')';
393 str = '(' + author + ", " + year + ')';
396 case biblio::CITEALT:
397 str = author + ' ' + year ;
400 case biblio::CITEALP:
401 str = author + ", " + year ;
404 case biblio::CITEAUTHOR:
408 case biblio::CITEYEAR:
412 case biblio::CITEYEARPAR:
413 str = '(' + year + ')';
422 void BiblioInfo::fillWithBibKeys(Buffer const * const buf)
424 /// if this is a child document and the parent is already loaded
425 /// use the parent's list instead [ale990412]
426 Buffer const * const tmp = buf->masterBuffer();
429 this->fillWithBibKeys(tmp);
433 for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
434 it->fillWithBibKeys(*this, it);
440 //////////////////////////////////////////////////////////////////////
444 //////////////////////////////////////////////////////////////////////
449 char const * const citeCommands[] = {
450 "cite", "nocite", "citet", "citep", "citealt", "citealp",
451 "citeauthor", "citeyear", "citeyearpar" };
453 unsigned int const nCiteCommands =
454 sizeof(citeCommands) / sizeof(char *);
456 CiteStyle const citeStyles[] = {
457 CITE, NOCITE, CITET, CITEP, CITEALT,
458 CITEALP, CITEAUTHOR, CITEYEAR, CITEYEARPAR };
460 unsigned int const nCiteStyles =
461 sizeof(citeStyles) / sizeof(CiteStyle);
463 CiteStyle const citeStylesFull[] = {
464 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
466 unsigned int const nCiteStylesFull =
467 sizeof(citeStylesFull) / sizeof(CiteStyle);
469 CiteStyle const citeStylesUCase[] = {
470 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
472 unsigned int const nCiteStylesUCase =
473 sizeof(citeStylesUCase) / sizeof(CiteStyle);
478 CitationStyle::CitationStyle(string const & command)
479 : style(CITE), full(false), forceUCase(false)
484 string cmd = command;
490 string::size_type const n = cmd.size() - 1;
491 if (cmd != "cite" && cmd[n] == '*') {
493 cmd = cmd.substr(0,n);
496 char const * const * const last = citeCommands + nCiteCommands;
497 char const * const * const ptr = find(citeCommands, last, cmd);
500 size_t idx = ptr - citeCommands;
501 style = citeStyles[idx];
506 string const CitationStyle::asLatexStr() const
508 string cite = citeCommands[style];
510 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
511 if (find(citeStylesFull, last, style) != last)
516 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
517 if (find(citeStylesUCase, last, style) != last)
525 vector<CiteStyle> const getCiteStyles(CiteEngine const engine)
527 unsigned int nStyles = 0;
528 unsigned int start = 0;
535 case ENGINE_NATBIB_AUTHORYEAR:
536 case ENGINE_NATBIB_NUMERICAL:
537 nStyles = nCiteStyles - 1;
541 nStyles = nCiteStyles;
546 typedef vector<CiteStyle> cite_vec;
548 cite_vec styles(nStyles);
551 for (; i != styles.size(); ++i, ++j)
552 styles[i] = citeStyles[j];
557 } // namespace biblio