3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS
15 #pragma implementation
20 #include "gettext.h" // for _()
21 #include "helper_funcs.h"
22 #include "support/lstrings.h"
23 #include "support/LAssert.h"
25 #include <boost/regex.hpp>
37 using namespace biblio;
39 char const * const citeCommands[] = {
40 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
41 "citeyear", "citeyearpar" };
43 unsigned int const nCiteCommands =
44 sizeof(citeCommands) / sizeof(char *);
46 CiteStyle const citeStyles[] = {
47 CITE, CITET, CITEP, CITEALT, CITEALP,
48 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
50 unsigned int const nCiteStyles =
51 sizeof(citeStyles) / sizeof(CiteStyle);
53 CiteStyle const citeStylesFull[] = {
54 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
56 unsigned int const nCiteStylesFull =
57 sizeof(citeStylesFull) / sizeof(CiteStyle);
59 CiteStyle const citeStylesUCase[] = {
60 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
62 unsigned int const nCiteStylesUCase =
63 sizeof(citeStylesUCase) / sizeof(CiteStyle);
66 // The functions doing the dirty work for the search.
67 vector<string>::const_iterator
68 simpleSearch(InfoMap const & theMap,
69 vector<string> const & keys,
71 vector<string>::const_iterator start,
79 vector<string> searchwords = getVectorFromString(tmp, " ");
81 // Loop over all keys from start...
82 for (vector<string>::const_iterator it = start;
83 // End condition is direction-dependent.
84 (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
85 // increment is direction-dependent.
86 (dir == FORWARD) ? (++it) : (--it)) {
89 InfoMap::const_iterator info = theMap.find(*it);
90 if (info != theMap.end())
91 data += " " + info->second;
93 data = lowercase(data);
97 // Loop over all search words...
98 for (vector<string>::const_iterator sit = searchwords.begin();
99 sit != searchwords.end(); ++sit) {
100 if (data.find(*sit) == string::npos) {
106 if (found) return it;
113 vector<string>::const_iterator
114 regexSearch(InfoMap const & theMap,
115 vector<string> const & keys,
117 vector<string>::const_iterator start,
120 boost::regex reg(STRCONV(expr));
122 for (vector<string>::const_iterator it = start;
123 // End condition is direction-dependent.
124 (dir == FORWARD) ? (it < keys.end()) : (it >= keys.begin());
125 // increment is direction-dependent.
126 (dir == FORWARD) ? (++it) : (--it)) {
129 InfoMap::const_iterator info = theMap.find(*it);
130 if (info != theMap.end())
131 data += " " + info->second;
133 if (boost::regex_match(STRCONV(data), reg)) {
145 string const familyName(string const & name)
147 // Very simple parser
150 // possible authorname combinations are:
151 // "Surname, FirstName"
153 // "FirstName Surname"
155 string::size_type idx = fname.find(",");
156 if (idx != string::npos)
157 return ltrim(fname.substr(0, idx));
158 idx = fname.rfind(".");
159 if (idx != string::npos)
160 fname = ltrim(fname.substr(idx + 1));
161 // test if we have a LaTeX Space in front
162 if (fname[0] == '\\')
163 return fname.substr(2);
169 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
171 lyx::Assert(!map.empty());
173 InfoMap::const_iterator it = map.find(key);
176 string const & data = it->second;
178 // Is the entry a BibTeX one or one from lyx-layout "bibliography"?
179 string::size_type const pos = data.find("TheBibliographyRef");
180 if (pos != string::npos) {
185 string const opt = trim(data.substr(0, pos - 1));
190 split(opt, authors, '(');
194 string author = parseBibTeX(data, "author");
196 author = parseBibTeX(data, "editor");
198 if (author.empty()) {
199 author = parseBibTeX(data, "key");
205 vector<string> const authors = getVectorFromString(author, " and ");
209 author = familyName(authors[0]);
210 if (authors.size() == 2)
211 author += _(" and ") + familyName(authors[1]);
212 else if (authors.size() > 2)
213 author += _(" et al.");
219 string const getYear(InfoMap const & map, string const & key)
221 lyx::Assert(!map.empty());
223 InfoMap::const_iterator it = map.find(key);
226 string const & data = it->second;
228 // Is the entry a BibTeX one or one from lyx-layout "bibliography"?
229 string::size_type const pos = data.find("TheBibliographyRef");
230 if (pos != string::npos) {
236 trim(data.substr(0, pos - 1));
241 string const tmp = split(opt, authors, '(');
243 split(tmp, year, ')');
248 string year = parseBibTeX(data, "year");
258 // A functor for use with std::sort, leading to case insensitive sorting
259 struct compareNoCase: public std::binary_function<string, string, bool>
261 bool operator()(string const & s1, string const & s2) const {
262 return compare_ascii_no_case(s1, s2) < 0;
269 vector<string> const getKeys(InfoMap const & map)
271 vector<string> bibkeys;
272 InfoMap::const_iterator it = map.begin();
273 InfoMap::const_iterator end = map.end();
274 for (; it != end; ++it) {
275 bibkeys.push_back(it->first);
278 std::sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
283 string const getInfo(InfoMap const & map, string const & key)
285 lyx::Assert(!map.empty());
287 InfoMap::const_iterator it = map.find(key);
290 string const & data = it->second;
292 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
293 string const separator("TheBibliographyRef");
294 string::size_type const pos = data.find(separator);
295 if (pos != string::npos) {
296 string::size_type const pos2 = pos + separator.size();
297 string const info = trim(data.substr(pos2));
301 // Search for all possible "required" keys
302 string author = parseBibTeX(data, "author");
304 author = parseBibTeX(data, "editor");
306 string year = parseBibTeX(data, "year");
307 string title = parseBibTeX(data, "title");
308 string booktitle = parseBibTeX(data, "booktitle");
309 string chapter = parseBibTeX(data, "chapter");
310 string number = parseBibTeX(data, "number");
311 string volume = parseBibTeX(data, "volume");
312 string pages = parseBibTeX(data, "pages");
314 string media = parseBibTeX(data, "journal");
316 media = parseBibTeX(data, "publisher");
318 media = parseBibTeX(data, "school");
320 media = parseBibTeX(data, "institution");
322 ostringstream result;
324 result << author << ", ";
327 if (!booktitle.empty())
328 result << ", in " << booktitle;
329 if (!chapter.empty())
330 result << ", Ch. " << chapter;
332 result << ", " << media;
334 result << ", vol. " << volume;
336 result << ", no. " << number;
338 result << ", pp. " << pages;
340 result << ", " << year;
342 string const result_str = rtrim(STRCONV(result.str()));
343 if (!result_str.empty())
346 // This should never happen (or at least be very unusual!)
351 vector<string>::const_iterator
352 searchKeys(InfoMap const & theMap,
353 vector<string> const & keys,
355 vector<string>::const_iterator start,
360 // Preliminary checks
361 if (start < keys.begin() || start >= keys.end())
364 string search_expr = trim(expr);
365 if (search_expr.empty())
369 return simpleSearch(theMap, keys, search_expr, start, dir,
372 return regexSearch(theMap, keys, search_expr, start, dir);
376 string const parseBibTeX(string data, string const & findkey)
379 // at first we delete all characters right of '%' and
380 // replace tabs through a space and remove leading spaces
381 // we read the data line by line so that the \n are
385 string dummy = token(data,'\n', Entries);
386 while (!dummy.empty()) {
387 dummy = subst(dummy, '\t', ' '); // no tabs
388 dummy = ltrim(dummy); // no leading spaces
389 // ignore lines with a beginning '%' or ignore all right of %
390 string::size_type const idx =
391 dummy.empty() ? string::npos : dummy.find('%');
392 if (idx != string::npos)
393 dummy.erase(idx, string::npos);
394 // do we have a new token or a new line of
395 // the same one? In the first case we ignore
396 // the \n and in the second we replace it
398 if (!dummy.empty()) {
399 if (!contains(dummy, "="))
400 data_ += (' ' + dummy);
404 dummy = token(data, '\n', ++Entries);
407 // replace double commas with "" for easy scanning
408 data = subst(data_, ",,", "\"\"");
414 // now get only the important line of the bibtex entry.
415 // all entries are devided by ',' except the last one.
416 data += ','; // now we have same behaviour for all entries
417 // because the last one is "blah ... }"
420 // parsing of title and booktitle is different from the
421 // others, because booktitle contains title
423 dummy = token(data, ',', Entries++);
424 if (!dummy.empty()) {
425 found = contains(ascii_lowercase(dummy), findkey);
426 if (findkey == "title" &&
427 contains(ascii_lowercase(dummy), "booktitle"))
430 } while (!found && !dummy.empty());
435 // we are not sure, if we get all, because "key= "blah, blah" is
437 // Therefore we read all until the next "=" character, which follows a
440 dummy = token(data, ',', Entries++);
441 while (!contains(dummy, '=') && !dummy.empty()) {
442 keyvalue += (',' + dummy);
443 dummy = token(data, ',', Entries++);
446 // replace double "" with originals ,, (two commas)
447 // leaving us with the all-important line
448 data = subst(keyvalue, "\"\"", ",,");
453 // 2. if there is no opening '{' then a closing '{' is probably cruft.
454 if (!contains(data, '{'))
455 data = rtrim(data, "}");
456 // happens, when last keyword
457 string::size_type const idx =
458 !data.empty() ? data.find('=') : string::npos;
460 if (idx == string::npos)
463 data = trim(data.substr(idx));
465 if (data.length() < 2 || data[0] != '=') { // a valid entry?
468 // delete '=' and the following spaces
469 data = ltrim(data, " =");
470 if (data.length() < 2) {
471 return data; // not long enough to find delimiters
473 string::size_type keypos = 1;
475 if (data[0] == '{') {
477 } else if (data[0] == '"') {
480 // no {} and no "", pure data but with a
481 // possible ',' at the end
482 return rtrim(data, ",");
484 string tmp = data.substr(keypos);
485 while (tmp.find('{') != string::npos &&
486 tmp.find('}') != string::npos &&
487 tmp.find('{') < tmp.find('}') &&
488 tmp.find('{') < tmp.find(enclosing)) {
490 keypos += tmp.find('{') + 1;
491 tmp = data.substr(keypos);
492 keypos += tmp.find('}') + 1;
493 tmp = data.substr(keypos);
495 if (tmp.find(enclosing) == string::npos)
498 keypos += tmp.find(enclosing);
499 return data.substr(1, keypos - 1);
506 CitationStyle const getCitationStyle(string const & command)
508 if (command.empty()) return CitationStyle();
511 string cmd = command;
514 cs.forceUCase = true;
518 size_t n = cmd.size() - 1;
521 cmd = cmd.substr(0,n);
524 char const * const * const last = citeCommands + nCiteCommands;
525 char const * const * const ptr = std::find(citeCommands, last, cmd);
528 size_t idx = ptr - citeCommands;
529 cs.style = citeStyles[idx];
536 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
538 string cite = citeCommands[command];
540 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
541 if (std::find(citeStylesFull, last, command) != last)
546 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
547 if (std::find(citeStylesUCase, last, command) != last)
555 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
557 unsigned int nStyles = 1;
558 unsigned int start = 0;
560 nStyles = nCiteStyles - 1;
564 vector<CiteStyle> styles(nStyles);
566 vector<CiteStyle>::size_type i = 0;
568 for (; i != styles.size(); ++i, ++j) {
569 styles[i] = citeStyles[j];
577 getNumericalStrings(string const & key,
578 InfoMap const & map, vector<CiteStyle> const & styles)
581 return vector<string>();
584 string const author = getAbbreviatedAuthor(map, key);
585 string const year = getYear(map, key);
586 if (author.empty() || year.empty())
587 return vector<string>();
589 vector<string> vec(styles.size());
590 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
600 str = author + " [#ID]";
604 str = author + " #ID";
620 str = "(" + year + ")";
632 getAuthorYearStrings(string const & key,
633 InfoMap const & map, vector<CiteStyle> const & styles)
636 return vector<string>();
639 string const author = getAbbreviatedAuthor(map, key);
640 string const year = getYear(map, key);
641 if (author.empty() || year.empty())
642 return vector<string>();
644 vector<string> vec(styles.size());
645 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
651 str = author + " (" + year + ")";
655 str = "(" + author + ", " + year + ")";
659 str = author + " " + year ;
663 str = author + ", " + year ;
675 str = "(" + year + ")";
685 } // namespace biblio