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>
20 #pragma implementation
25 #include "gettext.h" // for _()
26 #include "helper_funcs.h"
27 #include "support/lstrings.h"
28 #include "support/LAssert.h"
29 #include "support/LRegex.h"
41 using namespace biblio;
43 char const * const citeCommands[] = {
44 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
45 "citeyear", "citeyearpar" };
47 unsigned int const nCiteCommands =
48 sizeof(citeCommands) / sizeof(char *);
50 CiteStyle const citeStyles[] = {
51 CITE, CITET, CITEP, CITEALT, CITEALP,
52 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
54 unsigned int const nCiteStyles =
55 sizeof(citeStyles) / sizeof(CiteStyle);
57 CiteStyle const citeStylesFull[] = {
58 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
60 unsigned int const nCiteStylesFull =
61 sizeof(citeStylesFull) / sizeof(CiteStyle);
63 CiteStyle const citeStylesUCase[] = {
64 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
66 unsigned int const nCiteStylesUCase =
67 sizeof(citeStylesUCase) / sizeof(CiteStyle);
70 // The functions doing the dirty work for the search.
71 vector<string>::const_iterator
72 simpleSearch(InfoMap const & theMap,
73 vector<string> const & keys,
75 vector<string>::const_iterator start,
83 vector<string> searchwords = getVectorFromString(tmp, " ");
85 // Loop over all keys from start...
86 for (vector<string>::const_iterator it = start;
87 // End condition is direction-dependent.
88 (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
89 // increment is direction-dependent.
90 (dir == FORWARD) ? (++it) : (--it)) {
93 InfoMap::const_iterator info = theMap.find(*it);
94 if (info != theMap.end())
95 data += " " + info->second;
97 data = lowercase(data);
101 // Loop over all search words...
102 for (vector<string>::const_iterator sit = searchwords.begin();
103 sit != searchwords.end(); ++sit) {
104 if (data.find(*sit) == string::npos) {
110 if (found) return it;
117 vector<string>::const_iterator
118 regexSearch(InfoMap const & theMap,
119 vector<string> const & keys,
121 vector<string>::const_iterator start,
126 for (vector<string>::const_iterator it = start;
127 // End condition is direction-dependent.
128 (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
129 // increment is direction-dependent.
130 (dir == FORWARD) ? (++it) : (--it)) {
133 InfoMap::const_iterator info = theMap.find(*it);
134 if (info != theMap.end())
135 data += " " + info->second;
137 if (reg.exec(data).size() > 0)
144 string const familyName(string const & name)
146 // Very simple parser
149 // possible authorname combinations are:
150 // "Surname, FirstName"
152 // "FirstName Surname"
154 string::size_type idx = fname.find(",");
155 if (idx != string::npos)
156 return frontStrip(fname.substr(0,idx));
157 idx = fname.rfind(".");
158 if (idx != string::npos)
159 fname = frontStrip(fname.substr(idx+1));
165 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
167 lyx::Assert(!map.empty());
169 InfoMap::const_iterator it = map.find(key);
172 if (it != map.end()) {
173 author = parseBibTeX(it->second, "author");
175 author = parseBibTeX(it->second, "editor");
177 vector<string> authors = getVectorFromString(author, "and");
179 if (!authors.empty()) {
182 for (vector<string>::iterator it = authors.begin();
183 it != authors.end(); ++it) {
184 *it = familyName(strip(*it));
188 if (authors.size() == 2)
189 author += _(" and ") + authors[1];
190 else if (authors.size() > 2)
191 author += _(" et al.");
196 author = _("Caesar et al.");
202 string const getYear(InfoMap const & map, string const & key)
204 lyx::Assert(!map.empty());
206 InfoMap::const_iterator it = map.find(key);
211 year = parseBibTeX(it->second, "year");
223 // A functor for use with std::sort, leading to case insensitive sorting
224 struct compareNoCase: public std::binary_function<string, string, bool>
226 bool operator()(string const & s1, string const & s2) const {
227 return compare_no_case(s1, s2) < 0;
231 vector<string> const getKeys(InfoMap const & map)
233 vector<string> bibkeys;
235 for (InfoMap::const_iterator it = map.begin(); it != map.end(); ++it) {
236 bibkeys.push_back(it->first);
239 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
244 string const getInfo(InfoMap const & map, string const & key)
246 lyx::Assert(!map.empty());
248 InfoMap::const_iterator it = map.find(key);
251 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
252 if (!contains(it->second,'='))
253 return it->second.c_str();
255 // Search for all possible "required" keys
256 string author = parseBibTeX(it->second, "author");
258 author = parseBibTeX(it->second, "editor");
260 string year = parseBibTeX(it->second, "year");
261 string title = parseBibTeX(it->second, "title");
262 string booktitle = parseBibTeX(it->second, "booktitle");
263 string chapter = parseBibTeX(it->second, "chapter");
264 string number = parseBibTeX(it->second, "number");
265 string volume = parseBibTeX(it->second, "volume");
266 string pages = parseBibTeX(it->second, "pages");
268 string media = parseBibTeX(it->second, "journal");
270 media = parseBibTeX(it->second, "publisher");
272 media = parseBibTeX(it->second, "school");
274 media = parseBibTeX(it->second, "institution");
276 ostringstream result;
278 result << author << ", ";
281 if (!booktitle.empty())
282 result << ", in " << booktitle;
283 if (!chapter.empty())
284 result << ", Ch. " << chapter;
286 result << ", " << media;
288 result << ", vol. " << volume;
290 result << ", no. " << number;
292 result << ", pp. " << pages;
294 result << ", " << year;
296 char const * const tmp = result.str().c_str();
297 string result_str = tmp ? strip(tmp) : string();
299 if (result_str.empty())
300 // This should never happen (or at least be very unusual!)
301 result_str = it->second;
307 vector<string>::const_iterator
308 searchKeys(InfoMap const & theMap,
309 vector<string> const & keys,
311 vector<string>::const_iterator start,
316 // Preliminary checks
317 if (start < keys.begin() || start >= keys.end())
320 string search_expr = frontStrip(strip(expr));
321 if (search_expr.empty())
325 return simpleSearch(theMap, keys, search_expr, start, dir,
328 return regexSearch(theMap, keys, search_expr, start, dir);
332 string const parseBibTeX(string data, string const & findkey)
335 // at first we delete all characters right of '%' and
336 // replace tabs through a space and remove leading spaces
339 string dummy = token(data,'\n', Entries);
340 while (!dummy.empty()) {
341 dummy = subst(dummy, '\t', ' '); // no tabs
342 dummy = frontStrip(dummy); // no leading spaces
343 string::size_type const idx =
344 dummy.empty() ? string::npos : dummy.find('%');
345 if (idx != string::npos) {
346 // ignore lines with a beginning '%'
348 data_ += dummy.substr(0,data.find('%'));
353 dummy = token(data, '\n', ++Entries);
361 // now get only the important line of the bibtex entry.
362 // all entries are devided by ',' except the last one.
363 data += ','; // now we have same behaviour for all entries
364 // because the last one is "blah ... }"
366 dummy = token(data, ',', Entries);
367 while (!contains(lowercase(dummy), findkey) && !dummy.empty())
368 dummy = token(data, ',', ++Entries);
370 return string(); // no such keyword
371 // we are not sure, if we get all, because "key= "blah, blah" is allowed.
372 // therefore we read all until the next "=" character, which follows a
375 dummy = token(data, ',', ++Entries);
376 while (!contains(dummy, '=') && !dummy.empty()) {
377 keyvalue += (',' + dummy);
378 dummy = token(data, ',', ++Entries);
380 data = keyvalue; // now we have the important line
381 data = strip(data, ' '); // all spaces
382 if (!contains(data, '{')) // no opening '{'
383 data = strip(data, '}');// maybe there is a main closing '}'
384 // happens, when last keyword
385 string::size_type const idx =
386 !data.empty() ? data.find('=') : string::npos;
388 if (idx == string::npos)
391 data = data.substr(idx);
392 data = frontStrip(strip(data));
394 if (data.length() < 2 || data[0] != '=') { // a valid entry?
397 data = frontStrip(data.substr(1, data.length() - 1));
398 if (data.length() < 2) {
399 return data; // not long enough to find delimiters
401 string::size_type keypos = 1;
403 if (data[0] == '{') {
405 } else if (data[0] == '"') {
408 return data; // no {} and no "", pure data
410 string tmp = data.substr(keypos);
411 while (tmp.find('{') != string::npos &&
412 tmp.find('}') != string::npos &&
413 tmp.find('{') < tmp.find('}') &&
414 tmp.find('{') < tmp.find(enclosing)) {
416 keypos += tmp.find('{') + 1;
417 tmp = data.substr(keypos);
418 keypos += tmp.find('}') + 1;
419 tmp = data.substr(keypos);
421 if (tmp.find(enclosing) == string::npos)
424 keypos += tmp.find(enclosing);
425 return data.substr(1, keypos - 1);
432 CitationStyle const getCitationStyle(string const & command)
434 if (command.empty()) return CitationStyle();
437 string cmd = command;
440 cs.forceUCase = true;
444 size_t n = cmd.size()-1;
447 cmd = cmd.substr(0,n);
450 char const * const * const last = citeCommands + nCiteCommands;
451 char const * const * const ptr = std::find(citeCommands, last, cmd);
454 size_t idx = ptr - citeCommands;
455 cs.style = citeStyles[idx];
462 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
464 string cite = citeCommands[command];
466 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
467 if (std::find(citeStylesFull, last, command) != last)
472 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
473 if (std::find(citeStylesUCase, last, command) != last)
481 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
483 unsigned int nStyles = 1;
484 unsigned int start = 0;
486 nStyles = nCiteStyles - 1;
490 vector<CiteStyle> styles(nStyles);
492 vector<CiteStyle>::size_type i = 0;
494 for (; i != styles.size(); ++i, ++j) {
495 styles[i] = citeStyles[j];
503 getNumericalStrings(string const & key,
504 InfoMap const & map, vector<CiteStyle> const & styles)
507 vector<string> vec(1);
508 vec[0] = _("No database");
512 vector<string> vec(styles.size());
514 string const author = getAbbreviatedAuthor(map, key);
515 string const year = getYear(map, key);
517 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
527 str = author + " [#ID]";
531 str = author + " #ID";
547 str = "(" + year + ")";
559 getAuthorYearStrings(string const & key,
560 InfoMap const & map, vector<CiteStyle> const & styles)
563 vector<string> vec(1);
564 vec[0] = _("No database");
568 vector<string> vec(styles.size());
570 string const author = getAbbreviatedAuthor(map, key);
571 string const year = getYear(map, key);
573 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
578 str = author + " (" + year + ")";
583 str = "(" + author + ", " + year + ")";
587 str = author + " " + year ;
591 str = author + ", " + year ;
603 str = "(" + year + ")";
613 } // namespace biblio