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"
20 #include "InsetIterator.h"
21 #include "Paragraph.h"
23 #include "insets/Inset.h"
24 #include "insets/InsetBibitem.h"
25 #include "insets/InsetBibtex.h"
26 #include "insets/InsetInclude.h"
28 #include "support/lstrings.h"
29 #include "support/docstream.h"
31 #include "boost/regex.hpp"
41 using support::bformat;
42 using support::compare_no_case;
43 using support::getVectorFromString;
47 //////////////////////////////////////////////////////////////////////
51 //////////////////////////////////////////////////////////////////////
53 BibTeXInfo::BibTeXInfo()
58 bool BibTeXInfo::hasField(docstring const & field) const
60 return count(field) == 1;
64 docstring const & BibTeXInfo::getValueForField(docstring const & field) const
66 BibTeXInfo::const_iterator it = find(field);
69 static docstring const empty_value = docstring();
74 docstring const & BibTeXInfo::getValueForField(string const & field) const
76 return getValueForField(from_ascii(field));
80 static docstring familyName(docstring const & name)
86 docstring fname = name;
88 // possible authorname combinations are:
89 // "Surname, FirstName"
91 // "FirstName Surname"
93 docstring::size_type idx = fname.find(',');
94 if (idx != docstring::npos)
95 return ltrim(fname.substr(0, idx));
96 idx = fname.rfind('.');
97 if (idx != docstring::npos && idx + 1 < fname.size())
98 fname = ltrim(fname.substr(idx + 1));
99 // test if we have a LaTeX Space in front
100 if (fname[0] == '\\')
101 return fname.substr(2);
106 docstring const BibTeXInfo::getAbbreviatedAuthor() const
111 docstring author = getValueForField("author");
113 if (author.empty()) {
114 author = getValueForField("editor");
119 // OK, we've got some names. Let's format them.
120 // Try to split the author list on " and "
121 vector<docstring> const authors =
122 getVectorFromString(author, from_ascii(" and "));
124 if (authors.size() == 2)
125 return bformat(_("%1$s and %2$s"),
126 familyName(authors[0]), familyName(authors[1]));
128 if (authors.size() > 2)
129 return bformat(_("%1$s et al."), familyName(authors[0]));
131 return familyName(authors[0]);
135 docstring const BibTeXInfo::getYear() const
140 docstring year = getValueForField("year");
147 docstring const BibTeXInfo::getInfo() const
150 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
155 // This could be made a lot better using the entryType
156 // field to customize the output based upon entry type.
158 // Search for all possible "required" fields
159 docstring author = getValueForField("author");
161 author = getValueForField("editor");
163 docstring year = getValueForField("year");
164 docstring title = getValueForField("title");
165 docstring docLoc = getValueForField("pages");
166 if (docLoc.empty()) {
167 docLoc = getValueForField("chapter");
169 docLoc = from_ascii("Ch. ") + docLoc;
171 docLoc = from_ascii("pp. ") + docLoc;
174 docstring media = getValueForField("journal");
176 media = getValueForField("publisher");
178 media = getValueForField("school");
180 media = getValueForField("institution");
183 docstring volume = getValueForField("volume");
185 odocstringstream result;
187 result << author << ", ";
191 result << ", " << media;
193 result << ", " << year;
195 result << ", " << docLoc;
197 docstring const result_str = rtrim(result.str());
198 if (!result_str.empty())
201 // This should never happen (or at least be very unusual!)
206 //////////////////////////////////////////////////////////////////////
210 //////////////////////////////////////////////////////////////////////
213 // A functor for use with std::sort, leading to case insensitive sorting
214 class compareNoCase: public std::binary_function<docstring, docstring, bool>
217 bool operator()(docstring const & s1, docstring const & s2) const {
218 return compare_no_case(s1, s2) < 0;
224 vector<docstring> const BiblioInfo::getKeys() const
226 vector<docstring> bibkeys;
227 BiblioInfo::const_iterator it = begin();
228 for (; it != end(); ++it)
229 bibkeys.push_back(it->first);
230 std::sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
235 vector<docstring> const BiblioInfo::getFields() const
237 vector<docstring> bibfields;
238 set<docstring>::const_iterator it = fieldNames.begin();
239 set<docstring>::const_iterator end = fieldNames.end();
240 for (; it != end; ++it)
241 bibfields.push_back(*it);
242 std::sort(bibfields.begin(), bibfields.end());
247 vector<docstring> const BiblioInfo::getEntries() const
249 vector<docstring> bibentries;
250 set<docstring>::const_iterator it = entryTypes.begin();
251 set<docstring>::const_iterator end = entryTypes.end();
252 for (; it != end; ++it)
253 bibentries.push_back(*it);
254 std::sort(bibentries.begin(), bibentries.end());
259 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
261 BiblioInfo::const_iterator it = find(key);
264 BibTeXInfo const & data = it->second;
265 return data.getAbbreviatedAuthor();
269 docstring const BiblioInfo::getYear(docstring const & key) const
271 BiblioInfo::const_iterator it = find(key);
274 BibTeXInfo const & data = it->second;
275 return data.getYear();
279 docstring const BiblioInfo::getInfo(docstring const & key) const
281 BiblioInfo::const_iterator it = find(key);
284 BibTeXInfo const & data = it->second;
285 return data.getInfo();
289 vector<docstring> const BiblioInfo::getCiteStrings(
290 docstring const & key, Buffer const & buf) const
292 biblio::CiteEngine const engine = buf.params().getEngine();
293 if (engine == biblio::ENGINE_NATBIB_NUMERICAL)
294 return getNumericalStrings(key, buf);
296 return getAuthorYearStrings(key, buf);
300 vector<docstring> const BiblioInfo::getNumericalStrings(
301 docstring const & key, Buffer const & buf) const
304 return vector<docstring>();
306 docstring const author = getAbbreviatedAuthor(key);
307 docstring const year = getYear(key);
308 if (author.empty() || year.empty())
309 return vector<docstring>();
311 vector<biblio::CiteStyle> const & styles =
312 biblio::getCiteStyles(buf.params().getEngine());
314 vector<docstring> vec(styles.size());
315 for (size_t i = 0; i != vec.size(); ++i) {
321 str = from_ascii("[#ID]");
325 str = author + " [#ID]";
328 case biblio::CITEALT:
329 str = author + " #ID";
332 case biblio::CITEALP:
333 str = from_ascii("#ID");
336 case biblio::CITEAUTHOR:
340 case biblio::CITEYEAR:
344 case biblio::CITEYEARPAR:
345 str = '(' + year + ')';
356 vector<docstring> const BiblioInfo::getAuthorYearStrings(
357 docstring const & key, Buffer const & buf) const
360 return vector<docstring>();
362 docstring const author = getAbbreviatedAuthor(key);
363 docstring const year = getYear(key);
364 if (author.empty() || year.empty())
365 return vector<docstring>();
367 vector<biblio::CiteStyle> const & styles =
368 getCiteStyles(buf.params().getEngine());
370 vector<docstring> vec(styles.size());
371 for (vector<docstring>::size_type 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 = author + " (" + year + ')';
386 str = '(' + author + ", " + year + ')';
389 case biblio::CITEALT:
390 str = author + ' ' + year ;
393 case biblio::CITEALP:
394 str = author + ", " + year ;
397 case biblio::CITEAUTHOR:
401 case biblio::CITEYEAR:
405 case biblio::CITEYEARPAR:
406 str = '(' + year + ')';
415 void BiblioInfo::fillWithBibKeys(Buffer const * const buf)
417 /// if this is a child document and the parent is already loaded
418 /// use the parent's list instead [ale990412]
419 Buffer const * const tmp = buf->masterBuffer();
422 this->fillWithBibKeys(tmp);
426 // Pre-load all child documents.
427 buf->loadChildDocuments();
429 for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
430 it->fillWithBibKeys(*buf, *this, it);
436 //////////////////////////////////////////////////////////////////////
440 //////////////////////////////////////////////////////////////////////
445 char const * const citeCommands[] = {
446 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
447 "citeyear", "citeyearpar" };
449 unsigned int const nCiteCommands =
450 sizeof(citeCommands) / sizeof(char *);
452 CiteStyle const citeStyles[] = {
453 CITE, CITET, CITEP, CITEALT, CITEALP,
454 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
456 unsigned int const nCiteStyles =
457 sizeof(citeStyles) / sizeof(CiteStyle);
459 CiteStyle const citeStylesFull[] = {
460 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
462 unsigned int const nCiteStylesFull =
463 sizeof(citeStylesFull) / sizeof(CiteStyle);
465 CiteStyle const citeStylesUCase[] = {
466 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
468 unsigned int const nCiteStylesUCase =
469 sizeof(citeStylesUCase) / sizeof(CiteStyle);
474 CitationStyle::CitationStyle(string const & command)
475 : style(CITE), full(false), forceUCase(false)
480 string cmd = command;
486 string::size_type const n = cmd.size() - 1;
487 if (cmd != "cite" && cmd[n] == '*') {
489 cmd = cmd.substr(0,n);
492 char const * const * const last = citeCommands + nCiteCommands;
493 char const * const * const ptr = std::find(citeCommands, last, cmd);
496 size_t idx = ptr - citeCommands;
497 style = citeStyles[idx];
502 string const CitationStyle::asLatexStr() const
504 string cite = citeCommands[style];
506 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
507 if (std::find(citeStylesFull, last, style) != last)
512 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
513 if (std::find(citeStylesUCase, last, style) != last)
521 vector<CiteStyle> const getCiteStyles(CiteEngine const engine)
523 unsigned int nStyles = 0;
524 unsigned int start = 0;
531 case ENGINE_NATBIB_AUTHORYEAR:
532 case ENGINE_NATBIB_NUMERICAL:
533 nStyles = nCiteStyles - 1;
537 nStyles = nCiteStyles;
542 typedef vector<CiteStyle> cite_vec;
544 cite_vec styles(nStyles);
547 for (; i != styles.size(); ++i, ++j)
548 styles[i] = citeStyles[j];
553 } // namespace biblio