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 CiteEngine const engine = buf.params().citeEngine();
291 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
292 return getNumericalStrings(key, buf);
294 return getAuthorYearStrings(key, buf);
298 vector<docstring> const BiblioInfo::getNumericalStrings(
299 docstring const & key, Buffer const & buf) const
302 return vector<docstring>();
304 docstring const author = getAbbreviatedAuthor(key);
305 docstring const year = getYear(key);
306 if (author.empty() || year.empty())
307 return vector<docstring>();
309 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
311 vector<docstring> vec(styles.size());
312 for (size_t i = 0; i != vec.size(); ++i) {
318 str = from_ascii("[#ID]");
322 str = _("Add to bibliography only.");
326 str = author + " [#ID]";
330 str = author + " #ID";
334 str = from_ascii("#ID");
346 str = '(' + year + ')';
357 vector<docstring> const BiblioInfo::getAuthorYearStrings(
358 docstring const & key, Buffer const & buf) const
361 return vector<docstring>();
363 docstring const author = getAbbreviatedAuthor(key);
364 docstring const year = getYear(key);
365 if (author.empty() || year.empty())
366 return vector<docstring>();
368 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
370 vector<docstring> vec(styles.size());
371 for (size_t i = 0; i != vec.size(); ++i) {
376 // jurabib only: Author/Annotator
377 // (i.e. the "before" field, 2nd opt arg)
378 str = author + "/<" + _("before") + '>';
382 str = _("Add to bibliography only.");
386 str = author + " (" + year + ')';
390 str = '(' + author + ", " + year + ')';
394 str = author + ' ' + year ;
398 str = author + ", " + year ;
410 str = '(' + year + ')';
419 void BiblioInfo::fillWithBibKeys(Buffer const * const buf)
421 /// if this is a child document and the parent is already loaded
422 /// use the parent's list instead [ale990412]
423 Buffer const * const tmp = buf->masterBuffer();
424 LASSERT(tmp, return);
426 this->fillWithBibKeys(tmp);
430 for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
431 it->fillWithBibKeys(*this, it);
435 //////////////////////////////////////////////////////////////////////
439 //////////////////////////////////////////////////////////////////////
444 char const * const citeCommands[] = {
445 "cite", "nocite", "citet", "citep", "citealt", "citealp",
446 "citeauthor", "citeyear", "citeyearpar" };
448 unsigned int const nCiteCommands =
449 sizeof(citeCommands) / sizeof(char *);
451 CiteStyle const citeStylesArray[] = {
452 CITE, NOCITE, CITET, CITEP, CITEALT,
453 CITEALP, CITEAUTHOR, CITEYEAR, CITEYEARPAR };
455 unsigned int const nCiteStyles =
456 sizeof(citeStylesArray) / sizeof(CiteStyle);
458 CiteStyle const citeStylesFull[] = {
459 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
461 unsigned int const nCiteStylesFull =
462 sizeof(citeStylesFull) / sizeof(CiteStyle);
464 CiteStyle const citeStylesUCase[] = {
465 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
467 unsigned int const nCiteStylesUCase =
468 sizeof(citeStylesUCase) / sizeof(CiteStyle);
473 CitationStyle citationStyleFromString(string const & command)
479 string cmd = command;
481 s.forceUpperCase = true;
485 size_t const n = cmd.size() - 1;
486 if (cmd != "cite" && cmd[n] == '*') {
488 cmd = cmd.substr(0,n);
491 char const * const * const last = citeCommands + nCiteCommands;
492 char const * const * const ptr = find(citeCommands, last, cmd);
495 size_t idx = ptr - citeCommands;
496 s.style = citeStylesArray[idx];
502 string citationStyleToString(const CitationStyle & s)
504 string cite = citeCommands[s.style];
506 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
507 if (find(citeStylesFull, last, s.style) != last)
511 if (s.forceUpperCase) {
512 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
513 if (find(citeStylesUCase, last, s.style) != last)
520 vector<CiteStyle> citeStyles(CiteEngine engine)
522 unsigned int nStyles = 0;
523 unsigned int start = 0;
530 case ENGINE_NATBIB_AUTHORYEAR:
531 case ENGINE_NATBIB_NUMERICAL:
532 nStyles = nCiteStyles - 1;
536 nStyles = nCiteStyles;
541 vector<CiteStyle> styles(nStyles);
544 for (; i != styles.size(); ++i, ++j)
545 styles[i] = citeStylesArray[j];