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 string::size_type idx = fname.rfind(".");
150 if (idx != string::npos)
151 fname = frontStrip(fname.substr(idx+1));
157 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
159 lyx::Assert(!map.empty());
161 InfoMap::const_iterator it = map.find(key);
164 if (it != map.end()) {
165 author = parseBibTeX(it->second, "author");
167 author = parseBibTeX(it->second, "editor");
169 vector<string> authors = getVectorFromString(author, "and");
171 if (!authors.empty()) {
174 for (vector<string>::iterator it = authors.begin();
175 it != authors.end(); ++it) {
176 *it = familyName(strip(*it));
180 if (authors.size() == 2)
181 author += _(" and ") + authors[1];
182 else if (authors.size() > 2)
183 author += _(" et al.");
188 author = _("Caesar et al.");
194 string const getYear(InfoMap const & map, string const & key)
196 lyx::Assert(!map.empty());
198 InfoMap::const_iterator it = map.find(key);
203 year = parseBibTeX(it->second, "year");
219 // A functor for use with std::sort, leading to case insensitive sorting
220 struct compareNoCase: public std::binary_function<string, string, bool>
222 bool operator()(string const & s1, string const & s2) const {
223 return compare_no_case(s1, s2) < 0;
227 vector<string> const getKeys(InfoMap const & map)
229 vector<string> bibkeys;
231 for (InfoMap::const_iterator it = map.begin(); it != map.end(); ++it) {
232 bibkeys.push_back(it->first);
235 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
240 string const getInfo(InfoMap const & map, string const & key)
242 lyx::Assert(!map.empty());
244 InfoMap::const_iterator it = map.find(key);
245 if (it == map.end()) return string();
247 // Search for all possible "required" keys
248 string author = parseBibTeX(it->second, "author");
250 author = parseBibTeX(it->second, "editor");
252 string year = parseBibTeX(it->second, "year");
253 string title = parseBibTeX(it->second, "title");
254 string booktitle = parseBibTeX(it->second, "booktitle");
255 string chapter = parseBibTeX(it->second, "chapter");
256 string number = parseBibTeX(it->second, "number");
257 string volume = parseBibTeX(it->second, "volume");
258 string pages = parseBibTeX(it->second, "pages");
260 string media = parseBibTeX(it->second, "journal");
262 media = parseBibTeX(it->second, "publisher");
264 media = parseBibTeX(it->second, "school");
266 media = parseBibTeX(it->second, "institution");
268 ostringstream result;
270 result << author << ", ";
273 if (!booktitle.empty())
274 result << ", in " << booktitle;
275 if (!chapter.empty())
276 result << ", Ch. " << chapter;
278 result << ", " << media;
280 result << ", vol. " << volume;
282 result << ", no. " << number;
284 result << ", pp. " << pages;
286 result << ", " << year;
288 if (result.str().empty()) // not a BibTeX record
289 result << it->second;
291 return result.str().c_str();
295 vector<string>::const_iterator
296 searchKeys(InfoMap const & theMap,
297 vector<string> const & keys,
299 vector<string>::const_iterator start,
304 // Preliminary checks
305 if (start < keys.begin() || start >= keys.end())
308 string search_expr = frontStrip(strip(expr));
309 if (search_expr.empty())
313 return simpleSearch(theMap, keys, search_expr, start, dir,
316 return regexSearch(theMap, keys, search_expr, start, dir);
320 string const parseBibTeX(string data, string const & findkey)
323 // at first we delete all characters right of '%' and
324 // replace tabs through a space and remove leading spaces
327 string dummy = token(data,'\n', Entries);
328 while (!dummy.empty()) {
329 dummy = subst(dummy, '\t', ' '); // no tabs
330 dummy = frontStrip(dummy); // no leading spaces
331 if (dummy.find('%') != string::npos) {
332 if (dummy.find('%') > 0)
333 data_ += dummy.substr(0,data.find('%'));
337 dummy = token(data, '\n', ++Entries);
340 // now get only the important line of the bibtex entry.
341 // all entries are devided by ',' except the last one.
342 data += ','; // now we have same behaviour for all entries
343 // because the last one is "blah ... }"
345 dummy = token(data, ',', Entries);
346 while (!contains(lowercase(dummy), findkey) && !dummy.empty())
347 dummy = token(data, ',', ++Entries);
349 return string(); // no such keyword
350 // we are not sure, if we get all, because "key= "blah, blah" is allowed.
351 // therefore we read all until the next "=" character, which follows a
354 dummy = token(data, ',', ++Entries);
355 while (!contains(dummy, '=') && !dummy.empty()) {
356 keyvalue += (',' + dummy);
357 dummy = token(data, ',', ++Entries);
359 data = keyvalue; // now we have the important line
360 data = strip(data, ' '); // all spaces
361 if (!contains(data, '{')) // no opening '{'
362 data = strip(data, '}');// maybe there is a main closing '}'
363 // happens, when last keyword
364 string key = lowercase(data.substr(0, data.find('=')));
365 data = data.substr(data.find('='), data.length() - 1);
366 data = frontStrip(strip(data));
367 if (data.length() < 2 || data[0] != '=') { // a valid entry?
370 data = frontStrip(data.substr(1, data.length() - 1));
371 if (data.length() < 2) {
372 return data; // not long enough to find delimiters
374 string::size_type keypos = 1;
376 if (data[0] == '{') {
378 } else if (data[0] == '"') {
381 return data; // no {} and no "", pure data
383 string tmp = data.substr(keypos, data.length()-1);
384 while (tmp.find('{') != string::npos &&
385 tmp.find('}') != string::npos &&
386 tmp.find('{') < tmp.find('}') &&
387 tmp.find('{') < tmp.find(enclosing)) {
389 keypos += tmp.find('{') + 1;
390 tmp = data.substr(keypos, data.length() - 1);
391 keypos += tmp.find('}') + 1;
392 tmp = data.substr(keypos, data.length() - 1);
394 if (tmp.find(enclosing) == string::npos)
397 keypos += tmp.find(enclosing);
398 return data.substr(1, keypos - 1);
405 CitationStyle const getCitationStyle(string const & command)
407 if (command.empty()) return CitationStyle();
410 string cmd = command;
413 cs.forceUCase = true;
417 size_t n = cmd.size()-1;
420 cmd = cmd.substr(0,n);
423 char const * const * const last = citeCommands + nCiteCommands;
424 char const * const * const ptr = std::find(citeCommands, last, cmd);
427 size_t idx = ptr - citeCommands;
428 cs.style = citeStyles[idx];
435 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
437 string cite = citeCommands[command];
439 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
440 if (std::find(citeStylesFull, last, command) != last)
445 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
446 if (std::find(citeStylesUCase, last, command) != last)
454 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
456 unsigned int nStyles = 1;
457 unsigned int start = 0;
459 nStyles = nCiteStyles - 1;
463 vector<CiteStyle> styles(nStyles);
465 vector<CiteStyle>::size_type i = 0;
467 for (; i != styles.size(); ++i, ++j) {
468 styles[i] = citeStyles[j];
476 getNumericalStrings(string const & key,
477 InfoMap const & map, vector<CiteStyle> const & styles)
480 vector<string> vec(1);
481 vec[0] = _("No database");
485 vector<string> vec(styles.size());
487 string const author = getAbbreviatedAuthor(map, key);
488 string const year = getYear(map, key);
490 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
500 str = author + " [#ID]";
504 str = author + " #ID";
520 str = "(" + year + ")";
532 getAuthorYearStrings(string const & key,
533 InfoMap const & map, vector<CiteStyle> const & styles)
536 vector<string> vec(1);
537 vec[0] = _("No database");
541 vector<string> vec(styles.size());
543 string const author = getAbbreviatedAuthor(map, key);
544 string const year = getYear(map, key);
546 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
551 str = author + " (" + year + ")";
556 str = "(" + author + ", " + year + ")";
560 str = author + " " + year ;
564 str = author + ", " + year ;
576 str = "(" + year + ")";
586 } // namespace biblio