1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2001 The LyX Team.
8 * ======================================================
11 * \author Angus Leeming <a.leeming@ic.ac.uk>
12 * \author Herbert Voss <voss@perce.de>
21 #pragma implementation
26 #include "gettext.h" // for _()
27 #include "helper_funcs.h"
28 #include "support/lstrings.h"
29 #include "support/LAssert.h"
30 #include "support/LRegex.h"
42 using namespace biblio;
44 char const * const citeCommands[] = {
45 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
46 "citeyear", "citeyearpar" };
48 unsigned int const nCiteCommands =
49 sizeof(citeCommands) / sizeof(char *);
51 CiteStyle const citeStyles[] = {
52 CITE, CITET, CITEP, CITEALT, CITEALP,
53 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
55 unsigned int const nCiteStyles =
56 sizeof(citeStyles) / sizeof(CiteStyle);
58 CiteStyle const citeStylesFull[] = {
59 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
61 unsigned int const nCiteStylesFull =
62 sizeof(citeStylesFull) / sizeof(CiteStyle);
64 CiteStyle const citeStylesUCase[] = {
65 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
67 unsigned int const nCiteStylesUCase =
68 sizeof(citeStylesUCase) / sizeof(CiteStyle);
71 // The functions doing the dirty work for the search.
72 vector<string>::const_iterator
73 simpleSearch(InfoMap const & theMap,
74 vector<string> const & keys,
76 vector<string>::const_iterator start,
84 vector<string> searchwords = getVectorFromString(tmp, " ");
86 // Loop over all keys from start...
87 for (vector<string>::const_iterator it = start;
88 // End condition is direction-dependent.
89 (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
90 // increment is direction-dependent.
91 (dir == FORWARD) ? (++it) : (--it)) {
94 InfoMap::const_iterator info = theMap.find(*it);
95 if (info != theMap.end())
96 data += " " + info->second;
98 data = lowercase(data);
102 // Loop over all search words...
103 for (vector<string>::const_iterator sit = searchwords.begin();
104 sit != searchwords.end(); ++sit) {
105 if (data.find(*sit) == string::npos) {
111 if (found) return it;
118 vector<string>::const_iterator
119 regexSearch(InfoMap const & theMap,
120 vector<string> const & keys,
122 vector<string>::const_iterator start,
127 for (vector<string>::const_iterator it = start;
128 // End condition is direction-dependent.
129 (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
130 // increment is direction-dependent.
131 (dir == FORWARD) ? (++it) : (--it)) {
134 InfoMap::const_iterator info = theMap.find(*it);
135 if (info != theMap.end())
136 data += " " + info->second;
138 if (reg.exec(data).size() > 0)
149 string const familyName(string const & name)
151 // Very simple parser
154 // possible authorname combinations are:
155 // "Surname, FirstName"
157 // "FirstName Surname"
159 string::size_type idx = fname.find(",");
160 if (idx != string::npos)
161 return frontStrip(fname.substr(0,idx));
162 idx = fname.rfind(".");
163 if (idx != string::npos)
164 fname = frontStrip(fname.substr(idx+1));
165 // test if we have a LaTeX Space in front
166 if (fname[0] == '\\')
167 return fname.substr(2);
173 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
175 lyx::Assert(!map.empty());
177 InfoMap::const_iterator it = map.find(key);
181 string::size_type const pos = it->second.find("TheBibliographyRef");
182 if (pos != string::npos) {
188 strip(frontStrip(it->second.substr(0, pos-1)));
193 split(opt, authors, '(');
197 string author = parseBibTeX(it->second, "author");
199 author = parseBibTeX(it->second, "editor");
201 if (author.empty()) {
202 author = parseBibTeX(it->second, "key");
208 vector<string> authors = getVectorFromString(author, " and");
210 if (!authors.empty()) {
213 for (vector<string>::iterator it = authors.begin();
214 it != authors.end(); ++it) {
215 *it = familyName(strip(*it));
219 if (authors.size() == 2)
220 author += _(" and ") + authors[1];
221 else if (authors.size() > 2)
222 author += _(" et al.");
229 string const getYear(InfoMap const & map, string const & key)
231 lyx::Assert(!map.empty());
233 InfoMap::const_iterator it = map.find(key);
237 string::size_type const pos = it->second.find("TheBibliographyRef");
238 if (pos != string::npos) {
244 strip(frontStrip(it->second.substr(0, pos-1)));
249 string const tmp = split(opt, authors, '(');
251 split(tmp, year, ')');
256 string year = parseBibTeX(it->second, "year");
264 // A functor for use with std::sort, leading to case insensitive sorting
265 struct compareNoCase: public std::binary_function<string, string, bool>
267 bool operator()(string const & s1, string const & s2) const {
268 return compare_no_case(s1, s2) < 0;
272 vector<string> const getKeys(InfoMap const & map)
274 vector<string> bibkeys;
276 for (InfoMap::const_iterator it = map.begin(); it != map.end(); ++it) {
277 bibkeys.push_back(it->first);
280 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
285 string const getInfo(InfoMap const & map, string const & key)
287 lyx::Assert(!map.empty());
289 InfoMap::const_iterator it = map.find(key);
293 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
294 string const separator("TheBibliographyRef");
295 string::size_type const pos = it->second.find(separator);
296 if (pos != string::npos) {
297 string::size_type const pos2 = pos + separator.size();
298 string const info = strip(frontStrip(it->second.substr(pos2)));
302 // Search for all possible "required" keys
303 string author = parseBibTeX(it->second, "author");
305 author = parseBibTeX(it->second, "editor");
307 string year = parseBibTeX(it->second, "year");
308 string title = parseBibTeX(it->second, "title");
309 string booktitle = parseBibTeX(it->second, "booktitle");
310 string chapter = parseBibTeX(it->second, "chapter");
311 string number = parseBibTeX(it->second, "number");
312 string volume = parseBibTeX(it->second, "volume");
313 string pages = parseBibTeX(it->second, "pages");
315 string media = parseBibTeX(it->second, "journal");
317 media = parseBibTeX(it->second, "publisher");
319 media = parseBibTeX(it->second, "school");
321 media = parseBibTeX(it->second, "institution");
323 ostringstream result;
325 result << author << ", ";
328 if (!booktitle.empty())
329 result << ", in " << booktitle;
330 if (!chapter.empty())
331 result << ", Ch. " << chapter;
333 result << ", " << media;
335 result << ", vol. " << volume;
337 result << ", no. " << number;
339 result << ", pp. " << pages;
341 result << ", " << year;
343 string const result_str = strip(result.str().c_str());
344 if (!result_str.empty())
347 // This should never happen (or at least be very unusual!)
352 vector<string>::const_iterator
353 searchKeys(InfoMap const & theMap,
354 vector<string> const & keys,
356 vector<string>::const_iterator start,
361 // Preliminary checks
362 if (start < keys.begin() || start >= keys.end())
365 string search_expr = frontStrip(strip(expr));
366 if (search_expr.empty())
370 return simpleSearch(theMap, keys, search_expr, start, dir,
373 return regexSearch(theMap, keys, search_expr, start, dir);
377 string const parseBibTeX(string data, string const & findkey)
380 // at first we delete all characters right of '%' and
381 // replace tabs through a space and remove leading spaces
382 // we read the data line by line so that the \n are
386 string dummy = token(data,'\n', Entries);
387 while (!dummy.empty()) {
388 dummy = subst(dummy, '\t', ' '); // no tabs
389 dummy = frontStrip(dummy); // no leading spaces
390 // ignore lines with a beginning '%' or ignore all right of %
391 string::size_type const idx =
392 dummy.empty() ? string::npos : dummy.find('%');
393 if (idx != string::npos)
394 dummy.erase(idx, string::npos);
395 // do we have a new token or a new line of
396 // the same one? In the first case we ignore
397 // the \n and in the second we replace it
399 if (!dummy.empty()) {
400 if (!contains(dummy, "="))
401 data_ += (' ' + dummy);
405 dummy = token(data, '\n', ++Entries);
408 // replace double commas with "" for easy scanning
409 data = subst(data_, ",,", "\"\"");
415 // now get only the important line of the bibtex entry.
416 // all entries are devided by ',' except the last one.
417 data += ','; // now we have same behaviour for all entries
418 // because the last one is "blah ... }"
421 // parsing of title and booktitle is different from the
422 // others, because booktitle contains title
424 dummy = token(data, ',', Entries++);
425 if (!dummy.empty()) {
426 found = contains(lowercase(dummy), findkey);
427 if (findkey == "title" &&
428 contains(lowercase(dummy), "booktitle"))
431 } while (!found && !dummy.empty());
436 // we are not sure, if we get all, because "key= "blah, blah" is
438 // Therefore we read all until the next "=" character, which follows a
441 dummy = token(data, ',', Entries++);
442 while (!contains(dummy, '=') && !dummy.empty()) {
443 keyvalue += (',' + dummy);
444 dummy = token(data, ',', Entries++);
447 // replace double "" with originals ,, (two commas)
448 // leaving us with the all-important line
449 data = subst(keyvalue, "\"\"", ",,");
453 data = strip(data, ' ');
454 // 2. if there is no opening '{' then a closing '{' is probably cruft.
455 if (!contains(data, '{'))
456 data = strip(data, '}');
457 // happens, when last keyword
458 string::size_type const idx =
459 !data.empty() ? data.find('=') : string::npos;
461 if (idx == string::npos)
464 data = data.substr(idx);
465 data = frontStrip(strip(data));
467 if (data.length() < 2 || data[0] != '=') { // a valid entry?
470 // delete '=' and the following spaces
471 data = frontStrip(frontStrip(data,'='));
472 if (data.length() < 2) {
473 return data; // not long enough to find delimiters
475 string::size_type keypos = 1;
477 if (data[0] == '{') {
479 } else if (data[0] == '"') {
482 // no {} and no "", pure data but with a
483 // possible ',' at the end
484 return strip(data,',');
486 string tmp = data.substr(keypos);
487 while (tmp.find('{') != string::npos &&
488 tmp.find('}') != string::npos &&
489 tmp.find('{') < tmp.find('}') &&
490 tmp.find('{') < tmp.find(enclosing)) {
492 keypos += tmp.find('{') + 1;
493 tmp = data.substr(keypos);
494 keypos += tmp.find('}') + 1;
495 tmp = data.substr(keypos);
497 if (tmp.find(enclosing) == string::npos)
500 keypos += tmp.find(enclosing);
501 return data.substr(1, keypos - 1);
508 CitationStyle const getCitationStyle(string const & command)
510 if (command.empty()) return CitationStyle();
513 string cmd = command;
516 cs.forceUCase = true;
520 size_t n = cmd.size()-1;
523 cmd = cmd.substr(0,n);
526 char const * const * const last = citeCommands + nCiteCommands;
527 char const * const * const ptr = std::find(citeCommands, last, cmd);
530 size_t idx = ptr - citeCommands;
531 cs.style = citeStyles[idx];
538 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
540 string cite = citeCommands[command];
542 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
543 if (std::find(citeStylesFull, last, command) != last)
548 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
549 if (std::find(citeStylesUCase, last, command) != last)
557 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
559 unsigned int nStyles = 1;
560 unsigned int start = 0;
562 nStyles = nCiteStyles - 1;
566 vector<CiteStyle> styles(nStyles);
568 vector<CiteStyle>::size_type i = 0;
570 for (; i != styles.size(); ++i, ++j) {
571 styles[i] = citeStyles[j];
579 getNumericalStrings(string const & key,
580 InfoMap const & map, vector<CiteStyle> const & styles)
583 return vector<string>();
586 string const author = getAbbreviatedAuthor(map, key);
587 string const year = getYear(map, key);
588 if (author.empty() || year.empty())
589 return vector<string>();
591 vector<string> vec(styles.size());
592 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
602 str = author + " [#ID]";
606 str = author + " #ID";
622 str = "(" + year + ")";
634 getAuthorYearStrings(string const & key,
635 InfoMap const & map, vector<CiteStyle> const & styles)
638 return vector<string>();
641 string const author = getAbbreviatedAuthor(map, key);
642 string const year = getYear(map, key);
643 if (author.empty() || year.empty())
644 return vector<string>();
646 vector<string> vec(styles.size());
647 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
653 str = author + " (" + year + ")";
657 str = "(" + author + ", " + year + ")";
661 str = author + " " + year ;
665 str = author + ", " + year ;
677 str = "(" + year + ")";
687 } // namespace biblio