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()
50 bool BibTeXInfo::hasField(docstring const & field) const
52 return count(field) == 1;
56 docstring const & BibTeXInfo::getValueForField(docstring const & field) const
58 BibTeXInfo::const_iterator it = find(field);
61 static docstring const empty_value = docstring();
66 docstring const & BibTeXInfo::getValueForField(string const & field) const
68 return getValueForField(from_ascii(field));
72 static docstring familyName(docstring const & name)
78 docstring fname = name;
80 // possible authorname combinations are:
81 // "Surname, FirstName"
83 // "FirstName Surname"
85 docstring::size_type idx = fname.find(',');
86 if (idx != docstring::npos)
87 return ltrim(fname.substr(0, idx));
88 idx = fname.rfind('.');
89 if (idx != docstring::npos && idx + 1 < fname.size())
90 fname = ltrim(fname.substr(idx + 1));
91 // test if we have a LaTeX Space in front
93 return fname.substr(2);
98 docstring const BibTeXInfo::getAbbreviatedAuthor() const
103 docstring author = getValueForField("author");
105 if (author.empty()) {
106 author = getValueForField("editor");
111 // OK, we've got some names. Let's format them.
112 // Try to split the author list on " and "
113 vector<docstring> const authors =
114 getVectorFromString(author, from_ascii(" and "));
116 if (authors.size() == 2)
117 return bformat(_("%1$s and %2$s"),
118 familyName(authors[0]), familyName(authors[1]));
120 if (authors.size() > 2)
121 return bformat(_("%1$s et al."), familyName(authors[0]));
123 return familyName(authors[0]);
127 docstring const BibTeXInfo::getYear() const
132 docstring year = getValueForField("year");
139 docstring const BibTeXInfo::getInfo() const
142 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
147 // This could be made a lot better using the entryType
148 // field to customize the output based upon entry type.
150 // Search for all possible "required" fields
151 docstring author = getValueForField("author");
153 author = getValueForField("editor");
155 docstring year = getValueForField("year");
156 docstring title = getValueForField("title");
157 docstring docLoc = getValueForField("pages");
158 if (docLoc.empty()) {
159 docLoc = getValueForField("chapter");
161 docLoc = from_ascii("Ch. ") + docLoc;
163 docLoc = from_ascii("pp. ") + docLoc;
166 docstring media = getValueForField("journal");
168 media = getValueForField("publisher");
170 media = getValueForField("school");
172 media = getValueForField("institution");
175 docstring volume = getValueForField("volume");
177 odocstringstream result;
179 result << author << ", ";
183 result << ", " << media;
185 result << ", " << year;
187 result << ", " << docLoc;
189 docstring const result_str = rtrim(result.str());
190 if (!result_str.empty())
193 // This should never happen (or at least be very unusual!)
198 //////////////////////////////////////////////////////////////////////
202 //////////////////////////////////////////////////////////////////////
205 // A functor for use with sort, leading to case insensitive sorting
206 class compareNoCase: public binary_function<docstring, docstring, bool>
209 bool operator()(docstring const & s1, docstring const & s2) const {
210 return compare_no_case(s1, s2) < 0;
216 vector<docstring> const BiblioInfo::getKeys() const
218 vector<docstring> bibkeys;
219 BiblioInfo::const_iterator it = begin();
220 for (; it != end(); ++it)
221 bibkeys.push_back(it->first);
222 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
227 vector<docstring> const BiblioInfo::getFields() const
229 vector<docstring> bibfields;
230 set<docstring>::const_iterator it = fieldNames.begin();
231 set<docstring>::const_iterator end = fieldNames.end();
232 for (; it != end; ++it)
233 bibfields.push_back(*it);
234 sort(bibfields.begin(), bibfields.end());
239 vector<docstring> const BiblioInfo::getEntries() const
241 vector<docstring> bibentries;
242 set<docstring>::const_iterator it = entryTypes.begin();
243 set<docstring>::const_iterator end = entryTypes.end();
244 for (; it != end; ++it)
245 bibentries.push_back(*it);
246 sort(bibentries.begin(), bibentries.end());
251 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
253 BiblioInfo::const_iterator it = find(key);
256 BibTeXInfo const & data = it->second;
257 return data.getAbbreviatedAuthor();
261 docstring const BiblioInfo::getYear(docstring const & key) const
263 BiblioInfo::const_iterator it = find(key);
266 BibTeXInfo const & data = it->second;
267 return data.getYear();
271 docstring const BiblioInfo::getInfo(docstring const & key) const
273 BiblioInfo::const_iterator it = find(key);
276 BibTeXInfo const & data = it->second;
277 return data.getInfo();
281 vector<docstring> const BiblioInfo::getCiteStrings(
282 docstring const & key, Buffer const & buf) const
284 biblio::CiteEngine const engine = buf.params().getEngine();
285 if (engine == biblio::ENGINE_NATBIB_NUMERICAL)
286 return getNumericalStrings(key, buf);
288 return getAuthorYearStrings(key, buf);
292 vector<docstring> const BiblioInfo::getNumericalStrings(
293 docstring const & key, Buffer const & buf) const
296 return vector<docstring>();
298 docstring const author = getAbbreviatedAuthor(key);
299 docstring const year = getYear(key);
300 if (author.empty() || year.empty())
301 return vector<docstring>();
303 vector<biblio::CiteStyle> const & styles =
304 biblio::getCiteStyles(buf.params().getEngine());
306 vector<docstring> vec(styles.size());
307 for (size_t i = 0; i != vec.size(); ++i) {
313 str = from_ascii("[#ID]");
317 str = author + " [#ID]";
320 case biblio::CITEALT:
321 str = author + " #ID";
324 case biblio::CITEALP:
325 str = from_ascii("#ID");
328 case biblio::CITEAUTHOR:
332 case biblio::CITEYEAR:
336 case biblio::CITEYEARPAR:
337 str = '(' + year + ')';
348 vector<docstring> const BiblioInfo::getAuthorYearStrings(
349 docstring const & key, Buffer const & buf) const
352 return vector<docstring>();
354 docstring const author = getAbbreviatedAuthor(key);
355 docstring const year = getYear(key);
356 if (author.empty() || year.empty())
357 return vector<docstring>();
359 vector<biblio::CiteStyle> const & styles =
360 getCiteStyles(buf.params().getEngine());
362 vector<docstring> vec(styles.size());
363 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
368 // jurabib only: Author/Annotator
369 // (i.e. the "before" field, 2nd opt arg)
370 str = author + "/<" + _("before") + '>';
374 str = author + " (" + year + ')';
378 str = '(' + author + ", " + year + ')';
381 case biblio::CITEALT:
382 str = author + ' ' + year ;
385 case biblio::CITEALP:
386 str = author + ", " + year ;
389 case biblio::CITEAUTHOR:
393 case biblio::CITEYEAR:
397 case biblio::CITEYEARPAR:
398 str = '(' + year + ')';
407 void BiblioInfo::fillWithBibKeys(Buffer const * const buf)
409 /// if this is a child document and the parent is already loaded
410 /// use the parent's list instead [ale990412]
411 Buffer const * const tmp = buf->masterBuffer();
414 this->fillWithBibKeys(tmp);
418 // Pre-load all child documents.
419 buf->loadChildDocuments();
421 for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
422 it->fillWithBibKeys(*buf, *this, it);
428 //////////////////////////////////////////////////////////////////////
432 //////////////////////////////////////////////////////////////////////
437 char const * const citeCommands[] = {
438 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
439 "citeyear", "citeyearpar" };
441 unsigned int const nCiteCommands =
442 sizeof(citeCommands) / sizeof(char *);
444 CiteStyle const citeStyles[] = {
445 CITE, CITET, CITEP, CITEALT, CITEALP,
446 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
448 unsigned int const nCiteStyles =
449 sizeof(citeStyles) / sizeof(CiteStyle);
451 CiteStyle const citeStylesFull[] = {
452 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
454 unsigned int const nCiteStylesFull =
455 sizeof(citeStylesFull) / sizeof(CiteStyle);
457 CiteStyle const citeStylesUCase[] = {
458 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
460 unsigned int const nCiteStylesUCase =
461 sizeof(citeStylesUCase) / sizeof(CiteStyle);
466 CitationStyle::CitationStyle(string const & command)
467 : style(CITE), full(false), forceUCase(false)
472 string cmd = command;
478 string::size_type const n = cmd.size() - 1;
479 if (cmd != "cite" && cmd[n] == '*') {
481 cmd = cmd.substr(0,n);
484 char const * const * const last = citeCommands + nCiteCommands;
485 char const * const * const ptr = find(citeCommands, last, cmd);
488 size_t idx = ptr - citeCommands;
489 style = citeStyles[idx];
494 string const CitationStyle::asLatexStr() const
496 string cite = citeCommands[style];
498 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
499 if (find(citeStylesFull, last, style) != last)
504 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
505 if (find(citeStylesUCase, last, style) != last)
513 vector<CiteStyle> const getCiteStyles(CiteEngine const engine)
515 unsigned int nStyles = 0;
516 unsigned int start = 0;
523 case ENGINE_NATBIB_AUTHORYEAR:
524 case ENGINE_NATBIB_NUMERICAL:
525 nStyles = nCiteStyles - 1;
529 nStyles = nCiteStyles;
534 typedef vector<CiteStyle> cite_vec;
536 cite_vec styles(nStyles);
539 for (; i != styles.size(); ++i, ++j)
540 styles[i] = citeStyles[j];
545 } // namespace biblio