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 string const result_str = strip(result.str().c_str());
298 if (!result_str.empty())
301 // This should never happen (or at least be very unusual!)
306 vector<string>::const_iterator
307 searchKeys(InfoMap const & theMap,
308 vector<string> const & keys,
310 vector<string>::const_iterator start,
315 // Preliminary checks
316 if (start < keys.begin() || start >= keys.end())
319 string search_expr = frontStrip(strip(expr));
320 if (search_expr.empty())
324 return simpleSearch(theMap, keys, search_expr, start, dir,
327 return regexSearch(theMap, keys, search_expr, start, dir);
331 string const parseBibTeX(string data, string const & findkey)
334 // at first we delete all characters right of '%' and
335 // replace tabs through a space and remove leading spaces
338 string dummy = token(data,'\n', Entries);
339 while (!dummy.empty()) {
340 dummy = subst(dummy, '\t', ' '); // no tabs
341 dummy = frontStrip(dummy); // no leading spaces
342 string::size_type const idx =
343 dummy.empty() ? string::npos : dummy.find('%');
344 if (idx != string::npos) {
345 // ignore lines with a beginning '%'
347 data_ += dummy.substr(0,data.find('%'));
352 dummy = token(data, '\n', ++Entries);
360 // now get only the important line of the bibtex entry.
361 // all entries are devided by ',' except the last one.
362 data += ','; // now we have same behaviour for all entries
363 // because the last one is "blah ... }"
365 dummy = token(data, ',', Entries);
366 while (!contains(lowercase(dummy), findkey) && !dummy.empty())
367 dummy = token(data, ',', ++Entries);
369 return string(); // no such keyword
370 // we are not sure, if we get all, because "key= "blah, blah" is allowed.
371 // therefore we read all until the next "=" character, which follows a
374 dummy = token(data, ',', ++Entries);
375 while (!contains(dummy, '=') && !dummy.empty()) {
376 keyvalue += (',' + dummy);
377 dummy = token(data, ',', ++Entries);
379 data = keyvalue; // now we have the important line
380 data = strip(data, ' '); // all spaces
381 if (!contains(data, '{')) // no opening '{'
382 data = strip(data, '}');// maybe there is a main closing '}'
383 // happens, when last keyword
384 string::size_type const idx =
385 !data.empty() ? data.find('=') : string::npos;
387 if (idx == string::npos)
390 data = data.substr(idx);
391 data = frontStrip(strip(data));
393 if (data.length() < 2 || data[0] != '=') { // a valid entry?
396 // delete '=' and the following spaces
397 data = frontStrip(frontStrip(data,'='));
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 // no {} and no "", pure data but with a
409 // possible ',' at the end
410 return strip(data,',');
412 string tmp = data.substr(keypos);
413 while (tmp.find('{') != string::npos &&
414 tmp.find('}') != string::npos &&
415 tmp.find('{') < tmp.find('}') &&
416 tmp.find('{') < tmp.find(enclosing)) {
418 keypos += tmp.find('{') + 1;
419 tmp = data.substr(keypos);
420 keypos += tmp.find('}') + 1;
421 tmp = data.substr(keypos);
423 if (tmp.find(enclosing) == string::npos)
426 keypos += tmp.find(enclosing);
427 return data.substr(1, keypos - 1);
434 CitationStyle const getCitationStyle(string const & command)
436 if (command.empty()) return CitationStyle();
439 string cmd = command;
442 cs.forceUCase = true;
446 size_t n = cmd.size()-1;
449 cmd = cmd.substr(0,n);
452 char const * const * const last = citeCommands + nCiteCommands;
453 char const * const * const ptr = std::find(citeCommands, last, cmd);
456 size_t idx = ptr - citeCommands;
457 cs.style = citeStyles[idx];
464 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
466 string cite = citeCommands[command];
468 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
469 if (std::find(citeStylesFull, last, command) != last)
474 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
475 if (std::find(citeStylesUCase, last, command) != last)
483 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
485 unsigned int nStyles = 1;
486 unsigned int start = 0;
488 nStyles = nCiteStyles - 1;
492 vector<CiteStyle> styles(nStyles);
494 vector<CiteStyle>::size_type i = 0;
496 for (; i != styles.size(); ++i, ++j) {
497 styles[i] = citeStyles[j];
505 getNumericalStrings(string const & key,
506 InfoMap const & map, vector<CiteStyle> const & styles)
509 vector<string> vec(1);
510 vec[0] = _("No database");
514 vector<string> vec(styles.size());
516 string const author = getAbbreviatedAuthor(map, key);
517 string const year = getYear(map, key);
519 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
529 str = author + " [#ID]";
533 str = author + " #ID";
549 str = "(" + year + ")";
561 getAuthorYearStrings(string const & key,
562 InfoMap const & map, vector<CiteStyle> const & styles)
565 vector<string> vec(1);
566 vec[0] = _("No database");
570 vector<string> vec(styles.size());
572 string const author = getAbbreviatedAuthor(map, key);
573 string const year = getYear(map, key);
575 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
580 str = author + " (" + year + ")";
585 str = "(" + author + ", " + year + ")";
589 str = author + " " + year ;
593 str = author + ", " + year ;
605 str = "(" + year + ")";
615 } // namespace biblio