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);
180 if (it != map.end()) {
181 author = parseBibTeX(it->second, "author");
183 author = parseBibTeX(it->second, "editor");
185 vector<string> authors = getVectorFromString(author, "and");
187 if (!authors.empty()) {
190 for (vector<string>::iterator it = authors.begin();
191 it != authors.end(); ++it) {
192 *it = familyName(strip(*it));
196 if (authors.size() == 2)
197 author += _(" and ") + authors[1];
198 else if (authors.size() > 2)
199 author += _(" et al.");
204 author = _("Caesar et al.");
210 string const getYear(InfoMap const & map, string const & key)
212 lyx::Assert(!map.empty());
214 InfoMap::const_iterator it = map.find(key);
219 year = parseBibTeX(it->second, "year");
228 // A functor for use with std::sort, leading to case insensitive sorting
229 struct compareNoCase: public std::binary_function<string, string, bool>
231 bool operator()(string const & s1, string const & s2) const {
232 return compare_no_case(s1, s2) < 0;
236 vector<string> const getKeys(InfoMap const & map)
238 vector<string> bibkeys;
240 for (InfoMap::const_iterator it = map.begin(); it != map.end(); ++it) {
241 bibkeys.push_back(it->first);
244 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
249 string const getInfo(InfoMap const & map, string const & key)
251 lyx::Assert(!map.empty());
253 InfoMap::const_iterator it = map.find(key);
256 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
257 if (!contains(it->second,'='))
258 return it->second.c_str();
260 // Search for all possible "required" keys
261 string author = parseBibTeX(it->second, "author");
263 author = parseBibTeX(it->second, "editor");
265 string year = parseBibTeX(it->second, "year");
266 string title = parseBibTeX(it->second, "title");
267 string booktitle = parseBibTeX(it->second, "booktitle");
268 string chapter = parseBibTeX(it->second, "chapter");
269 string number = parseBibTeX(it->second, "number");
270 string volume = parseBibTeX(it->second, "volume");
271 string pages = parseBibTeX(it->second, "pages");
273 string media = parseBibTeX(it->second, "journal");
275 media = parseBibTeX(it->second, "publisher");
277 media = parseBibTeX(it->second, "school");
279 media = parseBibTeX(it->second, "institution");
281 ostringstream result;
283 result << author << ", ";
286 if (!booktitle.empty())
287 result << ", in " << booktitle;
288 if (!chapter.empty())
289 result << ", Ch. " << chapter;
291 result << ", " << media;
293 result << ", vol. " << volume;
295 result << ", no. " << number;
297 result << ", pp. " << pages;
299 result << ", " << year;
301 string const result_str = strip(result.str().c_str());
302 if (!result_str.empty())
305 // This should never happen (or at least be very unusual!)
310 vector<string>::const_iterator
311 searchKeys(InfoMap const & theMap,
312 vector<string> const & keys,
314 vector<string>::const_iterator start,
319 // Preliminary checks
320 if (start < keys.begin() || start >= keys.end())
323 string search_expr = frontStrip(strip(expr));
324 if (search_expr.empty())
328 return simpleSearch(theMap, keys, search_expr, start, dir,
331 return regexSearch(theMap, keys, search_expr, start, dir);
335 string const parseBibTeX(string data, string const & findkey)
338 // at first we delete all characters right of '%' and
339 // replace tabs through a space and remove leading spaces
342 string dummy = token(data,'\n', Entries);
343 while (!dummy.empty()) {
344 dummy = subst(dummy, '\t', ' '); // no tabs
345 dummy = frontStrip(dummy); // no leading spaces
346 string::size_type const idx =
347 dummy.empty() ? string::npos : dummy.find('%');
348 if (idx != string::npos) {
349 // ignore lines with a beginning '%'
351 data_ += dummy.substr(0,data.find('%'));
356 dummy = token(data, '\n', ++Entries);
358 // replace double commas with "" for easy scanning
359 data = subst(data_, ",,", "\"\"");
365 // now get only the important line of the bibtex entry.
366 // all entries are devided by ',' except the last one.
367 data += ','; // now we have same behaviour for all entries
368 // because the last one is "blah ... }"
370 dummy = token(data, ',', Entries);
371 while (!contains(lowercase(dummy), findkey) && !dummy.empty())
372 dummy = token(data, ',', ++Entries);
377 // we are not sure, if we get all, because "key= "blah, blah" is
379 // Therefore we read all until the next "=" character, which follows a
382 dummy = token(data, ',', ++Entries);
383 while (!contains(dummy, '=') && !dummy.empty()) {
384 keyvalue += (',' + dummy);
385 dummy = token(data, ',', ++Entries);
388 // replace double "" with originals ,, (two commas)
389 // leaving us with the all-important line
390 data = subst(keyvalue, "\"\"", ",,");
394 data = strip(data, ' ');
395 // 2. if there is no opening '{' then a closing '{' is probably cruft.
396 if (!contains(data, '{'))
397 data = strip(data, '}');
398 // happens, when last keyword
399 string::size_type const idx =
400 !data.empty() ? data.find('=') : string::npos;
402 if (idx == string::npos)
405 data = data.substr(idx);
406 data = frontStrip(strip(data));
408 if (data.length() < 2 || data[0] != '=') { // a valid entry?
411 // delete '=' and the following spaces
412 data = frontStrip(frontStrip(data,'='));
413 if (data.length() < 2) {
414 return data; // not long enough to find delimiters
416 string::size_type keypos = 1;
418 if (data[0] == '{') {
420 } else if (data[0] == '"') {
423 // no {} and no "", pure data but with a
424 // possible ',' at the end
425 return strip(data,',');
427 string tmp = data.substr(keypos);
428 while (tmp.find('{') != string::npos &&
429 tmp.find('}') != string::npos &&
430 tmp.find('{') < tmp.find('}') &&
431 tmp.find('{') < tmp.find(enclosing)) {
433 keypos += tmp.find('{') + 1;
434 tmp = data.substr(keypos);
435 keypos += tmp.find('}') + 1;
436 tmp = data.substr(keypos);
438 if (tmp.find(enclosing) == string::npos)
441 keypos += tmp.find(enclosing);
442 return data.substr(1, keypos - 1);
449 CitationStyle const getCitationStyle(string const & command)
451 if (command.empty()) return CitationStyle();
454 string cmd = command;
457 cs.forceUCase = true;
461 size_t n = cmd.size()-1;
464 cmd = cmd.substr(0,n);
467 char const * const * const last = citeCommands + nCiteCommands;
468 char const * const * const ptr = std::find(citeCommands, last, cmd);
471 size_t idx = ptr - citeCommands;
472 cs.style = citeStyles[idx];
479 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
481 string cite = citeCommands[command];
483 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
484 if (std::find(citeStylesFull, last, command) != last)
489 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
490 if (std::find(citeStylesUCase, last, command) != last)
498 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
500 unsigned int nStyles = 1;
501 unsigned int start = 0;
503 nStyles = nCiteStyles - 1;
507 vector<CiteStyle> styles(nStyles);
509 vector<CiteStyle>::size_type i = 0;
511 for (; i != styles.size(); ++i, ++j) {
512 styles[i] = citeStyles[j];
520 getNumericalStrings(string const & key,
521 InfoMap const & map, vector<CiteStyle> const & styles)
524 vector<string> vec(1);
525 vec[0] = _("No database");
529 vector<string> vec(styles.size());
531 string const author = getAbbreviatedAuthor(map, key);
532 string const year = getYear(map, key);
534 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
544 str = author + " [#ID]";
548 str = author + " #ID";
564 str = "(" + year + ")";
576 getAuthorYearStrings(string const & key,
577 InfoMap const & map, vector<CiteStyle> const & styles)
580 vector<string> vec(1);
581 vec[0] = _("No database");
585 vector<string> vec(styles.size());
587 string const author = getAbbreviatedAuthor(map, key);
588 string const year = getYear(map, key);
590 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
595 str = author + " (" + year + ")";
600 str = "(" + author + ", " + year + ")";
604 str = author + " " + year ;
608 str = author + ", " + year ;
620 str = "(" + year + ")";
630 } // namespace biblio