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)
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 frontStrip(fname.substr(0,idx));
158 idx = fname.rfind(".");
159 if (idx != string::npos)
160 fname = frontStrip(fname.substr(idx+1));
166 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
168 lyx::Assert(!map.empty());
170 InfoMap::const_iterator it = map.find(key);
173 if (it != map.end()) {
174 author = parseBibTeX(it->second, "author");
176 author = parseBibTeX(it->second, "editor");
178 vector<string> authors = getVectorFromString(author, "and");
180 if (!authors.empty()) {
183 for (vector<string>::iterator it = authors.begin();
184 it != authors.end(); ++it) {
185 *it = familyName(strip(*it));
189 if (authors.size() == 2)
190 author += _(" and ") + authors[1];
191 else if (authors.size() > 2)
192 author += _(" et al.");
197 author = _("Caesar et al.");
203 string const getYear(InfoMap const & map, string const & key)
205 lyx::Assert(!map.empty());
207 InfoMap::const_iterator it = map.find(key);
212 year = parseBibTeX(it->second, "year");
224 // A functor for use with std::sort, leading to case insensitive sorting
225 struct compareNoCase: public std::binary_function<string, string, bool>
227 bool operator()(string const & s1, string const & s2) const {
228 return compare_no_case(s1, s2) < 0;
232 vector<string> const getKeys(InfoMap const & map)
234 vector<string> bibkeys;
236 for (InfoMap::const_iterator it = map.begin(); it != map.end(); ++it) {
237 bibkeys.push_back(it->first);
240 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
245 string const getInfo(InfoMap const & map, string const & key)
247 lyx::Assert(!map.empty());
249 InfoMap::const_iterator it = map.find(key);
252 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
253 if (!contains(it->second,'='))
254 return it->second.c_str();
256 // Search for all possible "required" keys
257 string author = parseBibTeX(it->second, "author");
259 author = parseBibTeX(it->second, "editor");
261 string year = parseBibTeX(it->second, "year");
262 string title = parseBibTeX(it->second, "title");
263 string booktitle = parseBibTeX(it->second, "booktitle");
264 string chapter = parseBibTeX(it->second, "chapter");
265 string number = parseBibTeX(it->second, "number");
266 string volume = parseBibTeX(it->second, "volume");
267 string pages = parseBibTeX(it->second, "pages");
269 string media = parseBibTeX(it->second, "journal");
271 media = parseBibTeX(it->second, "publisher");
273 media = parseBibTeX(it->second, "school");
275 media = parseBibTeX(it->second, "institution");
277 ostringstream result;
279 result << author << ", ";
282 if (!booktitle.empty())
283 result << ", in " << booktitle;
284 if (!chapter.empty())
285 result << ", Ch. " << chapter;
287 result << ", " << media;
289 result << ", vol. " << volume;
291 result << ", no. " << number;
293 result << ", pp. " << pages;
295 result << ", " << year;
297 char const * const tmp = result.str().c_str();
298 string result_str = tmp ? strip(tmp) : string();
300 if (result_str.empty())
301 // This should never happen (or at least be very unusual!)
302 result_str = it->second;
308 vector<string>::const_iterator
309 searchKeys(InfoMap const & theMap,
310 vector<string> const & keys,
312 vector<string>::const_iterator start,
317 // Preliminary checks
318 if (start < keys.begin() || start >= keys.end())
321 string search_expr = frontStrip(strip(expr));
322 if (search_expr.empty())
326 return simpleSearch(theMap, keys, search_expr, start, dir,
329 return regexSearch(theMap, keys, search_expr, start, dir);
333 string const parseBibTeX(string data, string const & findkey)
336 // at first we delete all characters right of '%' and
337 // replace tabs through a space and remove leading spaces
340 string dummy = token(data,'\n', Entries);
341 while (!dummy.empty()) {
342 dummy = subst(dummy, '\t', ' '); // no tabs
343 dummy = frontStrip(dummy); // no leading spaces
344 string::size_type const idx =
345 dummy.empty() ? string::npos : dummy.find('%');
346 if (idx != string::npos) {
347 // ignore lines with a beginning '%'
349 data_ += dummy.substr(0,data.find('%'));
354 dummy = token(data, '\n', ++Entries);
362 // now get only the important line of the bibtex entry.
363 // all entries are devided by ',' except the last one.
364 data += ','; // now we have same behaviour for all entries
365 // because the last one is "blah ... }"
367 dummy = token(data, ',', Entries);
368 while (!contains(lowercase(dummy), findkey) && !dummy.empty())
369 dummy = token(data, ',', ++Entries);
371 return string(); // no such keyword
372 // we are not sure, if we get all, because "key= "blah, blah" is allowed.
373 // therefore we read all until the next "=" character, which follows a
376 dummy = token(data, ',', ++Entries);
377 while (!contains(dummy, '=') && !dummy.empty()) {
378 keyvalue += (',' + dummy);
379 dummy = token(data, ',', ++Entries);
381 data = keyvalue; // now we have the important line
382 data = strip(data, ' '); // all spaces
383 if (!contains(data, '{')) // no opening '{'
384 data = strip(data, '}');// maybe there is a main closing '}'
385 // happens, when last keyword
386 string::size_type const idx =
387 !data.empty() ? data.find('=') : string::npos;
389 if (idx == string::npos)
392 data = data.substr(idx);
393 data = frontStrip(strip(data));
395 if (data.length() < 2 || data[0] != '=') { // a valid entry?
398 // delete '=' and the following spaces
399 data = frontStrip(frontStrip(data,'='));
400 if (data.length() < 2) {
401 return data; // not long enough to find delimiters
403 string::size_type keypos = 1;
405 if (data[0] == '{') {
407 } else if (data[0] == '"') {
410 // no {} and no "", pure data but with a
411 // possible ',' at the end
412 return strip(data,',');
414 string tmp = data.substr(keypos);
415 while (tmp.find('{') != string::npos &&
416 tmp.find('}') != string::npos &&
417 tmp.find('{') < tmp.find('}') &&
418 tmp.find('{') < tmp.find(enclosing)) {
420 keypos += tmp.find('{') + 1;
421 tmp = data.substr(keypos);
422 keypos += tmp.find('}') + 1;
423 tmp = data.substr(keypos);
425 if (tmp.find(enclosing) == string::npos)
428 keypos += tmp.find(enclosing);
429 return data.substr(1, keypos - 1);
436 CitationStyle const getCitationStyle(string const & command)
438 if (command.empty()) return CitationStyle();
441 string cmd = command;
444 cs.forceUCase = true;
448 size_t n = cmd.size()-1;
451 cmd = cmd.substr(0,n);
454 char const * const * const last = citeCommands + nCiteCommands;
455 char const * const * const ptr = std::find(citeCommands, last, cmd);
458 size_t idx = ptr - citeCommands;
459 cs.style = citeStyles[idx];
466 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
468 string cite = citeCommands[command];
470 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
471 if (std::find(citeStylesFull, last, command) != last)
476 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
477 if (std::find(citeStylesUCase, last, command) != last)
485 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
487 unsigned int nStyles = 1;
488 unsigned int start = 0;
490 nStyles = nCiteStyles - 1;
494 vector<CiteStyle> styles(nStyles);
496 vector<CiteStyle>::size_type i = 0;
498 for (; i != styles.size(); ++i, ++j) {
499 styles[i] = citeStyles[j];
507 getNumericalStrings(string const & key,
508 InfoMap const & map, vector<CiteStyle> const & styles)
511 vector<string> vec(1);
512 vec[0] = _("No database");
516 vector<string> vec(styles.size());
518 string const author = getAbbreviatedAuthor(map, key);
519 string const year = getYear(map, key);
521 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
531 str = author + " [#ID]";
535 str = author + " #ID";
551 str = "(" + year + ")";
563 getAuthorYearStrings(string const & key,
564 InfoMap const & map, vector<CiteStyle> const & styles)
567 vector<string> vec(1);
568 vec[0] = _("No database");
572 vector<string> vec(styles.size());
574 string const author = getAbbreviatedAuthor(map, key);
575 string const year = getYear(map, key);
577 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
582 str = author + " (" + year + ")";
587 str = "(" + author + ", " + year + ")";
591 str = author + " " + year ;
595 str = author + ", " + year ;
607 str = "(" + year + ")";
617 } // namespace biblio