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"
40 using support::bformat;
41 using support::compare_no_case;
42 using support::getVectorFromString;
46 ////////////////////////////////////////
50 ////////////////////////////////////////
52 BibTeXInfo::BibTeXInfo() : isBibTeX(true)
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));
79 docstring const familyName(docstring const & name)
85 docstring fname = name;
87 // possible authorname combinations are:
88 // "Surname, FirstName"
90 // "FirstName Surname"
92 docstring::size_type idx = fname.find(',');
93 if (idx != docstring::npos)
94 return ltrim(fname.substr(0, idx));
95 idx = fname.rfind('.');
96 if (idx != docstring::npos && idx + 1 < fname.size())
97 fname = ltrim(fname.substr(idx + 1));
98 // test if we have a LaTeX Space in front
100 return fname.substr(2);
107 docstring const BibTeXInfo::getAbbreviatedAuthor() const
112 docstring author = getValueForField("author");
114 if (author.empty()) {
115 author = getValueForField("editor");
120 //OK, we've got some names. Let's format them.
121 //Try to split the author list on " and "
122 vector<docstring> const authors =
123 getVectorFromString(author, from_ascii(" and "));
125 if (authors.size() == 2)
126 return bformat(_("%1$s and %2$s"),
127 familyName(authors[0]), familyName(authors[1]));
128 else if (authors.size() > 2)
129 return bformat(_("%1$s et al."), familyName(authors[0]));
131 return familyName(authors[0]);
134 docstring const BibTeXInfo::getYear() const
139 docstring year = getValueForField("year");
146 docstring const BibTeXInfo::getInfo() const
149 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
154 //This could be made alot better using the entryType
155 //field to customize the output based upon entry type.
157 //Search for all possible "required" fields
158 docstring author = getValueForField("author");
160 author = getValueForField("editor");
162 docstring year = getValueForField("year");
163 docstring title = getValueForField("title");
164 docstring docLoc = getValueForField("pages");
165 if (docLoc.empty()) {
166 docLoc = getValueForField("chapter");
168 docLoc = from_ascii("Ch. ") + docLoc;
170 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 std::sort, leading to case insensitive sorting
211 class compareNoCase: public std::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 std::sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
232 vector<docstring> const BiblioInfo::getFields() const
234 vector<docstring> bibfields;
235 set<docstring>::const_iterator it = fieldNames.begin();
236 set<docstring>::const_iterator end = fieldNames.end();
237 for (; it != end; ++it)
238 bibfields.push_back(*it);
239 std::sort(bibfields.begin(), bibfields.end());
244 vector<docstring> const BiblioInfo::getEntries() const
246 vector<docstring> bibentries;
247 set<docstring>::const_iterator it = entryTypes.begin();
248 set<docstring>::const_iterator end = entryTypes.end();
249 for (; it != end; ++it)
250 bibentries.push_back(*it);
251 std::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_NATBIB_NUMERICAL)
291 return getNumericalStrings(key, buf);
293 return getAuthorYearStrings(key, buf);
297 vector<docstring> const BiblioInfo::getNumericalStrings(
298 docstring const & key, Buffer const & buf) const
301 return vector<docstring>();
303 docstring const author = getAbbreviatedAuthor(key);
304 docstring const year = getYear(key);
305 if (author.empty() || year.empty())
306 return vector<docstring>();
308 vector<biblio::CiteStyle> const & styles =
309 biblio::getCiteStyles(buf.params().getEngine());
311 vector<docstring> vec(styles.size());
312 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
318 str = from_ascii("[#ID]");
322 str = author + " [#ID]";
325 case biblio::CITEALT:
326 str = author + " #ID";
329 case biblio::CITEALP:
330 str = from_ascii("#ID");
333 case biblio::CITEAUTHOR:
337 case biblio::CITEYEAR:
341 case biblio::CITEYEARPAR:
342 str = '(' + year + ')';
353 vector<docstring> const BiblioInfo::getAuthorYearStrings(
354 docstring const & key, Buffer const & buf) const
357 return vector<docstring>();
359 docstring const author = getAbbreviatedAuthor(key);
360 docstring const year = getYear(key);
361 if (author.empty() || year.empty())
362 return vector<docstring>();
364 vector<biblio::CiteStyle> const & styles =
365 getCiteStyles(buf.params().getEngine());
367 vector<docstring> vec(styles.size());
368 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
373 // jurabib only: Author/Annotator
374 // (i.e. the "before" field, 2nd opt arg)
375 str = author + "/<" + _("before") + '>';
379 str = author + " (" + year + ')';
383 str = '(' + author + ", " + year + ')';
386 case biblio::CITEALT:
387 str = author + ' ' + year ;
390 case biblio::CITEALP:
391 str = author + ", " + year ;
394 case biblio::CITEAUTHOR:
398 case biblio::CITEYEAR:
402 case biblio::CITEYEARPAR:
403 str = '(' + year + ')';
412 void BiblioInfo::fillWithBibKeys(Buffer const * const buf)
414 /// if this is a child document and the parent is already loaded
415 /// use the parent's list instead [ale990412]
416 Buffer const * const tmp = buf->masterBuffer();
419 this->fillWithBibKeys(tmp);
423 // Pre-load all child documents.
424 buf->loadChildDocuments();
426 for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
427 it->fillWithBibKeys(*buf, *this, it);
433 ////////////////////////////////////////
437 ////////////////////////////////////////
442 char const * const citeCommands[] = {
443 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
444 "citeyear", "citeyearpar" };
446 unsigned int const nCiteCommands =
447 sizeof(citeCommands) / sizeof(char *);
449 CiteStyle const citeStyles[] = {
450 CITE, CITET, CITEP, CITEALT, CITEALP,
451 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
453 unsigned int const nCiteStyles =
454 sizeof(citeStyles) / sizeof(CiteStyle);
456 CiteStyle const citeStylesFull[] = {
457 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
459 unsigned int const nCiteStylesFull =
460 sizeof(citeStylesFull) / sizeof(CiteStyle);
462 CiteStyle const citeStylesUCase[] = {
463 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
465 unsigned int const nCiteStylesUCase =
466 sizeof(citeStylesUCase) / sizeof(CiteStyle);
471 CitationStyle::CitationStyle(string const & command)
472 : style(CITE), full(false), forceUCase(false)
477 string cmd = command;
483 string::size_type const n = cmd.size() - 1;
484 if (cmd != "cite" && cmd[n] == '*') {
486 cmd = cmd.substr(0,n);
489 char const * const * const last = citeCommands + nCiteCommands;
490 char const * const * const ptr = std::find(citeCommands, last, cmd);
493 size_t idx = ptr - citeCommands;
494 style = citeStyles[idx];
499 string const CitationStyle::asLatexStr() const
501 string cite = citeCommands[style];
503 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
504 if (std::find(citeStylesFull, last, style) != last)
509 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
510 if (std::find(citeStylesUCase, last, style) != last)
518 vector<CiteStyle> const getCiteStyles(CiteEngine const engine)
520 unsigned int nStyles = 0;
521 unsigned int start = 0;
528 case ENGINE_NATBIB_AUTHORYEAR:
529 case ENGINE_NATBIB_NUMERICAL:
530 nStyles = nCiteStyles - 1;
534 nStyles = nCiteStyles;
539 typedef vector<CiteStyle> cite_vec;
541 cite_vec styles(nStyles);
542 cite_vec::size_type i = 0;
544 for (; i != styles.size(); ++i, ++j)
545 styles[i] = citeStyles[j];
550 } // namespace biblio