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>
39 using namespace biblio;
41 char const * const citeCommands[] = {
42 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
43 "citeyear", "citeyearpar" };
45 unsigned int const nCiteCommands =
46 sizeof(citeCommands) / sizeof(char *);
48 CiteStyle const citeStyles[] = {
49 CITE, CITET, CITEP, CITEALT, CITEALP,
50 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
52 unsigned int const nCiteStyles =
53 sizeof(citeStyles) / sizeof(CiteStyle);
55 CiteStyle const citeStylesFull[] = {
56 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
58 unsigned int const nCiteStylesFull =
59 sizeof(citeStylesFull) / sizeof(CiteStyle);
61 CiteStyle const citeStylesUCase[] = {
62 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
64 unsigned int const nCiteStylesUCase =
65 sizeof(citeStylesUCase) / sizeof(CiteStyle);
68 // The functions doing the dirty work for the search.
69 vector<string>::const_iterator
70 simpleSearch(InfoMap const & theMap,
71 vector<string> const & keys,
73 vector<string>::const_iterator start,
81 vector<string> searchwords = getVectorFromString(tmp, " ");
83 // Loop over all keys from start...
84 for (vector<string>::const_iterator it = start;
85 // End condition is direction-dependent.
86 (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
87 // increment is direction-dependent.
88 (dir == FORWARD) ? (++it) : (--it)) {
91 InfoMap::const_iterator info = theMap.find(*it);
92 if (info != theMap.end())
93 data += " " + info->second;
95 data = lowercase(data);
99 // Loop over all search words...
100 for (vector<string>::const_iterator sit = searchwords.begin();
101 sit != searchwords.end(); ++sit) {
102 if (data.find(*sit) == string::npos) {
108 if (found) return it;
115 vector<string>::const_iterator
116 regexSearch(InfoMap const & theMap,
117 vector<string> const & keys,
119 vector<string>::const_iterator start,
122 boost::regex reg(expr);
124 for (vector<string>::const_iterator it = start;
125 // End condition is direction-dependent.
126 (dir == FORWARD) ? (it < keys.end()) : (it >= keys.begin());
127 // increment is direction-dependent.
128 (dir == FORWARD) ? (++it) : (--it)) {
131 InfoMap::const_iterator info = theMap.find(*it);
132 if (info != theMap.end())
133 data += " " + info->second;
135 if (boost::regex_match(data, reg)) {
147 string const familyName(string const & name)
149 // Very simple parser
152 // possible authorname combinations are:
153 // "Surname, FirstName"
155 // "FirstName Surname"
157 string::size_type idx = fname.find(",");
158 if (idx != string::npos)
159 return ltrim(fname.substr(0, idx));
160 idx = fname.rfind(".");
161 if (idx != string::npos)
162 fname = ltrim(fname.substr(idx + 1));
163 // test if we have a LaTeX Space in front
164 if (fname[0] == '\\')
165 return fname.substr(2);
171 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
173 lyx::Assert(!map.empty());
175 InfoMap::const_iterator it = map.find(key);
179 string::size_type const pos = it->second.find("TheBibliographyRef");
180 if (pos != string::npos) {
185 string const opt = trim(it->second.substr(0, pos - 1));
190 split(opt, authors, '(');
194 string author = parseBibTeX(it->second, "author");
196 author = parseBibTeX(it->second, "editor");
198 if (author.empty()) {
199 author = parseBibTeX(it->second, "key");
205 vector<string> authors = getVectorFromString(author, " and");
207 if (!authors.empty()) {
210 for (vector<string>::iterator it = authors.begin();
211 it != authors.end(); ++it) {
212 *it = familyName(rtrim(*it));
216 if (authors.size() == 2)
217 author += _(" and ") + authors[1];
218 else if (authors.size() > 2)
219 author += _(" et al.");
226 string const getYear(InfoMap const & map, string const & key)
228 lyx::Assert(!map.empty());
230 InfoMap::const_iterator it = map.find(key);
234 string::size_type const pos = it->second.find("TheBibliographyRef");
235 if (pos != string::npos) {
241 trim(it->second.substr(0, pos - 1));
246 string const tmp = split(opt, authors, '(');
248 split(tmp, year, ')');
253 string year = parseBibTeX(it->second, "year");
261 // A functor for use with std::sort, leading to case insensitive sorting
262 struct compareNoCase: public std::binary_function<string, string, bool>
264 bool operator()(string const & s1, string const & s2) const {
265 return compare_ascii_no_case(s1, s2) < 0;
269 vector<string> const getKeys(InfoMap const & map)
271 vector<string> bibkeys;
273 for (InfoMap::const_iterator it = map.begin(); it != map.end(); ++it) {
274 bibkeys.push_back(it->first);
277 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
282 string const getInfo(InfoMap const & map, string const & key)
284 lyx::Assert(!map.empty());
286 InfoMap::const_iterator it = map.find(key);
290 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
291 string const separator("TheBibliographyRef");
292 string::size_type const pos = it->second.find(separator);
293 if (pos != string::npos) {
294 string::size_type const pos2 = pos + separator.size();
295 string const info = trim(it->second.substr(pos2));
299 // Search for all possible "required" keys
300 string author = parseBibTeX(it->second, "author");
302 author = parseBibTeX(it->second, "editor");
304 string year = parseBibTeX(it->second, "year");
305 string title = parseBibTeX(it->second, "title");
306 string booktitle = parseBibTeX(it->second, "booktitle");
307 string chapter = parseBibTeX(it->second, "chapter");
308 string number = parseBibTeX(it->second, "number");
309 string volume = parseBibTeX(it->second, "volume");
310 string pages = parseBibTeX(it->second, "pages");
312 string media = parseBibTeX(it->second, "journal");
314 media = parseBibTeX(it->second, "publisher");
316 media = parseBibTeX(it->second, "school");
318 media = parseBibTeX(it->second, "institution");
320 ostringstream result;
322 result << author << ", ";
325 if (!booktitle.empty())
326 result << ", in " << booktitle;
327 if (!chapter.empty())
328 result << ", Ch. " << chapter;
330 result << ", " << media;
332 result << ", vol. " << volume;
334 result << ", no. " << number;
336 result << ", pp. " << pages;
338 result << ", " << year;
340 string const result_str = rtrim(result.str().c_str());
341 if (!result_str.empty())
344 // This should never happen (or at least be very unusual!)
349 vector<string>::const_iterator
350 searchKeys(InfoMap const & theMap,
351 vector<string> const & keys,
353 vector<string>::const_iterator start,
358 // Preliminary checks
359 if (start < keys.begin() || start >= keys.end())
362 string search_expr = trim(expr);
363 if (search_expr.empty())
367 return simpleSearch(theMap, keys, search_expr, start, dir,
370 return regexSearch(theMap, keys, search_expr, start, dir);
374 string const parseBibTeX(string data, string const & findkey)
377 // at first we delete all characters right of '%' and
378 // replace tabs through a space and remove leading spaces
379 // we read the data line by line so that the \n are
383 string dummy = token(data,'\n', Entries);
384 while (!dummy.empty()) {
385 dummy = subst(dummy, '\t', ' '); // no tabs
386 dummy = ltrim(dummy); // no leading spaces
387 // ignore lines with a beginning '%' or ignore all right of %
388 string::size_type const idx =
389 dummy.empty() ? string::npos : dummy.find('%');
390 if (idx != string::npos)
391 dummy.erase(idx, string::npos);
392 // do we have a new token or a new line of
393 // the same one? In the first case we ignore
394 // the \n and in the second we replace it
396 if (!dummy.empty()) {
397 if (!contains(dummy, "="))
398 data_ += (' ' + dummy);
402 dummy = token(data, '\n', ++Entries);
405 // replace double commas with "" for easy scanning
406 data = subst(data_, ",,", "\"\"");
412 // now get only the important line of the bibtex entry.
413 // all entries are devided by ',' except the last one.
414 data += ','; // now we have same behaviour for all entries
415 // because the last one is "blah ... }"
418 // parsing of title and booktitle is different from the
419 // others, because booktitle contains title
421 dummy = token(data, ',', Entries++);
422 if (!dummy.empty()) {
423 found = contains(ascii_lowercase(dummy), findkey);
424 if (findkey == "title" &&
425 contains(ascii_lowercase(dummy), "booktitle"))
428 } while (!found && !dummy.empty());
433 // we are not sure, if we get all, because "key= "blah, blah" is
435 // Therefore we read all until the next "=" character, which follows a
438 dummy = token(data, ',', Entries++);
439 while (!contains(dummy, '=') && !dummy.empty()) {
440 keyvalue += (',' + dummy);
441 dummy = token(data, ',', Entries++);
444 // replace double "" with originals ,, (two commas)
445 // leaving us with the all-important line
446 data = subst(keyvalue, "\"\"", ",,");
451 // 2. if there is no opening '{' then a closing '{' is probably cruft.
452 if (!contains(data, '{'))
453 data = rtrim(data, "}");
454 // happens, when last keyword
455 string::size_type const idx =
456 !data.empty() ? data.find('=') : string::npos;
458 if (idx == string::npos)
461 data = trim(data.substr(idx));
463 if (data.length() < 2 || data[0] != '=') { // a valid entry?
466 // delete '=' and the following spaces
467 data = ltrim(data, " =");
468 if (data.length() < 2) {
469 return data; // not long enough to find delimiters
471 string::size_type keypos = 1;
473 if (data[0] == '{') {
475 } else if (data[0] == '"') {
478 // no {} and no "", pure data but with a
479 // possible ',' at the end
480 return rtrim(data, ",");
482 string tmp = data.substr(keypos);
483 while (tmp.find('{') != string::npos &&
484 tmp.find('}') != string::npos &&
485 tmp.find('{') < tmp.find('}') &&
486 tmp.find('{') < tmp.find(enclosing)) {
488 keypos += tmp.find('{') + 1;
489 tmp = data.substr(keypos);
490 keypos += tmp.find('}') + 1;
491 tmp = data.substr(keypos);
493 if (tmp.find(enclosing) == string::npos)
496 keypos += tmp.find(enclosing);
497 return data.substr(1, keypos - 1);
504 CitationStyle const getCitationStyle(string const & command)
506 if (command.empty()) return CitationStyle();
509 string cmd = command;
512 cs.forceUCase = true;
516 size_t n = cmd.size() - 1;
519 cmd = cmd.substr(0,n);
522 char const * const * const last = citeCommands + nCiteCommands;
523 char const * const * const ptr = std::find(citeCommands, last, cmd);
526 size_t idx = ptr - citeCommands;
527 cs.style = citeStyles[idx];
534 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
536 string cite = citeCommands[command];
538 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
539 if (std::find(citeStylesFull, last, command) != last)
544 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
545 if (std::find(citeStylesUCase, last, command) != last)
553 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
555 unsigned int nStyles = 1;
556 unsigned int start = 0;
558 nStyles = nCiteStyles - 1;
562 vector<CiteStyle> styles(nStyles);
564 vector<CiteStyle>::size_type i = 0;
566 for (; i != styles.size(); ++i, ++j) {
567 styles[i] = citeStyles[j];
575 getNumericalStrings(string const & key,
576 InfoMap const & map, vector<CiteStyle> const & styles)
579 return vector<string>();
582 string const author = getAbbreviatedAuthor(map, key);
583 string const year = getYear(map, key);
584 if (author.empty() || year.empty())
585 return vector<string>();
587 vector<string> vec(styles.size());
588 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
598 str = author + " [#ID]";
602 str = author + " #ID";
618 str = "(" + year + ")";
630 getAuthorYearStrings(string const & key,
631 InfoMap const & map, vector<CiteStyle> const & styles)
634 return vector<string>();
637 string const author = getAbbreviatedAuthor(map, key);
638 string const year = getYear(map, key);
639 if (author.empty() || year.empty())
640 return vector<string>();
642 vector<string> vec(styles.size());
643 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
649 str = author + " (" + year + ")";
653 str = "(" + author + ", " + year + ")";
657 str = author + " " + year ;
661 str = author + ", " + year ;
673 str = "(" + year + ")";
683 } // namespace biblio