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/lstrings.h"
31 #include "boost/regex.hpp"
34 using namespace lyx::support;
39 //////////////////////////////////////////////////////////////////////
43 //////////////////////////////////////////////////////////////////////
45 BibTeXInfo::BibTeXInfo(bool ib)
50 BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
51 : is_bibtex_(true), bib_key_(key), entry_type_(type)
55 bool BibTeXInfo::hasField(docstring const & field) const
57 return count(field) == 1;
61 docstring const & BibTeXInfo::getValueForField(docstring const & field) const
63 BibTeXInfo::const_iterator it = find(field);
66 static docstring const empty_value = docstring();
71 docstring const & BibTeXInfo::getValueForField(string const & field) const
73 return getValueForField(from_ascii(field));
77 static docstring familyName(docstring const & name)
83 docstring fname = name;
85 // possible authorname combinations are:
86 // "Surname, FirstName"
88 // "FirstName Surname"
90 docstring::size_type idx = fname.find(',');
91 if (idx != docstring::npos)
92 return ltrim(fname.substr(0, idx));
93 idx = fname.rfind('.');
94 if (idx != docstring::npos && idx + 1 < fname.size())
95 fname = ltrim(fname.substr(idx + 1));
96 // test if we have a LaTeX Space in front
98 return fname.substr(2);
103 docstring const BibTeXInfo::getAbbreviatedAuthor() const
108 docstring author = getValueForField("author");
110 if (author.empty()) {
111 author = getValueForField("editor");
116 // OK, we've got some names. Let's format them.
117 // Try to split the author list on " and "
118 vector<docstring> const authors =
119 getVectorFromString(author, from_ascii(" and "));
121 if (authors.size() == 2)
122 return bformat(_("%1$s and %2$s"),
123 familyName(authors[0]), familyName(authors[1]));
125 if (authors.size() > 2)
126 return bformat(_("%1$s et al."), familyName(authors[0]));
128 return familyName(authors[0]);
132 docstring const BibTeXInfo::getYear() const
137 docstring year = getValueForField("year");
144 docstring const BibTeXInfo::getInfo() const
147 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
152 // This could be made a lot better using the entryType
153 // field to customize the output based upon entry type.
155 // Search for all possible "required" fields
156 docstring author = getValueForField("author");
158 author = getValueForField("editor");
160 docstring year = getValueForField("year");
161 docstring title = getValueForField("title");
162 docstring docLoc = getValueForField("pages");
163 if (docLoc.empty()) {
164 docLoc = getValueForField("chapter");
166 docLoc = from_ascii("Ch. ") + docLoc;
168 docLoc = from_ascii("pp. ") + docLoc;
171 docstring media = getValueForField("journal");
173 media = getValueForField("publisher");
175 media = getValueForField("school");
177 media = getValueForField("institution");
180 docstring volume = getValueForField("volume");
182 odocstringstream result;
184 result << author << ", ";
188 result << ", " << media;
190 result << ", " << year;
192 result << ", " << docLoc;
194 docstring const result_str = rtrim(result.str());
195 if (!result_str.empty())
198 // This should never happen (or at least be very unusual!)
203 //////////////////////////////////////////////////////////////////////
207 //////////////////////////////////////////////////////////////////////
210 // A functor for use with sort, leading to case insensitive sorting
211 class compareNoCase: public binary_function<docstring, docstring, bool>
214 bool operator()(docstring const & s1, docstring const & s2) const {
215 return compare_no_case(s1, s2) < 0;
221 vector<docstring> const BiblioInfo::getKeys() const
223 vector<docstring> bibkeys;
224 BiblioInfo::const_iterator it = begin();
225 for (; it != end(); ++it)
226 bibkeys.push_back(it->first);
227 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
232 vector<docstring> const BiblioInfo::getFields() const
234 vector<docstring> bibfields;
235 set<docstring>::const_iterator it = field_names_.begin();
236 set<docstring>::const_iterator end = field_names_.end();
237 for (; it != end; ++it)
238 bibfields.push_back(*it);
239 sort(bibfields.begin(), bibfields.end());
244 vector<docstring> const BiblioInfo::getEntries() const
246 vector<docstring> bibentries;
247 set<docstring>::const_iterator it = entry_types_.begin();
248 set<docstring>::const_iterator end = entry_types_.end();
249 for (; it != end; ++it)
250 bibentries.push_back(*it);
251 sort(bibentries.begin(), bibentries.end());
256 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
258 BiblioInfo::const_iterator it = find(key);
261 BibTeXInfo const & data = it->second;
262 return data.getAbbreviatedAuthor();
266 docstring const BiblioInfo::getYear(docstring const & key) const
268 BiblioInfo::const_iterator it = find(key);
271 BibTeXInfo const & data = it->second;
272 return data.getYear();
276 docstring const BiblioInfo::getInfo(docstring const & key) const
278 BiblioInfo::const_iterator it = find(key);
281 BibTeXInfo const & data = it->second;
282 return data.getInfo();
286 vector<docstring> const BiblioInfo::getCiteStrings(
287 docstring const & key, Buffer const & buf) const
289 biblio::CiteEngine const engine = buf.params().getEngine();
290 if (engine == biblio::ENGINE_BASIC ||
291 engine == biblio::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<biblio::CiteStyle> const & styles =
310 biblio::getCiteStyles(buf.params().getEngine());
312 vector<docstring> vec(styles.size());
313 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
319 str = from_ascii("[#ID]");
323 str = _("Add to bibliography only.");
327 str = author + " [#ID]";
330 case biblio::CITEALT:
331 str = author + " #ID";
334 case biblio::CITEALP:
335 str = from_ascii("#ID");
338 case biblio::CITEAUTHOR:
342 case biblio::CITEYEAR:
346 case biblio::CITEYEARPAR:
347 str = '(' + year + ')';
358 vector<docstring> const BiblioInfo::getAuthorYearStrings(
359 docstring const & key, Buffer const & buf) const
362 return vector<docstring>();
364 docstring const author = getAbbreviatedAuthor(key);
365 docstring const year = getYear(key);
366 if (author.empty() || year.empty())
367 return vector<docstring>();
369 vector<biblio::CiteStyle> const & styles =
370 getCiteStyles(buf.params().getEngine());
372 vector<docstring> vec(styles.size());
373 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
378 // jurabib only: Author/Annotator
379 // (i.e. the "before" field, 2nd opt arg)
380 str = author + "/<" + _("before") + '>';
384 str = _("Add to bibliography only.");
388 str = author + " (" + year + ')';
392 str = '(' + author + ", " + year + ')';
395 case biblio::CITEALT:
396 str = author + ' ' + year ;
399 case biblio::CITEALP:
400 str = author + ", " + year ;
403 case biblio::CITEAUTHOR:
407 case biblio::CITEYEAR:
411 case biblio::CITEYEARPAR:
412 str = '(' + year + ')';
421 void BiblioInfo::fillWithBibKeys(Buffer const * const buf)
423 /// if this is a child document and the parent is already loaded
424 /// use the parent's list instead [ale990412]
425 Buffer const * const tmp = buf->masterBuffer();
428 this->fillWithBibKeys(tmp);
432 // Pre-load all child documents.
433 buf->loadChildDocuments();
435 for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
436 it->fillWithBibKeys(*buf, *this, it);
442 //////////////////////////////////////////////////////////////////////
446 //////////////////////////////////////////////////////////////////////
451 char const * const citeCommands[] = {
452 "cite", "nocite", "citet", "citep", "citealt", "citealp",
453 "citeauthor", "citeyear", "citeyearpar" };
455 unsigned int const nCiteCommands =
456 sizeof(citeCommands) / sizeof(char *);
458 CiteStyle const citeStyles[] = {
459 CITE, NOCITE, CITET, CITEP, CITEALT,
460 CITEALP, CITEAUTHOR, CITEYEAR, CITEYEARPAR };
462 unsigned int const nCiteStyles =
463 sizeof(citeStyles) / sizeof(CiteStyle);
465 CiteStyle const citeStylesFull[] = {
466 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
468 unsigned int const nCiteStylesFull =
469 sizeof(citeStylesFull) / sizeof(CiteStyle);
471 CiteStyle const citeStylesUCase[] = {
472 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
474 unsigned int const nCiteStylesUCase =
475 sizeof(citeStylesUCase) / sizeof(CiteStyle);
480 CitationStyle::CitationStyle(string const & command)
481 : style(CITE), full(false), forceUCase(false)
486 string cmd = command;
492 string::size_type const n = cmd.size() - 1;
493 if (cmd != "cite" && cmd[n] == '*') {
495 cmd = cmd.substr(0,n);
498 char const * const * const last = citeCommands + nCiteCommands;
499 char const * const * const ptr = find(citeCommands, last, cmd);
502 size_t idx = ptr - citeCommands;
503 style = citeStyles[idx];
508 string const CitationStyle::asLatexStr() const
510 string cite = citeCommands[style];
512 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
513 if (find(citeStylesFull, last, style) != last)
518 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
519 if (find(citeStylesUCase, last, style) != last)
527 vector<CiteStyle> const getCiteStyles(CiteEngine const engine)
529 unsigned int nStyles = 0;
530 unsigned int start = 0;
537 case ENGINE_NATBIB_AUTHORYEAR:
538 case ENGINE_NATBIB_NUMERICAL:
539 nStyles = nCiteStyles - 1;
543 nStyles = nCiteStyles;
548 typedef vector<CiteStyle> cite_vec;
550 cite_vec styles(nStyles);
553 for (; i != styles.size(); ++i, ++j)
554 styles[i] = citeStyles[j];
559 } // namespace biblio